Never emit 0 as close code; use 1005 instead

This commit is contained in:
Alex Hultman
2020-06-27 00:57:42 +02:00
parent 10bbfaa22f
commit 13e0a93441
2 changed files with 9 additions and 5 deletions
+3 -2
View File
@@ -124,8 +124,9 @@ public:
return true;
}
/* Send websocket close frame, emit close event, send FIN if successful */
void end(int code, std::string_view message = {}) {
/* Send websocket close frame, emit close event, send FIN if successful.
* Will not append a close reason if code is 0 or 1005. */
void end(int code = 0, std::string_view message = {}) {
/* Check if we already called this one */
WebSocketData *webSocketData = (WebSocketData *) us_socket_ext(SSL, (us_socket_t *) this);
if (webSocketData->isShuttingDown) {
+6 -3
View File
@@ -151,20 +151,23 @@ struct CloseFrame {
};
static inline CloseFrame parseClosePayload(char *src, size_t length) {
CloseFrame cf = {};
/* If we get no code or message, default to reporting 1005 no status code present */
CloseFrame cf = {1005};
if (length >= 2) {
memcpy(&cf.code, src, 2);
cf = {cond_byte_swap<uint16_t>(cf.code), src + 2, length - 2};
if (cf.code < 1000 || cf.code > 4999 || (cf.code > 1011 && cf.code < 4000) ||
(cf.code >= 1004 && cf.code <= 1006) || !isValidUtf8((unsigned char *) cf.message, cf.length)) {
return {};
/* Even though we got a WebSocket close frame, it in itself is abnormal */
return {1006};
}
}
return cf;
}
static inline size_t formatClosePayload(char *dst, uint16_t code, const char *message, size_t length) {
if (code) {
/* We could have more strict checks here, but never append code 0 or 1005 or 1006 */
if (code && code != 1005 && code != 1006) {
code = cond_byte_swap<uint16_t>(code);
memcpy(dst, &code, 2);
/* It is invalid to pass nullptr to memcpy, even though length is 0 */