mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-01-30 14:58:38 +00:00
netlib.utils.get_header_tokens -> netlib.http1.read.get_header_tokens
Placing this next to its only use.
This commit is contained in:
parent
da074bff01
commit
15b2374ef9
@ -9,6 +9,18 @@ from .. import Request, Response, Headers
|
||||
from .. import url
|
||||
|
||||
|
||||
def get_header_tokens(headers, key):
|
||||
"""
|
||||
Retrieve all tokens for a header key. A number of different headers
|
||||
follow a pattern where each header line can containe comma-separated
|
||||
tokens, and headers can be set multiple times.
|
||||
"""
|
||||
if key not in headers:
|
||||
return []
|
||||
tokens = headers[key].split(",")
|
||||
return [token.strip() for token in tokens]
|
||||
|
||||
|
||||
def read_request(rfile, body_size_limit=None):
|
||||
request = read_request_head(rfile)
|
||||
expected_body_size = expected_http_body_size(request)
|
||||
@ -148,7 +160,7 @@ def connection_close(http_version, headers):
|
||||
"""
|
||||
# At first, check if we have an explicit Connection header.
|
||||
if "connection" in headers:
|
||||
tokens = utils.get_header_tokens(headers, "connection")
|
||||
tokens = get_header_tokens(headers, "connection")
|
||||
if "close" in tokens:
|
||||
return True
|
||||
elif "keep-alive" in tokens:
|
||||
|
@ -177,18 +177,6 @@ def is_valid_port(port):
|
||||
return 0 <= port <= 65535
|
||||
|
||||
|
||||
def get_header_tokens(headers, key):
|
||||
"""
|
||||
Retrieve all tokens for a header key. A number of different headers
|
||||
follow a pattern where each header line can containe comma-separated
|
||||
tokens, and headers can be set multiple times.
|
||||
"""
|
||||
if key not in headers:
|
||||
return []
|
||||
tokens = headers[key].split(",")
|
||||
return [token.strip() for token in tokens]
|
||||
|
||||
|
||||
def hostport(scheme, host, port):
|
||||
"""
|
||||
Returns the host component, with a port specifcation if needed.
|
||||
|
@ -7,11 +7,22 @@ from netlib.http.http1.read import (
|
||||
read_request, read_response, read_request_head,
|
||||
read_response_head, read_body, connection_close, expected_http_body_size, _get_first_line,
|
||||
_read_request_line, _parse_authority_form, _read_response_line, _check_http_version,
|
||||
_read_headers, _read_chunked
|
||||
_read_headers, _read_chunked, get_header_tokens
|
||||
)
|
||||
from netlib.tutils import treq, tresp, raises
|
||||
|
||||
|
||||
def test_get_header_tokens():
|
||||
headers = Headers()
|
||||
assert get_header_tokens(headers, "foo") == []
|
||||
headers["foo"] = "bar"
|
||||
assert get_header_tokens(headers, "foo") == ["bar"]
|
||||
headers["foo"] = "bar, voing"
|
||||
assert get_header_tokens(headers, "foo") == ["bar", "voing"]
|
||||
headers.set_all("foo", ["bar, voing", "oink"])
|
||||
assert get_header_tokens(headers, "foo") == ["bar", "voing", "oink"]
|
||||
|
||||
|
||||
def test_read_request():
|
||||
rfile = BytesIO(b"GET / HTTP/1.1\r\n\r\nskip")
|
||||
r = read_request(rfile)
|
||||
|
@ -38,17 +38,6 @@ def test_pretty_size():
|
||||
assert utils.pretty_size(1024 * 1024) == "1MB"
|
||||
|
||||
|
||||
def test_get_header_tokens():
|
||||
headers = Headers()
|
||||
assert utils.get_header_tokens(headers, "foo") == []
|
||||
headers["foo"] = "bar"
|
||||
assert utils.get_header_tokens(headers, "foo") == ["bar"]
|
||||
headers["foo"] = "bar, voing"
|
||||
assert utils.get_header_tokens(headers, "foo") == ["bar", "voing"]
|
||||
headers.set_all("foo", ["bar, voing", "oink"])
|
||||
assert utils.get_header_tokens(headers, "foo") == ["bar", "voing", "oink"]
|
||||
|
||||
|
||||
def test_multipartdecode():
|
||||
boundary = 'somefancyboundary'
|
||||
headers = Headers(
|
||||
|
Loading…
Reference in New Issue
Block a user