Multiple headers as tuple

未關閉
#69 0 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

@benoitc 已經在處理了。

開始於 2020年4月17日。

評估

這個 Issue 還沒有評估資料。

描述

improvement

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

主要語言
C
星號
345
分支
93
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

benoitc/http-parser 的其他 Issue

查看 benoitc/http-parser 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。