rnet.header
HTTP header management with case-insensitive keys and multiple values support.
rnet.header
HTTP Header Management
This module provides efficient storage and manipulation of HTTP headers with support for multiple values per header name. The HeaderMap class is designed to handle the complexities of HTTP header processing, including case-insensitive header names and multiple header values.
The implementation follows HTTP specifications (RFC 7230) for header handling, including proper support for headers that can have multiple values (like Set-Cookie, Accept-Encoding, etc.).
HeaderMap
A case-insensitive HTTP header map supporting multiple values per header.
This class provides efficient storage and retrieval of HTTP headers, automatically handling case-insensitive header names and supporting headers with multiple values (such as Set-Cookie or Accept-Encoding).
The implementation follows HTTP/1.1 specifications for header handling and provides both dictionary-like access and specialized methods for HTTP header manipulation.
Source code in rnet/header.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
__getitem__
__setitem__
__delitem__
__contains__
__len__
__iter__
__str__
__init__
Create a new HeaderMap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init
|
Dict[str, str] | None
|
Optional dictionary to initialize headers from |
None
|
capacity
|
int | None
|
Optional initial capacity hint for performance |
None
|
Returns:
| Type | Description |
|---|---|
None
|
A new HeaderMap instance |
Example
Source code in rnet/header.py
contains_key
Check if the header map contains the given key.
This is equivalent to using the 'in' operator but provides an explicit method name. Header name comparison is case-insensitive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The header name to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the header exists, False otherwise |
Source code in rnet/header.py
insert
Insert a header, replacing any existing values.
This method replaces all existing values for the given header name with the new value. For adding additional values, use append() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The header name (case-insensitive) |
required |
value
|
str
|
The header value to set |
required |
Source code in rnet/header.py
append
Append a value to an existing header or create a new one.
If the header already exists, this adds an additional value. If the header doesn't exist, it creates a new header with this value. This is useful for headers that can have multiple values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The header name (case-insensitive) |
required |
value
|
str
|
The header value to append |
required |
Source code in rnet/header.py
remove
Remove all values for a header name.
This removes the header entirely from the map. If the header doesn't exist, this method does nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The header name to remove (case-insensitive) |
required |
Source code in rnet/header.py
get
Get the first value for a header name with optional default.
Returns the first value associated with the header name, or the default value if the header doesn't exist. For headers with multiple values, use get_all() to retrieve all values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The header name (case-insensitive) |
required |
default
|
bytes | None
|
Value to return if header doesn't exist |
None
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
The first header value as bytes, or the default value |
Source code in rnet/header.py
get_all
Get all values for a header name.
Returns an iterator over all values associated with the header name. This is useful for headers that can have multiple values, such as Set-Cookie, Accept-Encoding, or custom headers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The header name (case-insensitive) |
required |
Returns:
| Type | Description |
|---|---|
Iterator[bytes]
|
An iterator over all header values |
Source code in rnet/header.py
values
Iterate over all header values.
Returns:
| Type | Description |
|---|---|
Iterator[bytes]
|
An iterator over all header values as bytes. |
keys
Iterate over unique header names.
Returns:
| Type | Description |
|---|---|
Iterator[bytes]
|
An iterator over unique header names as bytes. |
len
Get the total number of header values.
This returns the total count of header values, which can be greater than the number of unique header names if some headers have multiple values.
Returns:
| Type | Description |
|---|---|
int
|
Total number of header values stored |
Source code in rnet/header.py
keys_len
Get the number of unique header names.
This returns the count of unique header names, regardless of how many values each header has.
Returns:
| Type | Description |
|---|---|
int
|
Number of unique header names |
is_empty
Check if the header map is empty.
Returns:
| Type | Description |
|---|---|
bool
|
True if no headers are stored, False otherwise |
clear
Remove all headers from the map.
After calling this method, the header map will be empty and is_empty() will return True.
OrigHeaderMap
A map from header names to their original casing as received in an HTTP message.
OrigHeaderMap not only preserves the original case of each header name as it appeared in the HTTP message, but also maintains the insertion order of headers. This makes it suitable for use cases where the order of headers matters, such as HTTP/1.x message serialization, proxying, or reproducing requests/responses exactly as received.
The map stores a mapping between the case-insensitive (standard) header name and the original case-sensitive header name as it appeared in the HTTP message.
Example
If an HTTP message included the following headers:
x-Bread: Baguette
X-BREAD: Pain
x-bread: Ficelle
Then the OrigHeaderMap would preserve both the exact casing and order of these headers: - Standard name "x-bread" maps to original "x-Bread" - Standard name "x-bread" maps to original "X-BREAD" - Standard name "x-bread" maps to original "x-bread"
This allows the client to reproduce the exact header casing when forwarding or reconstructing the HTTP message.
Source code in rnet/header.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
__init__
Creates a new OrigHeaderMap from an optional list of header names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init
|
Sequence[str] | None
|
Optional list of header names to initialize with. |
None
|
capacity
|
int | None
|
Optional initial capacity for the map. |
None
|
Source code in rnet/header.py
__iter__
Returns an iterator over the (standard_name, original_name) pairs.
Returns:
| Type | Description |
|---|---|
Iterator[Tuple[bytes, bytes]]
|
An iterator over header name pairs. |
__len__
insert
Insert a new header name into the collection.
If the map did not previously have this key present, then False is returned. If the map did have this key present, the new value is pushed to the end of the list of values currently associated with the key. The key is not updated, though; this matters for types that can be == without being identical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The header name to insert. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the key was newly inserted, False if it already existed. |
Source code in rnet/header.py
extend
Extends the map with all entries from another OrigHeaderMap, preserving order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
OrigHeaderMap
|
Another OrigHeaderMap to extend from. |
required |