Update students_0 files.
This commit is contained in:
parent
b184f8c9c6
commit
b15e506c8f
|
@ -14,7 +14,6 @@ A package for learning network programming in Python.
|
|||
This module (file) manages the parsing of HTTP requests.
|
||||
"""
|
||||
|
||||
# TODO: Gestion de la casse des paramètres ?
|
||||
|
||||
def parse_request(buf: bytes) -> dict[str, dict]:
|
||||
"""Parses a full HTTP request bytes buffer into a dict.
|
||||
|
@ -26,6 +25,21 @@ def parse_request(buf: bytes) -> dict[str, dict]:
|
|||
- params: dict[str, str]
|
||||
List of the HTTP parameters (i.e. the following lines);
|
||||
output of `parse_request_params`.
|
||||
|
||||
An example of return:
|
||||
```
|
||||
{
|
||||
'head': { 'verb': 'GET', 'resource': '//index.html'},
|
||||
'params': {
|
||||
'Host': 'localhost:8000',
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'en-GB,en;q=0.5',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
[SNIP]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Parameters
|
||||
----------
|
||||
|
@ -42,14 +56,8 @@ def parse_request(buf: bytes) -> dict[str, dict]:
|
|||
ValueError
|
||||
The request is not valid HTTP.
|
||||
"""
|
||||
if buf == b'':
|
||||
raise ValueError("Received empty request")
|
||||
lines = buf.decode('utf-8').strip().splitlines()
|
||||
|
||||
req_head = parse_request_head(lines[0])
|
||||
req_head = dict()
|
||||
req_params = dict()
|
||||
if len(lines) > 1:
|
||||
req_params = parse_request_params(lines[1:])
|
||||
|
||||
return dict(
|
||||
head=req_head,
|
||||
|
@ -82,13 +90,12 @@ def parse_request_head(line: str) -> dict[str, str]:
|
|||
ValueError
|
||||
The request header is not valid HTTP.
|
||||
"""
|
||||
fields = line.split(' ')
|
||||
if len(fields) != 3:
|
||||
raise ValueError(f"Request header is invalid: {line}")
|
||||
|
||||
pass
|
||||
|
||||
return dict(
|
||||
verb=fields[0].upper(),
|
||||
resource=fields[1]
|
||||
verb=None,
|
||||
resource=None
|
||||
)
|
||||
|
||||
def parse_request_params(lines: list[str]) -> dict[str, str]:
|
||||
|
@ -117,13 +124,7 @@ def parse_request_params(lines: list[str]) -> dict[str, str]:
|
|||
The provided lines are not valid HTTP.
|
||||
"""
|
||||
params = dict()
|
||||
for l in lines:
|
||||
kv = l.strip().split(': ')
|
||||
|
||||
if len(kv) != 2 or len(kv[0]) == 0 or len(kv[1]) == 0:
|
||||
raise ValueError(f"Request line is not a valid key/value pair: {l}")
|
||||
|
||||
params[kv[0]] = kv[1]
|
||||
return params
|
||||
|
||||
|
||||
|
|
|
@ -28,8 +28,9 @@ def log(msg: str):
|
|||
- msg : str
|
||||
The message string to print.
|
||||
"""
|
||||
_time = now_rfc2616()
|
||||
print(f"{_time} - {msg}")
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def log_address(addr: tuple[str, int], msg: str):
|
||||
"""
|
||||
|
@ -44,8 +45,9 @@ def log_address(addr: tuple[str, int], msg: str):
|
|||
- msg: str
|
||||
The message string to print.
|
||||
"""
|
||||
_addr = f"{addr[0]}:{addr[1]}"
|
||||
log(f"{_addr} - {msg}")
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def log_request(addr: tuple[str, int], req: dict[str, dict]):
|
||||
"""
|
||||
|
@ -63,17 +65,11 @@ def log_request(addr: tuple[str, int], req: dict[str, dict]):
|
|||
- req: dict[str, dict]
|
||||
The request to print.
|
||||
"""
|
||||
assert 'head' in req \
|
||||
and 'verb' in req['head'] and 'resource' in req['head']
|
||||
|
||||
verb = req['head']['verb']
|
||||
res = req['head']['resource']
|
||||
msg = f"{verb} {res}"
|
||||
|
||||
if 'params' in req and 'User-Agent' in req['params']:
|
||||
msg += f" - {req['params']['User-Agent']}"
|
||||
msg = None
|
||||
|
||||
log_address(addr, msg)
|
||||
|
||||
|
||||
def log_reply(addr: tuple[str, int], req: dict[str, dict], code: int):
|
||||
"""
|
||||
|
@ -93,15 +89,8 @@ def log_reply(addr: tuple[str, int], req: dict[str, dict], code: int):
|
|||
- code: int
|
||||
The replied code to print.
|
||||
"""
|
||||
assert 'head' in req \
|
||||
and 'verb' in req['head'] and 'resource' in req['head']
|
||||
|
||||
verb = req['head']['verb']
|
||||
res = req['head']['resource']
|
||||
msg = f"{verb} {res} - {code}"
|
||||
|
||||
if 'params' in req and 'User-Agent' in req['params']:
|
||||
msg += f" - {req['params']['User-Agent']}"
|
||||
|
||||
msg = None
|
||||
|
||||
log_address(addr, msg)
|
||||
|
||||
|
|
Loading…
Reference in a new issue