使用cohttp库(或其他)的持久HTTP连接

时间:2016-12-03 14:49:41

标签: ocaml

似乎(基于wireshark),cohttp客户端在收到对GET请求的响应后自动关闭其连接。
有没有办法让这个连接保持活着(让它持久)? 如果没有任何其他HTTP库来创建持久连接?<​​/ p>

1 个答案:

答案 0 :(得分:2)

查看github处的代码,看起来没有这样的选项。

let call ?(ctx=default_ctx) ?headers ?(body=`Empty) ?chunked meth uri =
  ...
  Net.connect_uri ~ctx uri >>= fun (conn, ic, oc) ->
  let closefn () = Net.close ic oc in
  ...
  read_response ~closefn ic oc meth

read_response的位置:

let read_response ~closefn ic oc meth =
  ...
   match has_body with
    | `Yes | `Unknown ->
      let reader = Response.make_body_reader res ic in
      let stream = Body.create_stream Response.read_body_chunk reader in
      let closefn = closefn in
      Lwt_stream.on_terminate stream closefn;
      let gcfn st = closefn () in
      Gc.finalise gcfn stream;
      let body = Body.of_stream stream in
      return (res, body)

如果我正确读取此信息,一旦GC清理了流,连接就会关闭。

相关问题