︙ | | | ︙ | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# Design rules:
#
# (1) All identifiers in the global namespace begin with "wapp"
#
# (2) Indentifiers intended for internal use only begin with "wappInt"
#
# Add text to the end of the HTTP reply. wapp and wapp-safe work the
# same. The only difference is in how wapp-safety-check deals with these
# procs during analysis.
#
proc wapp {txt} {
global wapp
dict append wapp .reply $txt
}
# Add text to the page under construction. Do no escaping on the text.
|
|
|
<
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# Design rules:
#
# (1) All identifiers in the global namespace begin with "wapp"
#
# (2) Indentifiers intended for internal use only begin with "wappInt"
#
# Add text to the end of the HTTP reply. No interpretation or transformation
# of the text is performs. The argument should be enclosed within {...}
#
proc wapp {txt} {
global wapp
dict append wapp .reply $txt
}
# Add text to the page under construction. Do no escaping on the text.
|
︙ | | | ︙ | |
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
|
# Though occasionally necessary, the use of this interface should be minimized.
#
proc wapp-unsafe {txt} {
global wapp
dict append wapp .reply $txt
}
############################ Begin Deprecated Interfaces ######################
# Append text after escaping it for HTML.
#
# The following commands are the same:
#
# wapp-escape-html TEXT
# wapp-subst %html(TEXT)
#
proc wapp-escape-html {txt} {
global wapp
dict append wapp .reply [string map {& & < < > >} $txt]
}
# Append text after escaping it for URL query parameters.
#
# The following commands are the same:
#
# wapp-escape-url TEXT
# wapp-subst %url(TEXT)
#
proc wapp-escape-url {txt} {
global wapp
dict append wapp .reply [wappInt-enc-url $txt]
}
########################### End Deprecated Interfaces #########################
# The argument should be in {...}. Substitions of %html(...) encode ...
# escaped for safe insertion into HTML. %url(...) substitions encode the
# argument for safe insertion into query parameters of URLs. Backslash
# substitutions are also performed, but variable substitutions are not,
# except within %html() and %url().
#
proc wapp-subst {txt} {
global wapp
regsub -all {%(html|url|qp|string|unsafe)\(([^)]+)\)} $txt \
{[wappInt-enc-\1 "\2"]} txt
dict append wapp .reply [uplevel 1 [list subst -novariables $txt]]
}
|
|
<
<
<
<
<
<
|
|
<
<
<
|
|
<
<
<
|
|
<
<
<
<
<
|
<
<
<
|
|
|
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
|
# Though occasionally necessary, the use of this interface should be minimized.
#
proc wapp-unsafe {txt} {
global wapp
dict append wapp .reply $txt
}
# Add text to the end of the reply under construction. The following
# substitutions are made:
#
# %html(...) Escape text for inclusion in HTML
# %url(...) Escape text for use as a URL
# %qp(...) Escape text for use as a URI query parameter
# %string(...) Escape text for use within a JSON string
# %unsafe(...) No transformations of the text
#
# The %unsafe substitution should be avoided whenever possible, obviously.
# In addition to the substitutions above, the text also does backslash
# escapes.
#
proc wapp-subst {txt} {
global wapp
regsub -all {%(html|url|qp|string|unsafe)\(([^)]+)\)} $txt \
{[wappInt-enc-\1 "\2"]} txt
dict append wapp .reply [uplevel 1 [list subst -novariables $txt]]
}
|
︙ | | | ︙ | |
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
global wapp
regsub -all {\n\s+} [string trim $txt] \n txt
regsub -all {%(html|url|qp|string|unsafe)\(([^)]+)\)} $txt \
{[wappInt-enc-\1 "\2"]} txt
dict append wapp .reply [uplevel 1 [list subst -novariables $txt]]
}
# This is a helper routine for wappInt-enc-url and wappInt-enc-qp. It returns
# an appropriate %HH encoding for the single character c. If c is a unicode
# character, then this routine might return multiple bytes: %HH%HH%HH
#
proc wappInt-%HHchar {c} {
if {$c==" "} {return +}
return [regsub -all .. [binary encode hex [encoding convertto utf-8 $c]] {%&}]
|
<
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
global wapp
regsub -all {\n\s+} [string trim $txt] \n txt
regsub -all {%(html|url|qp|string|unsafe)\(([^)]+)\)} $txt \
{[wappInt-enc-\1 "\2"]} txt
dict append wapp .reply [uplevel 1 [list subst -novariables $txt]]
}
# This is a helper routine for wappInt-enc-url and wappInt-enc-qp. It returns
# an appropriate %HH encoding for the single character c. If c is a unicode
# character, then this routine might return multiple bytes: %HH%HH%HH
#
proc wappInt-%HHchar {c} {
if {$c==" "} {return +}
return [regsub -all .. [binary encode hex [encoding convertto utf-8 $c]] {%&}]
|
︙ | | | ︙ | |
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
|
regsub -all -- \
{%([CDcd][A-Fa-f0-9])%([89ABab][A-Fa-f0-9])} \
$str {[encoding convertfrom utf-8 [binary decode hex \1\2]]} str
regsub -all -- {%([0-7][A-Fa-f0-9])} $str {\\u00\1} str
return [subst -novar $str]
}
# Do URL encoding
#
# Reset the document back to an empty string.
#
proc wapp-reset {} {
global wapp
dict set wapp .reply {}
}
# Change the mime-type of the result document.
proc wapp-mimetype {x} {
global wapp
dict set wapp .mimetype $x
}
# Change the reply code.
#
|
<
<
<
>
|
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
regsub -all -- \
{%([CDcd][A-Fa-f0-9])%([89ABab][A-Fa-f0-9])} \
$str {[encoding convertfrom utf-8 [binary decode hex \1\2]]} str
regsub -all -- {%([0-7][A-Fa-f0-9])} $str {\\u00\1} str
return [subst -novar $str]
}
# Reset the document back to an empty string.
#
proc wapp-reset {} {
global wapp
dict set wapp .reply {}
}
# Change the mime-type of the result document.
#
proc wapp-mimetype {x} {
global wapp
dict set wapp .mimetype $x
}
# Change the reply code.
#
|
︙ | | | ︙ | |
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
|
# Redirect to a different web page
#
proc wapp-redirect {uri} {
wapp-reply-code {307 Redirect}
wapp-reply-extra Location $uri
}
# Return the value of a query parameter or environment variable.
#
proc wapp-param {name {dflt {}}} {
global wapp
if {![dict exists $wapp $name]} {return $dflt}
return [dict get $wapp $name]
}
# Return the value of a query parameter or environment variable.
#
proc wapp-param-exists {name} {
global wapp
return [dict exists $wapp $name]
}
# Set the value of a parameter
#
proc wapp-set-param {name value} {
global wapp
dict set wapp $name $value
}
# Return all parameter names that match the GLOB pattern, or all
|
|
|
|
|
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
|
# Redirect to a different web page
#
proc wapp-redirect {uri} {
wapp-reply-code {307 Redirect}
wapp-reply-extra Location $uri
}
# Return the value of a wapp parameter
#
proc wapp-param {name {dflt {}}} {
global wapp
if {![dict exists $wapp $name]} {return $dflt}
return [dict get $wapp $name]
}
# Return true if a and only if the wapp parameter $name exists
#
proc wapp-param-exists {name} {
global wapp
return [dict exists $wapp $name]
}
# Set the value of a wapp parameter
#
proc wapp-set-param {name value} {
global wapp
dict set wapp $name $value
}
# Return all parameter names that match the GLOB pattern, or all
|
︙ | | | ︙ | |
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# Start up the wapp framework. Parameters are a list passed as the
# single argument.
#
# -server $PORT Listen for HTTP requests on this TCP port $PORT
#
# -scgi $PORT Listen for SCGI requests on TCP port $PORT
#
# -cgi Perform a single CGI request
#
# With no arguments, the behavior is called "auto". In "auto" mode,
# if the GATEWAY_INTERFACE environment variable indicates CGI, then run
# as CGI. Otherwise, start an HTTP server bound to the loopback address
# only, on an arbitrary TCP port, and automatically launch a web browser
# on that TCP port.
#
proc wapp-start {arglist} {
global env
set mode auto
set port 0
set n [llength $arglist]
for {set i 0} {$i<$n} {incr i} {
|
|
>
>
>
>
>
>
>
>
>
>
>
|
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
|
# Start up the wapp framework. Parameters are a list passed as the
# single argument.
#
# -server $PORT Listen for HTTP requests on this TCP port $PORT
#
# -scgi $PORT Listen for SCGI requests on TCP port $PORT
#
# -cgi Handle a single CGI request
#
# With no arguments, the behavior is called "auto". In "auto" mode,
# if the GATEWAY_INTERFACE environment variable indicates CGI, then run
# as CGI. Otherwise, start an HTTP server bound to the loopback address
# only, on an arbitrary TCP port, and automatically launch a web browser
# on that TCP port.
#
# Additional options:
#
# -trace "puts" each request URL as it is handled, for
# debugging
#
# -lint Run wapp-safety-check on the application instead
# of running the application itself
#
# -Dvar=value Set TCL global variable "var" to "value"
#
#
proc wapp-start {arglist} {
global env
set mode auto
set port 0
set n [llength $arglist]
for {set i 0} {$i<$n} {incr i} {
|
︙ | | | ︙ | |