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
  
// This is a roxen module. (c) Informationsvävarna AB 1996. 
 
constant cvs_version = "$Id: http.pike,v 1.60 1998/03/11 19:26:56 neotron Exp $"; 
// HTTP protocol module. 
#include <config.h> 
private inherit "roxenlib"; 
// int first; 
#if efun(gethrtime) 
# define HRTIME() gethrtime() 
# define HRSEC(X) ((int)((X)*1000000)) 
# define SECHR(X) ((X)/(float)1000000) 
#else 
# define HRTIME() (predef::time()) 
# define HRSEC(X) (X) 
# define SECHR(X) ((float)(X)) 
#endif 
 
#ifdef PROFILE 
int req_time = HRTIME(); 
#endif 
 
constant decode=roxen->decode; 
constant find_supports=roxen->find_supports; 
constant version=roxen->version; 
constant handle=roxen->handle; 
constant _query=roxen->query; 
constant thepipe = roxen->pipe; 
constant _time=predef::time; 
 
private static array(string) cache; 
private static int wanted_data, have_data; 
 
object conf; 
 
#include <roxen.h> 
#include <module.h> 
 
#undef QUERY 
#if constant(cpp) 
#define QUERY(X)    _query( #X ) 
#else /* !constant(cpp) */ 
#define QUERY(X)    _query("X") 
#endif /* constant(cpp) */ 
 
int time; 
string raw_url; 
int do_not_disconnect; 
 
mapping (string:string) variables = ([ ]); 
mapping (string:mixed) misc = ([ ]); 
mapping (string:string) cookies = ([ ]); 
 
multiset   (string) prestate     = (< >); 
multiset   (string) config       = (< >); 
multiset   (string) supports; 
multiset (string) pragma    = (< >); 
 
string remoteaddr, host; 
 
array  (string) client; 
array  (string) referer; 
 
mapping file; 
 
object my_fd; /* The client. */ 
object pipe; 
 
// string range; 
string prot; 
string clientprot; 
string method; 
 
string realfile, virtfile; 
string rest_query=""; 
string raw; 
string query; 
string not_query; 
string extra_extension = ""; // special hack for the language module 
string data, leftovers; 
array (int|string) auth; 
string rawauth, realauth; 
string since; 
 
// Parse a HTTP/1.1 HTTP/1.0 or 0.9 request, including form data and 
// state variables.  Return 0 if more is expected, 1 if done, and -1 
// if fatal error. 
 
void end(string|void a,int|void b); 
 
private void setup_pipe() 
{ 
  if(!my_fd)  
  { 
    end(); 
    return; 
  } 
  if(!pipe) pipe=thepipe(); 
} 
 
void send(string|object what, int|void len) 
{ 
  if(!what) return; 
  if(!pipe) setup_pipe(); 
  if(!pipe) return; 
  if(stringp(what))  pipe->write(what); 
  else               pipe->input(what,len); 
} 
 
string scan_for_query( string f ) 
{ 
  if(sscanf(f,"%s?%s", f, query) == 2) 
  { 
    string v, a, b; 
 
    foreach(query / "&", v) 
      if(sscanf(v, "%s=%s", a, b) == 2) 
      { 
        a = http_decode_string(replace(a, "+", " ")); 
        b = http_decode_string(replace(b, "+", " ")); 
         
        if(variables[ a ]) 
          variables[ a ] +=  "\0" + b; 
        else 
          variables[ a ] = b; 
      } else 
        if(strlen( rest_query )) 
          rest_query += "&" + http_decode_string( v ); 
        else 
          rest_query = http_decode_string( v ); 
    rest_query=replace(rest_query, "+", "\000"); /* IDIOTIC STUPID STANDARD */ 
  }  
  return f; 
} 
 
 
private int really_set_config(array mod_config) 
{ 
  string url, m; 
  string base; 
  base = conf->query("MyWorldLocation")||"/"; 
  if(supports->cookies) 
  { 
#ifdef REQUEST_DEBUG 
    perror("Setting cookie..\n"); 
#endif 
    if(mod_config) 
      foreach(mod_config, m) 
        if(m[-1]=='-') 
          config[m[1..]]=0; 
        else 
          config[m]=1; 
       
    if(sscanf(replace(raw_url,({"%3c","%3e","%3C","%3E" }), 
                      ({"<",">","<",">"})),"/<%*s>/%s",url)!=2) 
      url = "/"; 
 
    if ((base[-1] == '/') && (strlen(url) && url[0] == '/')) { 
      url = base + url[1..]; 
    } else { 
      url = base + url; 
    } 
 
    my_fd->write(prot + " 302 Config in cookie!\r\n" 
                 "Set-Cookie: " 
                  + http_roxen_config_cookie(indices(config) * ",") + "\r\n" 
                 "Location: " + url + "\r\n" 
                 "Content-Type: text/html\r\n" 
                 "Content-Length: 0\r\n\r\n"); 
  } else { 
#ifdef REQUEST_DEBUG 
    perror("Setting {config} for user without Cookie support..\n"); 
#endif 
    if(mod_config) 
      foreach(mod_config, m) 
        if(m[-1]=='-') 
          prestate[m[1..]]=0; 
        else 
          prestate[m]=1; 
       
    if (sscanf(replace(raw_url, ({ "%3c", "%3e", "%3C", "%3E" }),  
                       ({ "<", ">", "<", ">" })),   "/<%*s>/%s", url) == 2) { 
      url = "/" + url; 
    } 
    if (sscanf(replace(url, ({ "%28", "%29" }), ({ "(", ")" })), 
               "/(%*s)/%s", url) == 2) { 
      url = "/" + url; 
    } 
 
    url = add_pre_state(url, prestate); 
 
    if (base[-1] == '/') { 
      url = base + url[1..]; 
    } else { 
      url = base + url; 
    } 
 
    my_fd->write(prot + " 302 Config In Prestate!\r\n" 
                 "\r\nLocation: " + url + "\r\n" 
                 "Content-Type: text/html\r\n" 
                 "Content-Length: 0\r\n\r\n"); 
  } 
  return -2; 
} 
 
private static mixed f, line; 
 
private int parse_got(string s) 
{ 
  multiset (string) sup; 
  array mod_config; 
  string a, b, linename, contents; 
  int config_in_url; 
 
//  roxen->httpobjects[my_id] = "Parsed data..."; 
  raw = s; 
 
  if (!line) { 
    int start = search(s, "\r\n"); 
 
    if ((< -1, 0 >)[start]) { 
      // Not enough data, or malformed request. 
      return ([ -1:0, 0:2 ])[start]; 
    } 
    line = s[..start-1]; 
 
    // Parse the command 
    start = search(line, " "); 
    if (start != -1) { 
      method = upper_case(line[..start-1]); 
 
      int end = search(reverse(line[start+1..]), " "); 
      if (end != -1) { 
        f = line[start+1..sizeof(line)-(end+2)]; 
        clientprot = line[sizeof(line)-end..]; 
        if (clientprot != "HTTP/0.9") { 
          prot = "HTTP/1.0"; 
 
          // Check that the request is complete 
          int end; 
          if ((end = search(s, "\r\n\r\n")) == -1) { 
            // No, we need more data. 
            return 0; 
          } 
          data = s[end+4..]; 
          s = s[sizeof(line)+2..end-1]; 
        } else { 
          prot = clientprot; 
          data = s[sizeof(line)+2..]; 
          s = "";   // No headers. 
        } 
      } else { 
        f = line[start+1..]; 
        prot = clientprot = "HTTP/0.9"; 
        data = s[sizeof(line)+2..]; 
        s = "";             // No headers. 
      } 
    } else { 
      method = upper_case(line); 
      f = "/"; 
      prot = clientprot = "HTTP/0.9"; 
    } 
  } else { 
    // HTTP/1.0 or later 
    // Check that the request is complete 
    int end; 
    if ((end = search(s, "\r\n\r\n")) == -1) { 
      // No, we still need more data. 
      return 0; 
    } 
    data = s[end+4..]; 
    s = s[sizeof(line)+2..end-1]; 
  } 
 
 
  if(method == "PING") 
  {  
    my_fd->write("PONG\n");  
    return -2;  
  } 
 
  raw_url    = f; 
  time       = _time(1); 
   
 
  if(!remoteaddr) 
  { 
    if(my_fd) catch(remoteaddr = ((my_fd->query_address()||"")/" ")[0]); 
    if(!remoteaddr) { 
      end(); 
      return 0; 
    } 
  } 
 
  f = scan_for_query( f ); 
//  f = http_decode_string( f ); 
 
  if (sscanf(f, "/<%s>/%s", a, f)==2) 
  { 
    config_in_url = 1; 
    mod_config = (a/","); 
    f = "/"+f; 
  } 
   
  if ((sscanf(f, "/(%s)/%s", a, f)==2) && strlen(a)) 
  { 
    prestate = aggregate_multiset(@(a/","-({""}))); 
    f = "/"+f; 
  } 
   
  not_query = simplify_path(http_decode_string(f)); 
 
  if(sizeof(s)) { 
//    sscanf(s, "%s\r\n\r\n%s", s, data); 
 
//     s = replace(s, "\n\t", ", ") - "\r";  
//     Handle rfc822 continuation lines and strip \r 
   
    foreach(s/"\r\n" - ({ "" }), line) 
    { 
      linename=contents=0; 
      sscanf(line, "%s:%s", linename, contents); 
      if(linename&&contents) 
      { 
        linename=lower_case(linename); 
        sscanf(contents, "%*[\t ]%s", contents); 
         
        if(strlen(contents)) 
        { 
          switch (linename) { 
          case "content-length": 
            misc->len = (int)(contents-" "); 
            if(!misc->len) continue; 
            if(method == "POST") 
            { 
              if(!data) data=""; 
              int l = misc->len-1; /* Length - 1 */ 
              wanted_data=l; 
              have_data=strlen(data); 
 
              if(strlen(data) <= l) // \r are included.  
              { 
                return 0; 
              } 
              leftovers = data[l+1..]; 
              data = data[..l]; 
              switch(lower_case(((misc["content-type"]||"")/";")[0]-" ")) 
              { 
              default: // Normal form data. 
                string v; 
                if(l < 200000) 
                { 
                  foreach(replace(data-"\n", "+", " ")/"&", v) 
                    if(sscanf(v, "%s=%s", a, b) == 2) 
                    { 
                      a = http_decode_string( a ); 
                      b = http_decode_string( b ); 
                      
                      if(variables[ a ]) 
                        variables[ a ] +=  "\0" + b; 
                      else 
                        variables[ a ] = b; 
                    } 
                } 
                break; 
 
              case "multipart/form-data": 
//            perror("Multipart/form-data post detected\n"); 
                object messg = MIME.Message(data, misc); 
                foreach(messg->body_parts, object part) { 
                  if(part->disp_params->filename) { 
                    variables[part->disp_params->name]=part->getdata(); 
                    variables[part->disp_params->name+".filename"]= 
                      part->disp_params->filename; 
                    if(!misc->files) 
                      misc->files = ({ part->disp_params->name }); 
                    else 
                      misc->files += ({ part->disp_params->name }); 
                  } else { 
                    variables[part->disp_params->name]=part->getdata(); 
                  } 
                } 
                break; 
              } 
            } 
            break; 
           
          case "authorization": 
            string *y; 
            rawauth = contents; 
            y = contents /= " "; 
            if(sizeof(y) < 2) 
              break; 
            y[1] = decode(y[1]); 
            realauth=y[1]; 
            if(conf && conf->auth_module) 
              y = conf->auth_module->auth( y, this_object() ); 
            auth=y; 
            break; 
           
          case "proxy-authorization": 
            string *y; 
            y = contents /= " "; 
            if(sizeof(y) < 2) 
              break; 
            y[1] = decode(y[1]); 
            if(conf && conf->auth_module) 
              y = conf->auth_module->auth( y, this_object() ); 
            misc->proxyauth=y; 
            break; 
           
          case "pragma": 
            pragma|=aggregate_multiset(@replace(contents, " ", "")/ ","); 
            break; 
 
          case "user-agent": 
            if(!client) 
            { 
              sscanf(contents, "%s via", contents); 
              client = contents/" " - ({ "" }); 
            } 
            break; 
 
            /* Some of M$'s non-standard user-agent info */ 
          case "ua-pixels":     /* Screen resolution */ 
          case "ua-color":      /* Color scheme */ 
          case "ua-os":         /* OS-name */ 
          case "ua-cpu":        /* CPU-type */ 
            /* None of the above are interresting or useful for us */ 
            /* IGNORED */ 
            break; 
 
          case "referer": 
            referer = contents/" "; 
            break; 
             
           case "extension": 
#ifdef DEBUG 
          perror("Client extension: "+contents+"\n"); 
#endif 
           
           case "connection": 
            contents = lower_case(contents); 
             
           case "content-type": 
            misc[linename] = lower_case(contents); 
            break; 
 
          case "accept": 
          case "accept-encoding": 
          case "accept-charset": 
          case "accept-language": 
          case "session-id": 
          case "message-id": 
          case "from": 
            if(misc[linename]) 
              misc[linename] += (contents-" ") / ","; 
            else 
              misc[linename] = (contents-" ") / ","; 
            break; 
 
          case "cookie": /* This header is quite heavily parsed */ 
            string c; 
            misc->cookies = contents; 
            foreach(contents/";", c) 
            { 
              string name, value; 
              while(c[0]==' ') c=c[1..]; 
              if(sscanf(c, "%s=%s", name, value) == 2) 
              { 
                value=http_decode_string(value); 
                name=http_decode_string(name); 
                cookies[ name ]=value; 
                if(name == "RoxenConfig" && strlen(value)) 
                { 
                  array tmpconfig = value/"," + ({ }); 
                  string m; 
 
                  if(mod_config && sizeof(mod_config)) 
                    foreach(mod_config, m) 
                      if(!strlen(m)) 
                      { continue; } /* Bug in parser force { and } */ 
                      else if(m[0]=='-') 
                        tmpconfig -= ({ m[1..] }); 
                      else 
                        tmpconfig |= ({ m }); 
                  mod_config = 0; 
                  config = aggregate_multiset(@tmpconfig); 
                } 
              } 
            } 
            break; 
 
          case "host": 
          case "proxy-connection": 
          case "security-scheme": 
            misc[linename] = contents; 
            break;       
 
          case "proxy-by": 
          case "proxy-maintainer": 
          case "proxy-software": 
          case "mime-version": 
            break; 
             
          case "if-modified-since": 
            since=contents; 
            break; 
             
          case "via": 
          case "cache-control": 
          case "negotiate": 
          case "forwarded": 
            misc[linename]=contents; 
            break; 
 
#ifdef DEBUG 
        default: 
            /*   x-* headers are experimental.    */ 
            if(linename[0] != 'x') 
              perror("Unknown header: `"+linename+"' -> `"+contents+"'\n"); 
#endif 
        } 
        } 
      } 
    }  
  } 
  if(!client) client = ({ "unknown" }); 
  if(!supports) 
    supports = find_supports(lower_case(client*" ")); 
  if(!referer) referer = ({ }); 
  if(misc->proxyauth) { 
    // The Proxy-authorization header should be removed... So there. 
    mixed tmp1,tmp2; 
     
    foreach(tmp2 = (raw / "\n"), tmp1) { 
      if(!search(lower_case(tmp1), "proxy-authorization:")) 
        tmp2 -= ({tmp1}); 
    } 
    raw = tmp2 * "\n";  
  } 
 
  if(config_in_url) { 
    return really_set_config( mod_config ); 
  } 
  if(!supports->cookies) 
    config = prestate; 
  else 
    if(conf 
       && QUERY(set_cookie) 
       && !cookies->RoxenUserID && strlen(not_query) 
       && not_query[0]=='/' && method!="PUT") 
    { 
      if (!(QUERY(set_cookie_only_once) && 
            cache_lookup("hosts_for_cookie",remoteaddr))) { 
        misc->moreheads = ([ "Set-Cookie":http_roxen_id_cookie(), ]); 
      } 
      if (QUERY(set_cookie_only_once)) 
        cache_set("hosts_for_cookie",remoteaddr,1); 
    } 
  return 1; // Done. 
} 
 
void disconnect() 
{ 
  if(do_not_disconnect)  return; 
  catch(file && file->close()); 
  file = 0; 
  my_fd = 0; 
  destruct(); 
} 
 
void end(string|void s, int|void keepit) 
{ 
#ifdef PROFILE 
  if(conf) 
  { 
    float elapsed = SECHR(HRTIME()-req_time); 
    array p; 
    if(!(p=conf->profile_map[not_query])) 
      p = conf->profile_map[not_query] = ({0,0.0,0.0}); 
    conf->profile_map[not_query][0]++; 
    p[1] += elapsed; 
    if(elapsed > p[2]) p[2]=elapsed; 
  } 
#endif 
 
#ifdef KEEP_ALIVE 
  if(keepit && 
     (!(file->raw || file->len<=0)) 
     && (misc->connection || (prot == "HTTP/1.1")) 
     && my_fd) 
  { 
    // Now.. Transfer control to a new http-object. Reset all variables etc.. 
    object o = object_program(this_object())(); 
    o->remoteaddr = remoteaddr; 
    o->supports = supports; 
    o->host = host; 
    o->client = client; 
    object fd = my_fd; 
    my_fd=0; 
    o->chain(fd,conf,leftovers); 
    disconnect(); 
    return; 
  } 
#endif 
 
  if(objectp(my_fd)) 
  { 
    catch { 
      my_fd->set_close_callback(0); 
      my_fd->set_read_callback(0); 
//       my_fd->set_blocking(); 
      if(s) my_fd->write(s); 
      my_fd->close(); 
      destruct(my_fd); 
    }; 
    my_fd = 0; 
  } 
  disconnect();   
} 
 
static void do_timeout(mapping foo) 
{ 
  int elapsed = _time()-time; 
  if(elapsed > 30) 
  { 
    end("HTTP/1.0 408 Timeout\r\n" 
        "Content-type: text/plain\r\n" 
        "Server: Roxen Challenger\r\n" 
        "\r\n" 
        "Your connection timed out.\n" 
        "Please try again.\n"); 
  } else { 
    // premature call_out... *¤#!" 
    call_out(do_timeout, 10); 
  } 
} 
 
 
 
mapping internal_error(array err) 
{ 
  if(QUERY(show_internals)) 
    file = http_low_answer(500, "<h1>Error: Internal server error.</h1>" 
                           + "<font size=+1><pre>"+ describe_backtrace(err) 
                           + "</pre></font>"); 
  else 
    file = http_low_answer(500, "<h1>Error: The server failed to " 
                           "fulfill your query.</h1>"); 
   
   
  report_error("Internal server error: " + 
               describe_backtrace(err) + "\n"); 
} 
 
int wants_more() 
{ 
  return !!cache; 
} 
 
constant errors = 
([ 
  200:"200 OK", 
  201:"201 URI follows", 
  202:"202 Accepted", 
  203:"203 Provisional Information", 
  204:"204 No Content", 
   
  300:"300 Moved", 
  301:"301 Permanent Relocation", 
  302:"302 Temporary Relocation", 
  303:"303 Temporary Relocation method and URI", 
  304:"304 Not Modified", 
 
  400:"400 Bad Request", 
  401:"401 Access denied", 
  402:"402 Payment Required", 
  403:"403 Forbidden", 
  404:"404 No such file or directory.", 
  405:"405 Method not allowed", 
  407:"407 Proxy authorization needed", 
  408:"408 Request timeout", 
  409:"409 Conflict", 
  410:"410 This document is no more. It has gone to meet it's creator. It is gone. It will not be coming back. Give up. I promise. There is no such file or directory.", 
   
  500:"500 Internal Server Error.", 
  501:"501 Not Implemented", 
  502:"502 Gateway Timeout", 
  503:"503 Service unavailable", 
   
  ]); 
 
 
void do_log() 
{ 
  if(conf) 
  { 
    int len; 
    if(pipe) file->len = pipe->bytes_sent(); 
    if(conf) 
    { 
      if(file->len > 0) conf->sent+=file->len; 
      conf->log(file, this_object()); 
    } 
  } 
  end("",1); 
  return; 
} 
 
void handle_request( ) 
{ 
  mixed *err; 
  int tmp; 
  function funp; 
  mapping heads; 
  string head_string; 
  object thiso=this_object(); 
 
  remove_call_out(do_timeout); 
 
  if(conf) 
  { 
//  perror("Handle request, got conf.\n"); 
    object oc = conf; 
    foreach(conf->first_modules(), funp)  
    { 
      if(file = funp( thiso)) break; 
      if(conf != oc) { 
        handle_request(); 
        return; 
      } 
    }     
    if(!file) err=catch(file = conf->get_file(thiso)); 
 
    if(err) internal_error(err); 
 
    if(!mappingp(file)) 
      foreach(conf->last_modules(), funp) if(file = funp(thiso)) break; 
  } else if(err=catch(file = roxen->configuration_parse( thiso ))) { 
    if(err==-1) return; 
    internal_error(err); 
  } 
 
  if(!mappingp(file)) 
  { 
    if(misc->error_code) 
      file = http_low_answer(misc->error_code, errors[misc->error]); 
    else if(method != "GET" && method != "HEAD" && method != "POST") 
      file = http_low_answer(501, "Not implemented."); 
    else 
      if(catch { 
        file=http_low_answer(404, 
                             replace(parse_rxml(conf->query("ZNoSuchFile"), 
                                                thiso), 
                                     ({"$File", "$Me"}),  
                                     ({not_query, 
                                       conf->query("MyWorldLocation")})));}) 
        internal_error(err); 
  } else { 
    if((file->file == -1) || file->leave_me)  
    { 
      if(do_not_disconnect) return; 
      my_fd = 0; objectp(file) && file->close(); file = 0; 
      return; 
    } 
 
    if(file->type == "raw")  file->raw = 1; 
    else if(!file->type)     file->type="text/plain"; 
  } 
   
  if(!file->raw && prot != "HTTP/0.9") 
  { 
    string h; 
    heads= 
      (["MIME-Version":(file["mime-version"] || "1.0"), 
        "Content-type":file["type"], 
        "Server":replace(version(), " ", "·"), 
        "Date":http_date(time) ]);     
 
    if(file->encoding) 
      heads["Content-Encoding"] = file->encoding; 
     
    if(!file->error)  
      file->error=200; 
     
    if(file->expires) 
      heads->Expires = http_date(file->expires); 
 
    if(!file->len) 
    { 
      if(objectp(file->file)) 
        if(!file->stat && !(file->stat=misc->stat)) 
          file->stat = (int *)file->file->stat(); 
      array fstat; 
      if(arrayp(fstat = file->stat)) 
      { 
        if(file->file && !file->len) 
          file->len = fstat[1]; 
     
     
        heads["Last-Modified"] = http_date(fstat[3]); 
         
        if(since) 
        { 
          if(is_modified(since, fstat[3], fstat[1])) 
          { 
            file->error = 304; 
            file->file = 0; 
            file->data=""; 
//        method=""; 
          } 
        } 
      } 
      if(stringp(file->data))  
        file->len += strlen(file->data); 
    } 
     
    if(mappingp(file->extra_heads)) { 
      heads |= file->extra_heads; 
    } 
 
    if(mappingp(misc->moreheads)) { 
      heads |= misc->moreheads; 
    } 
     
    array myheads = ({prot+" "+(file->rettext||errors[file->error])}); 
    foreach(indices(heads), h) 
      if(arrayp(heads[h])) 
        foreach(heads[h], tmp) 
          myheads += ({ `+(h,": ", tmp)}); 
      else 
        myheads +=  ({ `+(h, ": ", heads[h])}); 
     
 
    if(file->len > -1) 
      myheads += ({"Content-length: " + file->len }); 
#ifdef KEEP_ALIVE 
    myheads += ({ "Connection: Keep-Alive" }); 
#endif 
    head_string = (myheads+({"",""}))*"\r\n"; 
     
    if(conf) conf->hsent+=strlen(head_string||""); 
  } 
 
  if(method == "HEAD") 
  { 
    file->file = 0; 
    file->data=""; 
  } 
 
 
  if(!leftovers) leftovers = data||""; 
 
  if(file->len > 0 && file->len < 2000) 
  { 
    my_fd->write(head_string + (file->file?file->file->read():file->data)); 
    do_log(); 
    return; 
  } 
 
  if(head_string) send(head_string); 
 
   
 
  if(method != "HEAD") 
  { 
    if(file->data && strlen(file->data)) 
      send(file->data, file->len); 
    if(file->file)   
      send(file->file, file->len); 
  } 
  if (pipe) { 
    pipe->set_done_callback( do_log ); 
    pipe->output(my_fd); 
  } else { 
    my_fd->close(); 
    do_log(); 
  } 
} 
 
/* We got some data on a socket. 
 * =================================================  
 */ 
int processed; 
void got_data(mixed fooid, string s) 
{ 
  int tmp; 
  remove_call_out(do_timeout); 
  call_out(do_timeout, 30); // Close down if we don't get more data  
                         // within 30 seconds. Should be more than enough. 
  if(wanted_data) 
  { 
    if(strlen(s)+have_data < wanted_data) 
    { 
      cache += ({ s }); 
      have_data += strlen(s); 
      return; 
    } 
  } 
   
  if(cache)  
  { 
    s = cache*""+s;  
    cache = 0; 
  } 
  sscanf(s, "%*[\n\r]%s", s); 
  if(strlen(s)) tmp = parse_got(s); 
 
  switch(-tmp) 
  {  
   case 0: 
    cache = ({ s });            // More on the way. 
    return; 
     
   case 1: 
    end(prot+" 500 Stupid Client Error\r\nContent-Length: 0\r\n\r\n"); 
    return;                 // Stupid request. 
     
   case 2: 
    end(); 
    return; 
  } 
 
  if(conf) 
  { 
    conf->received += strlen(s); 
    conf->requests++; 
  } 
 
  my_fd->set_close_callback(0);  
  my_fd->set_read_callback(0);  
//   my_fd->set_blocking(); 
  processed=1; 
#ifdef THREADS 
  roxen->handle(this_object()->handle_request); 
#else 
  handle_request(); 
#endif 
} 
 
/* Get a somewhat identical copy of this object, used when doing  
 * 'simulated' requests. */ 
 
object clone_me() 
{ 
  object c,t; 
  c=object_program(t=this_object())(); 
 
// c->first = first; 
  c->conf = conf; 
  c->time = time; 
  c->raw_url = raw_url; 
// c->do_not_disconnect = do_not_disconnect;  // No use where there is no fd.. 
  c->variables = copy_value(variables); 
  c->misc = copy_value(misc); 
  c->misc->orig = t; 
 
  c->prestate = prestate; 
  c->supports = supports; 
  c->config = config; 
 
  c->remoteaddr = remoteaddr; 
  c->host = host; 
 
  c->client = client; 
  c->referer = referer; 
  c->pragma = pragma; 
 
  c->cookies = cookies; 
// file.. 
  c->my_fd = 0; 
// pipe.. 
 
  c->prot = prot; 
  c->clientprot = clientprot; 
  c->method = method; 
   
// realfile virtfile   // Should not be copied.   
  c->rest_query = rest_query; 
  c->raw = raw; 
  c->query = query; 
  c->not_query = not_query; 
  c->extra_extension = extra_extension; 
  c->data = data; 
   
  c->auth = auth; 
  c->realauth = realauth; 
  c->rawauth = rawauth; 
  c->since = since; 
  return c; 
} 
 
void clean() 
{ 
  if(!(my_fd && objectp(my_fd))) end(); 
  else if((_time(1) - time) > 4800) end(); 
} 
 
void create(object f, object c) 
{ 
  if(f) 
  { 
    my_fd = f; 
    conf = c; 
    mark_fd(my_fd->query_fd(), "HTTP connection"); 
    my_fd->set_close_callback(end); 
    my_fd->set_read_callback(got_data); 
    // No need to wait more than 30 seconds to get more data. 
    call_out(do_timeout, 30); 
  } 
} 
 
void chain(object f, object c, string le) 
{ 
  my_fd = f; 
  conf = c; 
  do_not_disconnect=-1; 
  if(strlen(le)) got_data(0,le); 
  if(!my_fd) 
  { 
    if(do_not_disconnect == -1) 
    { 
      do_not_disconnect=0; 
      disconnect(); 
    } 
  } else if(!processed) { 
    f->set_close_callback(end); 
    f->set_read_callback(got_data); 
  } 
} 
 
// void chain(object fd, object conf, string leftovers) 
// { 
//   call_out(real_chain,0,fd,conf,leftovers); 
// }