Skip to content

[#1804] Fix split CRLF reading in http_read_chunked_body - #1726

Open
hardikkaurani wants to merge 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/http-chunked-split-crlf
Open

hardikkaurani wants to merge 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/http-chunked-split-crlf

Conversation

@hardikkaurani

@hardikkaurani hardikkaurani commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Issue - #1804

http_read_chunked_body() in src/libpgmoneta/http.c had two bugs when reading the 2-byte trailing \r\n after both the terminating 0-size chunk and each data chunk:

  1. Prefetched 1-byte boundary discarded: When prefetched_size - prefetched_pos == 1 (e.g., header prefetch ended on \r), the remaining_prefetched >= 2 check fell into http_read_bytes(..., 2) without consuming that prefetched byte or advancing prefetched_pos, desynchronizing the chunk stream or failing with bytes_read != 2.
  2. Split socket reads: http_read_bytes(..., 2) performs a single read()/SSL_read(), so \r and \n arriving in separate reads failed with bytes_read != 2.

Fixes:

  • Extract http_read_chunked_trailing() to read each of the 2 trailing bytes from prefetched first (when available) and fall back to single-byte http_read_bytes(..., 1) reads from the socket/TLS stream, and use it in both places in http_read_chunked_body().
  • Add test_pgmoneta_http_get_chunked_split_crlf in test/testcases/test_http.c covering split trailing \r\n on both data chunks and terminating 0-size chunks.

Verification

  • Formatted all modified files with clang-format and verified git diff --check is clean.
  • Full C build and pgmoneta-test suite will run in GitHub Actions CI.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 06:52
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @jesperpedersen,

I opened this PR to fix chunked trailing \r\n reading in http_read_chunked_body() (src/libpgmoneta/http.c):

  • When prefetched_size - prefetched_pos == 1, both trailing \r\n reads discarded the remaining prefetched byte and read 2 bytes directly from the socket, and http_read_bytes(..., 2) could also return 1 byte if \r and \n arrived across separate reads.
  • Added http_read_chunked_trailing() so each byte is consumed from prefetched first and otherwise read 1 byte at a time via http_read_bytes(), used for both data chunks and the terminating 0-size chunk.
  • Added test_pgmoneta_http_get_chunked_split_crlf in test/testcases/test_http.c, formatted with clang-format.

Please let me know if any adjustments are needed.

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.

@ecleon26

Copy link
Copy Markdown
Collaborator

LGTM

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing, @ecleon26! Glad http_read_chunked_trailing() and test_pgmoneta_http_get_chunked_split_crlf look good to you. Let me know if anything else is needed before merge.

@ecleon26
ecleon26 self-requested a review November 28, 2026 19:39

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fix looks right, but the test only covers the prefetched \r case. When nothing is prefetched and \r / \n come in separate reads, it isn't reached: putting back http_read_bytes(..., 2) for that path still passes. A third send covers it:

"...5\r\nHello", "\r", "\n0\r\n\r\n"

Also (from reading, not run): if pgmoneta_http_create() fails, pthread_join() waits forever on the thread in accept().

pgexporter has the same code, I'll open an issue there.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Thanks for the review! I've updated the PR, squashed into a single commit, and rebased onto main:

  • Extended test_pgmoneta_http_get_chunked_split_crlf in test/testcases/test_http.c with a third send() (part3) to cover both the data chunk and terminating 0-size chunk trailing \r\n cases when nothing is prefetched and \r / \n arrive across separate recv() calls, alongside the prefetched \r boundary cases.
  • Updated run_split_chunk_case() / split_chunk_server_thread() to create the request before spawning the server thread and use select() with an atomic_bool running flag so pthread_join() never blocks indefinitely if pgmoneta_http_create() fails.
  • Formatted with clang-format.

@ecleon26

ecleon26 commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

@hardikkaurani Please, rebase

@ecleon26

ecleon26 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

@hardikkaurani please create an issue for this

@hardikkaurani
hardikkaurani force-pushed the fix/http-chunked-split-crlf branch from 9edbdb2 to 4eeef70 Compare December 2, 2026 21:44
@hardikkaurani hardikkaurani changed the title Fix split CRLF reading in http_read_chunked_body [#1804] Fix split CRLF reading in http_read_chunked_body Dec 2, 2026
@hardikkaurani

hardikkaurani commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@ecleon26 Opened issue #1804, updated the PR title/commit (4eeef70), and rebased onto main.

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.

4 participants