Skip to content

HttpHeader

Description

A single Key: Value HTTP header. key and value are both Str objects that own their backing storage through their stored allocator. When kept inside a Vec(HttpHeader), the deep-copy callbacks installed via HttpHeaderInit handle duplication and cleanup automatically.

Usage example (Cross-references)

Usage examples (Cross-references)
        Str key;
        Str value;
    } HttpHeader;
    
    ///
    #define HttpHeaderInit(...)         OVERLOAD(HttpHeaderInit, __VA_ARGS__)
    #define HttpHeaderInit_0()          HttpHeaderInit_1(MisraScope)
    #define HttpHeaderInit_1(alloc_ptr) ((HttpHeader) {.key = StrInit_1(alloc_ptr), .value = StrInit_1(alloc_ptr)})
    
    typedef Vec(HttpHeader) HttpHeaders;
    #define HttpHeaderInit_1(alloc_ptr) ((HttpHeader) {.key = StrInit_1(alloc_ptr), .value = StrInit_1(alloc_ptr)})
    
    typedef Vec(HttpHeader) HttpHeaders;
    
    ///
    /// TAGS: Http, Deinit, Header, Init
    ///
    void HttpHeaderDeinit(HttpHeader *header);
    
    ///
    /// TAGS: Http, Find, Header
    ///
    HttpHeader *http_headers_find_zstr(HttpHeaders *headers, Zstr key);
    HttpHeader *http_headers_find_str(HttpHeaders *headers, const Str *key);
    #define HttpHeadersFind(headers, key)                                                                                  \
    ///
    HttpHeader *http_headers_find_zstr(HttpHeaders *headers, Zstr key);
    HttpHeader *http_headers_find_str(HttpHeaders *headers, const Str *key);
    #define HttpHeadersFind(headers, key)                                                                                  \
        _Generic((key), Str *: http_headers_find_str, Zstr: http_headers_find_zstr, char *: http_headers_find_zstr)(       \
    // ---------------------------------------------------------------------------
    
    void HttpHeaderDeinit(HttpHeader *header) {
        if (!header) {
            LOG_FATAL("invalid arguments");
    void http_header_deinit(void *header_ptr, const Allocator *alloc) {
        (void)alloc; // each Str carries its own allocator handle
        HttpHeader *header = (HttpHeader *)header_ptr;
        StrDeinit(&header->key);
        StrDeinit(&header->value);
            LOG_FATAL("invalid arguments");
        }
        HttpHeader       *dst = (HttpHeader *)dst_ptr;
        const HttpHeader *src = (const HttpHeader *)src_ptr;
        }
        HttpHeader       *dst = (HttpHeader *)dst_ptr;
        const HttpHeader *src = (const HttpHeader *)src_ptr;
    
        dst->key   = StrInit((Allocator *)alloc);
    }
    
    HttpHeader *http_headers_find_zstr(HttpHeaders *headers, Zstr key) {
        if (!headers || !key) {
            LOG_FATAL("invalid arguments");
    }
    
    HttpHeader *http_headers_find_str(HttpHeaders *headers, const Str *key) {
        if (!headers || !key) {
            LOG_FATAL("invalid arguments");
            }
    
            HttpHeader hh = HttpHeaderInit(alloc);
            StrReadFmt(cursor, "{}: {}\r\n", hh.key, hh.value);
            if (cursor == line_start) {
        HttpHeaders headers = VecInitWithDeepCopy(http_header_init_copy, http_header_deinit, adbg);
    
        HttpHeader hh = HttpHeaderInit(adbg);
        StrAppendFmt(&hh.key, "X-A-Reasonably-Long-Header-Key-Name");
        StrAppendFmt(&hh.value, "a-reasonably-long-header-value-string");
                  (ZstrCompare(next, "body-bytes") == 0);
    
        HttpHeader *host = HttpHeadersFind(&req.headers, "Host");
        ok               = ok && host && ZstrCompare(StrBegin(&host->value), "example.com") == 0;
Last updated on