Skip to content

[#693] Make HTTP header retrieval and framing case-insensitive - #696

Open
hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix-http-header-case-sensitivity-693
Open

hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix-http-header-case-sensitivity-693

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Fixes #693

Problem

In src/libpgexporter/http.c, HTTP response body framing and header retrieval relied on exact case-sensitive string matching (strcmp) through pgexporter_deque_get().

Per RFC 9110 Section 5.1, HTTP header field names are case-insensitive. When communicating with HTTP endpoints, CDNs, or reverse proxies emitting lowercase headers (such as transfer-encoding: chunked or content-length: <N>):

  1. pgexporter_deque_get(http_response->payload.headers, "Transfer-Encoding") returned NULL.
  2. pgexporter_deque_get(http_response->payload.headers, "Content-Length") returned NULL.
  3. http_read_response_body() fell through to http_read_EOF_body(), causing hangs/timeouts on keep-alive connections or storing unparsed chunk length prefixes and \r\n boundaries directly into the payload body.
  4. Callers invoking pgexporter_http_get_response_header() or pgexporter_http_request_get_header() with different casing (e.g. content-type vs Content-Type) failed to find the header.

Solution

  1. In src/libpgexporter/http.c, implemented http_get_header() and http_remove_header() helper functions that iterate over headers and perform case-insensitive comparison using strcasecmp().
  2. Implemented contains_ignore_case() using strncasecmp() to reliably detect chunked transfer encoding irrespective of header value casing without relying on non-standard extensions.
  3. Updated pgexporter_http_request_get_header(), pgexporter_http_request_update_header(), pgexporter_http_request_remove_header(), and pgexporter_http_get_response_header() to use case-insensitive matching.
  4. Updated http_read_response_body() to look up Transfer-Encoding and Content-Length headers case-insensitively.
  5. In test/testcases/test_http.c, added regression unit tests:
    • test_http_headers_case_insensitivity: verifies case-insensitive request header retrieval, update, and removal across exact, uppercase, lowercase, and mixed cases.
    • test_http_response_headers_case_insensitivity: verifies chunked response parsing and header retrieval against an echo server returning lowercase headers (transfer-encoding: chunked, content-type: application/json).
    • test_http_response_content_length_case_insensitivity: verifies content-length response parsing and header retrieval against an echo server returning lowercase headers (content-length: 13).

Copilot AI balanced review requested due to automatic review settings November 22, 2026 20:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @jesperpedersen,

I have submitted this PR to address #693.

Summary of Changes:

  • Implemented http_get_header() and http_remove_header() in src/libpgexporter/http.c to look up and remove headers case-insensitively using strcasecmp(), adhering to RFC 9110 §5.1.
  • Updated http_read_response_body() so that framing headers (Transfer-Encoding and Content-Length) are matched regardless of header casing from upstream proxies or servers.
  • Added contains_ignore_case() via strncasecmp() for case-insensitive matching of chunked transfer coding.
  • Updated pgexporter_http_request_get_header(), pgexporter_http_request_update_header(), pgexporter_http_request_remove_header(), and pgexporter_http_get_response_header().
  • Added regression tests in test/testcases/test_http.c covering request and response header case-insensitivity as well as chunked and content-length framing against lowercase server headers.

All code has been formatted with clang-format according to project standards. Please let me know if any adjustments are needed!

@Pranav-error

Copy link
Copy Markdown
Collaborator

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

@hardikkaurani
hardikkaurani force-pushed the fix-http-header-case-sensitivity-693 branch from 74c336a to 5153b4b Compare December 2, 2026 21:32
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Rebased onto main (5153b4b) to pick up #692.

Per RFC 9110 Section 5.1, HTTP header field names are case-insensitive. Previously, pgexporter_http_request_get_header(), pgexporter_http_get_response_header(), and http_read_response_body() delegated directly to pgexporter_deque_get() using exact case strcmp().

When communicating with endpoints, CDNs, or reverse proxies emitting lowercase headers (such as transfer-encoding: chunked or content-length: <N>), the parser failed to detect the transfer coding or content length and fell through to http_read_EOF_body(), leading to timeouts on keep-alive connections or corrupted body framing for chunked payloads.

This commit introduces case-insensitive header lookup and removal helpers in src/libpgexporter/http.c using strcasecmp(), updates response body framing checks for Transfer-Encoding and Content-Length, and adds regression tests in test/testcases/test_http.c.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP response header lookups are case-sensitive, corrupting chunked responses and skipping Content-Length framing

3 participants