Skip to content

Commit 8bea939

Browse files
committed
FIX: HTTP protocol now closes connection in case of error
FEAT: removed old `idate-to-date` function and using recently modified `to-date` for conversion from a standard Internet date to Rebol date.
1 parent 6b4ef63 commit 8bea939

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

src/mezz/prot-http.r

+29-45
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ REBOL [
1111
}
1212
Name: 'http
1313
Type: 'module
14-
Version: 0.1.6
14+
Version: 0.2.0
1515
File: %prot-http.r
1616
Purpose: {
1717
This program defines the HTTP protocol scheme for REBOL 3.
1818
}
1919
Author: ["Gabriele Santilli" "Richard Smolak" "Oldes"]
20-
Date: 21-Mar-2019
20+
Date: 28-Mar-2019
2121
History: [
2222
0.1.1 22-Jun-2007 "Gabriele Santilli" "Version used in R3-Alpha"
2323
0.1.4 26-Nov-2012 "Richard Smolak" "Version from Atronix's fork"
@@ -26,36 +26,10 @@ REBOL [
2626
0.1.7 03-Dec-2018 "Oldes" "FEAT: Added support for QUERY/MODE action"
2727
0.1.8 21-Mar-2019 "Oldes" "FEAT: Using system trace outputs"
2828
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"
2930
]
3031
]
3132

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-
5933
sync-op: func [port body /local state encoding content-type code-page tmp] [
6034
unless port/state [open port port/state/close?: yes]
6135
state: port/state
@@ -66,12 +40,12 @@ sync-op: func [port body /local state encoding content-type code-page tmp] [
6640
;The timeout should be triggered only when the response from other side exceeds the timeout value.
6741
;--Richard
6842
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+
]
7247
switch state/state [
7348
inited [
74-
;@@net-log ["state/connection open? ->" open? state/connection]
7549
if not open? state/connection [
7650
state/error: make-http-error rejoin ["Internal " state/connection/spec/ref " connection closed"]
7751
break
@@ -88,7 +62,9 @@ sync-op: func [port body /local state encoding content-type code-page tmp] [
8862
]
8963
]
9064
]
91-
if state/error [do state/error]
65+
if state/error [
66+
state/awake make event! [type: 'error port: port]
67+
]
9268

9369
body: copy port
9470

@@ -150,7 +126,11 @@ read-sync-awake: func [event [event!] /local error] [
150126
error [
151127
error: event/port/state/error
152128
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]
154134
]
155135
] [
156136
false
@@ -163,7 +143,7 @@ http-awake: func [event /local port http-port state awake res] [
163143
if any-function? :http-port/awake [state/awake: :http-port/awake]
164144
awake: :state/awake
165145

166-
sys/log/debug 'HTTP ["awake:" event/type]
146+
sys/log/debug 'HTTP ["awake:^[[1m" event/type "^[[22mstate:^[[1m" state/state]
167147

168148
switch/default event/type [
169149
read [
@@ -181,7 +161,8 @@ http-awake: func [event /local port http-port state awake res] [
181161
state/state: 'ready
182162
awake make event! [type: 'connect port: http-port]
183163
]
184-
close [
164+
close
165+
error [
185166
;?? state/state
186167
res: switch state/state [
187168
ready [
@@ -205,7 +186,9 @@ http-awake: func [event /local port http-port state awake res] [
205186
]
206187
]
207188
]
189+
sys/log/debug 'HTTP ["Closing:^[[1m" http-port/spec/ref]
208190
close http-port
191+
if error? state/error [ do state/error ]
209192
res
210193
]
211194
] [true]
@@ -221,12 +204,7 @@ make-http-error: func [
221204
arg1: message
222205
]
223206
]
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+
230208
make-http-request: func [
231209
"Create an HTTP request (returns string!)"
232210
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
316294
info/headers: headers: construct/with d1 http-response-headers
317295
sys/log/info 'HTTP ["Headers:^[[22m" mold headers]
318296
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+
]
321305
remove/part conn/data d2
322306
state/state: 'reading-data
323307
]

0 commit comments

Comments
 (0)