Skip to content

HttpResponse

Description

HTTP response under construction. Same allocator-ownership story as HttpRequest.

Usage example (Cross-references)

Usage examples (Cross-references)
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        HttpResponse response = HttpResponseInit(alloc_base);
        Str          body     = StrInit(alloc_base);
        StrWriteFmt(&body, "<h1>hi</h1>");
    // ---------------------------------------------------------------------------
    
    HttpResponse *HttpRespondWithHtml(HttpResponse *response, HttpResponseCode status, const Str *html) {
        if (!response || !response->allocator || !html) {
            LOG_FATAL("invalid arguments");
    
    #if MISRA_HAVE_FILE
    HttpResponse *HttpRespondWithFile(
        HttpResponse    *response,
        HttpResponseCode status,
    #if MISRA_HAVE_FILE
    HttpResponse *HttpRespondWithFile(
        HttpResponse    *response,
        HttpResponseCode status,
        HttpContentType  content_type,
    #endif
    
    Str HttpResponseSerialize(const HttpResponse *response, Allocator *alloc) {
        Str out = StrInit(alloc);
    }
    
    void HttpResponseDeinit(HttpResponse *response) {
        if (!response) {
            LOG_FATAL("invalid arguments");
        HttpHeaders      headers;
        Str              body;
    } HttpResponse;
    
    #define HttpResponseInit(...) MISRA_OVERLOAD(HttpResponseInit, __VA_ARGS__)
    #define HttpResponseInit_0()  HttpResponseInit_1(MisraScope)
    #define HttpResponseInit_1(alloc_ptr)                                                                                  \
        ((HttpResponse) {.allocator    = ALLOCATOR_OF(alloc_ptr),                                                          \
                         .content_type = HTTP_CONTENT_TYPE_INVALID,                                                        \
                         .status_code  = HTTP_RESPONSE_CODE_INVALID,                                                       \
    /// FAILURE : Does not return - aborts on NULL arguments.
    ///
    HttpResponse *HttpRespondWithHtml(HttpResponse *response, HttpResponseCode status, const Str *html);
    
    #if MISRA_HAVE_FILE
    /// FAILURE : Returns NULL on I/O or allocation failure.
    ///
    HttpResponse *HttpRespondWithFile(
        HttpResponse    *response,
        HttpResponseCode status,
    ///
    HttpResponse *HttpRespondWithFile(
        HttpResponse    *response,
        HttpResponseCode status,
        HttpContentType  content_type,
    ///           (unknown response code, unknown content type, etc.).
    ///
    Str HttpResponseSerialize(const HttpResponse *response, Allocator *alloc);
    
    ///
    /// Release storage owned by `response` and zero the struct.
    ///
    void HttpResponseDeinit(HttpResponse *response);
    
    #endif // MISRA_PARSERS_HTTP_H
Last updated on