-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpd.tcl
More file actions
1102 lines (1002 loc) · 32.2 KB
/
httpd.tcl
File metadata and controls
1102 lines (1002 loc) · 32.2 KB
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
# httpd.tcl --
#
# Core HTTP server, which dispatches all handling of requests
# to a synchronous, application specific response handler.
#
# This code was derived from Stephen Uhler's simple httpd server,
# distributed as part of TclHttpd 3.3. Its copyright is below:
# Simple Sample httpd/1.[01] server
# Stephen Uhler (c) 1996-1997 Sun Microsystems
#
# Copyright (c) 2002-2011 Mark Roseman. All rights reserved.
#
# $Id: httpd.tcl 7535 2011-02-01 18:20:33Z roseman $
# httpd --
#
# Public interface to HTTP server facilities.
# The following commands are available:
#
# httpd listen port handler ?address? ?protocol?
# httpd stop
# httpd return sock body ?-mimetype mimetype? ?-static seconds?
# httpd outputheader sock attr val
# httpd outputheaders sock
# httpd returnredirect sock url
# httpd returnnotmodified sock ?modtime?
# httpd error sock code params
# httpd returnfile sock file filename mimetype timestamp ?view? ?flag?
# httpd loadrequest sock varname ?queryvar?
# httpd decode str
# httpd getuploadticket
# httpd uploadprogress ticket
# httpd contenttype pathname
#
# Arguments:
# command Specifies the particular command to invoke.
# args Options required by the command.
# Results:
# Depends on command.
proc httpd {command args} {
global Httpd
set argc [llength $args]
switch -exact $command {
"listen" {
if {$argc==4} {
Httpd_Server [lindex $args 0] [lindex $args 1] \
[lindex $args 2] [lindex $args 3]
} elseif {$argc==3} {
Httpd_Server [lindex $args 0] [lindex $args 1] \
[lindex $args 2]
} elseif {$argc==2} {
Httpd_Server [lindex $args 0] [lindex $args 1]
} else {
error "wrong # args, should be: httpd listen port handler ?address? ?protocol?"
}
}
"stop" {
if {$argc==1 && [lindex $args 0]=="-ssl"} {
catch {close $Httpd(listenssl); unset Httpd(listenssl)}
return ""
} elseif {$argc==0} {
catch {close $Httpd(listen); unset Httpd(listen)}
catch {close $Httpd(listenssl); unset Httpd(listenssl)}
return ""
} else {
error "wrong # args, should be: httpd stop ?-ssl?"
}
}
"return" {
if {$argc>=2} {
set sock [lindex $args 0]
set body [lindex $args 1]
set mimetype "text/html"
set static ""
for {set i 2} {$i<[llength $args]} {incr i} {
switch -exact -- [lindex $args $i] {
"-mimetype" {set mimetype [lindex $args [incr i]]}
"-static" {set static [lindex $args [incr i]]}
default {error "bad option"}
}
}
HttpdReturn $sock $body $mimetype $static
return
}
error "wrong # args, should be \"httpd return sock body ?-mimetype mimetype? ?-static seconds?\""
}
"outputheader" {
if {$argc==3} {
set sock [lindex $args 0]
upvar #0 Httpd$sock data
append data(outputheaders) "[lindex $args 1]: [lindex $args 2]\n"
} else {
error "wrong # args, should be: httpd outputheader sock attr val"
}
}
"outputheaders" {
if {$argc==1} {
set sock [lindex $args 0]
upvar #0 Httpd$sock data
if {![info exists data(outputheaders)]} {return ""}
return $data(outputheaders)
} else {
error "wrong # args, should be: httpd outputheaders sock"
}
}
"returnredirect" {
if {$argc==2} {
HttpdReturnRedirect [lindex $args 0] [lindex $args 1]
} else {
error "wrong # args, should be: httpd returnredirect sock url"
}
}
"returnnotmodified" {
if {$argc==2} {
HttpdReturnNotModified [lindex $args 0] [lindex $args 1]
} elseif {$argc==1} {
HttpdReturnNotModified [lindex $args 0]
} else {
error "wrong # args, should be: httpd returnnotmodified sock ?modtime?"
}
}
"error" {
if {$argc==3} {
HttpdError [lindex $args 0] [lindex $args 1] [lindex $args 2]
} else {
error "wrong # args, should be: httpd error sock code params"
}
}
"returnfile" {
if {$argc==7} {
HttpdReturnFile [lindex $args 0] [lindex $args 1] \
[lindex $args 2] [lindex $args 3] [lindex $args 4] [lindex $args 5] [lindex $args 6]
} elseif {$argc==6} {
HttpdReturnFile [lindex $args 0] [lindex $args 1] \
[lindex $args 2] [lindex $args 3] [lindex $args 4] [lindex $args 5]
} elseif {$argc==5} {
HttpdReturnFile [lindex $args 0] [lindex $args 1] \
[lindex $args 2] [lindex $args 3] [lindex $args 4]
} else {
error "wrong # args, should be httpd returnfile sock file\
filename mimetype timestamp ?view? ?flag?"
}
}
"loadrequest" {
if {$argc==3} {
uplevel 1 upvar #0 Httpd[lindex $args 0] [lindex $args 1]
# parse query or postdata into [lindex $args 2]
upvar #0 Httpd[lindex $args 0] data
catch {uplevel 1 unset [lindex $args 2]}
set query ""; catch {set query $data(query)}
if {[info exists data(proto)] && $data(proto)=="POST" \
&& [info exists data(postdata)]} {
set query $data(postdata)
}
#parray data
#puts "QUERY is $query"
foreach {x} [split [string trim $query] &] {
# suprisingly, a regexp here is hugely slow - PR#142
set posn [string first "=" $x]
if {$posn!=-1 && $posn==[string last "=" $x]} {
set varname [string range $x 0 [expr $posn-1]]
set val [string range $x [expr $posn+1] end]
catch {
uplevel 1 set [lindex $args 2]($varname) [list [HttpdDecode $val]]
}
}
}
} elseif {$argc==2} {
uplevel 1 upvar #0 Httpd[lindex $args 0] [lindex $args 1]
} else {
error "wrong # args, should be: httpd loadrequest sock varname ?queryvar?"
}
}
"decode" {
if {$argc==1} {
return [HttpdDecode [lindex $args 0]]
} else {
error "wrong # args, should be: httpd decode str"
}
}
"getuploadticket" {
if {$argc==0} {
if {![info exists Httpd(uploadticketcounter)]} {
set Httpd(uploadticketcounter) 0
}
incr Httpd(uploadticketcounter)
set tail [string range [expr int(rand()*100000+100000)] end-4 end]
return "up${Httpd(uploadticketcounter)}${tail}"
} else {
error "wrong # args, should be: httpd getuploadticket"
}
}
"uploadprogress" {
if {$argc==1} {
set t [lindex $args 0]
if {[info exists Httpd(uploadticket-sock-$t)]} {
set sock $Httpd(uploadticket-sock-$t)
upvar #0 Httpd$sock data
if {[info exists data(upload-totallength)] \
&& [info exists data(upload-currentlength)]} {
return [list $data(upload-currentlength) \
$data(upload-totallength)]
}
}
return ""
} else {
error "wrong # args, should be: httpd uploadprogress ticket"
}
}
"contenttype" {
if {$argc==1} {
return [HttpdContentType [lindex $args 0]]
} else {
error "wrong # args, should be: httpd contenttype pathname"
}
}
}
}
# Httpd is a global array containing the global server state
# port: The port this server is serving
# responsehandler: The application-specific handler to deal with http requests
# listen: the main listening socket id
# accepts: a count of accepted connections so far
# maxtime: The max time (msec) allowed to complete an http request (1h)
# uploadmaxtime: The max time (msec) allowed to complete an upload (12h)
# maxused: The max # of requests for a socket
# uploadticket-sock-<ticket>: Socket associated with an upload ticket
# HTTP/1.[01] error codes (the ones we use)
array set HttpdErrors {
204 {No Content}
400 {Bad Request}
404 {Not Found}
408 {Request Timeout}
411 {Length Required}
419 {Expectation Failed}
500 {Internal Server Error}
503 {Service Unavailable}
504 {Service Temporarily Unavailable}
}
array set Httpd {
bufsize 32768
maxtime 3600000
uploadmaxtime 43200000
maxused 25
}
# Start the server by listening for connections on the desired port.
proc Httpd_Server {port responsehandler {address {}} {protocol {}}} {
global Httpd
if {$protocol!="ssl" && $protocol!=""} {
error "bad protocol"
}
if {$protocol=="ssl"} {
if {[info commands tls::socket]==""} {
catch {bgerror "ssl not available (tls::socket)"}
error "ssl not available"
}
if {![info exists Httpd(tlsinitialized)]} {
if {[catch {
tls::init -request 0 -require 0 -ssl2 1 -ssl3 1 \
-tls1 0 -certfile public.pem -keyfile private.pem
} errmsg]!=0} {
catch {bgerror "could not initialize tls: $errmsg"}
error "could not initialize tls: $errmsg"
}
set Httpd(tlsinitialized) 1
}
catch {close $Httpd(listenssl)};# it might already be running
array set Httpd [list sslport $port responsehandler $responsehandler]
array set Httpd [list accepts 0 requests 0 errors 0]
if {$address!=""} {
set Httpd(listenssl) [tls::socket -server [list HttpdAccept https] -myaddr $address $port]
} else {
set Httpd(listenssl) [tls::socket -server [list HttpdAccept https] $port]
}
return $Httpd(sslport)
} else {
catch {close $Httpd(listen)};# it might already be running
array set Httpd [list port $port responsehandler $responsehandler]
array set Httpd [list accepts 0 requests 0 errors 0]
if {$address!=""} {
set Httpd(listen) [socket -server [list HttpdAccept http] -myaddr $address $port]
} else {
set Httpd(listen) [socket -server [list HttpdAccept http] $port]
}
### HttpdWatchdog $Httpd(listen)
return $Httpd(port)
}
}
proc HttpdWatchdog {sock} {
global Httpd
after 5000 "HttpdWatchdog $sock"
set l "ERROR"; catch {set l $Httpd(listen)}
set e "ERROR"; catch {set e [eof $sock]}
set f "ERROR"; catch {set f [fconfigure $sock]}
##logger bgerror "[clock format [clock seconds]] sock=$sock l=$l e=$e f=$f"
}
# Accept a new connection from the server and set up a handler
# to read the request from the client.
proc HttpdAccept {protocol sock ipaddr {port {}} {protocol {}}} {
global Httpd
upvar #0 Httpd$sock data
incr Httpd(accepts)
if {$protocol=="https"} {
fconfigure $sock -blocking 0
fileevent $sock readable [list HttpdHandshake $sock]
} else {
HttpdReset $sock http $Httpd(maxused) $ipaddr
Httpd_Log $sock Connect $ipaddr $port
}
}
# Complete the SSL handshake.
proc HttpdHandshake {sock} {
global Httpd errorCode
if {[catch {tls::handshake $sock} complete]} {
if {[lindex $errorCode 1]=="EAGAIN"} {
return
}
HttpdSockDone $sock 1
} elseif {$complete} {
HttpdReset $sock https $Httpd(maxused)
}
}
# Initialize or reset the socket state
proc HttpdReset {sock protocol left {ipaddr ""}} {
global Httpd
upvar #0 Httpd$sock data
array set data [list state start linemode 1 version 0 left $left protocol $protocol]
if {$ipaddr!=""} {set data(ipaddr) $ipaddr}
set data(cancel) [after $Httpd(maxtime) [list HttpdTimeout $sock]]
fconfigure $sock -blocking 0 -buffersize $Httpd(bufsize) \
-translation {auto crlf}
fileevent $sock readable [list HttpdRead $sock]
}
# Read data from a client request
# 1) read the request line
# 2) read the mime headers
# 3) read the additional data (if post && content-length not satisfied)
proc HttpdRead {sock} {
global Httpd
upvar #0 Httpd$sock data
if {![info exists data(linemode)]} {
Httpd_Log $sock Error "Missing socket data on read."
HttpdSockDone $sock 1
return
}
# Use line mode to read the request and the mime headers
if {$data(linemode)} {
if {[catch {gets $sock line} readCount]} {
Httpd_Log $sock Error "error on read: $readCount"
HttpdSockDone $sock 1
return
}
set state [string compare $readCount 0],$data(state)
switch -glob -- $state {
1,start {
if {[regexp {(HEAD|POST|GET) ([^?]+)\??([^ ]*) HTTP/1.([01])} $line \
x data(proto) data(url) data(query) data(version)]} {
set data(state) mime
incr Httpd(requests)
Httpd_Log $sock Request $data(left) $line
} else {
HttpdError $sock 400 $line
}
}
0,start {
Httpd_Log $sock Warning "Initial blank line fetching request"
}
1,mime {
if {[regexp {([^:]+):[ ]*(.*)} $line {} key value]} {
set key [string tolower $key]
set data(key) $key
if {[info exists data(mime,$key)]} {
append data(mime,$key) ", $value"
} else {
set data(mime,$key) $value
}
} elseif {[regexp {^[ ]+(.+)} $line {} value] && \
[info exists data(key)]} {
append data(mime,$data($key)) " " $value
} else {
HttpdError $sock 400 $line
}
}
0,mime {
if {$data(proto) == "POST" && \
[info exists data(mime,content-length)]} {
if {[info exists data(mime,content-type)] \
&& [string match -nocase "multipart/form-data;*" \
$data(mime,content-type)]} {
# switch to upload file mode
HttpdUploadInit $sock
} else {
set data(linemode) 0
set data(count) $data(mime,content-length)
if {$data(version) && [info exists data(mime,expect]} {
if {$data(mime,expect) == "100-continue"} {
puts $sock "100 Continue HTTP/1.1\n"
flush $sock
} else {
HttpdError $sock 419 $data(mime,expect)
}
}
fconfigure $sock -translation {binary crlf}
}
} elseif {$data(proto) != "POST"} {
HttpdRespond $sock
} else {
HttpdError $sock 411 "Confusing mime headers"
}
}
-1,* {
if {[eof $sock]} {
Httpd_Log $sock Error "Broken connection fetching request"
HttpdSockDone $sock 1
} else {
# puts stderr "Partial read, retrying"
}
}
default {
HttpdError $sock 404 "Invalid http state: $state,[eof $sock]"
}
}
# Use counted mode to get the post data
} elseif {![eof $sock]} {
append data(postdata) [read $sock $data(count)]
set data(count) [expr {$data(mime,content-length) - \
[string length $data(postdata)]}]
if {$data(count) == 0} {
HttpdRespond $sock
}
} else {
Httpd_Log $sock Error "Broken connection reading POST data"
HttpdSockDone $sock 1
}
}
# Done with the socket, either close it, or set up for next fetch
# sock: The socket I'm done with
# close: If true, close the socket, otherwise set up for reuse
proc HttpdSockDone {sock close} {
Httpd_Log $sock SockDone start $close
global Httpd
upvar #0 Httpd$sock data
if {[info exists data(cancel)] && $data(cancel)!=""} {
after cancel $data(cancel)
}
if {[info exists data(fcopy-fd)]} {
catch {close $data(fcopy-fd)} msg
if {$msg!=""} {
Httpd_Log $sock Error "aborting download on $data(fcopy-fd) $msg"
}
}
set left 0; catch {set left [incr data(left) -1]}
set protocol http; catch {set protocol $data(protocol)}
unset -nocomplain data
if {$close} {
# read any extra data off the socket; otherwise the close
# can generate a RST rather than a FIN packet; see PR#181
catch {
fconfigure $sock -blocking 0
read $sock
}; # PR#539
catch {close $sock}
} else {
HttpdReset $sock $protocol $left
}
if {[info exists Httpd(responsehandler)]} {
Httpd_Log $sock SockDone calling response handler
if {[catch {
uplevel #0 $Httpd(responsehandler) done $sock
} errmsg]!=0} {
catch {bgerror "Error doing http done callback on $sock"}
}
}
return ""
}
# A timeout happened
proc HttpdTimeout {sock} {
global Httpd
upvar #0 Httpd$sock data
HttpdError $sock 408
}
# Handle file system queries. This is a place holder for a more
# generic dispatch mechanism.
proc HttpdRespond {sock} {
global Httpd HttpdUrlCache
if {[info exists Httpd(responsehandler)]} {
upvar #0 Httpd$sock data
# The 'inprogress' stuff is used to prevent reentry to a handler that
# is part-way through progress; this is different but related to the
# 'pending' error condition we also handle below.
#
# Some routines (includemgr, centralauth) need to make http::geturl
# calls which can reenter the event loop. By doing the inprogress
# check, we can prevent calling the handler again while the first part
# of the http::geturl is running. When that first part completes, but
# before the async http call as a whole completes, the routine may well
# generate a "pending" error.
if {[info exists data(inprogress)] && $data(inprogress)==1} {return}
if {[info exists data(sendingfile)] && $data(sendingfile)==1} {return}
Httpd_Log $sock Respond start
set data(outputheaders) {}
set data(inprogress) 1
# set url $data(url)
if {[catch {
# set timer [time {
uplevel #0 $Httpd(responsehandler) handle $sock
# }]; puts stderr "-->$timer $url"
} errmsg]!=0} {
Httpd_Log $sock Respond completed error $errmsg
unset -nocomplain data(inprogress)
if {$errmsg=="pending"} {
Httpd_Log $sock Respond pending
# we're waiting on something else to complete, so no sense having our
# own HttpdRead keep getting called asking us to do something with this
# socket
fileevent $sock readable {}
} else {
upvar #0 Httpd$sock data
set url ""
if {[info exists data(url)]} {
set url $data(url)
}
HttpdError $sock 500 "Error processing request"
catch {bgerror "Error processing handler for $url:\n$::errorInfo"}
}
} else {
Httpd_Log $sock Respond completed ok
if {[info exists data] && (![info exists data(sendingfile)] || $data(sendingfile)!=1)} {
Httpd_Log $sock "Return had not been called during request processing; closing connection. data=[array names data]"
HttpdSockDone $sock 1
}
}
return
}
error "no responsehandler defined"
}
# Callback when file is done being output to client
# in: The fd for the file being copied
# sock: The client socket
# close: close the socket if true
# bytes: The # of bytes copied
# error: The error message (if any)
proc HttpdCopyDone {in sock close bytes {error {}}} {
global Httpd
upvar #0 Httpd$sock data
close $in
unset -nocomplain data(fcopy-fd)
unset -nocomplain data(sendingfile)
Httpd_Log $sock Done $bytes bytes
HttpdSockDone $sock $close
}
# convert the file suffix into a mime type
# add your own types as needed
array set HttpdMimeType {
{} text/plain
.txt text/plain
.html text/html
.css text/css
.js text/javascript
.gif image/gif
.jpg image/jpeg
.png image/png
.swf application/swf
.ico image/x-icon
}
proc HttpdContentType {path} {
global HttpdMimeType
set type text/html
catch {set type $HttpdMimeType([file extension $path])}
return $type
}
# Generic error response.
set HttpdErrorFormat {
<title>Error: %1$s</title>
Got the error: <b>%2$s</b><br>
while trying to obtain <b>%3$s</b>
}
# Respond with an error reply
# sock: The socket handle to the client
# code: The httpd error code
# args: Additional information for error logging
proc HttpdError {sock code args} {
upvar #0 Httpd$sock data
global Httpd HttpdErrors HttpdErrorFormat
append data(url) ""
set version "1"; if {[info exists data(version)] && $data(version)!=""} {set version $data(version)}
incr Httpd(errors)
set message [format $HttpdErrorFormat $code $HttpdErrors($code) $data(url)]
append head "HTTP/1.$version $code $HttpdErrors($code)" \n
append head "Date: [HttpdDate [clock seconds]]" \n
append head "Connection: close" \n
append head "Content-Length: [string length $message]" \n
# Because there is an error condition, the socket may be "dead"
catch {
fconfigure $sock -translation crlf
puts -nonewline $sock $head\n$message
flush $sock
} reason
HttpdSockDone $sock 1
Httpd_Log $sock Error $code $HttpdErrors($code) $args $reason
}
# Generate a date string in HTTP format.
proc HttpdDate {seconds {gmt ""}} {
if {$gmt==1} {
return [clock format $seconds -format {%a, %d %b %Y %T %Z} -gmt 1]
} else {
return [clock format $seconds -format {%a, %d %b %Y %T %Z}]
}
}
# Log an Httpd transaction.
# This should be replaced as needed.
proc Httpd_Log {sock args} {
catch {
logger httpd " httpd $sock $args"
}
##puts stderr "LOG: $sock $args"
}
# Decode url-encoded strings.
proc HttpdCgiMap {data} {
regsub -all {([][$\\])} $data {\\\1} data
regsub -all {%([0-9a-fA-F][0-9a-fA-F])} $data {[format %c 0x\1]} data
return [subst $data]
}
# from ncgi::encode
for {set i 1} {$i <= 256} {incr i} {
set c [format %c $i]
if {![string match \[a-zA-Z0-9\] $c]} {
set HttpdMap($c) %[format %.2X $i]
}
}
# These are handled specially
array set HttpdMap {
" " + \n %0D%0A
}
set HttpdCharmap [array get HttpdMap]
proc HttpdCgiEncode {string} {
return [string map $::HttpdCharmap $string]
}
# HttpdReturn --
#
# Return a HTML page to a client, and close the connection.
#
# Arguments:
# sock The socket to write to.
# body HTML body of page.
# mimetype If specified, MIME type to use instead of text/html.
# staticseconds If specified, this is static content, last modified
# at the 'clock seconds' time given by this parameter.
# Results:
# None.
proc HttpdReturn {sock body {mimetype "text/html"} {staticseconds ""}} {
puts $sock "HTTP/1.0 200 OK"
if {$mimetype=="text/html"} {
puts $sock "Content-Type: text/html; charset=utf-8"
} else {
puts $sock "Content-Type: $mimetype"
}
puts $sock "Date: [HttpdDate [clock seconds]]"
puts $sock "Connection: close"
if {$staticseconds==""} {
puts $sock "Expires: now"
puts $sock "Cache-Control: no-cache, must-revalidate"
puts $sock "Pragma: no-cache"
} else {
puts $sock "ETag: \"$staticseconds\""
puts $sock "Last-Modified: [HttpdDate $staticseconds 1]"
puts $sock "Expires: [HttpdDate [expr $staticseconds+86400] 1]"
puts $sock "Cache-Control: max-age=86400, must-revalidate"
}
upvar #0 Httpd$sock data
if {[info exists data(outputheaders)] && $data(outputheaders)!=""} {
puts -nonewline $sock $data(outputheaders)
}
puts $sock ""
if {$mimetype=="text/html"} {
fconfigure $sock -encoding utf-8
} else {
fconfigure $sock -translation binary
}
puts $sock $body
Httpd_Log $sock HttpdReturn
HttpdSockDone $sock 1
}
# HttpdReturnRedirect --
#
# Redirect the browser to another page.
#
# Arguments:
# sock The socket to write to.
# url URL to redirect user to.
# Results:
# None.
proc HttpdReturnRedirect {sock url} {
puts $sock "HTTP/1.0 302 Redirection"
puts $sock "Location: $url"
puts $sock "Connection: close"
upvar #0 Httpd$sock data
if {[info exists data(outputheaders)] && $data(outputheaders)!=""} {
puts -nonewline $sock $data(outputheaders)
}
puts $sock ""
puts $sock "Redirect to <a href=\"$url\">$url</a>"
Httpd_Log $sock HttpdReturnRedirect
HttpdSockDone $sock 1
}
# HttpdReturnNotModified --
#
# Return a not modified result.
#
# Arguments:
# sock The socket to write to.
# modtime If specified, modified time of resource.
# Results:
# None.
proc HttpdReturnNotModified {sock {modtime ""}} {
puts $sock "HTTP/1.0 304 Not Modified"
puts $sock "Connection: close"
if {$modtime!=""} {
puts $sock "ETag: \"$modtime\""
puts $sock "Last-Modified: [HttpdDate $modtime 1]"
}
upvar #0 Httpd$sock data
if {[info exists data(outputheaders)] && $data(outputheaders)!=""} {
puts -nonewline $sock $data(outputheaders)
}
puts $sock ""
puts $sock ""
Httpd_Log $sock HttpdReturnNotModified
HttpdSockDone $sock 1
}
# HttpdReturnFile --
#
# Return a file to a client, and close the connection.
#
# Arguments:
# sock The socket to write to.
# file File on disk.
# filename Name to return.
# mimetype MIME content type of file.
# timestamp File modification date.
# view If specified as "1", don't return as attachment.
# flag If specified as "-static" emit ETag etc. headers.
# Results:
# None.
proc HttpdReturnFile {sock file filename mimetype timestamp {view ""} {flag ""}} {
upvar #0 Httpd$sock data
set version "1"; if {[info exists data(version)] && $data(version)!=""} {set version $data(version)}
puts $sock "HTTP/1.$version 200 Data follows"
puts $sock "Date: [HttpdDate [clock seconds]]"
if {$view!="1"} {
puts $sock "Content-Disposition: attachment; filename=\"$filename\""
} else {
puts $sock "Content-Disposition: inline"
}
puts $sock "Last-Modified: [HttpdDate $timestamp]"
if {$flag=="-static"} {
puts $sock "ETag: \"$timestamp\""
puts $sock "Expires: [HttpdDate [expr $timestamp+86400]]"
puts $sock "Cache-Control: max-age=86400, must-revalidate"
}
puts $sock "Content-Type: $mimetype"
set size [file size $file]
puts $sock "Content-Length: $size"
set close 1
if {$close} {
puts $sock "Connection: close"
}
puts $sock ""
flush $sock
if {$data(proto)!="HEAD"} {
set data(sendingfile) 1
fconfigure $sock -translation binary
set in [open $file]
fconfigure $in -translation binary
set data(fcopy-fd) $in
if {$size<0 || $size>4294967295} {set size -1}
fcopy $in $sock -size $size \
-command [list HttpdCopyDone $in $sock $close]
} else {
HttpdSockDone $sock $close
}
}
proc HttpdDecode {str} {
# rewrite "+" back to space
# protect \ from quoting another '\'
set str [string map [list + { } "\\" "\\\\"] $str]
# prepare to process all %-escapes
regsub -all -- {%([A-Fa-f0-9][A-Fa-f0-9])} $str {\\u00\1} str
# process \u unicode mapped chars
set str [subst -novar -nocommand $str]
set str [encoding convertfrom utf-8 $str]
regsub -all {\r} $str {} str
return $str
}
# Stuff from here on originally based on TclHttpd3.4.1 lib/upload.tcl,
# but now substantially modified
proc HttpdUploadInit {sock} {
global Httpd
upvar #0 Httpd$sock data
if {![regexp {boundary=(.*)$} $data(mime,content-type) dummy boundary]} {
HttpdError $sock 400 "Could not find boundary"
}
# PR#441.. make sure we don't time out after the default 10 minutes
if {[info exists data(cancel)] && $data(cancel)!=""} {
after cancel $data(cancel)
}
set data(cancel) [after $Httpd(uploadmaxtime) [list HttpdTimeout $sock]]
set data(formvars) ""
set data(upload-boundary) $boundary
set data(upload-formname) ""
set data(upload-filename) ""
if {[info exists data(mime,content-length)]} {
set data(upload-totallength) $data(mime,content-length)
}
set data(upload-currentlength) 0
fileevent $sock readable [list HttpdUploadFindBoundary $sock]
}
proc HttpdUploadFindBoundary {sock} {
upvar #0 Httpd$sock data
if {[eof $sock]} {
HttpdSockDone $sock 1
return
}
if {[gets $sock line]>0} {
# NOTE: don't use regexp here, as the boundary can include characters like
# '+' which will be interpreted by regexp T#468
if {[string first "--$data(upload-boundary)" $line]==0} {
fileevent $sock readable [list HttpdUploadReadHeader $sock]
}
}
}
proc HttpdUploadReadHeader {sock} {
global Httpd
upvar #0 Httpd$sock data
if {[eof $sock]} {
HttpdSockDone $sock 1
return
}
while {[gets $sock line]>=0} {
### puts $line
if {[string length [string trim $line]]==0} {
# end of headers
fconfigure $sock -translation binary -encoding binary
set varname $data(upload-formname)
lappend data(formvars) $varname
if {[info exists data(upload-contenttype)]} {
set data(formvar-${varname}-contenttype) [string trim $data(upload-contenttype)]
}
if {[info exists data(upload-filename)] && $data(upload-filename)!=""} {
set data(formvar-${varname}-filename) \
[HttpdDecode $data(upload-filename)]
}
if {![info exists data(upload-filename)] || $data(upload-filename)==""} {
fileevent $sock readable [list HttpdUploadReadPart $sock]
} else {
set data(upload-lastLineExists) 0
if {![info exists Httpd(uploadcounter)]} {
set Httpd(uploadcounter) 0
}
set fn "upload[incr Httpd(uploadcounter)]"
set data(upload-fd) [open $fn w]
set data(formvar-${varname}-uploadfilename) $fn
if {[info exists data(formvar-uploadticket-value)]} {
set t $data(formvar-uploadticket-value)
set data(upload-ticket) $t
set Httpd(uploadticket-sock-$t) $sock
}
fconfigure $data(upload-fd) -translation binary -encoding binary
fileevent $sock readable [list HttpdUploadReadFile $sock]
}
return
}
if {[regexp {([^: ]+):(.*)$} $line x hdrname value]} {
set hdrname [string tolower $hdrname]
if {[string equal $hdrname "content-disposition"]} {
foreach {x} [split [string trim $value] ";"] {
if {[regexp -- {(.*)=\"*([^\"]*)\"*\;*} [string trim $x] dummy var val]} {
if {[string tolower $var]=="name"} {
set data(upload-formname) $val
} elseif {[string tolower $var]=="filename"} {
set data(upload-filename) $val
}
}
}
} elseif {[string equal $hdrname "content-type"]} {
set data(upload-contenttype) $value
} else {
# ignore other headers for now
}
}
}
}
proc HttpdUploadReadPart {sock} {
upvar #0 Httpd$sock data
if {[eof $sock]} {
HttpdSockDone $sock 1
return
}
if {[gets $sock line] > 0} {
### puts $line
if {[string first "--$data(upload-boundary)" $line]==0} {
set l [string length $data(upload-boundary)]
set end [string range $line [expr $l+2] [expr $l+3]]
if {$end == "--"} {
catch {
# Trim the string to remove carriage returns.