Skip to content

Commit 9e964db

Browse files
committed
fix: large request/response #24
1 parent 6906aa2 commit 9e964db

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

environment.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self):
1414
self.PROXY_DESTINATION: str = f'http://127.0.0.1:{self.TESTER_LISTEN_PORT}'
1515
self.TESTER_DESTINATION: str = f'http://127.0.0.1:{self.PROXY_LISTEN_PORT}'
1616
self.STORAGE_PATH: str = os.path.join('.', 'storage')
17+
self.HTTP_MAX_REQUEST_SIZE_MB: int = 16
1718

1819
@staticmethod
1920
def from_os_env() -> 'Environment':
@@ -28,7 +29,8 @@ def from_os_env() -> 'Environment':
2829
env.TESTER_MODE_ENABLED = env.TESTER_LISTEN_HOST is not None and env.TESTER_LISTEN_PORT > 0
2930
env.PROXY_DESTINATION = os.getenv('PROXY_DESTINATION',
3031
f'http://{env.TESTER_LISTEN_HOST}:{env.TESTER_LISTEN_PORT}')
31-
env.STORAGE_PATH = str = os.getenv('STORAGE_PATH', os.path.join('.', 'storage'))
32+
env.STORAGE_PATH = os.getenv('STORAGE_PATH', os.path.join('.', 'storage'))
33+
env.HTTP_MAX_REQUEST_SIZE_MB = int(os.getenv('HTTP_MAX_REQUEST_SIZE_MB', 16))
3234
return env
3335

3436
@staticmethod
@@ -49,6 +51,7 @@ def print(self):
4951
print('PROXY_DESTINATION:', self.PROXY_DESTINATION)
5052
print('PROXY_OVERRIDE_HOST_HEADER:', self.PROXY_OVERRIDE_HOST_HEADER)
5153
print('STORAGE_PATH:', self.STORAGE_PATH)
54+
print('HTTP_MAX_REQUEST_SIZE_MB:', self.HTTP_MAX_REQUEST_SIZE_MB)
5255
if self.TESTER_MODE_ENABLED:
5356
print('TESTER_LISTEN_HOST:', self.TESTER_LISTEN_HOST)
5457
print('TESTER_LISTEN_PORT:', self.TESTER_LISTEN_PORT)

proxy_log.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ def __init__(self, phase: ProxyLogPhase, id: int | None = None, start_time: floa
196196
self._response_headers: HttpHeaders | None = None
197197
self._response_body: ContentItem | None = None
198198
self._response_body_mime_type: str | None = None
199-
self.exception: Exception | None = None
199+
self.exception_message: str | None = None
200+
self.exception_type: str | None = None
200201
self.exception_traceback: str | None = None
201202

202203
def mutate(self, phase: ProxyLogPhase) -> 'RequestEntry':
@@ -275,10 +276,10 @@ def to_map(self) -> dict:
275276
encoded_content.update({'mimeType': self._response_body_mime_type})
276277
response['body'] = self._clean_dict(encoded_content)
277278
exception = None
278-
if self.exception is not None:
279+
if self.exception_type is not None:
279280
exception = {
280-
'message': str(self.exception),
281-
'type': str(type(self.exception)),
281+
'message': self.exception_message,
282+
'type': self.exception_type,
282283
'traceback': self.exception_traceback,
283284
}
284285
result = {

proxy_server.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ async def handle_proxy(request: aiohttp.web.Request):
4949
) as response:
5050
return await handel_proxy_response(response, log, proxy_log)
5151
except Exception as e:
52+
print(f"Exception: {e}")
5253
if log is not None:
5354
log = log.mutate(ProxyLogPhase.END)
54-
log.exception = e
55+
log.exception_type = str(type(e))
56+
log.exception_message = str(e)
5557
log.exception_traceback = traceback.format_exc()
5658
proxy_log.put(log)
5759
finally:
@@ -88,7 +90,7 @@ async def storage_garbage_task(app):
8890

8991

9092
def run_proxy_server(environment: Environment, proxy_log: ProxyLog):
91-
app = web.Application()
93+
app = web.Application(client_max_size=environment.HTTP_MAX_REQUEST_SIZE_MB * 1024 ** 2)
9294
app.setdefault('environment', environment)
9395
app.setdefault('proxy_log', proxy_log)
9496
app.router.add_route('*', '/{any:.*}', handle_proxy)

tester_server.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,34 @@ async def handle(request):
5555
})
5656

5757

58-
def run_rest_api_server(host: str, port: int):
59-
app = web.Application()
58+
def run_rest_api_server(environment: Environment):
59+
app = web.Application(client_max_size=environment.HTTP_MAX_REQUEST_SIZE_MB * 1024 ** 2)
6060
app.router.add_route('*', '/{any:.*}', handle)
6161
try:
62-
web.run_app(app, host=host, port=port)
62+
web.run_app(app, host=environment.TESTER_LISTEN_HOST, port=environment.TESTER_LISTEN_PORT)
6363
except KeyboardInterrupt:
6464
app.shutdown()
6565

6666

6767
def run_rest_api_client(tester_destination: str):
6868
async def fetch(session, method, url, body):
69-
print(f'Tester fetch {method} {url} {body}...')
70-
async with session.request(method, url, json=body) as response:
71-
return await response.text()
69+
body_str = ''
70+
if body is str:
71+
body_str = body
72+
elif body is dict:
73+
body_str = json.dumps(body)
74+
if len(body_str) > 128:
75+
body_str = body_str[:128] + '...'
76+
print(f'Tester fetch {method} {url} {body_str}...')
77+
if body is dict:
78+
async with session.request(method, url, json=body) as response:
79+
return await response.text()
80+
else:
81+
headers = {
82+
'content-type': 'text/plain',
83+
}
84+
async with session.request(method, url, data=body, headers=headers) as response:
85+
return await response.text()
7286

7387
async def main():
7488
async with aiohttp.ClientSession() as session:
@@ -86,6 +100,8 @@ async def main():
86100
if random_boolean():
87101
query_parameters_string = '?' + '&'.join(
88102
[f'key_{i}={random.randint(0, 100)}' for i in range(random.randint(1, 8))])
103+
if random.randint(0, 15) < 1:
104+
body = ''.join(random.choices(string.ascii_lowercase, k=2 * 1024 * 1024))
89105
response = await fetch(session, method, f'{tester_destination}/{path}{query_parameters_string}',
90106
body=body)
91107
if len(response) > 150:
@@ -101,7 +117,7 @@ async def main():
101117

102118

103119
def run_rest_api_tester(environment: Environment):
104-
Process(target=run_rest_api_server, args=(environment.TESTER_LISTEN_HOST, environment.TESTER_LISTEN_PORT,)).start()
120+
Process(target=run_rest_api_server, args=(environment,)).start()
105121
time.sleep(2)
106122
for _ in range(1):
107123
Process(target=run_rest_api_client, args=(environment.TESTER_DESTINATION,)).start()

0 commit comments

Comments
 (0)