|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +hyper/common/headers |
| 4 | +~~~~~~~~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | +Contains hyper's structures for storing and working with HTTP headers. |
| 7 | +""" |
| 8 | +import collections |
| 9 | + |
| 10 | + |
| 11 | +class HTTPHeaderMap(collections.MutableMapping): |
| 12 | + """ |
| 13 | + A structure that contains HTTP headers. |
| 14 | +
|
| 15 | + HTTP headers are a curious beast. At the surface level they look roughly |
| 16 | + like a name-value set, but in practice they have many variations that |
| 17 | + make them tricky: |
| 18 | +
|
| 19 | + - duplicate keys are allowed |
| 20 | + - keys are compared case-insensitively |
| 21 | + - duplicate keys are isomorphic to comma-separated values, *except when |
| 22 | + they aren't*! |
| 23 | + - they logically contain a form of ordering |
| 24 | +
|
| 25 | + This data structure is an attempt to preserve all of that information |
| 26 | + while being as user-friendly as possible. |
| 27 | + """ |
| 28 | + def __init__(self, *args, **kwargs): |
| 29 | + # The meat of the structure. In practice, headers are an ordered list |
| 30 | + # of tuples. This early version of the data structure simply uses this |
| 31 | + # directly under the covers. |
| 32 | + self._items = [] |
| 33 | + |
| 34 | + for arg in args: |
| 35 | + for item in arg: |
| 36 | + self._items.extend(canonical_form(*item)) |
| 37 | + |
| 38 | + for k, v in kwargs.items(): |
| 39 | + self._items.extend(canonical_form(k, v)) |
| 40 | + |
| 41 | + def __getitem__(self, key): |
| 42 | + """ |
| 43 | + Unlike the dict __getitem__, this returns a list of items in the order |
| 44 | + they were added. |
| 45 | + """ |
| 46 | + values = [] |
| 47 | + |
| 48 | + for k, v in self._items: |
| 49 | + if _keys_equal(k, key): |
| 50 | + values.append(v) |
| 51 | + |
| 52 | + if not values: |
| 53 | + raise KeyError() |
| 54 | + |
| 55 | + return values |
| 56 | + |
| 57 | + def __setitem__(self, key, value): |
| 58 | + """ |
| 59 | + Unlike the dict __setitem__, this appends to the list of items. It also |
| 60 | + splits out headers that can be split on the comma. |
| 61 | + """ |
| 62 | + self._items.extend(canonical_form(key, value)) |
| 63 | + |
| 64 | + def __delitem__(self, key): |
| 65 | + """ |
| 66 | + Sadly, __delitem__ is kind of stupid here, but the best we can do is |
| 67 | + delete all headers with a given key. To correctly achieve the 'KeyError |
| 68 | + on missing key' logic from dictionaries, we need to do this slowly. |
| 69 | + """ |
| 70 | + indices = [] |
| 71 | + for (i, (k, v)) in enumerate(self._items): |
| 72 | + if _keys_equal(k, key): |
| 73 | + indices.append(i) |
| 74 | + |
| 75 | + if not indices: |
| 76 | + raise KeyError() |
| 77 | + |
| 78 | + for i in indices[::-1]: |
| 79 | + self._items.pop(i) |
| 80 | + |
| 81 | + def __iter__(self): |
| 82 | + """ |
| 83 | + This mapping iterates like the list of tuples it is. |
| 84 | + """ |
| 85 | + for pair in self._items: |
| 86 | + yield pair |
| 87 | + |
| 88 | + def __len__(self): |
| 89 | + """ |
| 90 | + The length of this mapping is the number of individual headers. |
| 91 | + """ |
| 92 | + return len(self._items) |
| 93 | + |
| 94 | + def __contains__(self, key): |
| 95 | + """ |
| 96 | + If any header is present with this key, returns True. |
| 97 | + """ |
| 98 | + return any(_keys_equal(key, k) for k, _ in self._items) |
| 99 | + |
| 100 | + def keys(self): |
| 101 | + """ |
| 102 | + Returns an iterable of the header keys in the mapping. This explicitly |
| 103 | + does not filter duplicates, ensuring that it's the same length as |
| 104 | + len(). |
| 105 | + """ |
| 106 | + for n, _ in self._items: |
| 107 | + yield n |
| 108 | + |
| 109 | + def items(self): |
| 110 | + """ |
| 111 | + This mapping iterates like the list of tuples it is. |
| 112 | + """ |
| 113 | + for item in self: |
| 114 | + yield item |
| 115 | + |
| 116 | + def values(self): |
| 117 | + """ |
| 118 | + This is an almost nonsensical query on a header dictionary, but we |
| 119 | + satisfy it in the exact same way we satisfy 'keys'. |
| 120 | + """ |
| 121 | + for _, v in self._items: |
| 122 | + yield v |
| 123 | + |
| 124 | + def get(self, name, default=None): |
| 125 | + """ |
| 126 | + Unlike the dict get, this returns a list of items in the order |
| 127 | + they were added. |
| 128 | + """ |
| 129 | + try: |
| 130 | + return self[name] |
| 131 | + except KeyError: |
| 132 | + return default |
| 133 | + |
| 134 | + def __eq__(self, other): |
| 135 | + return self._items == other._items |
| 136 | + |
| 137 | + def __ne__(self, other): |
| 138 | + return self._items != other._items |
| 139 | + |
| 140 | + |
| 141 | +def canonical_form(k, v): |
| 142 | + """ |
| 143 | + Returns an iterable of key-value-pairs corresponding to the header in |
| 144 | + canonical form. This means that the header is split on commas unless for |
| 145 | + any reason it's a super-special snowflake (I'm looking at you Set-Cookie). |
| 146 | + """ |
| 147 | + SPECIAL_SNOWFLAKES = set(['set-cookie', 'set-cookie2']) |
| 148 | + |
| 149 | + k = k.lower() |
| 150 | + |
| 151 | + if k in SPECIAL_SNOWFLAKES: |
| 152 | + yield k, v |
| 153 | + else: |
| 154 | + for sub_val in v.split(','): |
| 155 | + yield k, sub_val.strip() |
| 156 | + |
| 157 | +def _keys_equal(x, y): |
| 158 | + """ |
| 159 | + Returns 'True' if the two keys are equal by the laws of HTTP headers. |
| 160 | + """ |
| 161 | + return x.lower() == y.lower() |
0 commit comments