77"""
88import collections
99
10- from hyper .compat import unicode , bytes , imap
10+ from hyper .common . util import to_bytestring , to_bytestring_tuple
1111
1212
1313class HTTPHeaderMap (collections .MutableMapping ):
@@ -64,18 +64,18 @@ def __init__(self, *args, **kwargs):
6464 self ._items = []
6565
6666 for arg in args :
67- self ._items .extend (map (lambda x : _to_bytestring_tuple (* x ), arg ))
67+ self ._items .extend (map (lambda x : to_bytestring_tuple (* x ), arg ))
6868
6969 for k , v in kwargs .items ():
70- self ._items .append (_to_bytestring_tuple (k , v ))
70+ self ._items .append (to_bytestring_tuple (k , v ))
7171
7272 def __getitem__ (self , key ):
7373 """
7474 Unlike the dict __getitem__, this returns a list of items in the order
7575 they were added. These items are returned in 'canonical form', meaning
7676 that comma-separated values are split into multiple values.
7777 """
78- key = _to_bytestring (key )
78+ key = to_bytestring (key )
7979 values = []
8080
8181 for k , v in self ._items :
@@ -91,15 +91,15 @@ def __setitem__(self, key, value):
9191 """
9292 Unlike the dict __setitem__, this appends to the list of items.
9393 """
94- self ._items .append (_to_bytestring_tuple (key , value ))
94+ self ._items .append (to_bytestring_tuple (key , value ))
9595
9696 def __delitem__ (self , key ):
9797 """
9898 Sadly, __delitem__ is kind of stupid here, but the best we can do is
9999 delete all headers with a given key. To correctly achieve the 'KeyError
100100 on missing key' logic from dictionaries, we need to do this slowly.
101101 """
102- key = _to_bytestring (key )
102+ key = to_bytestring (key )
103103 indices = []
104104 for (i , (k , v )) in enumerate (self ._items ):
105105 if _keys_equal (k , key ):
@@ -135,7 +135,7 @@ def __contains__(self, key):
135135 """
136136 If any header is present with this key, returns True.
137137 """
138- key = _to_bytestring (key )
138+ key = to_bytestring (key )
139139 return any (_keys_equal (key , k ) for k , _ in self ._items )
140140
141141 def keys (self ):
@@ -205,26 +205,6 @@ def canonical_form(k, v):
205205 yield k , sub_val .strip ()
206206
207207
208- def _to_bytestring (element ):
209- """
210- Converts a single string to a bytestring, encoding via UTF-8 if needed.
211- """
212- if isinstance (element , unicode ):
213- return element .encode ('utf-8' )
214- elif isinstance (element , bytes ):
215- return element
216- else :
217- raise ValueError ("Non string type." )
218-
219-
220- def _to_bytestring_tuple (* x ):
221- """
222- Converts the given strings to a bytestring if necessary, returning a
223- tuple.
224- """
225- return tuple (imap (_to_bytestring , x ))
226-
227-
228208def _keys_equal (x , y ):
229209 """
230210 Returns 'True' if the two keys are equal by the laws of HTTP headers.
0 commit comments