forked from gchiu/rebolbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prot-http.r
531 lines (530 loc) · 13.2 KB
/
prot-http.r
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
REBOL [
System: "REBOL [R3] Language Interpreter and Run-time Environment"
Title: "REBOL 3 HTTP protocol scheme"
Rights: {
Copyright 2012 REBOL Technologies
REBOL is a trademark of REBOL Technologies
}
License: {
Licensed under the Apache License, Version 2.0
See: http://www.apache.org/licenses/LICENSE-2.0
}
Name: 'http
Type: 'module
Version: 0.1.0
File: %prot-http.r
Purpose: {
This program defines the HTTP protocol scheme for REBOL 3.
}
Author: "Gabriele Santilli"
Date: 22-Jun-2007
]
sync-op: func [port body /local state] [
unless port/state [open port port/state/close?: yes]
state: port/state
state/awake: :read-sync-awake
do body
if state/state = 'ready [do-request port]
unless port? wait [state/connection port/spec/timeout] [http-error "Timeout"]
body: copy port
if all [
select state/info/headers 'Content-Type
state/info/headers/Content-Type
parse state/info/headers/Content-Type [
"text/" thru "; charset=UTF-8"
]
] [
body: to string! body
]
if state/close? [close port]
body
]
read-sync-awake: func [event [event!] /local error] [
switch/default event/type [
connect ready [
do-request event/port
false
]
done [
true
]
close [
true
]
error [
error: event/port/state/error
event/port/state/error: none
do error
]
] [
false
]
]
http-awake: func [event /local port http-port state awake res] [
port: event/port
http-port: port/locals
state: http-port/state
if any-function? :http-port/awake [state/awake: :http-port/awake]
awake: :state/awake
switch/default event/type [
read [
awake make event! [type: 'read port: http-port]
check-response http-port
]
wrote [
awake make event! [type: 'wrote port: http-port]
state/state: 'reading-headers
read port
false
]
lookup [open port false]
connect [
state/state: 'ready
awake make event! [type: 'connect port: http-port]
]
close [
res: switch state/state [
ready [
awake make event! [type: 'close port: http-port]
]
doing-request reading-headers [
state/error: make-http-error "Server closed connection"
awake make event! [type: 'error port: http-port]
]
reading-data [
either any [integer? state/info/headers/content-length state/info/headers/transfer-encoding = "chunked"] [
state/error: make-http-error "Server closed connection"
awake make event! [type: 'error port: http-port]
] [
any [
awake make event! [type: 'done port: http-port]
awake make event! [type: 'close port: http-port]
]
]
]
]
close http-port
res
]
] [true]
]
make-http-error: func [
"Make an error for the HTTP protocol"
message [string! block!]
] [
if block? message [message: ajoin message]
make error! [
type: 'Access
id: 'Protocol
arg1: message
]
]
http-error: func [
"Throw an error for the HTTP protocol"
message [string! block!]
] [
do make-http-error message
]
make-http-request: func [
"Create an HTTP request (returns string!)"
method [word! string!] "E.g. GET, HEAD, POST etc."
target [file! string!] {In case of string!, no escaping is performed (eg. useful to override escaping etc.). Careful!}
headers [block!] "Request headers (set-word! string! pairs)"
content [any-string! binary! none!] {Request contents (Content-Length is created automatically). Empty string not exactly like none.}
/local result
] [
result: rejoin [
uppercase form method #" "
either file? target [next mold target] [target]
" HTTP/1.0" CRLF
]
foreach [word string] headers [
repend result [mold word #" " string CRLF]
]
if content [
content: to binary! content
repend result ["Content-Length: " length? content CRLF]
]
append result CRLF
result: to binary! result
if content [append result content]
result
]
do-request: func [
"Perform an HTTP request"
port [port!]
/local spec info
] [
spec: port/spec
info: port/state/info
spec/headers: body-of make make object! [
Accept: "*/*"
Accept-Charset: "utf-8"
Host: either spec/port-id <> 80 [
rejoin [form spec/host #":" spec/port-id]
] [
form spec/host
]
User-Agent: "REBOL"
] spec/headers
port/state/state: 'doing-request
info/headers: info/response-line: info/response-parsed: port/data:
info/size: info/date: info/name: none
write port/state/connection
make-http-request spec/method to file! any [spec/path %/]
spec/headers spec/content
]
parse-write-dialect: func [port block /local spec] [
spec: port/spec
parse block [[set block word! (spec/method: block) | (spec/method: 'post)]
opt [set block [file! | url!] (spec/path: block)] [set block block! (spec/headers: block) | (spec/headers: [])] [set block [any-string! | binary!] (spec/content: block) | (spec/content: none)]
]
]
check-response: func [port /local conn res headers d1 d2 line info state awake spec] [
state: port/state
conn: state/connection
info: state/info
headers: info/headers
line: info/response-line
awake: :state/awake
spec: port/spec
if all [
not headers
d1: find conn/data crlfbin
d2: find/tail d1 crlf2bin
] [
info/response-line: line: to string! copy/part conn/data d1
info/headers: headers: construct/with d1 http-response-headers
info/name: to file! any [spec/path %/]
if headers/content-length [info/size: headers/content-length: to integer! headers/content-length]
if headers/last-modified [info/date: attempt [to date! headers/last-modified]]
remove/part conn/data d2
state/state: 'reading-data
]
unless headers [
read conn
return false
]
res: false
unless info/response-parsed [
;?? line
parse/all line [
"HTTP/1." [#"0" | #"1"] some #" " [
#"1" (info/response-parsed: 'info)
|
#"2" [["04" | "05"] (info/response-parsed: 'no-content)
| (info/response-parsed: 'ok)
]
|
#"3" [
"03" (info/response-parsed: 'see-other)
|
"04" (info/response-parsed: 'not-modified)
|
"05" (info/response-parsed: 'use-proxy)
| (info/response-parsed: 'redirect)
]
|
#"4" [
"01" (info/response-parsed: 'unauthorized)
|
"07" (info/response-parsed: 'proxy-auth)
| (info/response-parsed: 'client-error)
]
|
#"5" (info/response-parsed: 'server-error)
]
| (info/response-parsed: 'version-not-supported)
]
]
switch/all info/response-parsed [
ok [
either spec/method = 'head [
state/state: 'ready
res: awake make event! [type: 'done port: port]
unless res [res: awake make event! [type: 'ready port: port]]
] [
res: check-data port
if all [not res state/state = 'ready] [
res: awake make event! [type: 'done port: port]
unless res [res: awake make event! [type: 'ready port: port]]
]
]
]
redirect see-other [
either spec/method = 'head [
state/state: 'ready
res: awake make event! [type: 'custom port: port code: 0]
] [
res: check-data port
]
if all [not res state/state = 'ready] [
either all [
any [
find [get head] spec/method
all [
info/response-parsed = 'see-other
spec/method: 'get
]
]
in headers 'Location
] [
res: do-redirect port headers/location
] [
state/error: make-http-error "Redirect requires manual intervention"
res: awake make event! [type: 'error port: port]
]
]
]
unauthorized client-error server-error proxy-auth [
either spec/method = 'head [
state/state: 'ready
] [
check-data port
]
]
unauthorized [
state/error: make-http-error "Authentication not supported yet"
res: awake make event! [type: 'error port: port]
]
client-error server-error [
state/error: make-http-error ["Server error: " line]
res: awake make event! [type: 'error port: port]
]
not-modified [state/state: 'ready
res: awake make event! [type: 'done port: port]
unless res [res: awake make event! [type: 'ready port: port]]
]
use-proxy [
state/state: 'ready
state/error: make-http-error "Proxies not supported yet"
res: awake make event! [type: 'error port: port]
]
proxy-auth [
state/error: make-http-error "Authentication and proxies not supported yet"
res: awake make event! [type: 'error port: port]
]
no-content [
state/state: 'ready
res: awake make event! [type: 'done port: port]
unless res [res: awake make event! [type: 'ready port: port]]
]
info [
info/headers: info/response-line: info/response-parsed: port/data: none
state/state: 'reading-headers
read conn
]
version-not-supported [
state/error: make-http-error "HTTP response version not supported"
res: awake make event! [type: 'error port: port]
close port
]
]
res
]
crlfbin: #{0D0A}
crlf2bin: #{0D0A0D0A}
crlf2: to string! crlf2bin
http-response-headers: context [
Content-Length:
Transfer-Encoding:
Last-Modified: none
]
do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state] [
spec: port/spec
state: port/state
if #"/" = first new-uri [
new-uri: to url! ajoin [spec/scheme "://" spec/host new-uri]
]
new-uri: construct/with decode-url new-uri port/scheme/spec
if new-uri/scheme <> 'http [
state/error: make-http-error {Redirect to a protocol different from HTTP not supported}
return state/awake make event! [type: 'error port: port]
]
either all [
new-uri/host = spec/host
new-uri/port-id = spec/port-id
] [
spec/path: new-uri/path
do-request port
false
] [
state/error: make-http-error "Redirect to other host - requires custom handling"
state/awake make event! [type: 'error port: port]
]
]
check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer state conn] [
state: port/state
headers: state/info/headers
conn: state/connection
res: false
case [
headers/transfer-encoding = "chunked" [
data: conn/data
out: port/data: make binary! length? data
until [
either parse/all data [
copy chunk-size some hex-digits thru crlfbin mk1: to end
] [
chunk-size: to integer! to issue! chunk-size
either chunk-size = 0 [
if parse/all mk1 [
crlfbin (trailer: "") to end | copy trailer to crlf2bin to end
] [
trailer: construct trailer
append headers body-of trailer
state/state: 'ready
res: state/awake make event! [type: 'custom port: port code: 0]
clear data
]
true
] [
either parse/all mk1 [
chunk-size skip mk2: crlfbin to end
] [
insert/part tail out mk1 mk2
remove/part data skip mk2 2
empty? data
] [
true
]
]
] [
true
]
]
unless state/state = 'ready [read conn]
]
integer? headers/content-length [
port/data: conn/data
either headers/content-length <= length? port/data [
state/state: 'ready
conn/data: make binary! 32000
res: state/awake make event! [type: 'custom port: port code: 0]
] [
read conn
]
]
true [
port/data: conn/data
read conn
]
]
res
]
hex-digits: charset "1234567890abcdefABCDEF"
sys/make-scheme [
name: 'http
title: "HyperText Transport Protocol v1.1"
spec: make system/standard/port-spec-net [
path: %/
method: 'get
headers: []
content: none
timeout: 15
]
info: make system/standard/file-info [
response-line:
response-parsed:
headers: none
]
actor: [
read: func [
port [port!]
] [
either any-function? :port/awake [
unless open? port [cause-error 'Access 'not-open port/spec/ref]
if port/state/state <> 'ready [http-error "Port not ready"]
port/state/awake: :port/awake
do-request port
port
] [
sync-op port []
]
]
write: func [
port [port!]
value
] [
unless any [block? :value binary? :value any-string? :value] [value: form :value]
unless block? value [value: reduce [[Content-Type: "application/x-www-form-urlencoded; charset=utf-8"] value]]
either any-function? :port/awake [
unless open? port [cause-error 'Access 'not-open port/spec/ref]
if port/state/state <> 'ready [http-error "Port not ready"]
port/state/awake: :port/awake
parse-write-dialect port value
do-request port
port
] [
sync-op port [parse-write-dialect port value]
]
]
open: func [
port [port!]
/local conn
] [
if port/state [return port]
if none? port/spec/host [http-error "Missing host address"]
port/state: context [
state: 'inited
connection:
error: none
close?: no
info: make port/scheme/info [type: 'file]
awake: :port/awake
]
port/state/connection: conn: make port! [
scheme: 'tcp
host: port/spec/host
port-id: port/spec/port-id
ref: rejoin [tcp:// host ":" port-id]
]
conn/awake: :http-awake
conn/locals: port
open conn
port
]
open?: func [
port [port!]
] [
found? all [port/state open? port/state/connection]
]
close: func [
port [port!]
] [
if port/state [
close port/state/connection
port/state/connection/awake: none
port/state: none
]
port
]
copy: func [
port [port!]
] [
either all [port/spec/method = 'head port/state] [
reduce bind [name size date] port/state/info
] [
if port/data [copy port/data]
]
]
query: func [
port [port!]
/local error state
] [
if state: port/state [
either error? error: state/error [
state/error: none
error
] [
state/info
]
]
]
length?: func [
port [port!]
] [
either port/data [length? port/data] [0]
]
]
]