Skip to content

Commit 10565cf

Browse files
committed
Fix to fully transmit body
I noticed that when using the bytecode interpreter the request/response body was not always properly transmitted --- a test will appear in a separate PR. This simplifies the client to not try to write things concurrently and changes both sides to shutdown the connection partially after being done with writing or reading. This seems to have eliminated the issue.
1 parent 5517d78 commit 10565cf

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

lib/picos_stdio_cohttp/picos_stdio_cohttp.ml

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,14 @@ module Client =
176176
Fmt.(option ~none:(any "None") Dump.string)
177177
x
178178

179-
let connect uri =
180-
let ai = resolve uri in
181-
let socket = Unix.socket ~cloexec:true ai.ai_family ai.ai_socktype 0 in
182-
match
183-
Unix.set_nonblock socket;
184-
Unix.connect socket ai.ai_addr
185-
with
186-
| () -> socket
187-
| exception exn ->
188-
Unix.close socket;
189-
raise exn
190-
191179
let call ?headers ?body ?(chunked = false) meth uri =
192-
let@ socket = finally Unix.close @@ fun () -> connect uri in
180+
let ai = resolve uri in
181+
let@ socket =
182+
finally Unix.close @@ fun () ->
183+
Unix.socket ~cloexec:true ai.ai_family ai.ai_socktype 0
184+
in
185+
Unix.set_nonblock socket;
186+
Unix.connect socket ai.ai_addr;
193187
let body_length =
194188
if chunked then None
195189
else
@@ -202,15 +196,12 @@ module Client =
202196
~chunked:(Option.is_none body_length)
203197
?body_length meth uri
204198
in
205-
Bundle.join_after @@ fun bundle ->
206-
begin
207-
Bundle.fork bundle @@ fun () ->
208-
Request.write ~flush:false
209-
(match body with
210-
| None -> ignore
211-
| Some body -> Body.write_with Request.write_body body)
212-
request socket
213-
end;
199+
Request.write ~flush:false
200+
(match body with
201+
| None -> ignore
202+
| Some body -> Body.write_with Request.write_body body)
203+
request socket;
204+
Unix.shutdown socket SHUTDOWN_SEND;
214205
let input = Private.IO.ic socket in
215206
match Response.read input with
216207
| `Eof -> failwith "connection closed by peer"
@@ -289,6 +280,7 @@ module Server = struct
289280
let keep_alive =
290281
Http.Request.is_keep_alive req && Http.Response.is_keep_alive res
291282
in
283+
if not keep_alive then Unix.shutdown ic.file_descr SHUTDOWN_RECEIVE;
292284
let headers =
293285
Http.Header.add_unless_exists
294286
(Http.Response.headers res)

0 commit comments

Comments
 (0)