Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/websocket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ module Frame = struct
let content = Bytes.to_string content in
create ?opcode ?extension ?final ~content ()

let close code =
let content = Bytes.create 2 in
let close ?reason code =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check the maximum length (123 bytes, according to wikipedia)

let content = match reason with
| None -> Bytes.create 2
| Some reason -> Bytes.init (String.length reason + 2) @@ fun i ->
if i <= 1 then '\000' else reason.[i - 2]
in
Comment on lines +96 to +101
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let close ?reason code =
let content = match reason with
| None -> Bytes.create 2
| Some reason -> Bytes.init (String.length reason + 2) @@ fun i ->
if i <= 1 then '\000' else reason.[i - 2]
in
let close ?(reason=String.empty) code =
let content = Bytes.create (2 + String.length reason) in
Bytes.blit_string reason 0 content 2 (String.length reason);

EndianBytes.BigEndian.set_int16 content 0 code;
of_bytes ~opcode:Opcode.Close content
end
Expand Down
4 changes: 3 additions & 1 deletion core/websocket.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ module Frame : sig
unit ->
t

val close : int -> t
(** [close ?reason code] returns a close frame with status [code]
and an optional [reason]. *)
val close : ?reason:string -> int -> t
end

val check_origin :
Expand Down