File tree Expand file tree Collapse file tree 3 files changed +23
-0
lines changed Expand file tree Collapse file tree 3 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -128,6 +128,21 @@ have a newline inside a header value, and ``Content-Length: hello`` is
128
128
an error because `Content-Length ` should always be an integer. We may
129
129
add additional checks in the future.
130
130
131
+ While we make sure to expose header names as lowercased bytes, we also
132
+ preserve the original header casing that is used. Compliant HTTP
133
+ agents should always treat headers in a case insensitive manner, but
134
+ this may not always be the case. When sending bytes over the wire we
135
+ use send headers preserving whatever original header casing is used.
136
+
137
+ It is possible to access the headers in their raw original casing,
138
+ which may be useful for some user output or debugging purposes.
139
+
140
+ .. ipython :: python
141
+
142
+ original_headers = [(" Host" , " example.com" )]
143
+ req = h11.Request(method = " GET" , target = " /" , headers = original_headers)
144
+ req.headers.raw_items()
145
+
131
146
.. _http_version-format :
132
147
133
148
It's not just headers we normalize to being byte-strings: the same
Original file line number Diff line number Diff line change @@ -103,6 +103,13 @@ def __eq__(self, other):
103
103
def __len__ (self ):
104
104
return len (self ._full_items )
105
105
106
+ def __repr__ (self ):
107
+ return "<Headers(%s)>" % repr (list (self ))
108
+
109
+ def __getitem__ (self , idx ):
110
+ _ , name , value = self ._full_items [idx ]
111
+ return (name , value )
112
+
106
113
def raw_items (self ):
107
114
return [(raw_name , value ) for raw_name , _ , value in self ._full_items ]
108
115
Original file line number Diff line number Diff line change @@ -173,6 +173,7 @@ def test_header_casing():
173
173
http_version = "1.1" ,
174
174
)
175
175
assert len (r .headers ) == 2
176
+ assert r .headers [0 ] == (b"host" , b"example.org" )
176
177
assert r .headers == [(b"host" , b"example.org" ), (b"connection" , b"keep-alive" )]
177
178
assert r .headers .raw_items () == [
178
179
(b"Host" , b"example.org" ),
You can’t perform that action at this time.
0 commit comments