libwebsockets icon indicating copy to clipboard operation
libwebsockets copied to clipboard

Trying to understand usage of lws_add_http_header_by_name

Open qsc-jhndnn opened this issue 1 year ago • 1 comments

On the client side, when adding headers using lws_add_http_header_by_name in the LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER callback, what is the callers responsibility? I will be exposing the ability for users to create arbitrary sets of headers so I need to understand what error checking I need to do.

The comment in the header related to LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER only shows a single example. What would that look like for adding multiple headers?

const char* h1 = "header1:";
const char* v1 = "some value";
const char* h2  = "header2:";
const char* v2  = "another value;

unsigned char** p = (unsigned char**)in, * end = (*p) + len;

int ret = lws_add_http_header_by_name(current_session, h1, v1, p, end);

//increment p to go to next header?
p++;
ret = lws_add_http_header_by_name(current_session, h2, v2, p, end);

The user may supply things like super long header names or super long header values or 10,000 headers - what's the best way to deal with that without crashing? I understand that there is limited storage for headers. Do I need to track header storage or will lws_add_http_header_by_name return with some error code if it runs out of storage? If I need to track how should that be done? It doesn't appear as though lws_add_http_header_by_name updated the value of p so I would need to calculate something on my end?

qsc-jhndnn avatar Jun 01 '23 22:06 qsc-jhndnn

Experimentally I've determined that it isn't necessary to increment p to add multiple headers - passing in the same value into each call of lws_add_http_header_by_name appears to work fine. I'm also able to add at least 101 header entries with some being as long a 150 characters without any issue. Getting some feedback on what, if any, internal limits there are would still be very helpful.

qsc-jhndnn avatar Jun 05 '23 16:06 qsc-jhndnn