Skip to content

Multiple headers as tuple #69

@truekonrads

Description

@truekonrads

Hi,

This little patch introduces a new optional behavior wherby values of multiple headers are returned as a tuple rather than as a comma delimited string. The comma delimited string really doesn't work for cookies where comma is part of the cookie definition.

From f30d048134717d657ab519771a28588e1c3aafcc Mon Sep 17 00:00:00 2001
Date: Mon, 26 Dec 2016 18:57:36 +0000
Subject: [PATCH] New feature: if multiple headers are found, report them as  tuple rather than comma joined string

---
 http_parser/parser.pyx  | 21 +++++++++++++++++++--
 http_parser/pyparser.py | 13 ++++++++++---
 2 files changed, 29 insertions(+), 5 deletions(-)
 mode change 100644 => 100755 http_parser/parser.pyx
 mode change 100644 => 100755 http_parser/pyparser.py

diff --git a/http_parser/parser.pyx b/http_parser/parser.pyx
old mode 100644
new mode 100755
index 2ca92d7..94c7ba4
--- a/http_parser/parser.pyx
+++ b/http_parser/parser.pyx
@@ -97,9 +97,24 @@ cdef int on_header_value_cb(http_parser *parser, char *at,
     if res._last_field in res.headers:
         hval = res.headers[res._last_field]
         if not res._last_was_value:
-            header_value = "%s, %s" % (hval, header_value)
+            if res.multiple_headers_as_tuple:
+                if type(hval)!=tuple:
+                    tmp=(hval,)
+                header_value=tmp+(header_value,)
+            else:
+                header_value = "%s, %s" % (hval, header_value)
         else:
             header_value = "%s %s" % (hval, header_value)
+    
+    #if name in self._headers:
+    #            if self.multiple_headers_as_tuple:
+    #                if type(self._headers[name])!=tuple:
+    #                    value=(self._headers[name])
+    #                value+=(value,)
+    #            else:
+    #                value = "%s, %s" % (self._headers[name], value)
+
+
 
     # add to headers
     res.headers[res._last_field] = header_value
@@ -170,12 +185,14 @@ def get_errno_description(errno):
 
 class _ParserData(object):
 
-    def __init__(self, decompress=False, header_only=False):
+    def __init__(self, decompress=False, header_only=False,multiple_headers_as_tuple=False):
         self.url = ""
         self.body = []
         self.headers = IOrderedDict()
         self.header_only = header_only
 
+        self.multiple_headers_as_tuple=multiple_headers_as_tuple
+
         self.decompress = decompress
         self.decompressobj = None
         self._decompress_first_try = True
diff --git a/http_parser/pyparser.py b/http_parser/pyparser.py
old mode 100644
new mode 100755
index 835ca34..93e9457
--- a/http_parser/pyparser.py
+++ b/http_parser/pyparser.py
@@ -39,9 +39,10 @@ class InvalidChunkSize(Exception):
 
 class HttpParser(object):
 
-    def __init__(self, kind=2, decompress=False):
+    def __init__(self, kind=2, decompress=False,multiple_headers_as_tuple=False):
         self.kind = kind
         self.decompress = decompress
+        self.multiple_headers_as_tuple=multiple_headers_as_tuple
 
         # errors vars
         self.errno = None
@@ -355,9 +356,15 @@ class HttpParser(object):
                 value.append(curr)
             value = ''.join(value).rstrip()
 
-            # multiple headers
+
+
             if name in self._headers:
-                value = "%s, %s" % (self._headers[name], value)
+                if self.multiple_headers_as_tuple:
+                    if type(self._headers[name])!=tuple:
+                        tmp=(self._headers[name],)
+                    value=tmp+(value,)
+                else:
+                    value = "%s, %s" % (self._headers[name], value)
 
             # store new header value
             self._headers[name] = value
-- 
2.7.4

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions