Skip to content

Commit 6e4b8f9

Browse files
committed
FIX: handle situation when internal connection of HTTP port is closed unexpectedly
Without it the port was keeping in waiting state until timeout.
1 parent 5819e6d commit 6e4b8f9

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/mezz/prot-http.r

+20-3
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,18 @@ sync-op: func [port body /local state] [
6464
;--Richard
6565
while [not find [ready close] state/state][
6666
;@@net-log ["########### sync-op.." state/state]
67-
unless port? wait [state/connection port/spec/timeout] [http-error "Timeout"]
67+
unless port? wait [state/connection port/spec/timeout] [http-error "HTTP(s) Timeout"]
6868
;@@net-log ["########### sync-op wakeup" state/state]
6969
switch state/state [
70+
inited [
71+
;@@net-log ["state/connection open? ->" open? state/connection]
72+
if not open? state/connection [
73+
state/error: make-http-error rejoin ["Internal " state/connection/spec/ref " connection closed"]
74+
break
75+
]
76+
]
7077
reading-data [
78+
;? state/connection
7179
read state/connection
7280
]
7381
redirect [
@@ -77,6 +85,7 @@ sync-op: func [port body /local state] [
7785
]
7886
]
7987
]
88+
if state/error [do state/error]
8089
body: copy port
8190
if state/close? [close port]
8291
body
@@ -136,6 +145,7 @@ http-awake: func [event /local port http-port state awake res] [
136145
awake make event! [type: 'connect port: http-port]
137146
]
138147
close [
148+
;?? state/state
139149
res: switch state/state [
140150
ready [
141151
awake make event! [type: 'close port: http-port]
@@ -263,13 +273,16 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s
263273
]
264274
] [
265275
info/response-line: line: to string! copy/part conn/data d1
276+
;?? line
266277
info/headers: headers: construct/with d1 http-response-headers
267278
info/name: spec/ref
268279
if headers/content-length [info/size: headers/content-length: to integer! headers/content-length]
269280
if headers/last-modified [info/date: attempt [idate-to-date headers/last-modified]]
270281
remove/part conn/data d2
271282
state/state: 'reading-data
272283
]
284+
;? state
285+
;?? headers
273286
unless headers [
274287
read conn
275288
return false
@@ -317,6 +330,8 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s
317330
unless res [res: awake make event! [type: 'ready port: port]]
318331
] [
319332
res: check-data port
333+
;?? res
334+
;?? state/state
320335
if all [not res state/state = 'ready] [
321336
res: awake make event! [type: 'done port: port]
322337
unless res [res: awake make event! [type: 'ready port: port]]
@@ -455,6 +470,7 @@ check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer st
455470
res: false
456471

457472
;@@net-log ["[HTTP check-data] bytes:" length? conn/data]
473+
;? conn
458474

459475
case [
460476
headers/transfer-encoding = "chunked" [
@@ -577,7 +593,7 @@ sys/make-scheme [
577593
port [port!]
578594
/local conn
579595
] [
580-
;@@net-log ["[HTTP open]" port/state]
596+
;@@net-log ["[HTTP open] state:" port/state]
581597
if port/state [return port]
582598
if none? port/spec/host [http-error "Missing host address"]
583599
port/state: context [
@@ -588,6 +604,7 @@ sys/make-scheme [
588604
info: make port/scheme/info [type: 'url]
589605
awake: :port/awake
590606
]
607+
;? port/state/info
591608
port/state/connection: conn: make port! compose [
592609
scheme: (to lit-word! either port/spec/scheme = 'http ['tcp]['tls])
593610
host: port/spec/host
@@ -597,7 +614,7 @@ sys/make-scheme [
597614
;?? conn
598615
conn/awake: :http-awake
599616
conn/locals: port
600-
;@@net-log ["[HTTP opne]" conn/spec/scheme conn/spec/host]
617+
;@@net-log ["[HTTP open] scheme:" conn/spec/scheme conn/spec/host]
601618
;?? conn
602619
open conn
603620
port

0 commit comments

Comments
 (0)