-
Notifications
You must be signed in to change notification settings - Fork 3
/
webserver.reb
346 lines (327 loc) · 7.5 KB
/
webserver.reb
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
#!/usr/bin/r3
REBOL [Name: webserver]
-help: does [lib.print {
USAGE: r3 webserver.reb [OPTIONS]
OPTIONS:
-h, -help, --help : this help
-q : verbose: 0 (quiet)
-v : verbose: 2 (debug)
INTEGER : port number [8000]
OTHER : web root [system.options.path]
-a name : access-dir via name.*
EXAMPLE: 8080 /my/web/root -q -a index
}]
;; INIT
port: 8888
root-dir: %"./"
access-dir: false
verbose: 1
parse system.options.args [maybe some [
"-a", access-dir: [
<end> (true)
| "true" (true)
| "false" (false)
| to-file/ <any>
]
|
["-h" | "-help" | "--help" || (-help, quit)]
|
verbose: ["-q" (0) | "-v" (2)]
|
bad: into text! ["-" across to <end>] (
fail ["Unknown command line switch:" bad]
)
|
port: into text! [integer!]
|
root-dir: to-file/ <any>
]]
;; LIBS
delete-recur: adapt :lib.delete [
if file? port [
if not exists? port [return null]
if 'dir = exists? port [
port: dirize port
for-each x read port [
delete-recur %% (port)/(x)
]
]
]
]
import %httpd.reb
attempt [
rem: import 'rem
html: import 'html
]
rem-to-html: attempt[chain [:rem.load-rem :html.to-html]]
change-dir system.options.path
ext-map: [
"css" css
"gif" gif
"htm" html
"html" html
"jpg" jpeg
"jpeg" jpeg
"js" js
"json" json
"png" png
"r" rebol
"r3" rebol
"reb" rebol
"rem" rem
"svg" svg
"txt" text
"wasm" wasm
]
mime: make map! [
css "text/css"
gif "image/gif"
html "text/html"
jpeg "image/jpeg"
js "application/javascript"
json "application/json"
png "image/png"
r "text/plain"
svg "image/svg+xml"
text "text/plain"
wasm "application/wasm"
]
status-codes: [
200 "OK" 201 "Created" 204 "No Content"
301 "Moved Permanently" 302 "Moved temporarily" 303 "See Other" 307 "Temporary Redirect"
400 "Bad Request" 401 "No Authorization" 403 "Forbidden" 404 "Not Found" 411 "Length Required"
500 "Internal Server Error" 503 "Service Unavailable"
]
html-list-dir: function [
"Output dir contents in HTML"
return: [text!]
dir [file!]
][
if trap [list: read dir] [return _]
;;for-next list [if 'dir = exists? join dir list.1 [append list.1 %/]]
;; ^-- workaround for #838
sort/compare list func [x y] [
return case [
all [dir? x not dir? y] [true]
all [not dir? x dir? y] [false]
y > x [true]
true [false]
]
]
if dir != %/ [insert list %../]
data: copy {<head>
<meta name="viewport" content="initial-scale=1.0" />
<style> a {text-decoration: none}
body {font-family: monospace}
.b {font-weight: bold}
</style>
</head>
[>]: Navigate [V]: View [E]: Exec <hr/>
}
for-each i list [
is-rebol-file: did all [
not dir? i
did parse i [thru ".reb"]
]
append data unspaced [
{<a }
if dir? i [{class="b" }]
{href="} i
{?">[}
case [
is-rebol-file [{E}]
dir? i [{>}]
] else [{V}]
{]</a> }
{<a }
if dir? i [{class="b" }]
{href="} i
{">}
i
</a> <br/>
]
]
return data
]
parse-query: function [
return: [block!]
query
][
xchar: charset "=&"
r: make block! 0
k: v: _
query: to-text query
i: 0
parse query [any [
k: across [to xchar | to <end>]
[ "=" v: across [to "&" | to <end>]
| (v: k k: i: i + 1)
]
(
append r (attempt [dehex k] else [k])
append r (attempt [dehex v] else [v])
)
opt skip
]]
return r
]
request: _
handle-request: function [
return [integer! block!]
req [object!]
][
set 'request req ; global
req.target: my dehex
path-elements: next split req.target #"/"
; 'extern' url /http://ser.ver/...
parse req.request-uri ["//"] then [
lib.print req.request-uri
return reduce [200 mime/html "req/request-uri"]
] else [
path: join root-dir req.target
path-type: try exists? path
]
append req reduce ['real-path clean-path path]
if path-type = 'dir [
if not access-dir [return 403]
if req.query-string [
if data: html-list-dir path [
return reduce [200 mime/html data]
]
return 500
]
if file? access-dir [
for-each ext [%.reb %.rem %.html %.htm] [
dir-index: join access-dir ext
if 'file = try exists? join path dir-index [
if ext = %.reb [append dir-index "?"]
break
]
] then [dir-index: "?"]
] else [dir-index: "?"]
return redirect-response join req.target dir-index
]
if path-type = 'file [
pos: try find-last last path-elements
"."
file-ext: (if pos [copy next pos] else [_])
mimetype: try attempt [ext-map.(file-ext)]
if trap [data: read path] [return 403]
if all [
any [
mimetype = 'rem
all [
mimetype = 'html
"REBOL" = uppercase to-text copy/part data 5
]
]
action? :rem-to-html
any [
not req.query-string
not empty? req.query-string
]
][
rem.rem.request: req
if error: try trap [
data: rem-to-html data
] [ data: form error mimetype: 'text ]
else [ mimetype: 'html ]
]
if mimetype = 'rebol [
if req.query-string [
mimetype: 'html
e: try trap [
data: do data
]
if all [not error? e, action? :data] [
e: try trap [
data: data req
]
]
if error? e [data: e]
case [
block? :data [
mimetype: first data
data: next data
]
quoted? :data [
data: form eval data
mimetype: 'text
]
error? :data [mimetype: 'text]
]
data: form :data
] else [mimetype: 'text]
]
return reduce [200 try select mime :mimetype data]
]
return 404
]
redirect-response: lambda [target] [
reduce [200 mime/html unspaced [
{<html><head><meta http-equiv="Refresh" content="0; url=}
target {" /></head></html>}
]]
]
;; MAIN
server: open compose [
scheme: 'httpd (port) [
if verbose >= 2 [lib.print mold request]
if verbose >= 1 [
lib.print [
request.method
request.request-uri
]
]
; !!! This is a hook inserted for purposes of being
; able to know if a screenless emulator is running
; the console correctly. /data/local/tmp is a special
; writable folder in Android.
;
; https://github.com/metaeducation/rebol-server/issues/9
;
trap [
parse request.target [
"/testwrite" across thru <end>
] then testfile -> [
write as file! testfile "TESTWRITE!"
res: reduce [
200
"text/html"
unspaced [<pre> testfile _ "written" </pre>]
]
] else [
res: handle-request request
]
] then err -> [ ; handling (or testwrite) failed
res: reduce [
200
"text/html"
unspaced [<pre> mold err </pre>]
]
]
if integer? res [
response.status: res
response.type: "text/html"
response.content: unspaced [
<h2> res space select status-codes res </h2>
<b> request.method space request.request-uri </b>
<br> <pre> mold request </pre>
]
] else [
response.status: res.1
response.type: res.2
response.content: to-binary res.3
]
if verbose >= 1 [
lib.print spaced ["=>" response.status]
]
]
]
if verbose >= 1 [
lib.print ["Serving on port" port]
lib.print ["root-dir:" clean-path root-dir]
lib.print ["access-dir:" mold access-dir]
]
wait server
;; vim: set et sw=2: