package dream-httpaf

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file websocketaf_lwt.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Gluten_lwt = Dream_gluten_lwt.Gluten_lwt
module Httpaf = Dream_httpaf_.Httpaf

let sha1 s =
  s
  |> Digestif.SHA1.digest_string
  |> Digestif.SHA1.to_raw_string

include Websocketaf_lwt_intf

module Server (Server_runtime: Gluten_lwt.Server) = struct
  type socket = Server_runtime.socket

  (* TODO: should this error handler be a websocket error handler or an HTTP
   * error handler?*)
  let create_connection_handler
    ?(config = Httpaf.Config.default)
    ~websocket_handler
    ~error_handler = fun client_addr socket ->
    let connection =
      Websocketaf.Server_connection.create
        ~sha1
        ~error_handler:(error_handler client_addr)
        (websocket_handler client_addr)
    in
    Server_runtime.create_connection_handler
      ~read_buffer_size:config.read_buffer_size
      ~protocol:(module Websocketaf.Server_connection)
      connection
      client_addr
      socket
end

module Client (Client_runtime: Gluten_lwt.Client) = struct
  type t = Client_runtime.t
  type socket = Client_runtime.socket

  let connect
      ?(config=Httpaf.Config.default)
      ~nonce ~host ~port ~resource ~error_handler ~websocket_handler socket =
    let headers = Httpaf.Headers.of_list
      ["host", String.concat ":" [host; string_of_int port]]
    in
    let connection =
      Websocketaf.Client_connection.connect
        ~nonce
        ~headers
        ~sha1
        ~error_handler
        ~websocket_handler
        resource
    in
    Client_runtime.create
      ~read_buffer_size:config.read_buffer_size
      ~protocol:(module Websocketaf.Client_connection)
      connection
      socket

  let is_closed t = Client_runtime.is_closed t
end
OCaml

Innovation. Community. Security.