Writer#
A bounded append buffer that keeps counting after it runs out of room, so the same code can measure a message and then write it into a buffer of exactly the right size.
lwhttp_buffer_t b;
lwhttp_buffer_init(&b, NULL, 0);
lwhttp_write_header(&b, "Accept", "*/*");
lwhttp_write_end(&b);
char *out = malloc(b.len);
lwhttp_buffer_init(&b, out, b.len);
lwhttp_write_header(&b, "Accept", "*/*");
lwhttp_write_end(&b);
-
struct lwhttp_buffer_t#
Append-only output buffer that counts overflow instead of failing.
Appends that do not fit are not written but still advance
lwhttp_buffer_t.len, so a pass with aNULL/0-sized buffer measures the size needed, andlwhttp_buffer_ok()tells whether everything fit. The buffer is never NUL-terminated.-
char *data#
Output storage; may be
NULLwhenlwhttp_buffer_t.sizeis 0.
-
size_t size#
Capacity of
lwhttp_buffer_t.datain bytes.
-
size_t len#
Total bytes appended so far, including any that did not fit.
-
char *data#
-
void lwhttp_buffer_init(lwhttp_buffer_t *buffer, char *data, size_t size)#
Initialise an empty output buffer.
- Parameters:
buffer – Buffer to initialise.
data – Output storage, or
NULLwithsize0 to only measure.size – Capacity of
datain bytes.
-
void lwhttp_buffer_append(lwhttp_buffer_t *buffer, const void *data, size_t len)#
Append bytes. If they do not fit nothing is written, but the length still grows.
- Parameters:
buffer – The buffer.
data – Bytes to append.
len – Number of bytes.
-
void lwhttp_buffer_append_str(lwhttp_buffer_t *buffer, const char *s)#
Append a NUL-terminated string without its terminator.
- Parameters:
buffer – The buffer.
s – String to append; must not be
NULL.
-
void lwhttp_buffer_append_u32(lwhttp_buffer_t *buffer, uint32_t value)#
Append an unsigned decimal number without leading zeros.
- Parameters:
buffer – The buffer.
value – Number to append.
-
bool lwhttp_buffer_ok(const lwhttp_buffer_t *buffer)#
Check whether everything appended so far fit.
- Parameters:
buffer – The buffer.
- Returns:
true if
lwhttp_buffer_t.lendoes not exceedlwhttp_buffer_t.size.
-
bool lwhttp_write_header(lwhttp_buffer_t *buffer, const char *name, const char *value)#
Append a validated
Name: valueline followed by CRLF.Validation protects against header injection; capacity is not checked here, use
lwhttp_buffer_ok().- Parameters:
buffer – The buffer.
name – Field name; must be a token (see
lwhttp_is_token()).value – Field value; must not contain control characters other than horizontal tab.
- Returns:
true if the field was appended; false, with nothing appended, if
nameorvalueis invalid orNULL.
-
void lwhttp_write_end(lwhttp_buffer_t *buffer)#
Append the empty line (CRLF) that ends a header section.
- Parameters:
buffer – The buffer.