Skip to content

Allow to attach reasons to close frames#142

Open
mebsout wants to merge 1 commit into
vbmithr:masterfrom
mebsout:close-reason
Open

Allow to attach reasons to close frames#142
mebsout wants to merge 1 commit into
vbmithr:masterfrom
mebsout:close-reason

Conversation

@mebsout

@mebsout mebsout commented Jan 20, 2025

Copy link
Copy Markdown
Contributor

This MR allows applications to attach a reason when sending a close frame, as per https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1.

Note that we could add reasons for the various cases in which the library closes the connection.

Comment thread core/websocket.ml

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

Copy link
Copy Markdown
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)

Comment thread core/websocket.ml
Comment on lines +96 to +101
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

Copy link
Copy Markdown
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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants