/usr/share/tcltk/tcllib1.19/httpd/httpd.tcl is in tcllib 1.19-dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 | ###
# Amalgamated package for httpd
# Do not edit directly, tweak the source in src/ and rerun
# build.tcl
###
package require Tcl 8.6
package provide httpd 4.1.1
namespace eval ::httpd {}
set ::httpd::version 4.1.1
###
# START: core.tcl
###
###
# Author: Sean Woods, yoda@etoyoc.com
##
# Adapted from the "minihttpd.tcl" file distributed with Tclhttpd
#
# The working elements have been updated to operate as a TclOO object
# running with Tcl 8.6+. Global variables and hard coded tables are
# now resident with the object, allowing this server to be more easily
# embedded another program, as well as be adapted and extended to
# support the SCGI module
###
package require uri
package require cron
package require coroutine
package require tool
package require mime
package require fileutil
package require websocket
###
# Standard library of HTTP/SCGI content
# Each of these classes are intended to be mixed into
# either an HTTPD or SCGI reply
###
package require Markdown
package require fileutil::magic::filetype
namespace eval httpd::content {}
namespace eval ::url {}
namespace eval ::httpd {}
namespace eval ::scgi {}
###
# END: core.tcl
###
###
# START: reply.tcl
###
###
# Define the reply class
###
::tool::define ::httpd::reply {
array error_codes {
200 {Data follows}
204 {No Content}
302 {Found}
304 {Not Modified}
400 {Bad Request}
401 {Authorization Required}
403 {Permission denied}
404 {Not Found}
408 {Request Timeout}
411 {Length Required}
419 {Expectation Failed}
500 {Server Internal Error}
501 {Server Busy}
503 {Service Unavailable}
504 {Service Temporarily Unavailable}
505 {Internal Server Error}
}
constructor {ServerObj args} {
my variable chan
oo::objdefine [self] forward <server> $ServerObj
foreach {field value} [::oo::meta::args_to_options {*}$args] {
my meta set config $field: $value
}
}
###
# clean up on exit
###
destructor {
my close
}
method close {} {
my variable chan
if {[info exists chan] && $chan ne {}} {
catch {flush $chan}
catch {close $chan}
}
}
method HttpHeaders {sock {debug {}}} {
set result {}
###
# Set up a channel event to stream the data from the socket line by
# line. When a blank line is read, the HttpHeaderLine method will send
# a flag which will terminate the vwait.
#
# We do this rather than entering blocking mode to prevent the process
# from locking up if it's starved for input. (Or in the case of the test
# suite, when we are opening a blocking channel on the other side of the
# socket back to ourselves.)
###
chan configure $sock -translation {auto crlf} -blocking 0 -buffering line
try {
while 1 {
set readCount [::coroutine::util::gets_safety $sock 4096 line]
if {$readCount==0} break
append result $line \n
}
} trap {POSIX EBUSY} {err info} {
# Happens...
} on error {err info} {
puts "ERROR $err"
puts [dict print $info]
tailcall my destroy
}
###
# Return our buffer
###
return $result
}
method HttpHeaders_Default {} {
return {Status {200 OK}
Content-Size 0
Content-Type {text/html; charset=UTF-8}
Cache-Control {no-cache}
Connection close}
}
method dispatch {newsock datastate} {
my http_info replace $datastate
my variable chan rawrequest dipatched_time
set chan $newsock
chan event $chan readable {}
chan configure $chan -translation {auto crlf} -buffering line
set dispatched_time [clock seconds]
try {
# Initialize the reply
my reset
# Process the incoming MIME headers
set rawrequest [my HttpHeaders $chan]
my request parse $rawrequest
# Invoke the URL implementation.
my content
} on error {err info} {
#dict print $info
#puts stderr $::errorInfo
my error 500 $err [dict get $info -errorinfo]
} finally {
my output
}
}
dictobj http_info http_info {
initialize {
CONTENT_LENGTH 0
}
netstring {
set result {}
foreach {name value} $%VARNAME% {
append result $name \x00 $value \x00
}
return "[string length $result]:$result,"
}
}
method error {code {msg {}} {errorInfo {}}} {
my http_info set HTTP_ERROR $code
my reset
my variable error_codes
set qheaders [my http_info dump]
if {![info exists error_codes($code)]} {
set errorstring "Unknown Error Code"
} else {
set errorstring $error_codes($code)
}
dict with qheaders {}
my reply replace {}
my reply set Status "$code $errorstring"
my reply set Content-Type {text/html; charset=UTF-8}
my puts "
<HTML>
<HEAD>
<TITLE>$code $errorstring</TITLE>
</HEAD>
<BODY>"
if {$msg eq {}} {
my puts "
Got the error <b>$code $errorstring</b>
<p>
while trying to obtain $REQUEST_URI
"
} else {
my puts "
Guru meditation #[clock seconds]
<p>
The server encountered an internal error:
<p>
<pre>$msg</pre>
<p>
For deeper understanding:
<p>
<pre>$errorInfo</pre>
"
}
my puts "</BODY>
</HTML>"
}
###
# REPLACE ME:
# This method is the "meat" of your application.
# It writes to the result buffer via the "puts" method
# and can tweak the headers via "meta put header_reply"
###
method content {} {
my puts "<HTML>"
my puts "<BODY>"
my puts "<H1>HELLO WORLD!</H1>"
my puts "</BODY>"
my puts "</HTML>"
}
method EncodeStatus {status} {
return "HTTP/1.0 $status"
}
method output {} {
my variable chan
chan event $chan writable [namespace code {my DoOutput}]
}
###
# Output the result or error to the channel
# and destroy this object
###
method DoOutput {} {
my variable reply_body chan
chan event $chan writable {}
catch {
chan configure $chan -translation {binary binary}
###
# Return dynamic content
###
set length [string length $reply_body]
set result {}
if {${length} > 0} {
my reply set Content-Length [string length $reply_body]
append result [my reply output] \n
append result $reply_body
} else {
append result [my reply output]
}
chan puts -nonewline $chan $result
} err
puts $err
my destroy
}
method Url_Decode data {
regsub -all {\+} $data " " 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]
}
method FormData {} {
my variable chan formdata rawrequest
# Run this only once
if {[info exists formdata]} {
return $formdata
}
if {![my request exists Content-Length]} {
set length 0
} else {
set length [my request get Content-Length]
}
set formdata {}
if {[my http_info get REQUEST_METHOD] in {"POST" "PUSH"}} {
set rawtype [my request get Content-Type]
if {[string toupper [string range $rawtype 0 8]] ne "MULTIPART"} {
set type $rawtype
} else {
set type multipart
}
switch $type {
multipart {
###
# Ok, Multipart MIME is troublesome, farm out the parsing to a dedicated tool
###
set body $rawrequest
append body \n [my PostData $length]
set token [::mime::initialize -string $body]
foreach item [::mime::getheader $token -names] {
dict set formdata $item [::mime::getheader $token $item]
}
foreach item {content encoding params parts size} {
dict set formdata MIME_[string toupper $item] [::mime::getproperty $token $item]
}
dict set formdata MIME_TOKEN $token
}
application/x-www-form-urlencoded {
# These foreach loops are structured this way to ensure there are matched
# name/value pairs. Sometimes query data gets garbled.
set body [my PostData $length]
set result {}
foreach pair [split $body "&"] {
foreach {name value} [split $pair "="] {
lappend formdata [my Url_Decode $name] [my Url_Decode $value]
}
}
}
}
} else {
foreach pair [split [my http_info getnull QUERY_STRING] "&"] {
foreach {name value} [split $pair "="] {
lappend formdata [my Url_Decode $name] [my Url_Decode $value]
}
}
}
return $formdata
}
###
# Minimalist MIME Header Parser
###
method MimeParse mimetext {
set data(mimeorder) {}
foreach line [split $mimetext \n] {
# This regexp picks up
# key: value
# MIME headers. MIME headers may be continue with a line
# that starts with spaces or a tab
if {[string length [string trim $line]]==0} break
if {[regexp {^([^ :]+):[ ]*(.*)} $line dummy key value]} {
# The following allows something to
# recreate the headers exactly
lappend data(headerlist) $key $value
# The rest of this makes it easier to pick out
# headers from the data(mime,headername) array
#set key [string tolower $key]
if {[info exists data(mime,$key)]} {
append data(mime,$key) ,$value
} else {
set data(mime,$key) $value
lappend data(mimeorder) $key
}
set data(key) $key
} elseif {[regexp {^[ ]+(.*)} $line dummy value]} {
# Are there really continuation lines in the spec?
if {[info exists data(key)]} {
append data(mime,$data(key)) " " $value
} else {
my error 400 "INVALID HTTP HEADER FORMAT: $line"
tailcall my output
}
} else {
my error 400 "INVALID HTTP HEADER FORMAT: $line"
tailcall my output
}
}
###
# To make life easier for our SCGI implementation rig things
# such that CONTENT_LENGTH is always first
# Also map all headers specified in rfc2616 to their canonical case
###
set result {}
dict set result Content-Length 0
foreach {key} $data(mimeorder) {
switch [string tolower $key] {
content-length {
set key Content-Length
}
content-encoding {
set key Content-Encoding
}
content-language {
set key Content-Language
}
content-location {
set key Content-Location
}
content-md5 {
set key Content-MD5
}
content-range {
set key Content-Range
}
content-type {
set key Content-Type
}
expires {
set key Expires
}
last-modified {
set key Last-Modified
}
}
dict set result $key $data(mime,$key)
}
return $result
}
method PostData {length} {
my variable postdata
# Run this only once
if {[info exists postdata]} {
return $postdata
}
set postdata {}
if {[my http_info get REQUEST_METHOD] in {"POST" "PUSH"}} {
my variable chan
chan configure $chan -translation binary -blocking 0 -buffering full -buffersize 4096
set postdata [::coroutine::util::read $chan $length]
}
return $postdata
}
method TransferComplete args {
foreach c $args {
catch {close $c}
}
my destroy
}
###
# Append to the result buffer
###
method puts line {
my variable reply_body
append reply_body $line \n
}
method RequestFind {field} {
my variable request
if {[dict exists $request $field]} {
return $field
}
foreach item [dict gets $request] {
if {[string tolower $item] eq [string tolower $field]} {
return $item
}
}
return $field
}
dictobj request request {
field {
tailcall my RequestFind [lindex $args 0]
}
get {
set field [my RequestFind [lindex $args 0]]
if {![dict exists $request $field]} {
return {}
}
tailcall dict get $request $field
}
getnull {
set field [my RequestFind [lindex $args 0]]
if {![dict exists $request $field]} {
return {}
}
tailcall dict get $request $field
}
exists {
set field [my RequestFind [lindex $args 0]]
tailcall dict exists $request $field
}
parse {
set request [my MimeParse [lindex $args 0]]
}
}
dictobj reply reply {
output {
set result {}
if {![dict exists $reply Status]} {
set status {200 OK}
} else {
set status [dict get $reply Status]
}
set result "[my EncodeStatus $status]\n"
foreach {f v} $reply {
if {$f in {Status}} continue
append result "[string trimright $f :]: $v\n"
}
#append result \n
return $result
}
}
###
# Reset the result
###
method reset {} {
my variable reply_body
my reply replace [my HttpHeaders_Default]
my reply set Server [my <server> cget server_string]
my reply set Date [my timestamp]
set reply_body {}
}
###
# Return true of this class as waited too long to respond
###
method timeOutCheck {} {
my variable dipatched_time
if {([clock seconds]-$dipatched_time)>30} {
###
# Something has lasted over 2 minutes. Kill this
###
my error 505 {Operation Timed out}
my output
}
}
###
# Return a timestamp
###
method timestamp {} {
return [clock format [clock seconds] -format {%a, %d %b %Y %T %Z}]
}
}
###
# END: reply.tcl
###
###
# START: server.tcl
###
###
# An httpd server with a template engine
# and a shim to insert URL domains
###
::tool::define ::httpd::server {
option port {default: auto}
option myaddr {default: 127.0.0.1}
option server_string [list default: [list TclHttpd $::httpd::version]]
option server_name [list default: [list [info hostname]]]
option doc_root {default {}}
property socket buffersize 32768
property socket translation {auto crlf}
property reply_class ::httpd::reply
array template
variable url_patterns {}
constructor {args} {
my configure {*}$args
my start
}
destructor {
my stop
}
method add_uri {pattern info} {
my variable url_patterns
dict set url_patterns $pattern $info
}
method connect {sock ip port} {
###
# If an IP address is blocked
# send a "go to hell" message
###
if {[my Validate_Connection $sock $ip]} {
catch {close $sock}
return
}
set uuid [my Uuid_Generate]
set coro [coroutine [namespace current]::CORO$uuid {*}[namespace code [list my Connect $uuid $sock $ip]]]
chan event $sock readable $coro
}
method Connect {uuid sock ip} {
yield [info coroutine]
chan event $sock readable {}
chan configure $sock \
-blocking 0 \
-translation {auto crlf} \
-buffering line
my counter url_hit
set line {}
try {
set readCount [::coroutine::util::gets_safety $sock 4096 line]
dict set query REMOTE_ADDR $ip
dict set query REQUEST_METHOD [lindex $line 0]
set uriinfo [::uri::split [lindex $line 1]]
dict set query REQUEST_URI [lindex $line 1]
dict set query REQUEST_PATH [dict get $uriinfo path]
dict set query REQUEST_VERSION [lindex [split [lindex $line end] /] end]
if {[dict get $uriinfo host] eq {}} {
if {$ip eq "127.0.0.1"} {
dict set query HTTP_HOST localhost
} else {
dict set query HTTP_HOST [info hostname]
}
} else {
dict set query HTTP_HOST [dict get $uriinfo host]
}
dict set query HTTP_CLIENT_IP $ip
dict set query QUERY_STRING [dict get $uriinfo query]
dict set query REQUEST_RAW $line
} on error {err errdat} {
puts stderr $err
my log HttpError $line
catch {close $sock}
return
}
try {
set reply [my dispatch $query]
if {[llength $reply]} {
if {[dict exists $reply class]} {
set class [dict get $reply class]
} else {
set class [my cget reply_class]
}
set pageobj [$class create [namespace current]::reply$uuid [self]]
if {[dict exists $reply mixin]} {
oo::objdefine $pageobj mixin [dict get $reply mixin]
}
$pageobj dispatch $sock $reply
my log HttpAccess $line
} else {
try {
my log HttpMissing $line
chan puts $sock "HTTP/1.0 404 NOT FOUND"
dict with query {}
set body [subst [my template notfound]]
chan puts $sock "Content-Length: [string length $body]"
chan puts $sock {}
chan puts $sock $body
} on error {err errdat} {
puts stderr "FAILED ON 404: $err"
} finally {
catch {chan close $sock}
catch {destroy $pageobj}
}
}
} on error {err errdat} {
try {
#puts stderr [dict print $errdat]
chan puts $sock "HTTP/1.0 505 INTERNAL ERROR - server 119"
dict with query {}
set body [subst [my template internal_error]]
chan puts $sock "Content-Length: [string length $body]"
chan puts $sock {}
chan puts $sock $body
my log HttpError $line
} on error {err errdat} {
my log HttpFatal $::errorInfo
#puts stderr "FAILED ON 505: $::errorInfo"
} finally {
catch {chan close $sock}
catch {destroy $pageobj}
}
}
}
method counter which {
my variable counters
incr counters($which)
}
###
# Clean up any process that has gone out for lunch
###
method CheckTimeout {} {
foreach obj [info commands [namespace current]::reply::*] {
try {
$obj timeOutCheck
} on error {} {
catch {$obj destroy}
}
}
}
###
# Route a request to the appropriate handler
###
method dispatch {data} {
set reply $data
set uri [dict get $data REQUEST_PATH]
# Search from longest pattern to shortest
my variable url_patterns
foreach {pattern info} $url_patterns {
if {[string match ${pattern} /$uri]} {
set reply [dict merge $data $info]
if {![dict exists $reply prefix]} {
dict set reply prefix [my PrefixNormalize $pattern]
}
return $reply
}
}
set doc_root [my cget doc_root]
if {$doc_root ne {}} {
###
# Fall back to doc_root handling
###
dict set reply prefix {}
dict set reply path $doc_root
dict set reply mixin httpd::content.file
return $reply
}
return {}
}
method log args {
# Do nothing for now
}
method port_listening {} {
my variable port_listening
return $port_listening
}
method PrefixNormalize prefix {
set prefix [string trimright $prefix /]
set prefix [string trimright $prefix *]
set prefix [string trimright $prefix /]
return $prefix
}
method start {} {
# Build a namespace to contain replies
namespace eval [namespace current]::reply {}
my variable socklist port_listening
set port [my cget port]
if { $port in {auto {}} } {
package require nettool
set port [::nettool::allocate_port 8015]
}
set port_listening $port
set myaddr [my cget myaddr]
my log [list [self] listening on $port $myaddr]
if {$myaddr ni {all any * {}}} {
foreach ip $myaddr {
lappend socklist [socket -server [namespace code [list my connect]] -myaddr $ip $port]
}
} else {
lappend socklist [socket -server [namespace code [list my connect]] $port]
}
::cron::every [self] 120 [namespace code {my CheckTimeout}]
}
method stop {} {
my variable socklist
if {[info exists socklist]} {
foreach sock $socklist {
catch {close $sock}
}
}
set socklist {}
::cron::cancel [self]
}
method template page {
my variable template
if {[info exists template($page)]} {
return $template($page)
}
set template($page) [my TemplateSearch $page]
return $template($page)
}
method TemplateSearch page {
set doc_root [my cget doc_root]
if {$doc_root ne {} && [file exists [file join $doc_root $page.tml]]} {
return [::fileutil::cat [file join $doc_root $page.tml]]
}
if {$doc_root ne {} && [file exists [file join $doc_root $page.html]]} {
return [::fileutil::cat [file join $doc_root $page.html]]
}
switch $page {
internal_error {
return {
<HTML>
<HEAD><TITLE>505: Internal Server Error</TITLE></HEAD>
<BODY>
Error serving <b>${REQUEST_URI}</b>:
<p>
The server encountered an internal server error
<pre><code>
$::errorInfo
</code></pre>
</BODY>
</HTML>
}
}
notfound {
return {
<HTML>
<HEAD><TITLE>404: Page Not Found</TITLE></HEAD>
<BODY>
The page you are looking for: <b>${REQUEST_URI}</b> does not exist.
</BODY>
</HTML>
}
}
}
}
method Uuid_Generate {} {
my variable next_uuid
return [incr next_uuid]
}
###
# Return true if this IP address is blocked
# The socket will be closed immediately after returning
# This handler is welcome to send a polite error message
###
method Validate_Connection {sock ip} {
return 0
}
}
###
# Provide a backward compadible alias
###
::tool::define ::httpd::server::dispatch {
superclass ::httpd::server
}
###
# END: server.tcl
###
###
# START: dispatch.tcl
###
###
# END: dispatch.tcl
###
###
# START: file.tcl
###
###
# Class to deliver Static content
# When utilized, this class is fed a local filename
# by the dispatcher
###
::tool::define ::httpd::content.file {
method FileName {} {
set uri [string trimleft [my http_info get REQUEST_URI] /]
set path [my http_info get path]
set prefix [my http_info get prefix]
set fname [string range $uri [string length $prefix] end]
if {$fname in "{} index.html index.md index"} {
return $path
}
if {[file exists [file join $path $fname]]} {
return [file join $path $fname]
}
if {[file exists [file join $path $fname.md]]} {
return [file join $path $fname.md]
}
if {[file exists [file join $path $fname.html]]} {
return [file join $path $fname.html]
}
if {[file exists [file join $path $fname.tml]]} {
return [file join $path $fname.tml]
}
return {}
}
method DirectoryListing {local_file} {
set uri [string trimleft [my http_info get REQUEST_URI] /]
set path [my http_info get path]
set prefix [my http_info get prefix]
set fname [string range $uri [string length $prefix] end]
my puts "<HTML><HEAD><TITLE>Listing of /$fname/</TITLE></HEAD><BODY>"
my puts "Path: $path<br>"
my puts "Prefs: $prefix</br>"
my puts "URI: $uri</br>"
my puts "Listing contents of /$fname/"
my puts "<TABLE>"
if {$prefix ni {/ {}}} {
set updir [file dirname $prefix]
if {$updir ne {}} {
my puts "<TR><TD><a href=\"/$updir\">..</a></TD><TD></TD></TR>"
}
}
foreach file [glob -nocomplain [file join $local_file *]] {
if {[file isdirectory $file]} {
my puts "<TR><TD><a href=\"[file join / $uri [file tail $file]]\">[file tail $file]/</a></TD><TD></TD></TR>"
} else {
my puts "<TR><TD><a href=\"[file join / $uri [file tail $file]]\">[file tail $file]</a></TD><TD>[file size $file]</TD></TR>"
}
}
my puts "</TABLE></BODY></HTML>"
}
method dispatch {newsock datastate} {
# No need to process the rest of the headers
my variable chan dipatched_time
set dispatched_time [clock seconds]
my http_info replace $datastate
set chan $newsock
my content
my output
}
method content {} {
###
# When delivering static content, allow web caches to save
###
my reply set Cache-Control {max-age=3600}
my variable reply_file
set local_file [my FileName]
if {$local_file eq {} || ![file exist $local_file]} {
my <server> log httpNotFound [my http_info get REQUEST_URI]
tailcall my error 404 {Not Found}
}
if {[file isdirectory $local_file]} {
###
# Produce an index page
###
set idxfound 0
foreach name {
index.html
index.tml
index.md
} {
if {[file exists [file join $local_file $name]]} {
set idxfound 1
set local_file [file join $local_file $name]
break
}
}
if {!$idxfound} {
tailcall my DirectoryListing $local_file
}
}
switch [file extension $local_file] {
.md {
package require Markdown
my reply set Content-Type {text/html; charset=UTF-8}
set mdtxt [::fileutil::cat $local_file]
my puts [::Markdown::convert $mdtxt]
}
.tml {
my reply set Content-Type {text/html; charset=UTF-8}
set tmltxt [::fileutil::cat $local_file]
set headers [my http_info dump]
dict with headers {}
my puts [subst $tmltxt]
}
default {
###
# Assume we are returning a binary file
###
my reply set Content-Type [::fileutil::magic::filetype $local_file]
set reply_file $local_file
}
}
}
###
# Output the result or error to the channel
# and destroy this object
###
method DoOutput {} {
my variable chan
chan event $chan writable {}
my variable reply_body reply_file reply_chan chan
chan configure $chan -translation {binary binary}
if {![info exists reply_file]} {
###
# Return dynamic content
###
if {![info exists reply_body]} {
append result [my reply output]
} else {
set reply_body [string trim $reply_body]
my reply set Content-Length [string length $reply_body]
append result [my reply output] \n
append result $reply_body
chan puts -nonewline $chan $result
chan flush $chan
}
my destroy
} else {
###
# Return a stream of data from a file
###
set size [file size $reply_file]
my reply set Content-Length $size
append result [my reply output] \n
chan puts -nonewline $chan $result
set reply_chan [open $reply_file r]
chan configure $reply_chan -translation {binary binary}
chan copy $reply_chan $chan -command [namespace code [list my TransferComplete $reply_chan]]
}
}
}
###
# END: file.tcl
###
###
# START: scgi.tcl
###
###
# Return data from an SCGI process
###
::tool::define ::httpd::content.scgi {
method scgi_info {} {
###
# This method should check if a process is launched
# or launch it if needed, and return a list of
# HOST PORT SCRIPT_NAME
###
# return {localhost 8016 /some/path}
error unimplemented
}
method content {} {
my variable sock chan
set sockinfo [my scgi_info]
if {$sockinfo eq {}} {
my error 404 {Not Found}
return
}
lassign $sockinfo scgihost scgiport scgiscript
set sock [::socket $scgihost $scgiport]
chan configure $chan -translation binary -blocking 0 -buffering full -buffersize 4096
chan configure $sock -translation binary -blocking 0 -buffering full -buffersize 4096
###
# Convert our query headers into netstring format.
###
set info {CONTENT_LENGTH 0 SCGI 1.0}
dict set info SCRIPT_NAME $scgiscript
foreach {f v} [my http_info dump] {
dict set info $f $v
}
foreach {fo v} [my request dump] {
set f $fo
switch [string tolower $fo] {
content-length {
set f CONTENT_LENGTH
}
content-type {
set f CONTENT_TYPE
}
default {
if {[string range $f 0 3] ne "HTTP" && $f ne "CONTENT_TYPE"} {
set f HTTP_[string map {- _} [string toupper $f]]
}
}
}
dict set info $f $v
}
set length [dict get $info CONTENT_LENGTH]
set block {}
foreach {f v} $info {
append block [string toupper $f] \x00 $v \x00
}
chan puts -nonewline $sock "[string length $block]:$block,"
if {$length} {
###
# Send any POST/PUT/etc content
###
chan copy $chan $sock -size $length
}
chan flush $sock
###
# Wake this object up after the SCGI process starts to respond
###
#chan configure $sock -translation {auto crlf} -blocking 0 -buffering line
chan event $sock readable [namespace code {my output}]
}
method output {} {
if {[my http_info getnull HTTP_ERROR] ne {}} {
###
# If something croaked internally, handle this page as a normal reply
###
next
}
my variable sock chan
set replyhead [my HttpHeaders $sock]
set replydat [my MimeParse $replyhead]
if {![dict exists $replydat Content-Length]} {
set length 0
} else {
set length [dict get $replydat Content-Length]
}
###
# Convert the Status: header from the SCGI service to
# a standard service reply line from a web server, but
# otherwise spit out the rest of the headers verbatim
###
set replybuffer "HTTP/1.1 [dict get $replydat Status]\n"
append replybuffer $replyhead
chan configure $chan -translation {auto crlf} -blocking 0 -buffering full -buffersize 4096
puts $chan $replybuffer
###
# Output the body
###
chan configure $sock -translation binary -blocking 0 -buffering full -buffersize 4096
chan configure $chan -translation binary -blocking 0 -buffering full -buffersize 4096
if {$length} {
###
# Send any POST/PUT/etc content
###
chan copy $sock $chan -command [namespace code [list my TransferComplete $sock]]
} else {
catch {close $sock}
chan flush $chan
my destroy
}
}
}
tool::define ::httpd::reply.scgi {
superclass ::httpd::reply
###
# A modified dispatch method from a standard HTTP reply
# Unlike in HTTP, our headers were spoon fed to use from
# the server
###
method dispatch {newsock datastate} {
my http_info replace $datastate
my variable chan rawrequest dipatched_time
set chan $newsock
chan event $chan readable {}
chan configure $chan -translation {auto crlf} -buffering line
set dispatched_time [clock seconds]
try {
# Dispatch to the URL implementation.
# Convert SCGI headers to mime-ish equivilients
my reset
foreach {f v} $datastate {
switch $f {
CONTENT_LENGTH {
my request set Content-Length $v
}
default {
my request set $f $v
}
}
}
my content
} on error {err info} {
#puts stderr $::errorInfo
my error 500 $err [dict get $info -errorinfo]
} finally {
my output
}
}
method EncodeStatus {status} {
return "Status: $status"
}
}
###
# Act as an SCGI Server
###
tool::define ::httpd::server.scgi {
superclass ::httpd::server
property socket buffersize 32768
property socket blocking 0
property socket translation {binary binary}
property reply_class ::httpd::reply.scgi
method Connect {uuid sock ip} {
yield [info coroutine]
chan event $sock readable {}
chan configure $sock \
-blocking 1 \
-translation {binary binary} \
-buffersize 4096 \
-buffering none
my counter url_hit
try {
# Read the SCGI request on byte at a time until we reach a ":"
dict set query REQUEST_URI /
dict set query REMOTE_ADDR $ip
set size {}
while 1 {
set char [::coroutine::util::read $sock 1]
if {[chan eof $sock]} {
catch {close $sock}
return
}
if {$char eq ":"} break
append size $char
}
# With length in hand, read the netstring encoded headers
set inbuffer [::coroutine::util::read $sock [expr {$size+1}]]
chan configure $sock -blocking 0 -buffersize 4096 -buffering full
foreach {f v} [lrange [split [string range $inbuffer 0 end-1] \0] 0 end-1] {
dict set query $f $v
}
if {![dict exists $query REQUEST_PATH]} {
set uri [dict get $query REQUEST_URI]
set uriinfo [::uri::split $uri]
dict set query REQUEST_PATH [dict get $uriinfo path]
}
set reply [my dispatch $query]
dict with query {}
if {[llength $reply]} {
if {[dict exists $reply class]} {
set class [dict get $reply class]
} else {
set class [my cget reply_class]
}
set pageobj [$class create [namespace current]::reply$uuid [self]]
if {[dict exists $reply mixin]} {
oo::objdefine $pageobj mixin [dict get $reply mixin]
}
$pageobj dispatch $sock $reply
my log HttpAccess $REQUEST_URI
} else {
try {
my log HttpMissing $REQUEST_URI
puts $sock "Status: 404 NOT FOUND"
dict with query {}
set body [subst [my template notfound]]
puts $sock "Content-Length: [string length $body]"
puts $sock {}
puts $sock $body
} on error {err errdat} {
puts stderr "FAILED ON 404: $err"
} finally {
catch {close $sock}
}
}
} on error {err errdat} {
try {
#puts stderr $::errorInfo
puts $sock "Status: 505 INTERNAL ERROR - scgi 298"
dict with query {}
set body [subst [my template internal_error]]
puts $sock "Content-Length: [string length $body]"
puts $sock {}
puts $sock $body
my log HttpError $REQUEST_URI
} on error {err errdat} {
my log HttpFatal $::errorInfo
#puts stderr "FAILED ON 505: $err $::errorInfo"
} finally {
catch {close $sock}
}
}
}
}
###
# END: scgi.tcl
###
###
# START: proxy.tcl
###
# Act as a proxy server
::tool::define ::httpd::content.proxy {
# Options:
# proxy_host - Hostname to proxy
# proxy_port - Port on hostname to proxy
# proxy_script - Block of text to stream before sending the request
###
method proxy_info {} {
###
# This method should check if a process is launched
# or launch it if needed, and return a list of
# HOST PORT PROXYURI
###
# return {localhost 8016 /some/path}
error unimplemented
}
method content {} {
my variable chan sock rawrequest
set sockinfo [my proxy_info]
if {$sockinfo eq {}} {
tailcall my error 404 {Not Found}
}
lassign $sockinfo proxyhost proxyport proxyscript
set sock [::socket $proxyhost $proxyport]
chan configure $chan -translation binary -blocking 0 -buffering full -buffersize 4096
chan configure $sock -translation {auto crlf} -blocking 1 -buffering line
# Pass along our modified METHOD URI PROTO
chan puts $sock "$proxyscript"
# Pass along the headers as we saw them
chan puts $sock $rawrequest
set length [my http_info get CONTENT_LENGTH]
if {$length} {
###
# Send any POST/PUT/etc content
###
chan copy $chan $sock -size $length
}
chan flush $sock
###
# Wake this object up after the proxied process starts to respond
###
chan configure $sock -translation {auto crlf} -blocking 1 -buffering line
chan event $sock readable [namespace code {my output}]
}
method DoOutput {} {
my variable chan sock
chan event $chan writable {}
if {![info exists sock] || [my http_info getnull HTTP_ERROR] ne {}} {
###
# If something croaked internally, handle this page as a normal reply
###
next
return
}
set length 0
chan configure $sock -translation {crlf crlf} -blocking 1
set replystatus [gets $sock]
set replyhead [my HttpHeaders $sock]
set replydat [my MimeParse $replyhead]
###
# Pass along the status line and MIME headers
###
set replybuffer "$replystatus\n"
append replybuffer $replyhead
chan configure $chan -translation {auto crlf} -blocking 0 -buffering full -buffersize 4096
chan puts $chan $replybuffer
###
# Output the body
###
chan configure $sock -translation binary -blocking 0 -buffering full -buffersize 4096
chan configure $chan -translation binary -blocking 0 -buffering full -buffersize 4096
set length [dict get $replydat CONTENT_LENGTH]
if {$length} {
###
# Send any POST/PUT/etc content
###
chan copy $sock $chan -command [namespace code [list my TransferComplete $sock]]
} else {
my destroy
}
}
}
###
# END: proxy.tcl
###
###
# START: websocket.tcl
###
###
# Upgrade a connection to a websocket
###
::tool::define ::httpd::content.websocket {
}
###
# END: websocket.tcl
###
namespace eval ::httpd {
namespace export *
}
|