cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

Trailing headers not handled

Open sjoubert opened this issue 2 years ago • 0 comments

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,...?)

sjoubert avatar Feb 06 '23 14:02 sjoubert