libmicrohttpd icon indicating copy to clipboard operation
libmicrohttpd copied to clipboard

bug in BASE64Encode in websocket_threaded_example.c

Open timredfern opened this issue 1 year ago • 1 comments

I found an issue in the encoding function in this example. As it stands, the Sec-WebSocket-Key field is returned incorrectly and thus clients that check this cannot connect.

It's only one line to change so I don't know if it's worth getting permission to create a branch or fork the project.

This is the patch:

index 50919190..fec8e631 100644
--- a/src/examples/websocket_threaded_example.c
+++ b/src/examples/websocket_threaded_example.c
@@ -387,7 +387,7 @@ BASE64Encode (const void *in, size_t len, char **output)
     opt[ret++] = cvt[(int) c];
     if (i < len)
     {
-      c = (char) (c | ((data[i] << 2) & 0x3F));
+      c = (char) ((data[i] << 2) & 0x3F);
       if (++i < len)
       {
         c = (char) (c | ((data[i] >> 6) & 0x03));

timredfern avatar Aug 09 '24 10:08 timredfern

Thanks for the report. The whole "websockets" part is experimental in MHD (but the "upgrade" functionality is not).

I'll fix this example, as this function is badly broken on platforms with signed char.

Karlson2k avatar Aug 09 '24 11:08 Karlson2k