Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPClient - Fix case sensitiveness for header keys #8630

Merged

Conversation

lucasssvaz
Copy link
Collaborator

@lucasssvaz lucasssvaz commented Sep 13, 2023

Description of Change

According to the RFC, header keys for HTTP/1.x should be treated as case-insensitive.

String HTTPClient::header(const char* name) and bool HTTPClient::hasHeader(const char* name) will, incorrectly, perform case-sensitive comparisons.

This PR makes HTTPClient comparisons for header keys case-insensitive.

Tests scenarios

Tested using the following sketch on ESP32:

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

void setup() {

    USE_SERIAL.begin(115200);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    wifiMulti.addAP("myssid", "mypassword");

}

void loop() {
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;
        const char *headerKeys[] = {"Last-Modified"};
        const size_t headerKeysCount = sizeof(headerKeys) / sizeof(headerKeys[0]);

        USE_SERIAL.print("[HTTP] begin...\n");
        http.begin("http://example.com/index.html"); //HTTP

        USE_SERIAL.print("[HTTP] GET...\n");
        
        http.collectHeaders(headerKeys, headerKeysCount);

        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                USE_SERIAL.println();
                USE_SERIAL.print("Header Count: ");
                USE_SERIAL.println(http.headers());
                for (int i = 0; i < http.headers(); ++i) {
                    USE_SERIAL.print(http.headerName(i));
                    USE_SERIAL.print(": ");
                    USE_SERIAL.println(http.header(i));
                }
                USE_SERIAL.println();
                USE_SERIAL.print("last-modified value: ");
                USE_SERIAL.println(http.header("last-modified"));
                USE_SERIAL.print("Header has last-modified: ");
                USE_SERIAL.println(http.hasHeader("last-modified"));
                USE_SERIAL.println();
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(60000);
}

Output

[SETUP] WAIT 4...
[SETUP] WAIT 3...
[SETUP] WAIT 2...
[SETUP] WAIT 1...
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200

Header Count: 1
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT

last-modified value: Thu, 17 Oct 2019 07:18:26 GMT
Header has last-modified: 1

Related links

Closes #8524

@lucasssvaz lucasssvaz self-assigned this Sep 13, 2023
@me-no-dev me-no-dev added this to the 3.0.0 milestone Sep 14, 2023
@me-no-dev
Copy link
Member

@lucasssvaz please do the same for release/v2.x so we can have it in 2.0.13

Copy link
Collaborator

@SuGlider SuGlider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lucasssvaz!

@lucasssvaz lucasssvaz mentioned this pull request Sep 18, 2023
4 tasks
@P-R-O-C-H-Y P-R-O-C-H-Y added the Status: Pending Merge Pull Request is ready to be merged label Oct 3, 2023
@me-no-dev me-no-dev merged commit b5ceffb into espressif:esp-idf-v5.1-libs Oct 5, 2023
37 checks passed
@lucasssvaz lucasssvaz deleted the bugfix/http_header branch March 19, 2024 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Pending Merge Pull Request is ready to be merged
Projects
Development

Successfully merging this pull request may close these issues.

4 participants