Skip to content

HttpResponse

Description

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

Usage example (Cross-references)

Usage examples (Cross-references)
        HttpHeaders      headers;
        Str              body;
    } HttpResponse;
    
    ///
    #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,                                                       \
    /// TAGS: Http, Respond, Html
    ///
    HttpResponse *HttpRespondWithHtml(HttpResponse *response, HttpResponseCode status, const Str *html);
    
    #if FEATURE_FILE
    /// TAGS: Http, Response, Deinit, Init
    ///
    void HttpResponseDeinit(HttpResponse *response);
    
    #endif // MISRA_PARSERS_HTTP_H
    // ---------------------------------------------------------------------------
    
    HttpResponse *HttpRespondWithHtml(HttpResponse *response, HttpResponseCode status, const Str *html) {
        if (!response || !response->allocator || !html) {
            LOG_FATAL("invalid arguments");
    
    #if FEATURE_FILE
    HttpResponse *http_respond_with_file_zstr(
        HttpResponse    *response,
        HttpResponseCode status,
    #if FEATURE_FILE
    HttpResponse *http_respond_with_file_zstr(
        HttpResponse    *response,
        HttpResponseCode status,
        HttpContentType  content_type,
    }
    
    HttpResponse *http_respond_with_file_str(
        HttpResponse    *response,
        HttpResponseCode status,
    
    HttpResponse *http_respond_with_file_str(
        HttpResponse    *response,
        HttpResponseCode status,
        HttpContentType  content_type,
    #endif
    
    Str http_response_serialize(const HttpResponse *response, Allocator *alloc) {
        Str out = StrInit(alloc);
    }
    
    void HttpResponseDeinit(HttpResponse *response) {
        if (!response) {
            LOG_FATAL("invalid arguments");
        StrAppendFmt(&first, "<h1>first-body-long-enough-to-heap-allocate</h1>");
    
        HttpResponse response = HttpResponseInit(adbg);
        HttpRespondWithHtml(&response, HTTP_RESPONSE_CODE_OK, &first);
        Str first = StrInit(adbg);
        StrAppendFmt(&first, "<h1>old-body-long-enough-to-force-heap-here</h1>");
        HttpResponse response = HttpResponseInit(adbg);
        HttpRespondWithHtml(&response, HTTP_RESPONSE_CODE_OK, &first);
        StrDeinit(&first);
    
        // Serving the file must free the old (HTML) body before installing the new.
        HttpResponse *res =
            HttpRespondWithFile(&response, HTTP_RESPONSE_CODE_OK, HTTP_CONTENT_TYPE_TEXT_PLAIN, StrBegin(&path));
        ok = ok && (res != NULL);
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        HttpResponse response = HttpResponseInit(alloc_base);
        Str          body     = StrInit(alloc_base);
        StrAppendFmt(&body, "<h1>hi</h1>");
        FileClose(&f);
    
        HttpResponse  response = HttpResponseInit(alloc_base);
        HttpResponse *res =
            HttpRespondWithFile(&response, HTTP_RESPONSE_CODE_OK, HTTP_CONTENT_TYPE_TEXT_PLAIN, StrBegin(&path));
    
        HttpResponse  response = HttpResponseInit(alloc_base);
        HttpResponse *res =
            HttpRespondWithFile(&response, HTTP_RESPONSE_CODE_OK, HTTP_CONTENT_TYPE_TEXT_PLAIN, StrBegin(&path));
        FileClose(&f); // leave it empty
    
        HttpResponse  response = HttpResponseInit(alloc_base);
        HttpResponse *res =
            HttpRespondWithFile(&response, HTTP_RESPONSE_CODE_OK, HTTP_CONTENT_TYPE_TEXT_PLAIN, StrBegin(&path));
    
        HttpResponse  response = HttpResponseInit(alloc_base);
        HttpResponse *res =
            HttpRespondWithFile(&response, HTTP_RESPONSE_CODE_OK, HTTP_CONTENT_TYPE_TEXT_PLAIN, StrBegin(&path));
        Allocator       *alloc_base = ALLOCATOR_OF(&alloc);
    
        HttpResponse  response = HttpResponseInit(alloc_base);
        HttpResponse *res      = HttpRespondWithFile(
            &response,
    
        HttpResponse  response = HttpResponseInit(alloc_base);
        HttpResponse *res      = HttpRespondWithFile(
            &response,
            HTTP_RESPONSE_CODE_OK,
Last updated on