Skip to content
HttpResponseSerialize

HttpResponseSerialize

Description

Serialize response to its on-wire HTTP/1.1 form. Caller owns the returned Str and must deinit it. The result is exactly the bytes that should land on the transport (sockets / files / etc.) — no transport call is made.

Success

Returns a populated Str.

Failure

Returns an empty Str and logs the failing condition (unknown response code, unknown content type, etc.).

Usage example (Cross-references)

Usage examples (Cross-references)
        StrDeinit(&body);
    
        Str wire = HttpResponseSerialize(&response, alloc_base);
    
        // Spot-check the wire format:
    #endif
    
    Str HttpResponseSerialize(const HttpResponse *response, Allocator *alloc) {
        Str out = StrInit(alloc);
    
        if (!response) {
            LOG_ERROR("HttpResponseSerialize: response is NULL");
            return out;
        }
        const char *response_code = HttpResponseCodeToZstr(response->status_code);
        if (!response_code) {
            LOG_ERROR("HttpResponseSerialize: invalid/unknown response code {}", (u32)response->status_code);
            return out;
        }
        const char *content_type = HttpContentTypeToZstr(response->content_type);
        if (!content_type) {
            LOG_ERROR("HttpResponseSerialize: invalid/unknown content type {}", (u32)response->content_type);
            return out;
        }
            u64 head = out.length;
            if (!StrReserve(&out, head + response->body.length + 1)) {
                LOG_ERROR("HttpResponseSerialize: failed to reserve buffer");
                StrDeinit(&out);
                return StrInit(alloc);
Last updated on