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

Multiple headers as tuple #69

Open
truekonrads opened this issue Dec 26, 2016 · 0 comments
Open

Multiple headers as tuple #69

truekonrads opened this issue Dec 26, 2016 · 0 comments
Assignees

Comments

@truekonrads
Copy link

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

Repository owner deleted a comment from Ousret Apr 17, 2020
@benoitc benoitc self-assigned this Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants