Skip to content

Conversation

mebsout
Copy link
Contributor

@mebsout mebsout commented Jan 20, 2025

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.


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)

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
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