|
| 1 | +open Websocket |
| 2 | +open Astring |
| 3 | +open Eio |
| 4 | + |
| 5 | +type mode = Client of (int -> string) | Server |
| 6 | + |
| 7 | +let is_client mode = mode <> Server |
| 8 | + |
| 9 | +let xor mask msg = |
| 10 | + for i = 0 to Bytes.length msg - 1 do |
| 11 | + (* masking msg to send *) |
| 12 | + Bytes.set msg i |
| 13 | + Char.(to_int mask.[i mod 4] lxor to_int (Bytes.get msg i) |> of_byte) |
| 14 | + done |
| 15 | + |
| 16 | +let is_bit_set idx v = (v lsr idx) land 1 = 1 |
| 17 | +let set_bit v idx b = if b then v lor (1 lsl idx) else v land lnot (1 lsl idx) |
| 18 | +let int_value shift len v = (v lsr shift) land ((1 lsl len) - 1) |
| 19 | + |
| 20 | +let read_exactly src remaining = |
| 21 | + try |
| 22 | + Some (Buf_read.take remaining src) |
| 23 | + with End_of_file -> None |
| 24 | + |
| 25 | +let read_uint16 ic = |
| 26 | + match read_exactly ic 2 with |
| 27 | + | None -> None |
| 28 | + | Some s -> Some (EndianString.BigEndian.get_uint16 s 0) |
| 29 | + |
| 30 | +let read_int64 ic = |
| 31 | + match read_exactly ic 8 with |
| 32 | + | None -> None |
| 33 | + | Some s -> Some (Int64.to_int @@ EndianString.BigEndian.get_int64 s 0) |
| 34 | + |
| 35 | +let write_frame_to_buf ~mode buf fr = |
| 36 | + let open Frame in |
| 37 | + let content = Bytes.unsafe_of_string fr.content in |
| 38 | + let len = Bytes.length content in |
| 39 | + let opcode = Opcode.to_enum fr.opcode in |
| 40 | + let payload_len = |
| 41 | + match len with |
| 42 | + | n when n < 126 -> len |
| 43 | + | n when n < 1 lsl 16 -> 126 |
| 44 | + | _ -> 127 in |
| 45 | + let hdr = set_bit 0 15 fr.final in |
| 46 | + (* We do not support extensions for now *) |
| 47 | + let hdr = hdr lor (opcode lsl 8) in |
| 48 | + let hdr = set_bit hdr 7 (is_client mode) in |
| 49 | + let hdr = hdr lor payload_len in |
| 50 | + (* Payload len is guaranteed to fit in 7 bits *) |
| 51 | + Buf_write.BE.uint16 buf hdr; |
| 52 | + ( match len with |
| 53 | + | n when n < 126 -> () |
| 54 | + | n when n < 1 lsl 16 -> |
| 55 | + Buf_write.BE.uint16 buf n |
| 56 | + | n -> |
| 57 | + Buf_write.BE.uint64 buf Int64.(of_int n); |
| 58 | + ); |
| 59 | + ( match mode with |
| 60 | + | Server -> () |
| 61 | + | Client random_string -> |
| 62 | + let mask = random_string 4 in |
| 63 | + Buf_write.string buf mask ; |
| 64 | + if len > 0 then xor mask content ) ; |
| 65 | + Buf_write.bytes buf content |
| 66 | + |
| 67 | +let close_with_code mode dst code = |
| 68 | + write_frame_to_buf ~mode dst @@ Frame.close code |
| 69 | + |
| 70 | +let read_frame ic oc mode hdr = |
| 71 | + let hdr_part1 = EndianString.BigEndian.get_int8 hdr 0 in |
| 72 | + let hdr_part2 = EndianString.BigEndian.get_int8 hdr 1 in |
| 73 | + let final = is_bit_set 7 hdr_part1 in |
| 74 | + let extension = int_value 4 3 hdr_part1 in |
| 75 | + let opcode = int_value 0 4 hdr_part1 in |
| 76 | + let frame_masked = is_bit_set 7 hdr_part2 in |
| 77 | + let length = int_value 0 7 hdr_part2 in |
| 78 | + let opcode = Frame.Opcode.of_enum opcode in |
| 79 | + let payload_len = |
| 80 | + match length with |
| 81 | + | i when i < 126 -> i |
| 82 | + | 126 -> ( match read_uint16 ic with Some i -> i | None -> -1 ) |
| 83 | + | 127 -> ( match read_int64 ic with Some i -> i | None -> -1 ) |
| 84 | + | _ -> -1 in |
| 85 | + if payload_len = -1 then proto_error "payload len = %d" length |
| 86 | + else if extension <> 0 then ( |
| 87 | + close_with_code mode oc 1002 ; |
| 88 | + proto_error "unsupported extension" ) |
| 89 | + else if Frame.Opcode.is_ctrl opcode && payload_len > 125 then ( |
| 90 | + close_with_code mode oc 1002 ; |
| 91 | + proto_error "control frame too big" ) |
| 92 | + else |
| 93 | + let mask = |
| 94 | + if frame_masked then ( |
| 95 | + match read_exactly ic 4 with |
| 96 | + | None -> proto_error "could not read mask" |
| 97 | + | Some mask -> mask ) |
| 98 | + else String.empty in |
| 99 | + if payload_len = 0 then Frame.create ~opcode ~extension ~final () |
| 100 | + else ( |
| 101 | + match read_exactly ic payload_len with |
| 102 | + | None -> proto_error "could not read payload (len=%d)" payload_len |
| 103 | + | Some payload -> |
| 104 | + let payload = Bytes.unsafe_of_string payload in |
| 105 | + if frame_masked then xor mask payload ; |
| 106 | + let frame = Frame.of_bytes ~opcode ~extension ~final payload in |
| 107 | + frame ) |
| 108 | + |
| 109 | +let make_read_frame ~mode ic oc () = |
| 110 | + match read_exactly ic 2 with |
| 111 | + | None -> raise End_of_file |
| 112 | + | Some hdr -> read_frame ic oc mode hdr |
| 113 | + |
| 114 | +module Connected_client = struct |
| 115 | + type t = |
| 116 | + { buffer: Buf_write.t; |
| 117 | + endp: Conduit.endp; |
| 118 | + ic: Buf_read.t; |
| 119 | + http_request: Cohttp.Request.t; |
| 120 | + standard_frame_replies: bool; |
| 121 | + read_frame: unit -> Frame.t } |
| 122 | + |
| 123 | + let source {endp; _} = endp |
| 124 | + |
| 125 | + let create http_request endp ic oc = |
| 126 | + let read_frame = make_read_frame ~mode:Server ic oc in |
| 127 | + { buffer = oc; |
| 128 | + endp; |
| 129 | + ic; |
| 130 | + http_request; |
| 131 | + standard_frame_replies = false; |
| 132 | + read_frame } |
| 133 | + |
| 134 | + let send {buffer; _} frame = |
| 135 | + write_frame_to_buf ~mode:Server buffer frame |
| 136 | + |
| 137 | + let send_multiple {buffer; _} frames = |
| 138 | + List.iter (write_frame_to_buf ~mode:Server buffer) frames |
| 139 | + |
| 140 | + let standard_recv t = |
| 141 | + let fr = t.read_frame () in |
| 142 | + match fr.Frame.opcode with |
| 143 | + | Frame.Opcode.Ping -> |
| 144 | + send t @@ Frame.create ~opcode:Frame.Opcode.Pong () ; |
| 145 | + fr |
| 146 | + | Frame.Opcode.Close -> |
| 147 | + (* Immediately echo and pass this last message to the user *) |
| 148 | + if String.length fr.Frame.content >= 2 then |
| 149 | + send t |
| 150 | + @@ Frame.create ~opcode:Frame.Opcode.Close |
| 151 | + ~content: |
| 152 | + String.(sub ~start:0 ~stop:2 fr.Frame.content |> Sub.to_string) |
| 153 | + () |
| 154 | + else send t @@ Frame.close 1000 ; |
| 155 | + fr |
| 156 | + | _ -> fr |
| 157 | + |
| 158 | + let recv t = |
| 159 | + if t.standard_frame_replies then standard_recv t else t.read_frame () |
| 160 | + |
| 161 | + let http_request {http_request; _} = http_request |
| 162 | + let make_standard t = {t with standard_frame_replies= true} |
| 163 | +end |
0 commit comments