diff --git a/eio/io.ml b/eio/io.ml index 8b0f59d..78a50c8 100644 --- a/eio/io.ml +++ b/eio/io.ml @@ -19,12 +19,8 @@ let int_value shift len v = (v lsr shift) land ((1 lsl len) - 1) (* Eio only uses Cstructs so we lose 0copy :(! *) let read_exactly src remaining = - let open Cohttp_eio.Server in try - Buf_read.ensure src remaining ; - let t = Some (Cstruct.to_string (Buf_read.peek src) ~off:0 ~len:remaining) in - Buf_read.consume src remaining ; - t + Some (Buf_read.take remaining src) with End_of_file -> None let read_uint16 ic = @@ -38,7 +34,6 @@ let read_int64 ic = | Some s -> Some (Int64.to_int @@ EndianString.BigEndian.get_int64 s 0) let write_frame_to_buf ~mode buf fr = - let scratch = Bytes.create 8 in let open Frame in let content = Bytes.unsafe_of_string fr.content in let len = Bytes.length content in @@ -54,16 +49,14 @@ let write_frame_to_buf ~mode buf fr = let hdr = set_bit hdr 7 (is_client mode) in let hdr = hdr lor payload_len in (* Payload len is guaranteed to fit in 7 bits *) - EndianBytes.BigEndian.set_int16 scratch 0 hdr ; - Buf_write.bytes ~off:0 ~len:2 buf scratch ; + Buf_write.BE.uint16 buf hdr; ( match len with | n when n < 126 -> () | n when n < 1 lsl 16 -> - EndianBytes.BigEndian.set_int16 scratch 0 n ; - Buf_write.bytes ~off:0 ~len:2 buf scratch + Buf_write.BE.uint16 buf n | n -> - EndianBytes.BigEndian.set_int64 scratch 0 Int64.(of_int n) ; - Buf_write.bytes ~off:0 ~len:8 buf scratch ) ; + Buf_write.BE.uint64 buf Int64.(of_int n); + ); ( match mode with | Server -> () | Client random_string -> @@ -73,9 +66,7 @@ let write_frame_to_buf ~mode buf fr = Buf_write.bytes buf content let close_with_code mode dst code = - Buf_write.flush dst ; - write_frame_to_buf ~mode dst @@ Frame.close code ; - Buf_write.flush dst + write_frame_to_buf ~mode dst @@ Frame.close code let read_frame ic oc mode hdr = let hdr_part1 = EndianString.BigEndian.get_int8 hdr 0 in @@ -86,7 +77,6 @@ let read_frame ic oc mode hdr = let frame_masked = is_bit_set 7 hdr_part2 in let length = int_value 0 7 hdr_part2 in let opcode = Frame.Opcode.of_enum opcode in - Buf_write.flush oc ; let payload_len = match length with | i when i < 126 -> i @@ -103,14 +93,12 @@ let read_frame ic oc mode hdr = else let mask = if frame_masked then ( - Buf_write.flush oc ; match read_exactly ic 4 with | None -> proto_error "could not read mask" | Some mask -> mask ) else String.empty in if payload_len = 0 then Frame.create ~opcode ~extension ~final () else ( - Buf_write.flush oc ; match read_exactly ic payload_len with | None -> proto_error "could not read payload (len=%d)" payload_len | Some payload -> @@ -120,7 +108,6 @@ let read_frame ic oc mode hdr = frame ) let make_read_frame ~mode ic oc () = - Buf_write.flush oc ; match read_exactly ic 2 with | None -> raise End_of_file | Some hdr -> read_frame ic oc mode hdr @@ -136,24 +123,20 @@ module Connected_client = struct let source {endp; _} = endp - let create ?read_buf http_request endp ic oc = + let create http_request endp ic oc = let read_frame = make_read_frame ~mode:Server ic oc in - { buffer= oc; + { buffer = oc; endp; ic; http_request; - standard_frame_replies= false; + standard_frame_replies = false; read_frame } let send {buffer; _} frame = - Buf_write.flush buffer ; - write_frame_to_buf ~mode:Server buffer frame ; - Buf_write.flush buffer + write_frame_to_buf ~mode:Server buffer frame let send_multiple {buffer; _} frames = - Buf_write.flush buffer ; - List.iter (write_frame_to_buf ~mode:Server buffer) frames ; - Buf_write.flush buffer + List.iter (write_frame_to_buf ~mode:Server buffer) frames let standard_recv t = let fr = t.read_frame () in diff --git a/websocket-eio.opam b/websocket-eio.opam index cca7449..37a693a 100644 --- a/websocket-eio.opam +++ b/websocket-eio.opam @@ -46,3 +46,7 @@ build: [ ["dune" "install" "-p" name "--create-install-files" name] ] dev-repo: "git+https://github.com/vbmithr/ocaml-websocket.git" +pin-depends: [ + [ "http.dev" "git+https://github.com/mirage/ocaml-cohttp#ce5f271b69fe42471ede858c5b8ce8203e3c14ad" ] + [ "cohttp-eio.dev" "git+https://github.com/mirage/ocaml-cohttp#ce5f271b69fe42471ede858c5b8ce8203e3c14ad" ] +] \ No newline at end of file