File: uhttpd.tcl

package info (click to toggle)
tclthread3 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,752 kB
  • sloc: ansic: 8,259; tcl: 1,711; sh: 436; makefile: 38
file content (416 lines) | stat: -rw-r--r-- 9,041 bytes parent folder | download | duplicates (2)
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
#
# uhttpd.tcl --
#
# Simple Sample httpd/1.0 server in 250 lines of Tcl.
# Stephen Uhler / Brent Welch (c) 1996 Sun Microsystems.
#
# Modified to use namespaces and direct url-to-procedure access (zv).
# Eh, due to this, and nicer indenting, it's now 150 lines longer :-)
#
# Usage:
#    phttpd::create port
#
#    port         Tcp port where the server listens
#
# Example:
#
#    # tclsh9.0
#    % source uhttpd.tcl
#    % uhttpd::create 5000
#    % vwait forever
#
#    Starts the server on the port 5000. Also, look at the Httpd array
#    definition in the "uhttpd" namespace declaration to find out
#    about other options you may put on the command line.
#
#    You can use: http://localhost:5000/monitor URL to test the
#    server functionality.
#
# Copyright (c) Stephen Uhler / Brent Welch (c) 1996 Sun Microsystems.
# Copyright (c) 2002 by Zoran Vasiljevic.
#
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -----------------------------------------------------------------------------

namespace eval uhttpd {

    variable Httpd;           # Internal server state and config params
    variable MimeTypes;       # Cache of file-extension/mime-type
    variable HttpCodes;       # Portion of well-known http return codes
    variable ErrorPage;       # Format of error response page in html

    array set Httpd {
	-name    uhttpd
	-vers    1.0
	-root    ""
	-index   index.htm
    }
    array set HttpCodes {
	400  "Bad Request"
	401  "Not Authorized"
	404  "Not Found"
	500  "Server error"
    }
    array set MimeTypes {
	{}   "text/plain"
	.txt "text/plain"
	.htm "text/html"
	.htm "text/html"
	.gif "image/gif"
	.jpg "image/jpeg"
	.png "image/png"
    }
    set ErrorPage {
	<title>Error: %1$s %2$s</title>
	<h1>%3$s</h1>
	<p>Problem in accessing "%4$s" on this server.</p>
	<hr>
	<i>%5$s/%6$s Server at %7$s Port %8$s</i>
    }
}

proc uhttpd::create {port args} {

    # @c Start the server by listening for connections on the desired port.

    variable Httpd
    set arglen [llength $args]

    if {$arglen} {
	if {$arglen % 2} {
	    error "wrong \# arguments, should be: key1 val1 key2 val2..."
	}
	set opts [array names Httpd]
	foreach {arg val} $args {
	    if {[lsearch $opts $arg] < 0} {
		error "unknown option \"$arg\""
	    }
	    set Httpd($arg) $val
	}
    }

    set Httpd(port) $port
    set Httpd(host) [info hostname]

    socket -server [namespace current]::Accept $port
}

proc uhttpd::respond {s status contype data {length 0}} {

    puts $s "HTTP/1.0 $status"
    puts $s "Date: [Date]"
    puts $s "Content-Type: $contype"

    if {$length} {
	puts $s "Content-Length: $length"
    } else {
	puts $s "Content-Length: [string length $data]"
    }

    puts $s ""
    puts $s $data
}

proc uhttpd::Accept {newsock ipaddr port} {

    # @c Accept a new connection from the client.

    variable Httpd
    upvar \#0 [namespace current]::Httpd$newsock data

    fconfigure $newsock -blocking 0 -translation {auto crlf}

    set data(ipaddr) $ipaddr
    fileevent $newsock readable [list [namespace current]::Read $newsock]
}

proc uhttpd::Read {s} {

    # @c Read data from client

    variable Httpd
    upvar \#0 [namespace current]::Httpd$s data

    if {[catch {gets $s line} readCount] || [eof $s]} {
	return [Done $s]
    }
    if {$readCount < 0} {
	return ;# Insufficient data on non-blocking socket !
    }
    if {![info exists data(state)]} {
	set pat {(POST|GET) ([^?]+)\??([^ ]*) HTTP/1\.[0-9]}
	if {[regexp $pat $line x data(proto) data(url) data(query)]} {
	    return [set data(state) mime]
	} else {
	    Log error "bad request line: %s" $line
	    Error $s 400
	    return [Done $s]
	}
    }

    # string compare $readCount 0 maps -1 to -1, 0 to 0, and > 0 to 1

    set state [string compare $readCount 0],$data(state),$data(proto)
    switch -- $state {
	"0,mime,GET" - "0,query,POST" {
	    Respond $s
	}
	"0,mime,POST" {
	    set data(state) query
	    set data(query) ""
	}
	"1,mime,POST" - "1,mime,GET" {
	    if [regexp {([^:]+):[   ]*(.*)}  $line dummy key value] {
		set data(mime,[string tolower $key]) $value
	    }
	}
	"1,query,POST" {
	    append data(query) $line
	    set clen $data(mime,content-length)
	    if {($clen - [string length $data(query)]) <= 0} {
		Respond $s
	    }
	}
	default {
	    if [eof $s] {
		Log error "unexpected eof; client closed connection"
		return [Done $s]
	    } else {
		Log error "bad http protocol state: %s" $state
		Error $s 400
		return [Done $s]
	    }
	}
    }
}

proc uhttpd::Done {s} {

    # @c Close the connection socket and discard token

    close $s
    unset [namespace current]::Httpd$s
}

proc uhttpd::Respond {s} {

    # @c Respond to the query.

    variable Httpd
    upvar \#0 [namespace current]::Httpd$s data

    if {[uplevel \#0 info proc $data(url)] == $data(url)} {

	#
	# Service URL-procedure first
	#

	if {[catch {
	    puts $s "HTTP/1.0 200 OK"
	    puts $s "Date: [Date]"
	    puts $s "Last-Modified: [Date]"
	} err]} {
	    Log error "client closed connection prematurely: %s" $err
	    return [Done $s]
	}
	set data(sock) $s
	if {[catch {$data(url) data} err]} {
	    Log error "%s: %s" $data(url) $err
	}

    } else {

	#
	# Service regular file path next.
	#

	set mypath [Url2File $data(url)]
	if {![catch {open $mypath} i]} {
	    if {[catch {
		puts $s "HTTP/1.0 200 OK"
		puts $s "Date: [Date]"
		puts $s "Last-Modified: [Date [file mtime $mypath]]"
		puts $s "Content-Type: [ContentType $mypath]"
		puts $s "Content-Length: [file size $mypath]"
		puts $s ""
		fconfigure $s -translation binary -blocking 0
		fconfigure $i -translation binary
		fcopy $i $s
		close $i
	    } err]} {
		Log error "client closed connection prematurely: %s" $err
	    }
	} else {
	    Log error "%s: %s" $data(url) $i
	    Error $s 404
	}
    }

    Done $s
}

proc uhttpd::ContentType {path} {

    # @c Convert the file suffix into a mime type.

    variable MimeTypes

    set type "text/plain"
    catch {set type $MimeTypes([file extension $path])}

    return $type
}

proc uhttpd::Error {s code} {

    # @c Emit error page.

    variable Httpd
    variable HttpCodes
    variable ErrorPage

    upvar \#0 [namespace current]::Httpd$s data

    append data(url) ""
    set msg \
	[format $ErrorPage     \
	     $code             \
	     $HttpCodes($code) \
	     $HttpCodes($code) \
	     $data(url)        \
	     $Httpd(-name)     \
	     $Httpd(-vers)     \
	     $Httpd(host)      \
	     $Httpd(port)      \
	    ]
    if {[catch {
	puts $s "HTTP/1.0 $code $HttpCodes($code)"
	puts $s "Date: [Date]"
	puts $s "Content-Length: [string length $msg]"
	puts $s ""
	puts $s $msg
    } err]} {
	Log error "client closed connection prematurely: %s" $err
    }
}

proc uhttpd::Date {{seconds 0}} {

    # @c Generate a date string in HTTP format.

    if {$seconds == 0} {
	set seconds [clock seconds]
    }
    clock format $seconds -format {%a, %d %b %Y %T %Z} -gmt 1
}

proc uhttpd::Log {reason format args} {

    # @c Log an httpd transaction.

    set messg [eval format [list $format] $args]
    set stamp [clock format [clock seconds] -format "%d/%b/%Y:%H:%M:%S"]

    puts stderr "\[$stamp\] $reason: $messg"
}

proc uhttpd::Url2File {url} {

    # @c Convert a url into a pathname (this is probably not right)

    variable Httpd

    lappend pathlist $Httpd(-root)
    set level 0

    foreach part [split $url /] {
	set part [CgiMap $part]
	if [regexp {[:/]} $part] {
	    return ""
	}
	switch -- $part {
	    "." { }
	    ".." {incr level -1}
	    default {incr level}
	}
	if {$level <= 0} {
	    return ""
	}
	lappend pathlist $part
    }

    set file [eval file join $pathlist]

    if {[file isdirectory $file]} {
	return [file join $file $Httpd(-index)]
    } else {
	return $file
    }
}

proc uhttpd::CgiMap {data} {

    # @c Decode url-encoded strings

    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]
}

proc uhttpd::QueryMap {query} {

    # @c Decode url-encoded query into key/value pairs

    set res [list]

    regsub -all {[&=]} $query { }    query
    regsub -all {  }   $query { {} } query; # Othewise we lose empty values

    foreach {key val} $query {
	lappend res [CgiMap $key] [CgiMap $val]
    }
    return $res
}

proc /monitor {array} {

    upvar $array data ; # Holds the socket to remote client

    #
    # Emit headers
    #

    puts $data(sock) "HTTP/1.0 200 OK"
    puts $data(sock) "Date: [uhttpd::Date]"
    puts $data(sock) "Content-Type: text/html"
    puts $data(sock) ""

    #
    # Emit body
    #

    puts $data(sock) [subst {
	<html>
	<body>
	<h3>[clock format [clock seconds]]</h3>
    }]

    after 1 ; # Simulate blocking call

    puts $data(sock) [subst {
	</body>
	</html>
    }]
}

# EOF $RCSfile: uhttpd.tcl,v $
# Emacs Setup Variables
# Local Variables:
# mode: Tcl
# indent-tabs-mode: nil
# tcl-basic-offset: 4
# End: