summaryrefslogtreecommitdiff
path: root/sapi/thttpd/thttpd_patch
blob: 162e5685fc9f691e619aa24add2a6de7e5750b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
diff -ur thttpd-2.21b/Makefile.in thttpd-2.21b-cool/Makefile.in
--- thttpd-2.21b/Makefile.in	Thu Mar 29 20:36:21 2001
+++ thttpd-2.21b-cool/Makefile.in	Mon Sep 23 17:37:36 2002
@@ -46,13 +46,15 @@
 
 # You shouldn't need to edit anything below here.
 
+include php_makefile
+
 CC =		@CC@
 CCOPT =		@V_CCOPT@
 DEFS =		@DEFS@
 INCLS =		-I.
 CFLAGS =	$(CCOPT) $(DEFS) $(INCLS)
-LDFLAGS =	@LDFLAGS@
-LIBS =		@LIBS@
+LDFLAGS =	@LDFLAGS@ $(PHP_LDFLAGS)
+LIBS =		@LIBS@ $(PHP_LIBS)
 NETLIBS =	@V_NETLIBS@
 INSTALL =	@INSTALL@
 
@@ -62,7 +64,7 @@
 	@rm -f $@
 	$(CC) $(CFLAGS) -c $*.c
 
-SRC =		thttpd.c libhttpd.c fdwatch.c mmc.c timers.c match.c tdate_parse.c syslog.c
+SRC =		thttpd.c libhttpd.c fdwatch.c mmc.c timers.c match.c tdate_parse.c syslog.c php_thttpd.c
 
 OBJ =		$(SRC:.c=.o) @LIBOBJS@
 
@@ -151,6 +153,9 @@
 
 tags:
 	ctags -wtd *.c *.h
+
+php_thttpd.o: php_thttpd.c
+	$(CC) $(PHP_CFLAGS) $(CFLAGS) -c php_thttpd.c
 
 tar:
 	@name=`sed -n -e '/SERVER_SOFTWARE/!d' -e 's,.*thttpd/,thttpd-,' -e 's, .*,,p' version.h` ; \
diff -ur thttpd-2.21b/config.h thttpd-2.21b-cool/config.h
--- thttpd-2.21b/config.h	Mon Apr  9 23:57:36 2001
+++ thttpd-2.21b-cool/config.h	Sun Sep 22 22:44:56 2002
@@ -82,6 +82,11 @@
 */
 #define IDLE_READ_TIMELIMIT 60
 
+/* CONFIGURE: How many seconds to allow for reading the subsequent requests
+** on a keep-alive connection.  Should be simiar to LINGER_TIME
+*/
+#define IDLE_KEEPALIVE_TIMELIMIT 2
+
 /* CONFIGURE: How many seconds before an idle connection gets closed.
 */
 #define IDLE_SEND_TIMELIMIT 300
@@ -316,7 +321,7 @@
 /* CONFIGURE: A list of index filenames to check.  The files are searched
 ** for in this order.
 */
-#define INDEX_NAMES "index.html", "index.htm", "Default.htm", "index.cgi"
+#define INDEX_NAMES "index.php", "index.html", "index.htm", "Default.htm", "index.cgi"
 
 /* CONFIGURE: If this is defined then thttpd will automatically generate
 ** index pages for directories that don't have an explicit index file.
diff -ur thttpd-2.21b/fdwatch.c thttpd-2.21b-cool/fdwatch.c
--- thttpd-2.21b/fdwatch.c	Fri Apr 13 07:36:08 2001
+++ thttpd-2.21b-cool/fdwatch.c	Mon Sep 23 17:38:03 2002
@@ -460,7 +460,7 @@
 
     ridx = 0;
     for ( i = 0; i < npollfds; ++i )
-	if ( pollfds[i].revents & ( POLLIN | POLLOUT ) )
+	if ( pollfds[i].revents & ( POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL ) )
 	    poll_rfdidx[ridx++] = pollfds[i].fd;
 
     return r;
@@ -472,8 +472,8 @@
     {
     switch ( fd_rw[fd] )
 	{
-	case FDW_READ: return pollfds[poll_fdidx[fd]].revents & POLLIN;
-	case FDW_WRITE: return pollfds[poll_fdidx[fd]].revents & POLLOUT;
+	case FDW_READ: return pollfds[poll_fdidx[fd]].revents & ( POLLIN | POLLERR | POLLHUP | POLLNVAL );
+	case FDW_WRITE: return pollfds[poll_fdidx[fd]].revents & (  POLLOUT | POLLERR | POLLHUP | POLLNVAL );
 	default: return 0;
 	}
     }
diff -ur thttpd-2.21b/libhttpd.c thttpd-2.21b-cool/libhttpd.c
--- thttpd-2.21b/libhttpd.c	Tue Apr 24 00:42:40 2001
+++ thttpd-2.21b-cool/libhttpd.c	Mon Sep 23 17:29:24 2002
@@ -85,6 +85,8 @@
 #include "match.h"
 #include "tdate_parse.h"
 
+#include "php_thttpd.h"
+
 #ifndef STDIN_FILENO
 #define STDIN_FILENO 0
 #endif
@@ -242,6 +244,8 @@
 	free( (void*) hs->cwd );
     if ( hs->cgi_pattern != (char*) 0 )
 	free( (void*) hs->cgi_pattern );
+    if ( hs->php_pattern != (char*) 0 )
+	free( (void*) hs->php_pattern );
     if ( hs->charset != (char*) 0 )
 	free( (void*) hs->charset );
     if ( hs->url_pattern != (char*) 0 )
@@ -249,6 +253,7 @@
     if ( hs->local_pattern != (char*) 0 )
 	free( (void*) hs->local_pattern );
     free( (void*) hs );
+    thttpd_php_shutdown();
     }
 
 
@@ -312,6 +317,7 @@
 	}
 
     hs->port = port;
+    hs->php_pattern = strdup("**.php");
     if ( cgi_pattern == (char*) 0 )
 	hs->cgi_pattern = (char*) 0;
     else
@@ -385,6 +391,8 @@
 	return (httpd_server*) 0;
 	}
 
+    thttpd_php_init();
+
     /* Done initializing. */
     if ( hs->binding_hostname == (char*) 0 )
 	syslog( LOG_INFO, "%.80s starting on port %d", SERVER_SOFTWARE, hs->port );
@@ -582,6 +590,9 @@
     /* And send it, if necessary. */
     if ( hc->responselen > 0 )
 	{
+/*
+printf("**RESPONSE [%d]** len = %d\n%*.*s\n", hc->conn_fd, hc->responselen, hc->responselen, hc->responselen, hc->response); 
+*/
 	(void) write( hc->conn_fd, hc->response, hc->responselen );
 	hc->responselen = 0;
 	}
@@ -657,9 +668,9 @@
 	(void) my_snprintf(
 	    fixed_type, sizeof(fixed_type), type, hc->hs->charset );
 	(void) my_snprintf( buf, sizeof(buf),
-	    "%.20s %d %s\r\nServer: %s\r\nContent-Type: %s\r\nDate: %s\r\nLast-Modified: %s\r\nAccept-Ranges: bytes\r\nConnection: close\r\n",
+	    "%.20s %d %s\r\nServer: %s\r\nContent-Type: %s\r\nDate: %s\r\nLast-Modified: %s\r\nAccept-Ranges: bytes\r\n",
 	    hc->protocol, status, title, EXPOSED_SERVER_SOFTWARE, fixed_type,
-	    nowbuf, modbuf );
+	    nowbuf, modbuf);
 	add_response( hc, buf );
 	if ( encodings[0] != '\0' )
 	    {
@@ -681,6 +692,14 @@
 		"Content-Length: %d\r\n", length );
 	    add_response( hc, buf );
 	    }
+	else {
+		hc->do_keep_alive = 0;
+	}
+	if (hc->do_keep_alive) {
+	    add_response( hc, "Connection: keep-alive\r\n" );
+	} else {
+	    add_response( hc, "Connection: close\r\n" );
+	}
 	if ( extraheads[0] != '\0' )
 	    add_response( hc, extraheads );
 	add_response( hc, "\r\n" );
@@ -1603,6 +1622,61 @@
 
 
 int
+httpd_request_reset(httpd_conn* hc, int preserve_read_buf )
+{
+	if (!preserve_read_buf) {
+	    hc->read_idx = 0;
+    	hc->checked_idx = 0;
+	}
+    hc->checked_state = CHST_FIRSTWORD;
+    hc->method = METHOD_UNKNOWN;
+    hc->status = 0;
+    hc->bytes_to_send = 0;
+    hc->bytes_sent = 0;
+    hc->encodedurl = "";
+    hc->decodedurl[0] = '\0';
+    hc->protocol = "UNKNOWN";
+    hc->origfilename[0] = '\0';
+    hc->expnfilename[0] = '\0';
+    hc->encodings[0] = '\0';
+    hc->pathinfo[0] = '\0';
+    hc->query[0] = '\0';
+    hc->referer = "";
+    hc->useragent = "";
+    hc->accept[0] = '\0';
+    hc->accepte[0] = '\0';
+    hc->acceptl = "";
+    hc->cookie = "";
+    hc->contenttype = "";
+    hc->reqhost[0] = '\0';
+    hc->hdrhost = "";
+    hc->hostdir[0] = '\0';
+    hc->authorization = "";
+    hc->remoteuser[0] = '\0';
+    hc->response[0] = '\0';
+#ifdef TILDE_MAP_2
+    hc->altdir[0] = '\0';
+#endif /* TILDE_MAP_2 */
+    hc->responselen = 0;
+    hc->if_modified_since = (time_t) -1;
+    hc->range_if = (time_t) -1;
+    hc->contentlength = -1;
+    hc->type = "";
+    hc->hostname = (char*) 0;
+    hc->mime_flag = 1;
+    hc->one_one = 0;
+    hc->got_range = 0;
+    hc->tildemapped = 0;
+    hc->init_byte_loc = 0;
+    hc->end_byte_loc = -1;
+    hc->keep_alive = 0;
+    hc->do_keep_alive = 0;
+    hc->should_linger = 0;
+    hc->file_address = (char*) 0;
+    return GC_OK;
+}
+
+int
 httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc )
     {
     httpd_sockaddr sa;
@@ -1657,53 +1731,12 @@
     hc->hs = hs;
     memset( &hc->client_addr, 0, sizeof(hc->client_addr) );
     memcpy( &hc->client_addr, &sa, sockaddr_len( &sa ) );
-    hc->read_idx = 0;
-    hc->checked_idx = 0;
-    hc->checked_state = CHST_FIRSTWORD;
-    hc->method = METHOD_UNKNOWN;
-    hc->status = 0;
-    hc->bytes_to_send = 0;
-    hc->bytes_sent = 0;
-    hc->encodedurl = "";
-    hc->decodedurl[0] = '\0';
-    hc->protocol = "UNKNOWN";
-    hc->origfilename[0] = '\0';
-    hc->expnfilename[0] = '\0';
-    hc->encodings[0] = '\0';
-    hc->pathinfo[0] = '\0';
-    hc->query[0] = '\0';
-    hc->referer = "";
-    hc->useragent = "";
-    hc->accept[0] = '\0';
-    hc->accepte[0] = '\0';
-    hc->acceptl = "";
-    hc->cookie = "";
-    hc->contenttype = "";
-    hc->reqhost[0] = '\0';
-    hc->hdrhost = "";
-    hc->hostdir[0] = '\0';
-    hc->authorization = "";
-    hc->remoteuser[0] = '\0';
-    hc->response[0] = '\0';
-#ifdef TILDE_MAP_2
-    hc->altdir[0] = '\0';
-#endif /* TILDE_MAP_2 */
-    hc->responselen = 0;
-    hc->if_modified_since = (time_t) -1;
-    hc->range_if = (time_t) -1;
-    hc->contentlength = -1;
-    hc->type = "";
-    hc->hostname = (char*) 0;
-    hc->mime_flag = 1;
-    hc->one_one = 0;
-    hc->got_range = 0;
-    hc->tildemapped = 0;
-    hc->init_byte_loc = 0;
-    hc->end_byte_loc = -1;
-    hc->keep_alive = 0;
-    hc->should_linger = 0;
-    hc->file_address = (char*) 0;
-    return GC_OK;
+
+/*
+printf("doing httpd_get_con(%d)\n", hc->conn_fd);
+*/
+
+    return httpd_request_reset(hc, 0);
     }
 
 
@@ -1720,6 +1753,9 @@
     {
     char c;
 
+/*
+printf("**REQUEST [%d]**\n%*.*s\n", hc->conn_fd, hc->read_idx, hc->read_idx, hc->read_buf);
+*/
     for ( ; hc->checked_idx < hc->read_idx; ++hc->checked_idx )
 	{
 	c = hc->read_buf[hc->checked_idx];
@@ -1912,8 +1948,11 @@
 	    eol = strpbrk( protocol, " \t\n\r" );
 	    if ( eol != (char*) 0 )
 		*eol = '\0';
-	    if ( strcasecmp( protocol, "HTTP/1.0" ) != 0 )
+	    if ( strcasecmp( protocol, "HTTP/1.0" ) != 0 ) {
 		hc->one_one = 1;
+		hc->keep_alive = 1;
+		hc->do_keep_alive = 1;
+		}
 	    }
 	}
     /* Check for HTTP/1.1 absolute URL. */
@@ -2129,6 +2168,7 @@
 		cp = &buf[11];
 		cp += strspn( cp, " \t" );
 		if ( strcasecmp( cp, "keep-alive" ) == 0 )
+		    hc->do_keep_alive = 1;
 		    hc->keep_alive = 1;
 		}
 #ifdef LOG_UNKNOWN_HEADERS
@@ -2168,6 +2208,9 @@
 	    }
 	}
 
+/*
+printf("one_one = %d   keep_alive = %d\n", hc->one_one, hc->keep_alive);
+*/
     if ( hc->one_one )
 	{
 	/* Check that HTTP/1.1 requests specify a host, as required. */
@@ -2177,14 +2220,14 @@
 	    return -1;
 	    }
 
-	/* If the client wants to do keep-alives, it might also be doing
-	** pipelining.  There's no way for us to tell.  Since we don't
-	** implement keep-alives yet, if we close such a connection there
-	** might be unread pipelined requests waiting.  So, we have to
-	** do a lingering close.
+	/*
+	**  Disable keep alive support for bad browsers, 
+	**    list taken from Apache 1.3.19
 	*/
-	if ( hc->keep_alive )
-	    hc->should_linger = 1;
+	if ( hc->do_keep_alive && 
+	    ( strstr(hc->useragent, "Mozilla/2") != NULL ||
+	      strstr(hc->useragent, "MSIE 4.0b2;") != NULL))
+		hc->do_keep_alive = 0;
 	}
 
     /* Ok, the request has been parsed.  Now we resolve stuff that
@@ -2349,15 +2392,24 @@
 
 
 void
-httpd_close_conn( httpd_conn* hc, struct timeval* nowP )
-    {
-    make_log_entry( hc, nowP );
+httpd_complete_request( httpd_conn* hc, struct timeval* nowP, int logit )
+{
+	if (logit)
+		make_log_entry( hc, nowP );
 
-    if ( hc->file_address != (char*) 0 )
+	if ( hc->file_address == (char*) 1 )
+    {
+	thttpd_closed_conn(hc->conn_fd);
+    } else if ( hc->file_address != (char*) 0 )
 	{
 	mmc_unmap( hc->file_address, &(hc->sb), nowP );
 	hc->file_address = (char*) 0;
 	}
+	}
+
+void
+httpd_close_conn( httpd_conn* hc, struct timeval* nowP )
+{
     if ( hc->conn_fd >= 0 )
 	{
 	(void) close( hc->conn_fd );
@@ -3026,11 +3078,9 @@
 post_post_garbage_hack( httpd_conn* hc )
     {
     char buf[2];
-    int r;
 
-    r = recv( hc->conn_fd, buf, sizeof(buf), MSG_PEEK );
-    if ( r > 0 )
-	(void) read( hc->conn_fd, buf, r );
+	fcntl(hc->conn_fd, F_SETFL, O_NONBLOCK);
+	(void) read( hc->conn_fd, buf, 2 );
     }
 
 
@@ -3313,6 +3363,11 @@
     int r;
     ClientData client_data;
 
+    /*
+    **  We are not going to leave the socket open after a CGI... too hard
+    */
+    hc->do_keep_alive = 0;
+
     if ( hc->method == METHOD_GET || hc->method == METHOD_POST )
 	{
 	httpd_clear_ndelay( hc->conn_fd );
@@ -3369,6 +3424,7 @@
     int expnlen, indxlen;
     char* cp;
     char* pi;
+    int nocache = 0;
 
     expnlen = strlen( hc->expnfilename );
 
@@ -3561,6 +3617,11 @@
 	 match( hc->hs->cgi_pattern, hc->expnfilename ) )
 	return cgi( hc );
 
+	if ( hc->hs->php_pattern != (char*) 0 &&
+			match( hc->hs->php_pattern, hc->expnfilename)) {
+		return thttpd_php_request( hc );
+	}
+
     /* It's not CGI.  If it's executable or there's pathinfo, someone's
     ** trying to either serve or run a non-CGI file as CGI.   Either case
     ** is prohibited.
@@ -3594,6 +3655,8 @@
 	hc->end_byte_loc = hc->sb.st_size - 1;
 
     figure_mime( hc );
+    if ( strncmp(hc->decodedurl, "/nocache/", sizeof("/nocache/") - 1 ) == 0 )
+	nocache = 1;
 
     if ( hc->method == METHOD_HEAD )
 	{
@@ -3601,7 +3664,7 @@
 	    hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size,
 	    hc->sb.st_mtime );
 	}
-    else if ( hc->if_modified_since != (time_t) -1 &&
+    else if ( !nocache && hc->if_modified_since != (time_t) -1 &&
 	 hc->if_modified_since >= hc->sb.st_mtime )
 	{
 	hc->method = METHOD_HEAD;
@@ -3611,14 +3674,25 @@
 	}
     else
 	{
-	hc->file_address = mmc_map( hc->expnfilename, &(hc->sb), nowP );
+	char *extraheads = "";
+
+	if ( nocache ) 
+	    {
+	    extraheads = "Expires: Thu, 19 Nov 1981 08:52:00 GMT\r\n"
+		"Cache-Control: no-store, no-cache, must-revalidate, "
+		"post-check=0, pre-check=0\r\n"
+		"Pragma: no-cache\r\n";
+	    }
+		
+	hc->file_address = mmc_map( hc->expnfilename, &(hc->sb), nowP, nocache );
 	if ( hc->file_address == (char*) 0 )
 	    {
 	    httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
 	    return -1;
 	    }
+
 	send_mime(
-	    hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size,
+	    hc, 200, ok200title, hc->encodings, extraheads, hc->type, hc->sb.st_size,
 	    hc->sb.st_mtime );
 	}
 
diff -ur thttpd-2.21b/libhttpd.h thttpd-2.21b-cool/libhttpd.h
--- thttpd-2.21b/libhttpd.h	Tue Apr 24 00:36:50 2001
+++ thttpd-2.21b-cool/libhttpd.h	Sun Sep 22 22:44:56 2002
@@ -69,6 +69,7 @@
     char* server_hostname;
     int port;
     char* cgi_pattern;
+    char* php_pattern;
     char* charset;
     char* cwd;
     int listen4_fd, listen6_fd;
@@ -132,7 +133,7 @@
     int got_range;
     int tildemapped;	/* this connection got tilde-mapped */
     off_t init_byte_loc, end_byte_loc;
-    int keep_alive;
+    int keep_alive, do_keep_alive;
     int should_linger;
     struct stat sb;
     int conn_fd;
@@ -229,6 +230,8 @@
 ** If you don't have a current timeval handy just pass in 0.
 */
 extern void httpd_close_conn( httpd_conn* hc, struct timeval* nowP );
+void httpd_complete_request( httpd_conn* hc, struct timeval* nowP, int logit );
+int httpd_request_reset(httpd_conn* hc,int );
 
 /* Call this to de-initialize a connection struct and *really* free the
 ** mallocced strings.
diff -ur thttpd-2.21b/mime_encodings.txt thttpd-2.21b-cool/mime_encodings.txt
--- thttpd-2.21b/mime_encodings.txt	Wed May 10 03:22:28 2000
+++ thttpd-2.21b-cool/mime_encodings.txt	Sun Sep 22 22:44:56 2002
@@ -3,6 +3,6 @@
 # A list of file extensions followed by the corresponding MIME encoding.
 # Extensions not found in the table proceed to the mime_types table.
 
-Z	x-compress
-gz	x-gzip
+Z	compress
+gz	gzip
 uu	x-uuencode
diff -ur thttpd-2.21b/mime_types.txt thttpd-2.21b-cool/mime_types.txt
--- thttpd-2.21b/mime_types.txt	Sat Apr 14 04:53:30 2001
+++ thttpd-2.21b-cool/mime_types.txt	Sun Sep 22 22:44:56 2002
@@ -1,135 +1,138 @@
-# mime_types.txt
-#
-# A list of file extensions followed by the corresponding MIME type.
-# Extensions not found in the table are returned as text/plain.
-
-html	text/html; charset=%s
-htm	text/html; charset=%s
-txt	text/plain; charset=%s
-rtx	text/richtext
-etx	text/x-setext
-tsv	text/tab-separated-values
-css	text/css
-xml	text/xml
-dtd	text/xml
-
-gif	image/gif
-jpg	image/jpeg
-jpeg	image/jpeg
-jpe	image/jpeg
-jfif	image/jpeg
-tif	image/tiff
-tiff	image/tiff
-pbm	image/x-portable-bitmap
-pgm	image/x-portable-graymap
-ppm	image/x-portable-pixmap
-pnm	image/x-portable-anymap
-xbm	image/x-xbitmap
-xpm	image/x-xpixmap
-xwd	image/x-xwindowdump
-ief	image/ief
-png	image/png
-
-au	audio/basic
-snd	audio/basic
-aif	audio/x-aiff
-aiff	audio/x-aiff
-aifc	audio/x-aiff
-ra	audio/x-pn-realaudio
-ram	audio/x-pn-realaudio
-rm	audio/x-pn-realaudio
-rpm	audio/x-pn-realaudio-plugin
-wav	audio/wav
-mid	audio/midi
-midi	audio/midi
-kar	audio/midi
-mpga	audio/mpeg
-mp2	audio/mpeg
-mp3	audio/mpeg
-
-mpeg	video/mpeg
-mpg	video/mpeg
-mpe	video/mpeg
-qt	video/quicktime
-mov	video/quicktime
-avi	video/x-msvideo
-movie	video/x-sgi-movie
-mv	video/x-sgi-movie
-vx	video/x-rad-screenplay
-
-a	application/octet-stream
+ez	application/andrew-inset
+hqx	application/mac-binhex40
+cpt	application/mac-compactpro
+doc	application/msword
 bin	application/octet-stream
+dms	application/octet-stream
+lha	application/octet-stream
+lzh	application/octet-stream
 exe	application/octet-stream
-dump	application/octet-stream
-o	application/octet-stream
-class	application/java
-js	application/x-javascript
+class	application/octet-stream
+so	application/octet-stream
+dll	application/octet-stream
+oda	application/oda
+pdf	application/pdf
 ai	application/postscript
 eps	application/postscript
 ps	application/postscript
-dir	application/x-director
+smi	application/smil
+smil	application/smil
+mif	application/vnd.mif
+xls	application/vnd.ms-excel
+ppt	application/vnd.ms-powerpoint
+wbxml	application/vnd.wap.wbxml
+wmlc	application/vnd.wap.wmlc
+wmlsc	application/vnd.wap.wmlscriptc
+bcpio	application/x-bcpio
+vcd	application/x-cdlink
+pgn	application/x-chess-pgn
+cpio	application/x-cpio
+csh	application/x-csh
 dcr	application/x-director
+dir	application/x-director
 dxr	application/x-director
-fgd	application/x-director
-aam	application/x-authorware-map
-aas	application/x-authorware-seg
-aab	application/x-authorware-bin
-fh4	image/x-freehand
-fh7	image/x-freehand
-fh5	image/x-freehand
-fhc	image/x-freehand
-fh	image/x-freehand
-spl	application/futuresplash
-swf	application/x-shockwave-flash
 dvi	application/x-dvi
+spl	application/x-futuresplash
 gtar	application/x-gtar
 hdf	application/x-hdf
-hqx	application/mac-binhex40
-iv	application/x-inventor
+js	application/x-javascript
+skp	application/x-koan
+skd	application/x-koan
+skt	application/x-koan
+skm	application/x-koan
 latex	application/x-latex
-man	application/x-troff-man
-me	application/x-troff-me
-mif	application/x-mif
-ms	application/x-troff-ms
-oda	application/oda
-pdf	application/pdf
-rtf	application/rtf
-bcpio	application/x-bcpio
-cpio	application/x-cpio
-sv4cpio	application/x-sv4cpio
-sv4crc	application/x-sv4crc
-sh	application/x-shar
+nc	application/x-netcdf
+cdf	application/x-netcdf
+sh	application/x-sh
 shar	application/x-shar
+swf	application/x-shockwave-flash
 sit	application/x-stuffit
+sv4cpio	application/x-sv4cpio
+sv4crc	application/x-sv4crc
 tar	application/x-tar
+tcl	application/x-tcl
 tex	application/x-tex
-texi	application/x-texinfo
 texinfo	application/x-texinfo
+texi	application/x-texinfo
+t	application/x-troff
 tr	application/x-troff
 roff	application/x-troff
 man	application/x-troff-man
 me	application/x-troff-me
 ms	application/x-troff-ms
-zip	application/x-zip-compressed
-tsp	application/dsptype
-wsrc	application/x-wais-source
 ustar	application/x-ustar
-cdf	application/x-netcdf
-nc	application/x-netcdf
-doc	application/msword
-ppt	application/powerpoint
-
-crt	application/x-x509-ca-cert
-crl	application/x-pkcs7-crl
-
+src	application/x-wais-source
+xhtml	application/xhtml+xml
+xht	application/xhtml+xml
+zip	application/zip
+au	audio/basic
+snd	audio/basic
+mid	audio/midi
+midi	audio/midi
+kar	audio/midi
+mpga	audio/mpeg
+mp2	audio/mpeg
+mp3	audio/mpeg
+aif	audio/x-aiff
+aiff	audio/x-aiff
+aifc	audio/x-aiff
+m3u	audio/x-mpegurl
+ram	audio/x-pn-realaudio
+rm	audio/x-pn-realaudio
+rpm	audio/x-pn-realaudio-plugin
+ra	audio/x-realaudio
+wav	audio/x-wav
+pdb	chemical/x-pdb
+xyz	chemical/x-xyz
+bmp	image/bmp
+gif	image/gif
+ief	image/ief
+jpeg	image/jpeg
+jpg	image/jpeg
+jpe	image/jpeg
+png	image/png
+tiff	image/tiff
+tif	image/tiff
+djvu	image/vnd.djvu
+djv	image/vnd.djvu
+wbmp	image/vnd.wap.wbmp
+ras	image/x-cmu-raster
+pnm	image/x-portable-anymap
+pbm	image/x-portable-bitmap
+pgm	image/x-portable-graymap
+ppm	image/x-portable-pixmap
+rgb	image/x-rgb
+xbm	image/x-xbitmap
+xpm	image/x-xpixmap
+xwd	image/x-xwindowdump
+igs	model/iges
+iges	model/iges
+msh	model/mesh
+mesh	model/mesh
+silo	model/mesh
 wrl	model/vrml
 vrml	model/vrml
-mime	message/rfc822
-
-pac	application/x-ns-proxy-autoconfig
-
+css	text/css
+html	text/html; charset=%s
+htm	text/html; charset=%s
+asc	text/plain; charset=%s
+txt	text/plain; charset=%s
+rtx	text/richtext
+rtf	text/rtf
+sgml	text/sgml
+sgm	text/sgml
+tsv	text/tab-separated-values
 wml	text/vnd.wap.wml
-wmlc	application/vnd.wap.wmlc
 wmls	text/vnd.wap.wmlscript
-wmlsc	application/vnd.wap.wmlscriptc
-wbmp	image/vnd.wap.wbmp
+etx	text/x-setext
+xml	text/xml
+xsl	text/xml
+mpeg	video/mpeg
+mpg	video/mpeg
+mpe	video/mpeg
+qt	video/quicktime
+mov	video/quicktime
+mxu	video/vnd.mpegurl
+avi	video/x-msvideo
+movie	video/x-sgi-movie
+ice	x-conference/x-cooltalk
diff -ur thttpd-2.21b/mmc.c thttpd-2.21b-cool/mmc.c
--- thttpd-2.21b/mmc.c	Fri Apr 13 23:02:15 2001
+++ thttpd-2.21b-cool/mmc.c	Sun Sep 22 22:44:56 2002
@@ -70,6 +70,7 @@
     unsigned int hash;
     int hash_idx;
     struct MapStruct* next;
+	char nocache;
     } Map;
 
 
@@ -93,7 +94,7 @@
 
 
 void*
-mmc_map( char* filename, struct stat* sbP, struct timeval* nowP )
+mmc_map( char* filename, struct stat* sbP, struct timeval* nowP, int nocache )
     {
     time_t now;
     struct stat sb;
@@ -167,12 +168,13 @@
     m->ctime = sb.st_ctime;
     m->refcount = 1;
     m->reftime = now;
+	m->nocache = (char) nocache;
 
     /* Avoid doing anything for zero-length files; some systems don't like
     ** to mmap them, other systems dislike mallocing zero bytes.
     */
     if ( m->size == 0 )
-	m->addr = (void*) 1;	/* arbitrary non-NULL address */
+	m->addr = (void*) 5;	/* arbitrary non-NULL address */
     else
 	{
 #ifdef HAVE_MMAP
@@ -231,27 +233,32 @@
 void
 mmc_unmap( void* addr, struct stat* sbP, struct timeval* nowP )
     {
-    Map* m = (Map*) 0;
+    Map* m = (Map*) 0, **mm = &maps;
 
     /* Find the Map entry for this address.  First try a hash. */
     if ( sbP != (struct stat*) 0 )
 	{
 	m = find_hash( sbP->st_ino, sbP->st_dev, sbP->st_size, sbP->st_ctime );
-	if ( m != (Map*) 0 && m->addr != addr )
+	if ( m != (Map*) 0 && ( m->addr != addr || m->nocache == 1 ) )
 	    m = (Map*) 0;
 	}
     /* If that didn't work, try a full search. */
     if ( m == (Map*) 0 )
-	for ( m = maps; m != (Map*) 0; m = m->next )
+	for ( m = maps; m != (Map*) 0; m = m->next ) {
 	    if ( m->addr == addr )
 		break;
+		mm = &m->next;
+	}
     if ( m == (Map*) 0 )
 	syslog( LOG_ERR, "mmc_unmap failed to find entry!" );
     else if ( m->refcount <= 0 )
 	syslog( LOG_ERR, "mmc_unmap found zero or negative refcount!" );
     else
 	{
-	--m->refcount;
+	if ( --m->refcount == 0 && m->nocache == 1 ) {
+		really_unmap( mm );
+		return;
+	}
 	if ( nowP != (struct timeval*) 0 )
 	    m->reftime = nowP->tv_sec;
 	else
diff -ur thttpd-2.21b/mmc.h thttpd-2.21b-cool/mmc.h
--- thttpd-2.21b/mmc.h	Fri Apr 13 07:36:54 2001
+++ thttpd-2.21b-cool/mmc.h	Sun Sep 22 22:44:56 2002
@@ -31,8 +31,9 @@
 /* Returns an mmap()ed area for the given file, or (void*) 0 on errors.
 ** If you have a stat buffer on the file, pass it in, otherwise pass 0.
 ** Same for the current time.
+** Set nocache to 1, if this entry is supposed to be removed quickly.
 */
-extern void* mmc_map( char* filename, struct stat* sbP, struct timeval* nowP );
+extern void* mmc_map( char* filename, struct stat* sbP, struct timeval* nowP, int nocache );
 
 /* Done with an mmap()ed area that was returned by mmc_map().
 ** If you have a stat buffer on the file, pass it in, otherwise pass 0.
diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
--- thttpd-2.21b/thttpd.c	Tue Apr 24 00:41:57 2001
+++ thttpd-2.21b-cool/thttpd.c	Mon Sep 23 17:38:03 2002
@@ -95,6 +95,7 @@
     httpd_conn* hc;
     int tnums[MAXTHROTTLENUMS];         /* throttle indexes */
     int numtnums;
+    int keep_alive;
     long limit;
     time_t started_at;
     Timer* idle_read_timer;
@@ -111,12 +112,15 @@
 static int httpd_conn_count;
 
 /* The connection states. */
-#define CNST_FREE 0
-#define CNST_READING 1
-#define CNST_SENDING 2
-#define CNST_PAUSING 3
-#define CNST_LINGERING 4
-
+enum {
+	CNST_FREE = 0,
+	CNST_READING,
+	CNST_SENDING,
+	CNST_PAUSING,
+	CNST_LINGERING,
+	CNST_SENDING_RESP,
+	CNST_TOTAL_NR
+};
 
 static httpd_server* hs = (httpd_server*) 0;
 int terminate = 0;
@@ -140,11 +144,12 @@
 static int handle_newconnect( struct timeval* tvP, int listen_fd );
 static void handle_read( connecttab* c, struct timeval* tvP );
 static void handle_send( connecttab* c, struct timeval* tvP );
+static void handle_send_resp( connecttab* c, struct timeval* tvP );
 static void handle_linger( connecttab* c, struct timeval* tvP );
 static int check_throttles( connecttab* c );
 static void clear_throttles( connecttab* c, struct timeval* tvP );
 static void update_throttles( ClientData client_data, struct timeval* nowP );
-static void clear_connection( connecttab* c, struct timeval* tvP );
+static void clear_connection( connecttab* c, struct timeval* tvP, int );
 static void really_clear_connection( connecttab* c, struct timeval* tvP );
 static void idle_read_connection( ClientData client_data, struct timeval* nowP );
 static void idle_send_connection( ClientData client_data, struct timeval* nowP );
@@ -157,6 +162,12 @@
 static void logstats( struct timeval* nowP );
 static void thttpd_logstats( long secs );
 
+typedef void (*handler_func)(connecttab*, struct timeval *);
+
+handler_func handler_array[CNST_TOTAL_NR] =
+{NULL, handle_read, handle_send, NULL, handle_linger, handle_send_resp};
+
+#define RUN_HANDLER(type, c) handler_array[type](c, &tv)
 
 static void
 handle_term( int sig )
@@ -566,15 +577,17 @@
 	    if ( c == (connecttab*) 0 )
 		continue;
 	    hc = c->hc;
-	    if ( c->conn_state == CNST_READING &&
-		 fdwatch_check_fd( hc->conn_fd ) )
-		handle_read( c, &tv );
-	    else if ( c->conn_state == CNST_SENDING &&
-		 fdwatch_check_fd( hc->conn_fd ) )
-		handle_send( c, &tv );
-	    else if ( c->conn_state == CNST_LINGERING &&
-		 fdwatch_check_fd( hc->conn_fd ) )
-		handle_linger( c, &tv );
+	    switch (c->conn_state) {
+			case CNST_READING:
+			case CNST_SENDING:
+			case CNST_LINGERING:
+			case CNST_SENDING_RESP:
+				fdwatch_check_fd(hc->conn_fd);
+				RUN_HANDLER(c->conn_state, c);
+				break;
+		}
+		
+		
 	    }
 	tmr_run( &tv );
 
@@ -1196,8 +1209,10 @@
     logstats( &tv );
     for ( cnum = 0; cnum < maxconnects; ++cnum )
 	{
-	if ( connects[cnum].conn_state != CNST_FREE )
+	if ( connects[cnum].conn_state != CNST_FREE ) {
+	    httpd_complete_request( connects[cnum].hc, &tv, 1 );
 	    httpd_close_conn( connects[cnum].hc, &tv );
+	}
 	if ( connects[cnum].hc != (httpd_conn*) 0 )
 	    {
 	    httpd_destroy_conn( connects[cnum].hc );
@@ -1285,6 +1300,7 @@
 	c->linger_timer = (Timer*) 0;
 	c->bytes_sent = 0;
 	c->numtnums = 0;
+	c->keep_alive = 0;
 
 	/* Set the connection file descriptor to no-delay mode. */
 	httpd_set_ndelay( c->hc->conn_fd );
@@ -1297,12 +1313,40 @@
 	}
     }
 
+static void
+setup_sending(connecttab *c, int state, struct timeval *tvP)
+{
+	httpd_conn *hc = c->hc;
+    ClientData client_data;
+
+    c->conn_state = state;
+    c->started_at = tvP->tv_sec;
+    c->wouldblock_delay = 0;
+    client_data.p = c;
+    if (c->idle_read_timer != 0) {
+		tmr_cancel( c->idle_read_timer );
+	    c->idle_read_timer = (Timer*) 0;
+	}
+    c->idle_send_timer = tmr_create(
+	tvP, idle_send_connection, client_data, IDLE_SEND_TIMELIMIT * 1000L,
+	0 );
+    if ( c->idle_send_timer == (Timer*) 0 )
+	{
+	syslog( LOG_CRIT, "tmr_create(idle_send_connection) failed" );
+	exit( 1 );
+	}
+
+    fdwatch_del_fd( hc->conn_fd );
+    fdwatch_add_fd( hc->conn_fd, c, FDW_WRITE );
+}
+
+static void handle_request( connecttab *c, struct timeval *tvP);
+
 
 static void
 handle_read( connecttab* c, struct timeval* tvP )
     {
     int sz;
-    ClientData client_data;
     httpd_conn* hc = c->hc;
 
     /* Is there room in our buffer to read more bytes? */
@@ -1311,7 +1355,7 @@
 	if ( hc->read_size > 5000 )
 	    {
 	    httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
-	    clear_connection( c, tvP );
+	    clear_connection( c, tvP, 0 );
 	    return;
 	    }
 	httpd_realloc_str(
@@ -1327,29 +1371,69 @@
     ** EWOULDBLOCK; however, this apparently can happen if a packet gets
     ** garbled.
     */
-    if ( sz == 0 || ( sz < 0 && ( errno != EWOULDBLOCK ) ) )
-	{
-	httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
-	clear_connection( c, tvP );
+    if ( sz == 0 ) {
+    	if (! c->keep_alive) {
+		httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
+	}
+	clear_connection( c, tvP, 0 );
 	return;
+    } else if ( sz < 0 ) {
+    	if (errno != EWOULDBLOCK) {
+		httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
+		clear_connection( c, tvP, 0 );
+	} 
+	return;
+    }
+    if (sz > 0) hc->read_idx += sz;
+
+    /* 
+    ** if we start getting new data on this socket, "promote" it 
+    **  to read timeout
+    */
+    if ( hc->keep_alive ) {
+		ClientData client_data;
+
+	    if ( c->idle_read_timer != (Timer*) 0 )
+		{
+		tmr_cancel( c->idle_read_timer );
+		c->idle_read_timer = 0;
+		}
+
+	client_data.p = c;
+	c->idle_read_timer = tmr_create(
+	    tvP, idle_read_connection, client_data, IDLE_READ_TIMELIMIT * 1000L,
+	    0 );
+	if ( c->idle_read_timer == (Timer*) 0 )
+	    {
+	    syslog( LOG_CRIT, "tmr_create(idle_read_connection) failed" );
+	    exit( 1 );
+	    }
+    	
+    	hc->keep_alive = 0;
+    }
+	handle_request(c, tvP);
 	}
-    hc->read_idx += sz;
 
-    /* Do we have a complete request yet? */
+static void
+handle_request( connecttab *c, struct timeval *tvP)
+{
+    httpd_conn* hc = c->hc;
+    
+	/* Do we have a complete request yet? */
     switch ( httpd_got_request( hc ) )
 	{
 	case GR_NO_REQUEST:
 	return;
 	case GR_BAD_REQUEST:
 	httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 0 );
 	return;
 	}
 
     /* Yes.  Try parsing and resolving it. */
     if ( httpd_parse_request( hc ) < 0 )
 	{
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 0 );
 	return;
 	}
 
@@ -1358,7 +1442,7 @@
 	{
 	httpd_send_err(
 	    hc, 503, httpd_err503title, "", httpd_err503form, hc->encodedurl );
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 0 );
 	return;
 	}
 
@@ -1366,7 +1450,7 @@
     if ( httpd_start_request( hc, tvP ) < 0 )
 	{
 	/* Something went wrong.  Close down the connection. */
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 0 );
 	return;
 	}
 
@@ -1384,37 +1468,26 @@
 	{
 	/* No file address means someone else is handling it. */
 	c->bytes_sent = hc->bytes_sent;
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 1 );
 	return;
 	}
+	if (hc->file_address == (char *) 1) {
+		tmr_cancel( c->idle_read_timer );
+		c->idle_read_timer = (Timer*) 0;
+		c->wouldblock_delay = 0;
+		return;
+	}
     if ( c->bytes_sent >= c->bytes_to_send )
 	{
 	/* There's nothing to send. */
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 1 );
 	return;
 	}
 
     /* Cool, we have a valid connection and a file to send to it. */
-    c->conn_state = CNST_SENDING;
-    c->started_at = tvP->tv_sec;
-    c->wouldblock_delay = 0;
-    client_data.p = c;
-    tmr_cancel( c->idle_read_timer );
-    c->idle_read_timer = (Timer*) 0;
-    c->idle_send_timer = tmr_create(
-	tvP, idle_send_connection, client_data, IDLE_SEND_TIMELIMIT * 1000L,
-	0 );
-    if ( c->idle_send_timer == (Timer*) 0 )
-	{
-	syslog( LOG_CRIT, "tmr_create(idle_send_connection) failed" );
-	exit( 1 );
-	}
-
-    fdwatch_del_fd( hc->conn_fd );
-    fdwatch_add_fd( hc->conn_fd, c, FDW_WRITE );
+	setup_sending(c, CNST_SENDING, tvP);
     }
 
-
 static void
 handle_send( connecttab* c, struct timeval* tvP )
     {
@@ -1443,6 +1516,9 @@
 	iv[1].iov_base = &(hc->file_address[c->bytes_sent]);
 	iv[1].iov_len = MIN( c->bytes_to_send - c->bytes_sent, c->limit );
 	sz = writev( hc->conn_fd, iv, 2 );
+/*
+printf("**RESPONSE2 [%d]** len = %d\n%*.*s\n", hc->conn_fd, hc->responselen, hc->responselen, hc->responselen, hc->response); 
+*/
 	}
 
     if ( sz == 0 ||
@@ -1486,7 +1562,7 @@
 	*/
 	if ( errno != EPIPE && errno != EINVAL && errno != ECONNRESET )
 	    syslog( LOG_ERR, "write - %m sending %.80s", hc->encodedurl );
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 0 );
 	return;
 	}
 
@@ -1500,7 +1576,7 @@
 	    {
 	    /* Yes; move the unwritten part to the front of the buffer. */
 	    int newlen = hc->responselen - sz;
-	    (void) memcpy( hc->response, &(hc->response[sz]), newlen );
+	    (void) memmove( hc->response, &(hc->response[sz]), newlen );
 	    hc->responselen = newlen;
 	    sz = 0;
 	    }
@@ -1519,7 +1595,7 @@
     if ( c->bytes_sent >= c->bytes_to_send )
 	{
 	/* This conection is finished! */
-	clear_connection( c, tvP );
+	clear_connection( c, tvP, 1 );
 	return;
 	}
 
@@ -1560,6 +1636,9 @@
     char buf[1024];
     int r;
 
+/*
+printf("*LINGER read\n");
+*/
     /* In lingering-close mode we just read and ignore bytes.  An error
     ** or EOF ends things, otherwise we go until a timeout.
     */
@@ -1569,6 +1648,37 @@
     }
 
 
+static void
+handle_send_resp(connecttab *c, struct timeval *tvP)
+{
+	httpd_conn* hc = c->hc;
+	int n = send(hc->conn_fd, hc->response, hc->responselen, 0);
+	int dokeep = 1;
+	
+	if (n < 0) {
+		if (errno == EAGAIN)
+			return;
+
+		dokeep = 0;
+		goto clear;
+	}
+
+	tmr_reset( tvP, c->idle_send_timer );
+
+	if (n == hc->responselen) {
+clear:
+		hc->response = realloc(hc->response, hc->maxresponse + 1);
+		hc->responselen = 0;
+
+		clear_connection(c, tvP, dokeep);
+		return;
+	}
+
+	hc->responselen -= n;
+
+	memmove(hc->response, hc->response + n, hc->responselen);
+}
+
 static int
 check_throttles( connecttab* c )
     {
@@ -1635,12 +1745,17 @@
 
 
 static void
-clear_connection( connecttab* c, struct timeval* tvP )
+clear_connection( connecttab* c, struct timeval* tvP, int doKeep )
     {
     ClientData client_data;
+    int		linger;
 
     /* If we haven't actually sent the buffered response yet, do so now. */
-    httpd_write_response( c->hc );
+    if (c->hc->responselen && c->conn_state != CNST_SENDING_RESP) {
+		setup_sending(c, CNST_SENDING_RESP, tvP);
+
+		return;
+	}
 
     if ( c->idle_read_timer != (Timer*) 0 )
 	{
@@ -1669,13 +1784,45 @@
     ** circumstances that make a lingering close necessary.  If the flag
     ** isn't set we do the real close now.
     */
-    if ( c->hc->should_linger )
+
+    if ( c->hc->do_keep_alive && doKeep)
 	{
-	c->conn_state = CNST_LINGERING;
+		httpd_conn *hc = c->hc;
+	c->conn_state = CNST_READING;
+
+	client_data.p = c;
+	c->idle_read_timer = tmr_create(
+	    tvP, idle_read_connection, client_data, IDLE_KEEPALIVE_TIMELIMIT * 1000L,
+	    0 );
+	if ( c->idle_read_timer == (Timer*) 0 )
+	    {
+	    syslog( LOG_CRIT, "tmr_create(idle_read_connection) failed" );
+	    exit( 1 );
+	    }
+
+	c->bytes_sent = 0;
+	c->numtnums = 0;
+	c->keep_alive = 1;
+
+	httpd_complete_request( c->hc, tvP, 1 );
+
 	fdwatch_del_fd( c->hc->conn_fd );
 	fdwatch_add_fd( c->hc->conn_fd, c, FDW_READ );
+
+	httpd_request_reset( c->hc, 1 );
+
+	hc->read_idx -= hc->checked_idx;
+	memmove(hc->read_buf, hc->read_buf + hc->checked_idx, hc->read_idx);
+	hc->checked_idx = 0;
+	
 	/* Make sure we are still in no-delay mode. */
 	httpd_set_ndelay( c->hc->conn_fd );
+	handle_request(c, tvP);
+	}
+    else if ( c->hc->should_linger )
+        {
+	c->conn_state = CNST_LINGERING;
+
 	client_data.p = c;
 	c->linger_timer = tmr_create(
 	    tvP, linger_clear_connection, client_data, LINGER_TIME * 1000L, 0 );
@@ -1684,9 +1831,19 @@
 	    syslog( LOG_CRIT, "tmr_create(linger_clear_connection) failed" );
 	    exit( 1 );
 	    }
+
+    	httpd_complete_request( c->hc, tvP, 1 );
+
+	fdwatch_del_fd( c->hc->conn_fd );
+	fdwatch_add_fd( c->hc->conn_fd, c, FDW_READ );
+	/* Make sure we are still in no-delay mode. */
+	httpd_set_ndelay( c->hc->conn_fd );
 	}
-    else
+     else  
+        {
+    	httpd_complete_request( c->hc, tvP, !c->keep_alive );
 	really_clear_connection( c, tvP );
+	}
     }
 
 
@@ -1716,11 +1873,13 @@
     c->idle_read_timer = (Timer*) 0;
     if ( c->conn_state != CNST_FREE )
 	{
-	syslog( LOG_INFO,
-	    "%.80s connection timed out reading",
-	    httpd_ntoa( &c->hc->client_addr ) );
-	httpd_send_err( c->hc, 408, httpd_err408title, "", httpd_err408form, "" );
-	clear_connection( c, nowP );
+	if (! c->keep_alive) {
+		syslog( LOG_INFO,
+		    "%.80s connection timed out reading",
+		    httpd_ntoa( &c->hc->client_addr ) );
+		httpd_send_err( c->hc, 408, httpd_err408title, "", httpd_err408form, "" );
+	}
+	clear_connection( c, nowP, 0 );
 	}
     }
 
@@ -1737,7 +1896,7 @@
 	syslog( LOG_INFO,
 	    "%.80s connection timed out sending",
 	    httpd_ntoa( &c->hc->client_addr ) );
-	clear_connection( c, nowP );
+	clear_connection( c, nowP, 0 );
 	}
     }