headers: add length argument to Curl_headers_push()

- the length is already known by parent functions
- avoids strlen() calls
- avoids strchr() calls for trimming off newline characters

Closes #19886
This commit is contained in:
Daniel Stenberg 2025-12-09 09:58:48 +01:00
parent 70d71e8761
commit 4c3614304f
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 19 additions and 17 deletions

View file

@ -266,30 +266,31 @@ static CURLcode unfold_value(struct Curl_easy *data, const char *value,
/*
* Curl_headers_push() gets passed a full HTTP header to store. It gets called
* immediately before the header callback. The header is CRLF terminated.
* immediately before the header callback. The header is CRLF, CR or LF
* terminated.
*/
CURLcode Curl_headers_push(struct Curl_easy *data, const char *header,
size_t hlen, /* length of header */
unsigned char type)
{
char *value = NULL;
char *name = NULL;
char *end;
size_t hlen; /* length of the incoming header */
struct Curl_header_store *hs;
CURLcode result = CURLE_OUT_OF_MEMORY;
const size_t ilen = hlen;
if((header[0] == '\r') || (header[0] == '\n'))
/* ignore the body separator */
return CURLE_OK;
end = strchr(header, '\r');
if(!end) {
end = strchr(header, '\n');
if(!end)
/* neither CR nor LF as terminator is not a valid header */
return CURLE_WEIRD_SERVER_REPLY;
}
hlen = end - header;
/* trim off newline characters */
if(hlen && (header[hlen - 1] == '\n'))
hlen--;
if(hlen && (header[hlen - 1] == '\r'))
hlen--;
if(hlen == ilen)
/* neither CR nor LF as terminator is not a valid header */
return CURLE_WEIRD_SERVER_REPLY;
if((header[0] == ' ') || (header[0] == '\t')) {
if(data->state.prevhead)
@ -359,7 +360,7 @@ static CURLcode hds_cw_collect_write(struct Curl_easy *data,
(type & CLIENTWRITE_1XX ? CURLH_1XX :
(type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
CURLH_HEADER)));
CURLcode result = Curl_headers_push(data, buf, htype);
CURLcode result = Curl_headers_push(data, buf, blen, htype);
CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d",
htype, blen, result);
if(result)