ocaml-ssl icon indicating copy to clipboard operation
ocaml-ssl copied to clipboard

Ssl.input_string fails with Ssl.Read_error exception

Open ChrisVine opened this issue 11 months ago • 0 comments

Ssl.input_string's implementation in ocaml-ssl-0.7.0 is broken, because Ssl.read (which is used in that implementation) never returns 0 (or a negative number) - instead it raises a Ssl.Read_error exception, with an Error_zero_return payload on end-of-file being reached. Any application of Ssl.input_string as currently appearing in ssl.ml will therefore end with an exception.

For what it is worth, here is a version which works for me:

let input_string sock =
  let bufsize = 2048 in
  let buf = Bytes.create bufsize in
  let rec loop acc =
    match Ssl.read sock buf 0 bufsize with
      num ->
       let text = Bytes.sub_string buf 0 num in
       loop (acc ^ text)
    | exception Ssl.Read_error err ->
       match err with Ssl.Error_zero_return -> acc
                    | _ -> raise (Ssl.Read_error err) in
  loop ""

ChrisVine avatar Feb 27 '24 11:02 ChrisVine