@@ -11,13 +11,13 @@ REBOL [
11
11
}
12
12
Name: 'http
13
13
Type: 'module
14
- Version: 0.1.6
14
+ Version: 0.2.0
15
15
File: %prot-http.r
16
16
Purpose: {
17
17
This program defines the HTTP protocol scheme for REBOL 3.
18
18
}
19
19
Author: ["Gabriele Santilli" "Richard Smolak" "Oldes" ]
20
- Date: 21 -Mar-2019
20
+ Date: 28 -Mar-2019
21
21
History: [
22
22
0.1.1 22-Jun-2007 "Gabriele Santilli" "Version used in R3-Alpha"
23
23
0.1.4 26-Nov-2012 "Richard Smolak" "Version from Atronix's fork"
@@ -26,36 +26,10 @@ REBOL [
26
26
0.1.7 03-Dec-2018 "Oldes" "FEAT: Added support for QUERY/MODE action"
27
27
0.1.8 21-Mar-2019 "Oldes" "FEAT: Using system trace outputs"
28
28
0.1.9 21-Mar-2019 "Oldes" "FEAT: Added support for transfer compression"
29
+ 0.2.0 28-Mar-2019 "Oldes" "FIX: close connection in case of errors"
29
30
]
30
31
]
31
32
32
- ;@@ idate-to-date should be moved to other location!
33
- digit: charset [#"0" - #"9" ]
34
- alpha: charset [#"a" - #"z" #"A" - #"Z" ]
35
- idate-to-date : function [ date [string! ]] [
36
- either parse date [
37
- 5 skip
38
- copy day: 2 digit
39
- space
40
- copy month: 3 alpha
41
- space
42
- copy year: 4 digit
43
- space
44
- copy time: to space
45
- space
46
- copy zone: to end
47
- ][
48
- if zone = "GMT" [zone: copy "+0" ]
49
- to date! rejoin [day "-" month "-" year "/" time zone]
50
- ][
51
- none
52
- ]
53
- ]
54
- ;@@==================================================
55
-
56
- ;@@ just simple trace function
57
- ;@@net-log: :print
58
-
59
33
sync-op : func [ port body /local state encoding content-type code-page tmp] [
60
34
unless port/state [open port port/state/close?: yes]
61
35
state: port/state
@@ -66,12 +40,12 @@ sync-op: func [port body /local state encoding content-type code-page tmp] [
66
40
;The timeout should be triggered only when the response from other side exceeds the timeout value.
67
41
;--Richard
68
42
while [not find [ready close] state/state ][
69
- ;@@net-log ["########### sync-op.." state/state]
70
- unless port? wait [state/connection port/spec/timeout ] [http-error "HTTP(s) Timeout" ]
71
- ;@@net-log ["########### sync-op wakeup" state/state]
43
+ unless port? wait [state/connection port/spec/timeout ] [
44
+ state/error: make-http-error "HTTP(s) Timeout"
45
+ break
46
+ ]
72
47
switch state/state [
73
48
inited [
74
- ;@@net-log ["state/connection open? ->" open? state/connection]
75
49
if not open? state/connection [
76
50
state/error: make-http-error rejoin ["Internal " state/connection/spec/ref " connection closed" ]
77
51
break
@@ -88,7 +62,9 @@ sync-op: func [port body /local state encoding content-type code-page tmp] [
88
62
]
89
63
]
90
64
]
91
- if state/error [do state/error ]
65
+ if state/error [
66
+ state/awake make event! [type: 'error port: port]
67
+ ]
92
68
93
69
body: copy port
94
70
@@ -150,7 +126,11 @@ read-sync-awake: func [event [event!] /local error] [
150
126
error [
151
127
error: event/port/state/error
152
128
event/port/state/error: none
153
- do error
129
+ try [
130
+ sys/log/debug 'HTTP ["Closing (sync-awake):^[ [1m" event/port/spec/ref ]
131
+ close event/port
132
+ ]
133
+ if error? error [do error]
154
134
]
155
135
] [
156
136
false
@@ -163,7 +143,7 @@ http-awake: func [event /local port http-port state awake res] [
163
143
if any-function? : http-port/awake [state/awake: : http-port/awake ]
164
144
awake: : state/awake
165
145
166
- sys/log/debug 'HTTP ["awake:" event/type ]
146
+ sys/log/debug 'HTTP ["awake:^[ [1m " event/type " ^[ [22mstate: ^[ [1m" state /state ]
167
147
168
148
switch /default event/type [
169
149
read [
@@ -181,7 +161,8 @@ http-awake: func [event /local port http-port state awake res] [
181
161
state/state: 'ready
182
162
awake make event! [type: 'connect port: http-port]
183
163
]
184
- close [
164
+ close
165
+ error [
185
166
;?? state/state
186
167
res: switch state/state [
187
168
ready [
@@ -205,7 +186,9 @@ http-awake: func [event /local port http-port state awake res] [
205
186
]
206
187
]
207
188
]
189
+ sys/log/debug 'HTTP ["Closing:^[ [1m" http-port/spec/ref ]
208
190
close http-port
191
+ if error? state/error [ do state/error ]
209
192
res
210
193
]
211
194
] [true]
@@ -221,12 +204,7 @@ make-http-error: func [
221
204
arg1: message
222
205
]
223
206
]
224
- http-error : func [
225
- "Throw an error for the HTTP protocol"
226
- message [string! block! ]
227
- ] [
228
- do make-http-error message
229
- ]
207
+
230
208
make-http-request : func [
231
209
"Create an HTTP request (returns string!)"
232
210
method [word! string! ] "E.g. GET, HEAD, POST etc."
@@ -316,8 +294,14 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s
316
294
info/headers: headers: construct /with d1 http-response-headers
317
295
sys/log/info 'HTTP ["Headers:^[ [22m" mold headers]
318
296
info/name: spec/ref
319
- if headers/content-length [info/size: headers/content-length: to integer! headers/content-length ]
320
- if headers/last-modified [info/date: attempt [idate-to-date headers/last-modified ]]
297
+ if state/error: try [
298
+ ; make sure that values bellow are valid
299
+ if headers/content-length [info/size: headers/content-length: to integer! headers/content-length ]
300
+ if headers/last-modified [info/date: to-date/utc headers/last-modified ]
301
+ none ; no error
302
+ ][
303
+ awake make event! [type: 'error port: port]
304
+ ]
321
305
remove/part conn/data d2
322
306
state/state: 'reading-data
323
307
]
0 commit comments