chttpserver · Section 3
chttpsvr_resp_set_header
NAME
chttpsvr_resp_set_header - set a response header, replacing every field of that name
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chttpserver.h>
ccol_retval chttpsvr_resp_set_header(chttpsvr_resp *resp,
const char *name,
const char *value);
DESCRIPTION
Sets a response header to one value. If fields with that name already exist, the first one takes the new value and keeps its position and the others are removed, so the response ends up with exactly one field of that name. Names are compared without regard to case, as RFC 7230 requires. To send one more field of a name instead, such as a second Set-Cookie, use chttpsvr_resp_add_header(3). The fields go on the wire in the order of the calls that first set each of them. The server adds no Content-Type of its own. A response carries one only when the handler sets it, with this function or with chttpsvr_resp_write_json(3). The server doesn't guess one from the body as Go's net/http server does, and it has no default type like the default_type of nginx. The stored header name keeps the letter case of the FIRST call, and the letter case of every later call is ignored. HTTP/1.1 header names are case-insensitive on the wire, so this doesn't affect correctness, but a caller that reads the stored header names back directly has to allow for it. None of the three arguments may be NULL. A NULL in any of them gives ccol_invalid_args and changes nothing in the response. name must also be a non-empty token of tchar bytes only, as RFC 7230 SS3.2.6 defines them, so every byte must be an ASCII letter, an ASCII digit or one of "!#$%&'*+-.^_`|~". A name of zero length has no valid form on the wire. A name with any other byte, for example a space or a literal ':', can't inject a CRLF on its own, but it does make a malformed wire line that a strict parser further down the path can misread. value must contain no control byte, which means no byte below 0x20 other than the horizontal tab and no 0x7F (DEL). RFC 9110 section 5.5 allows only visible characters, the space, the tab and bytes from 0x80 to 0xFF in a field value, and a client that follows it, chttpclient included, refuses a response with any other byte. The server writes name and value onto the wire exactly as they are, as "name:value\r\n", with no further escaping. A CR or LF inside value would therefore let whoever controls the data that the caller reflects (a query parameter, a path parameter or an echoed request header put into a response header, for example) inject extra header lines or split the response in two. That attack is called HTTP response splitting, or CRLF injection. The function refuses such a byte with ccol_invalid_args instead of quietly stripping it or cutting the value short. A "Connection" header is accepted and checked like any other name, but the server never sends it. The server always writes its own Connection header, which shows whether it really keeps the connection open afterwards. A "Content-Length" value must be one or more ASCII digits whose number fits in a signed 64-bit integer, and any other value gives ccol_invalid_args. Whether the server sends it depends on the response. A response that sends a body carries the length of the body that chttpsvr_resp_write and its sibling functions really wrote, never the value you set, because that length is the framing on the wire. A 304, and a response to HEAD where the handler wrote no body, send no body whatever the field says. They carry the Content-Length you set, which is the length that a 200 to a GET would carry (RFC 9110 section 8.6), or none if you set none. A 205 always carries "content-length:0", whatever you set or wrote, because it has no content (RFC 9110 section 15.3.6), and a 204 never carries one. Go's net/http server follows the same rule. A "Date" header that you set is sent exactly as given and takes the place of the Date that the server adds otherwise, so the response carries exactly one. If you set none, every final response carries the current time in UTC in the IMF-fixdate form of RFC 9110 section 5.6.7, such as "Sun, 06 Nov 1994 08:49:37 GMT". The interim "100 Continue" carries none. This is also the rule of Go's net/http server. A second call with the same name, in any letter case, through this function or through chttpsvr_resp_add_header(3), replaces the value, so no sequence of calls can produce two Date lines. A name of "Transfer-Encoding", in any letter case, is refused outright instead of being accepted and filtered out later. The server never transfer-codes a response body, since it frames every response with Content-Length, so it can't obey a Transfer-Encoding header that a caller sets. On the wire such a header would also sit next to the Content-Length header that this function computes, over a body that nothing transfer-coded. That framing is ambiguous (RFC 7230 SS3.3.3), and an intermediary that gives Transfer-Encoding priority over Content-Length can misparse it, which is the same kind of hazard that the handling of Connection and Content-Length above prevents. On the client side chttp_request_set_header refuses Transfer-Encoding in the same way.
RETURN VALUE
ccol_success on success. ccol_invalid_args when resp, name or value is NULL; when name is empty or holds a byte outside the RFC 7230 tchar set; when value holds a byte below 0x20 other than the tab, or the byte 0x7F; when name is "Transfer-Encoding" in any letter case; or when name is "Content-Length" and value is not a number as described above. ccol_not_enough_memory when the underlying allocation fails. A failed call changes nothing in the response.
SEE ALSO
chttpsvr_resp_add_header(3), chttpsvr_resp_set_status(3), chttpsvr_resp_write(3)