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

Trailing headers not handled #1486

Closed
sjoubert opened this issue Feb 6, 2023 · 1 comment
Closed

Trailing headers not handled #1486

sjoubert opened this issue Feb 6, 2023 · 1 comment
Labels

Comments

@sjoubert
Copy link

sjoubert commented Feb 6, 2023

I use the library (as a client) against an API that makes use of Trailing headers in some of its chunked responses. The current code does not handle that case and I get a Read error instead.

I have tried to minimally modify the chunked reader code to validate my assumption. Using the following patch I no longer get a Read error and I can use the actual response correctly.

diff --git a/httplib.h b/httplib.h
index 7431dea..4401be4 100644
--- a/httplib.h
+++ b/httplib.h
@@ -3507,9 +3507,11 @@ inline bool read_content_chunked(Stream &strm,
   }
 
   if (chunk_len == 0) {
-    // Reader terminator after chunks
-    if (!line_reader.getline() || strcmp(line_reader.ptr(), "\r\n"))
-      return false;
+    // Read trailing headers until terminator
+    while(!line_reader.getline())
+    {
+      if (strcmp(line_reader.ptr(), "\r\n")) { break; }
+    }
   }
 
   return true;

This is a very crude approach that just ignores any trailing header. One might want to validate the trailing header names against the provided Trailer header value and then put those trailing headers in the response for potential use.

Is there any chance to get some form of fix for trailing headers in the library? I might have some time to do a PR if I get some advice on what needs to be done (just ignore trailing headers, add them to the response, validate them,...?)

@yhirose yhirose added the bug label Feb 7, 2023
@yhirose
Copy link
Owner

yhirose commented Feb 7, 2023

@sjoubert, thanks for the report. I haven't taken care of this situation, and I'll take a look at it when I have time. Thanks!

ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this issue May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants