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)
    
        if (!response) {
            LOG_FATAL("HttpResponseSerialize: response is NULL");
        }
        Zstr response_code = HttpResponseCodeToZstr(response->status_code);
        if (!response_code) {
            LOG_ERROR("HttpResponseSerialize: invalid/unknown response code {}", (u32)response->status_code);
            return out;
        }
        Zstr content_type = HttpContentTypeToZstr(response->content_type);
        if (!content_type) {
            LOG_ERROR("HttpResponseSerialize: invalid/unknown content type {}", (u32)response->content_type);
            return out;
        }
        if (StrLen(&response->body)) {
            if (!StrPushBackMany(&out, StrBegin(&response->body), StrLen(&response->body))) {
                LOG_ERROR("HttpResponseSerialize: failed to append body");
                StrDeinit(&out);
                return StrInit(alloc);
        StrDeinit(&body);
    
        Str wire = HttpResponseSerialize(&response, alloc_base);
    
        // Spot-check the wire format:
Last updated on