mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-07 19:55:36 -05:00
feat ws: a bit of cleanup, expose masking primitive
This commit is contained in:
parent
0014334010
commit
dbd00259da
3 changed files with 22 additions and 8 deletions
|
|
@ -192,7 +192,8 @@ module Reader = struct
|
||||||
type state =
|
type state =
|
||||||
| Begin (** At the beginning of a frame *)
|
| Begin (** At the beginning of a frame *)
|
||||||
| Reading_frame of { mutable remaining_bytes: int }
|
| Reading_frame of { mutable remaining_bytes: int }
|
||||||
(** Currently reading the payload of a frame with [remaining_bytes] left to read *)
|
(** Currently reading the payload of a frame with [remaining_bytes]
|
||||||
|
left to read from the underlying [ic] *)
|
||||||
| Close
|
| Close
|
||||||
|
|
||||||
type t = {
|
type t = {
|
||||||
|
|
@ -245,7 +246,7 @@ module Reader = struct
|
||||||
) else if len = 127 then (
|
) else if len = 127 then (
|
||||||
IO.Input.really_input self.ic self.header_buf 0 8;
|
IO.Input.really_input self.ic self.header_buf 0 8;
|
||||||
let len64 = Bytes.get_int64_be self.header_buf 0 in
|
let len64 = Bytes.get_int64_be self.header_buf 0 in
|
||||||
if compare len64 (Int64.of_int max_fragment_size) > 0 then (
|
if Int64.compare len64 (Int64.of_int max_fragment_size) > 0 then (
|
||||||
Log.error (fun k ->
|
Log.error (fun k ->
|
||||||
k "websocket: maximum frame fragment exceeded (%Ld > %d)" len64
|
k "websocket: maximum frame fragment exceeded (%Ld > %d)" len64
|
||||||
max_fragment_size);
|
max_fragment_size);
|
||||||
|
|
@ -267,14 +268,15 @@ module Reader = struct
|
||||||
self.header.payload_len self.header.mask);*)
|
self.header.payload_len self.header.mask);*)
|
||||||
()
|
()
|
||||||
|
|
||||||
external apply_masking_ : bytes -> bytes -> int -> int -> unit
|
external apply_masking_ : key:bytes -> buf:bytes -> int -> int -> unit
|
||||||
= "tiny_httpd_ws_apply_masking"
|
= "tiny_httpd_ws_apply_masking"
|
||||||
[@@noalloc]
|
[@@noalloc]
|
||||||
(** Apply masking to the parsed data *)
|
(** Apply masking to the parsed data *)
|
||||||
|
|
||||||
let[@inline] apply_masking ~mask_key (buf : bytes) off len : unit =
|
let[@inline] apply_masking ~mask_key (buf : bytes) off len : unit =
|
||||||
assert (off >= 0 && off + len <= Bytes.length buf);
|
assert (
|
||||||
apply_masking_ mask_key buf off len
|
Bytes.length mask_key = 4 && off >= 0 && off + len <= Bytes.length buf);
|
||||||
|
apply_masking_ ~key:mask_key ~buf off len
|
||||||
|
|
||||||
let read_body_to_string (self : t) : string =
|
let read_body_to_string (self : t) : string =
|
||||||
let len = self.header.payload_len in
|
let len = self.header.payload_len in
|
||||||
|
|
@ -465,3 +467,7 @@ let add_route_handler ?accept ?(accept_ws_protocol = fun _ -> true)
|
||||||
end) in
|
end) in
|
||||||
let up : Server.upgrade_handler = (module M) in
|
let up : Server.upgrade_handler = (module M) in
|
||||||
Server.add_upgrade_handler ?accept server route up
|
Server.add_upgrade_handler ?accept server route up
|
||||||
|
|
||||||
|
module Private_ = struct
|
||||||
|
let apply_masking = Reader.apply_masking
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,11 @@ val add_route_handler :
|
||||||
(** Add a route handler for a websocket endpoint.
|
(** Add a route handler for a websocket endpoint.
|
||||||
@param accept_ws_protocol decides whether this endpoint accepts the websocket protocol
|
@param accept_ws_protocol decides whether this endpoint accepts the websocket protocol
|
||||||
sent by the client. Default accepts everything. *)
|
sent by the client. Default accepts everything. *)
|
||||||
|
|
||||||
|
(**/**)
|
||||||
|
|
||||||
|
module Private_ : sig
|
||||||
|
val apply_masking : mask_key:bytes -> bytes -> int -> int -> unit
|
||||||
|
end
|
||||||
|
|
||||||
|
(**/**)
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@ CAMLprim value tiny_httpd_ws_apply_masking(value _mask_key, value _buf,
|
||||||
intnat len = Int_val(_len);
|
intnat len = Int_val(_len);
|
||||||
|
|
||||||
for (intnat i = 0; i < len; ++i) {
|
for (intnat i = 0; i < len; ++i) {
|
||||||
char c = buf[offset + i];
|
unsigned char c = buf[offset + i];
|
||||||
char c_m = mask_key[i & 0x3];
|
unsigned char c_m = mask_key[i & 0x3];
|
||||||
buf[offset + i] = c ^ c_m;
|
buf[offset + i] = (unsigned char)(c ^ c_m);
|
||||||
}
|
}
|
||||||
CAMLreturn(Val_unit);
|
CAMLreturn(Val_unit);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue