Does HTTPD offer anyway to see the RAW header?

Ravenholem
Posts: 20
Joined: Wed Nov 10, 2021 7:13 pm

Does HTTPD offer anyway to see the RAW header?

Postby Ravenholem » Thu Jun 06, 2024 10:10 pm

Hello,
So I'm porting over my janky little HTTP server library I home brewed. It fully functions with IOS captive portal and when I try to port that code to the HTTPD server it does not function exactly as it should. It does about 95% like it should but then just keeps reloading the page over and over.
I need some way to see what HTTP header the device is sending to the ESP32 so I can diag. I looked through the API and can not find anything that lets me print out the RAW http header. In my home brew code I could print out the complete header or contents depending on what I wanted. I see there is code for getting the contents and putting them into a buffer to read. Though I see nothing that does this for the Header.

Thanks all!

Ravenholem
Posts: 20
Joined: Wed Nov 10, 2021 7:13 pm

Re: Does HTTPD offer anyway to see the RAW header?

Postby Ravenholem » Fri Jun 07, 2024 5:38 pm

I found a solution though, I think it is very weird you can not access this information directly through the API

I copied this structure from esp_httpd_priv.h which I could not figure out how to included in my main.c.

Code: Select all

struct httpd_req_aux {
    struct sock_db *sd;                             /*!< Pointer to socket database */
    char            scratch[1024 + 1]; /*!< Temporary buffer for our operations (1 byte extra for null termination) */
    size_t          remaining_len;                  /*!< Amount of data remaining to be fetched */
    char           *status;                         /*!< HTTP response's status code */
    char           *content_type;                   /*!< HTTP response's content type */
    bool            first_chunk_sent;               /*!< Used to indicate if first chunk sent */
    unsigned        req_hdrs_count;                 /*!< Count of total headers in request packet */
    unsigned        resp_hdrs_count;                /*!< Count of additional headers in response packet */
    struct resp_hdr {
        const char *field;
        const char *value;
    } *resp_hdrs;                                   /*!< Additional headers in response packet */
    struct http_parser_url url_parse_res;           /*!< URL parsing result, used for retrieving URL elements */
#ifdef CONFIG_HTTPD_WS_SUPPORT
    bool ws_handshake_detect;                       /*!< WebSocket handshake detection flag */
    httpd_ws_type_t ws_type;                        /*!< WebSocket frame type */
    bool ws_final;                                  /*!< WebSocket FIN bit (final frame or not) */
    uint8_t mask_key[4];                            /*!< WebSocket mask key for this payload */
#endif
};
I then created this function. I call from any URI callback or http error handler

Code: Select all

esp_err_t httpd_req_print_header(httpd_req_t *r){
    struct httpd_req_aux *ra = r->aux;
    const char *hdr_ptr = ra->scratch;
    const char *val_ptr = NULL;

    printf("Header Count: %u\n", ra->req_hdrs_count);
    
    puts("---HEADER---");

    uint16_t count = ra->req_hdrs_count;

    while (count--)
    {
        if(count){
            val_ptr = hdr_ptr;
            hdr_ptr = 1 + strchr(hdr_ptr, '\0');
            while (*hdr_ptr == '\0') {
                hdr_ptr++;
            }
            printf("%s\n", val_ptr);
        }
    }
    
    puts("---END HEADER---");
    return ESP_OK;
}

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 134 guests