vbmithr / ocaml-websocket

Websocket library for OCaml
ISC License
161 stars 44 forks source link

Websocket_cohttp_lwt.upgrade_connection doesn't work with TLS connections #108

Closed hongchangwu closed 5 years ago

hongchangwu commented 5 years ago

e.g. if you do something like it doesn't really work:

let handle_websocket conn req body = 
  Websocket_cohttp_lwt.upgrade_connection
    req
    (fst conn)
    (fun frame -> ...)
  >>= fun (resp, body, send) ->
  ...
in
let handle_http body path = 
  ...
in
let callback conn req body =
  match req |> Request.uri |> Uri.path with
  | "/ws" -> handle_websocket conn req body
  | path  -> handle_http body path
in
let mode = `TLS (`Crt_file_path crt_file, `Key_file_path key_file, `No_password, `Port port) in
Cohttp_lwt_unix.Server ~mode (Cohttp_lwt_unix.Server.make ~callback ())

I think this is because upgrade_connection reads directly from the TCP sockets and bypasses the TLS layer of the cohttp server.

My workaround is to use the new expert mode of cohttp-lwt introduced in https://github.com/mirage/ocaml-cohttp/pull/647 which exposes input and output channels in the callback. But it does mean I have to duplicate a fair amount of logic from upgrade_connection. Is it possible to adapt upgrade_connection to work with the new expert mode of cohttp?

vbmithr commented 5 years ago

I started working on a fix but I got diverted by other things. I'll try to finish it. Thanks for the report.

vbmithr commented 5 years ago

This should be fixed in master, please check the upgrade_connection sample program.

hongchangwu commented 5 years ago

Thanks! I'll give it a try.

hongchangwu commented 5 years ago

The new API seems to work well.

vbmithr commented 5 years ago

Thanks.