[Libwebsockets] Sending close codes from server to client

Andy Green andy at warmcat.com
Wed Mar 17 07:36:00 CET 2021



On 3/17/21 1:58 AM, Zendels, Michael wrote:
> Dear Andy and list,
> 
>      Thank you for libwebsockets. We are using libwebsockets on a 
> client. We want to handle the server sending a close code of 1013 “Try 
> Again Later”, as mentioned on 
> https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent 
> <https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent>, from the 
> server to the client. The current code in client-parser.c in the case 

That moved to lib/roles/ws/client-parser-ws.c quite a while ago, I guess 
it means you are talking about some old lws version.  But I think this 
code is stable for a long while.

> statement LWSWSOPC_CLOSE looks like it changes close codes to 1002 
> LWS_CLOSE_STATUS_PROTOCOL_ERR. This change went in 7c2868. Is there any 
> reason why we should not pass the close code through instead of 
> overriding it to 1002?

+                       if (wsi->u.ws.rx_user_buffer_head >= 2) {
+                               close_code = (pp[0] << 8) | pp[1];
+                               if (close_code < 1000 || close_code == 
1004 ||
+                                   close_code == 1005 || close_code == 
1006 ||
+                                   close_code == 1012 || close_code == 
1013 ||
+                                   close_code == 1014 || close_code == 
1015 ||
+                                   (close_code >= 1016 && close_code < 
3000)
+                               ) {
+                                       pp[0] = 
(LWS_CLOSE_STATUS_PROTOCOL_ERR >> 8) & 0xff;
+                                       pp[1] = 
LWS_CLOSE_STATUS_PROTOCOL_ERR & 0xff;
+                               }
+                       }

Autobahn tests that we reject invalid close codes with a PROTOCOL_ERR, 
so we do.  Eg "Status codes in the range 0 to 999 are not used", 1004 is 
"reserved", 1005 / 1006 are "a reserved value and MUST NOT be set as a 
status code", etc.

https://tools.ietf.org/html/rfc6455#section-7.4.1

-Andy


More information about the Libwebsockets mailing list