[Libwebsockets] Strange off by one on lws_hdr_total_length & lws_hdr_copy

Bas Appelo bas at appelo.nl
Thu Mar 6 20:54:00 CET 2014


Hi,


Today I fought a bit with the mentioned function, (lws_hdr_total_length & lws_hdr_copy), and the code I had was:


       const int size = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE);

        if(size) {
            std::vector<char> buffer;
            buffer.resize(size + 1);

            char *cBuf = &(buffer.at(0));
            lws_hdr_copy(wsi, cBuf, size + 1, WSI_TOKEN_HTTP_COOKIE);


Now this works, the weird thing is the line:
            lws_hdr_copy(wsi, cBuf, size + 1, WSI_TOKEN_HTTP_COOKIE);
especially the size + 1, if I just give it size, the cBuf will stay empty, while size is just returned from lws_hdr_total_length, why the extra 1 to make it work?

Anyways, could also be me, otherwise good luck!
Bas


More information about the Libwebsockets mailing list