2019-12-08 22:43:49 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
class HTTP:
|
|
|
|
@staticmethod
|
2024-01-02 03:46:24 +01:00
|
|
|
def parse(data: bytes, request: bool = False, response: bool = False) -> Optional[Dict[str, Any]]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
A very lazy and hastily coded HTTP parser.
|
|
|
|
|
|
|
|
Assumes that data is a valid HTTP stream and just tokenizes relevant pieces.
|
|
|
|
This can probably be made far more robust by scrapping it and using flask or
|
|
|
|
another real parser. However, it does the job.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
data - A string blob that represents an HTTP request
|
|
|
|
request - Set to true if this is expected to be a request
|
|
|
|
response - Set to true if this is expected to be a response
|
|
|
|
|
|
|
|
Returns a dictionary containing:
|
|
|
|
version - HTTP version
|
|
|
|
headers - Dictionary of headers keyed by header name
|
|
|
|
data - Post body in unmolested form
|
|
|
|
uri - Requested URI when this is a request
|
|
|
|
method - Requested method on URI when this is a request
|
|
|
|
code - HTTP response code when this is a response
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Try to get the headers and post body as two separate elemenst
|
|
|
|
binary_headers, data = data.split(b"\r\n\r\n", 1)
|
|
|
|
except ValueError:
|
|
|
|
# Can't even separate header from post body
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Split headers individually
|
|
|
|
headerlist = binary_headers.split(b"\r\n")
|
|
|
|
|
|
|
|
try:
|
|
|
|
if request:
|
|
|
|
# Remove the first header as this is the HTTP request
|
2022-10-15 20:56:30 +02:00
|
|
|
method, uri, version = headerlist.pop(0).split(b" ", 2)
|
2019-12-08 22:43:49 +01:00
|
|
|
elif response:
|
|
|
|
# Remove the first header as this is the HTTP response
|
2022-10-15 20:56:30 +02:00
|
|
|
version, code, error = headerlist.pop(0).split(b" ", 2)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Logic error!")
|
|
|
|
except ValueError:
|
|
|
|
# Can't parse the headers returned
|
|
|
|
return None
|
|
|
|
|
|
|
|
headers: Dict[str, str] = {}
|
|
|
|
preserved: List[Tuple[str, str]] = []
|
|
|
|
|
|
|
|
# This is lazy because we can have multiple values, but whatever, it works
|
|
|
|
for header in headerlist:
|
|
|
|
name, info = header.split(b":", 1)
|
2022-10-15 20:56:30 +02:00
|
|
|
key = name.decode("ascii").lower()
|
|
|
|
value = info.decode("ascii").strip()
|
2019-12-08 22:43:49 +01:00
|
|
|
headers[key] = value
|
|
|
|
preserved.append((key, value))
|
|
|
|
|
|
|
|
# Cap post body to length if we have a content-length header
|
2022-10-15 20:56:30 +02:00
|
|
|
if "content-length" in headers:
|
|
|
|
data = data[: int(headers["content-length"])]
|
|
|
|
valid = len(data) == int(headers["content-length"])
|
2024-01-02 03:46:24 +01:00
|
|
|
elif "transfer-encoding" in headers and headers["transfer-encoding"] == "chunked":
|
2022-10-15 20:56:30 +02:00
|
|
|
real_data = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
size_bytes, rest = data.split(b"\r\n", 1)
|
|
|
|
except ValueError:
|
|
|
|
# Not enough values to unpack
|
2022-10-15 20:56:30 +02:00
|
|
|
size_bytes = b"0"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
size = int(size_bytes, 16)
|
|
|
|
|
|
|
|
if size == 0:
|
|
|
|
# End of chunks
|
|
|
|
break
|
|
|
|
|
|
|
|
# Grab the real data
|
|
|
|
real_data = real_data + rest[:size]
|
|
|
|
|
|
|
|
# Skip past data and \r\n
|
2022-10-15 20:56:30 +02:00
|
|
|
data = rest[(size + 2) :]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
data = real_data
|
|
|
|
valid = True
|
|
|
|
else:
|
|
|
|
valid = True
|
|
|
|
|
|
|
|
if request:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"method": method.decode("ascii").lower(),
|
|
|
|
"uri": uri.decode("ascii"),
|
|
|
|
"version": version.decode("ascii"),
|
|
|
|
"headers": headers,
|
|
|
|
"preserved_headers": preserved,
|
|
|
|
"data": data,
|
|
|
|
"valid": valid,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
elif response:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"code": code.decode("ascii"),
|
|
|
|
"version": version.decode("ascii"),
|
|
|
|
"error": error.decode("ascii"),
|
|
|
|
"headers": headers,
|
|
|
|
"preserved_headers": preserved,
|
|
|
|
"data": data,
|
|
|
|
"valid": valid,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
2022-10-15 20:56:30 +02:00
|
|
|
def generate(
|
|
|
|
parsed_headers: Dict[str, Any],
|
|
|
|
data: bytes,
|
|
|
|
request: bool = False,
|
|
|
|
response: bool = False,
|
|
|
|
) -> bytes:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
A very lazy and hastily coded HTTP packet generator.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
parsed_headers - A dictionary of headers to include
|
|
|
|
data - Bytes which should make up the body of the HTTP packet
|
|
|
|
request - Set to True if this is a request
|
|
|
|
response - Set to True if this is a response
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Binary data which can be sent over the wire to a HTTP server.
|
|
|
|
"""
|
|
|
|
out = []
|
|
|
|
|
|
|
|
# Add first part of header
|
|
|
|
if request:
|
2024-01-02 03:46:24 +01:00
|
|
|
out.append(f'{parsed_headers["method"]} {parsed_headers["uri"]} {parsed_headers["version"]}')
|
2019-12-08 22:43:49 +01:00
|
|
|
elif response:
|
2024-01-02 03:46:24 +01:00
|
|
|
out.append(f'{parsed_headers["version"]} {parsed_headers["code"]} {parsed_headers["error"]}')
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Logic error!")
|
|
|
|
|
|
|
|
# Add the rest of the headers
|
2022-10-15 20:56:30 +02:00
|
|
|
for header in parsed_headers["preserved_headers"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
name, value = header
|
2022-10-15 20:56:30 +02:00
|
|
|
if name.lower() == "content-length":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Fix this
|
|
|
|
value = len(data)
|
2022-10-15 20:56:30 +02:00
|
|
|
elif name.lower() == "transfer-encoding":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Either we support and strip this, or error!
|
2022-10-15 20:56:30 +02:00
|
|
|
if value.lower() == "chunked":
|
2019-12-08 22:43:49 +01:00
|
|
|
# We support parsing this, but aren't going to re-generate
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# Woah, can't figure this out!
|
2020-01-07 22:29:07 +01:00
|
|
|
raise Exception(f"Unknown transfer-encodign {value}")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2020-01-07 22:29:07 +01:00
|
|
|
out.append(f"{name}: {value}")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Concatenate it with the binary data
|
2022-10-15 20:56:30 +02:00
|
|
|
return "\r\n".join(out).encode("ascii") + b"\r\n\r\n" + data
|