Skip to content

Commit 9fbe035

Browse files
committed
typing list, tuple, dict
1 parent 051afe9 commit 9fbe035

18 files changed

+170
-72
lines changed

httpdbg/args.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# -*- coding: utf-8 -*-
22
import argparse
33
from pathlib import Path
4-
from typing import List, Tuple
54

65
from httpdbg.log import LogLevel
76

87

9-
def read_args(args: List[str]) -> Tuple[argparse.Namespace, List[str]]:
8+
def read_args(args: list[str]) -> tuple[argparse.Namespace, list[str]]:
109
httpdbg_args = args
1110
client_args = []
1211
for action in ["--console", "--module", "-m", "--script"]:

httpdbg/hooks/all.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from contextlib import contextmanager
33
from typing import Generator
4-
from typing import List
5-
from typing import Tuple
64
from typing import Union
75

86
from httpdbg.hooks.aiohttp import hook_aiohttp
@@ -26,10 +24,10 @@
2624
@contextmanager
2725
def httprecord(
2826
records: HTTPRecords = None,
29-
initiators: Union[List[str], None] = None,
27+
initiators: Union[list[str], None] = None,
3028
client: bool = True,
3129
server: bool = False,
32-
ignore: Tuple[Tuple[str, int], ...] = (),
30+
ignore: tuple[tuple[str, int], ...] = (),
3331
multiprocess: bool = True,
3432
) -> Generator[HTTPRecords, None, None]:
3533
if records is None:

httpdbg/hooks/external.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import tempfile
88
import time
99
import threading
10-
from typing import List
1110
from typing import Union
1211

1312
from httpdbg.env import HTTPDBG_MULTIPROCESS_DIR
@@ -18,7 +17,7 @@
1817
@contextmanager
1918
def watcher_external(
2019
records: HTTPRecords,
21-
initiators: Union[List[str], None] = None,
20+
initiators: Union[list[str], None] = None,
2221
server: bool = False,
2322
) -> Generator[HTTPRecords, None, None]:
2423
if HTTPDBG_MULTIPROCESS_DIR not in os.environ:

httpdbg/hooks/fastapi.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from contextlib import contextmanager
44
import functools
55
from functools import wraps
6-
from typing import Dict
76
from typing import Generator
87
from typing import Union
98

@@ -37,7 +36,7 @@ def hook(*args, **kwargs):
3736
def set_hook_fastapi_apirouter_add_api_route(
3837
records: HTTPRecords,
3938
method: Callable,
40-
already_mapped: Union[Dict[Callable, Callable], None] = None,
39+
already_mapped: Union[dict[Callable, Callable], None] = None,
4140
):
4241

4342
@wraps(method)
@@ -123,7 +122,7 @@ def hook_fastapi(records: HTTPRecords) -> Generator[None, None, None]:
123122
try:
124123
import fastapi.routing
125124

126-
already_mapped: Dict[Callable, Callable] = {}
125+
already_mapped: dict[Callable, Callable] = {}
127126

128127
set_hook_fastapi_apirouter_add_api_route_with_already_mapped = (
129128
functools.partial(

httpdbg/hooks/flask.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from contextlib import contextmanager
44
import functools
55
from functools import wraps
6-
from typing import Dict
76
from typing import Generator
87
from typing import Union
98

@@ -36,7 +35,7 @@ def hook(*args, **kwargs):
3635
def set_hook_flask_add_url_rule(
3736
records: HTTPRecords,
3837
method: Callable,
39-
already_mapped: Union[Dict[Callable, Callable], None] = None,
38+
already_mapped: Union[dict[Callable, Callable], None] = None,
4039
):
4140

4241
def hook(*args, **kwargs):
@@ -67,7 +66,7 @@ def hook(*args, **kwargs):
6766
def set_hook_flask_register_error_handler(
6867
records: HTTPRecords,
6968
method: Callable,
70-
already_mapped: Union[Dict[Callable, Callable], None] = None,
69+
already_mapped: Union[dict[Callable, Callable], None] = None,
7170
):
7271

7372
def hook(*args, **kwargs):
@@ -103,7 +102,7 @@ def hook_flask(records: HTTPRecords) -> Generator[None, None, None]:
103102

104103
# we must not apply the hook more than once on a mapped endpoint function
105104
# AssertionError: View function mapping is overwriting an existing endpoint function: xxxx
106-
already_mapped: Dict[Callable, Callable] = {}
105+
already_mapped: dict[Callable, Callable] = {}
107106

108107
set_hook_flask_add_url_rule_with_already_mapped = functools.partial(
109108
set_hook_flask_add_url_rule, already_mapped=already_mapped

httpdbg/hooks/generic.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from types import ModuleType
1010
from typing import Any
1111
from typing import Generator
12-
from typing import List
1312
from typing import Union
1413

1514
from httpdbg.hooks.utils import decorate
@@ -49,13 +48,13 @@ def hook(*args, **kwargs):
4948

5049
@contextmanager
5150
def hook_generic(
52-
records: HTTPRecords, initiators: Union[List[str], None] = None
51+
records: HTTPRecords, initiators: Union[list[str], None] = None
5352
) -> Generator[None, None, None]:
5453
if initiators:
5554
# we add a hook for a generic initiator only if the module is imported
5655

57-
hooks: List[Callable] = []
58-
already_hooked: List[str] = []
56+
hooks: list[Callable] = []
57+
already_hooked: list[str] = []
5958

6059
original_builtin_import = builtins.__import__
6160

@@ -130,7 +129,7 @@ def __hook__import__(
130129
fnc = undecorate(fnc)
131130

132131

133-
def list_callables_from_module(records: HTTPRecords, module: str) -> List[Any]:
132+
def list_callables_from_module(records: HTTPRecords, module: str) -> list[Any]:
134133
callables = []
135134

136135
try:
@@ -180,7 +179,7 @@ def list_callables_from_class(
180179
imported_module: ModuleType,
181180
module: str,
182181
classname: str,
183-
) -> List[Any]:
182+
) -> list[Any]:
184183
callables = []
185184

186185
try:

httpdbg/hooks/h2.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# -*- coding: utf-8 -*-
2+
from collections.abc import Callable
3+
from contextlib import contextmanager
4+
import traceback
5+
from typing import Generator
6+
7+
from httpdbg.hooks.utils import getcallargs
8+
from httpdbg.hooks.utils import decorate
9+
from httpdbg.hooks.utils import undecorate
10+
from httpdbg.initiator import httpdbg_initiator
11+
from httpdbg.records import HTTPRecords
12+
13+
14+
def set_hook_for_h2_send_headers(records: HTTPRecords, method: Callable):
15+
def hook(*args, **kwargs):
16+
callargs = getcallargs(method, *args, **kwargs)
17+
print(callargs)
18+
initiator_and_group = None
19+
try:
20+
with httpdbg_initiator(
21+
records, traceback.extract_stack(), method, *args, **kwargs
22+
) as initiator_and_group:
23+
ret = method(*args, **kwargs)
24+
return ret
25+
except Exception as ex:
26+
raise
27+
# callargs = getcallargs(method, *args, **kwargs)
28+
29+
# if "url" in callargs:
30+
# if initiator_and_group:
31+
# initiator, group, is_new = initiator_and_group
32+
# if is_new:
33+
# records.add_new_record_exception(
34+
# initiator, group, str(callargs["url"]), ex
35+
# )
36+
# raise
37+
38+
return hook
39+
40+
41+
def set_hook_for_h2_send_data(records: HTTPRecords, method: Callable):
42+
def hook(*args, **kwargs):
43+
callargs = getcallargs(method, *args, **kwargs)
44+
print(callargs)
45+
initiator_and_group = None
46+
try:
47+
with httpdbg_initiator(
48+
records, traceback.extract_stack(), method, *args, **kwargs
49+
) as initiator_and_group:
50+
ret = method(*args, **kwargs)
51+
return ret
52+
except Exception as ex:
53+
raise
54+
# callargs = getcallargs(method, *args, **kwargs)
55+
56+
# if "url" in callargs:
57+
# if initiator_and_group:
58+
# initiator, group, is_new = initiator_and_group
59+
# if is_new:
60+
# records.add_new_record_exception(
61+
# initiator, group, str(callargs["url"]), ex
62+
# )
63+
# raise
64+
65+
return hook
66+
67+
68+
def set_hook_for_h2_receive_data(records: HTTPRecords, method: Callable):
69+
def hook(*args, **kwargs):
70+
# callargs = getcallargs(method, *args, **kwargs)
71+
# print(callargs)
72+
initiator_and_group = None
73+
try:
74+
with httpdbg_initiator(
75+
records, traceback.extract_stack(), method, *args, **kwargs
76+
) as initiator_and_group:
77+
ret = method(*args, **kwargs)
78+
for event in ret:
79+
print(type(event))
80+
print(event)
81+
return ret
82+
except Exception as ex:
83+
raise
84+
# callargs = getcallargs(method, *args, **kwargs)
85+
86+
# if "url" in callargs:
87+
# if initiator_and_group:
88+
# initiator, group, is_new = initiator_and_group
89+
# if is_new:
90+
# records.add_new_record_exception(
91+
# initiator, group, str(callargs["url"]), ex
92+
# )
93+
# raise
94+
95+
return hook
96+
97+
98+
@contextmanager
99+
def hook_h2(records: HTTPRecords) -> Generator[None, None, None]:
100+
hooks = False
101+
try:
102+
import h2.connection
103+
104+
h2.connection.H2Connection.send_headers = decorate(records, h2.connection.H2Connection.send_headers, set_hook_for_h2_send_headers) # type: ignore[arg-type]
105+
h2.connection.H2Connection.send_data = decorate(records, h2.connection.H2Connection.send_data, set_hook_for_h2_send_data) # type: ignore[arg-type]
106+
h2.connection.H2Connection.receive_data = decorate(records, h2.connection.H2Connection.receive_data, set_hook_for_h2_receive_data) # type: ignore[arg-type]
107+
108+
hooks = True
109+
except ImportError:
110+
pass
111+
112+
yield
113+
114+
if hooks:
115+
h2.connection.H2Connection.send_headers = undecorate(h2.connection.H2Connection.send_headers) # type: ignore[arg-type]
116+
h2.connection.H2Connection.send_data = undecorate(h2.connection.H2Connection.send_data) # type: ignore[arg-type]
117+
h2.connection.H2Connection.receive_data = undecorate(h2.connection.H2Connection.receive_data) # type: ignore[arg-type]

httpdbg/hooks/record.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from abc import ABC, abstractmethod
33
import datetime
4-
from typing import List
5-
from typing import Tuple
64
from typing import Union
75

86
from httpdbg.utils import get_new_uuid
@@ -23,7 +21,7 @@ def rawheaders(self) -> bytes:
2321

2422
@property
2523
@abstractmethod
26-
def headers(self) -> List[HTTPDBGHeader]:
24+
def headers(self) -> list[HTTPDBGHeader]:
2725
pass
2826

2927
@property
@@ -51,7 +49,7 @@ class HTTPRecordRequest(HTTPRecordReqResp, ABC):
5149

5250
@property
5351
@abstractmethod
54-
def cookies(self) -> List[HTTPDBGCookie]:
52+
def cookies(self) -> list[HTTPDBGCookie]:
5553
pass
5654

5755
@property
@@ -74,7 +72,7 @@ class HTTPRecordResponse(HTTPRecordReqResp, ABC):
7472

7573
@property
7674
@abstractmethod
77-
def cookies(self) -> List[HTTPDBGCookie]:
75+
def cookies(self) -> list[HTTPDBGCookie]:
7876
pass
7977

8078
@property
@@ -107,7 +105,7 @@ def __init__(
107105
is_client: bool = True,
108106
) -> None:
109107
self.id = get_new_uuid()
110-
self.address: Tuple[str, int] = ("", 0)
108+
self.address: tuple[str, int] = ("", 0)
111109
self._url: Union[str, None] = None
112110
self.initiator_id: Union[str, None] = initiator_id
113111
self.exception: Union[Exception, None] = None

httpdbg/hooks/recordhttp1.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime
22
from urllib.parse import urlparse
3-
from typing import List
43

54
from httpdbg.preview import generate_preview
65
from httpdbg.hooks.record import HTTPRecord
@@ -18,7 +17,7 @@ class HTTP1RecordReqResp(HTTPRecordReqResp):
1817
def __init__(self) -> None:
1918
self._rawdata: bytes = bytes()
2019
self._rawheaders: bytes = bytes()
21-
self._headers: List[HTTPDBGHeader] = []
20+
self._headers: list[HTTPDBGHeader] = []
2221
self.last_update: datetime.datetime = datetime.datetime.now(
2322
datetime.timezone.utc
2423
)
@@ -38,7 +37,7 @@ def rawheaders(self) -> bytes:
3837
return self._rawheaders
3938

4039
@property
41-
def headers(self) -> List[HTTPDBGHeader]:
40+
def headers(self) -> list[HTTPDBGHeader]:
4241
if not self._headers:
4342
if self.rawheaders:
4443
for header in self.rawheaders[self.rawheaders.find(b"\r\n") :].split(
@@ -98,7 +97,7 @@ def __init__(self) -> None:
9897
self._protocol = bytes()
9998

10099
@property
101-
def cookies(self) -> List[HTTPDBGCookie]:
100+
def cookies(self) -> list[HTTPDBGCookie]:
102101
return list_cookies_headers_request_simple_cookies(self.headers)
103102

104103
def _parse_first_line(self) -> None:
@@ -133,7 +132,7 @@ def __init__(self):
133132
self._message = bytes()
134133

135134
@property
136-
def cookies(self) -> List[HTTPDBGCookie]:
135+
def cookies(self) -> list[HTTPDBGCookie]:
137136
return list_cookies_headers_response_simple_cookies(self.headers)
138137

139138
def _parse_first_line(self) -> None:

httpdbg/hooks/socket.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
import traceback
1212
from typing import Generator
13-
from typing import Dict, Tuple, Union
13+
from typing import Union
1414

1515
from httpdbg.initiator import httpdbg_initiator
1616
from httpdbg.log import logger
@@ -25,9 +25,9 @@
2525
class SocketRawData(object):
2626
"""Store the request data without encryption, even when using an SSLSocket."""
2727

28-
def __init__(self, id: int, address: Tuple[str, int], ssl: bool) -> None:
28+
def __init__(self, id: int, address: tuple[str, int], ssl: bool) -> None:
2929
self.id: int = id
30-
self.address: Tuple[str, int] = address
30+
self.address: tuple[str, int] = address
3131
self.ssl: bool = ssl
3232
self._rawdata: bytes = bytes()
3333
self.record: Union[HTTP1Record, None] = None
@@ -66,10 +66,10 @@ class TracerHTTP1:
6666

6767
def __init__(
6868
self,
69-
ignore: Tuple[Tuple[str, int], ...] = (),
69+
ignore: tuple[tuple[str, int], ...] = (),
7070
):
71-
self.sockets: Dict[int, SocketRawData] = {}
72-
self.ignore: Tuple[Tuple[str, int], ...] = ignore
71+
self.sockets: dict[int, SocketRawData] = {}
72+
self.ignore: tuple[tuple[str, int], ...] = ignore
7373

7474
def get_socket_data(
7575
self, obj, extra_sock=None, force_new=False, request=None, is_uvicorn=False

0 commit comments

Comments
 (0)