mco_lews_get_header_value

Prototype

    MCO_RET mco_lews_get_header_value(const char *buffer, mco_uint2 buffer_size,
                                  const char *header_name, char *out_buf, mco_uint2 out_buf_size);

Arguments

buffer
Pointer to the buffer containing raw HTTP headers.
buffer_size
Size (in bytes) of the buffer.
header_name
Null-terminated name of the HTTP header to search for (e.g., "Host").
out_buf
Output buffer where the header value will be stored if found.
out_buf_size
Size (in bytes) of the out_buf buffer.

Description

This function searches the provided HTTP header block for a header matching header_name and copies its value into out_buf, ensuring it is null-terminated.

Header names are matched case-insensitively, as per RFC 7230.

    lews_connection_context_t ctx;
    ...
    /*
    Incoming buffer contains the following request:
    "POST /login?log=usr&noval&pwd=qwe HTTP/1.1\r\n"
    "Host: example.com\r\n"
    "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"
    "Content-Length: 26\r\n\r\n"
    "login=user&password=qwerty&novalue&num=123";
    */
    ...
    rc = mco_lews_get_header_value((char*)(&ctx.incoming_buf) + ctx.header_pos,
                                ctx.header_len, "Host", tmp_buf, sizeof(tmp_buf));
    ASSERT_EQ(MCO_S_OK, rc);
    ASSERT_STREQ("example.com", tmp_buf);

    rc = mco_lews_get_header_value((char*)(&ctx.incoming_buf) + ctx.header_pos,
                                ctx.header_len, "Content-Length", tmp_buf, sizeof(tmp_buf));
    ASSERT_EQ(MCO_S_OK, rc);
    ASSERT_STREQ("26", tmp_buf);

    rc = mco_lews_get_header_value((char*)(&ctx.incoming_buf) + ctx.header_pos,
                                ctx.header_len, "SOMETHING", tmp_buf, sizeof(tmp_buf));
    ASSERT_EQ(MCO_E_LEWS_NONEXISTENT_PARAMETER, rc);

Return Codes

MCO_S_OK
The header was found, and its value was successfully copied to out_buf.
MCO_E_LEWS_NONEXISTENT_PARAMETER
The specified header name was not found in the buffer.
MCO_E_LEWS_NO_ENOUGH_MEMORY
The out_buf is too small to hold the header value (including the null terminator).

Files

Header file:
mcoewslite.h
Source file:
mcoewslite.c
Library:
mcoewslite.a