6
6
from multidict import CIMultiDict
7
7
8
8
from environment import Environment
9
- from http_helper import parse_query_params , get_host_from_url , http_remove_not_forwardable_headers
9
+ from http_helper import parse_query_params , get_host_with_port_from_url , http_remove_not_forwardable_headers , url_replace_host
10
10
from proxy_log import ProxyLog , ProxyLogPhase , HttpHeaders , ContentStorage , RequestEntry
11
11
12
12
@@ -16,15 +16,19 @@ async def handle_proxy(request: aiohttp.web.Request):
16
16
try :
17
17
environment : Environment = request .app .get ('environment' )
18
18
destination = environment .PROXY_DESTINATION
19
- host_from_destination = get_host_from_url (destination )
19
+ host_from_destination = get_host_with_port_from_url (destination )
20
20
forward_headers = CIMultiDict (request .headers )
21
21
if environment .PROXY_OVERRIDE_HOST_HEADER :
22
22
forward_headers ['host' ] = host_from_destination
23
23
request_headers = HttpHeaders .create_by (forward_headers )
24
24
http_remove_not_forwardable_headers (forward_headers )
25
+ request_query_params = request .query_string
26
+ if request_query_params :
27
+ request_query_params = f'?{ request_query_params } '
28
+ destination_url = f'{ destination } { request .path } { request_query_params } '
25
29
log = proxy_log .new_entry (
26
30
method = request .method ,
27
- url = str (request . url ), headers = request_headers ,
31
+ url = url_replace_host ( str (destination_url ), forward_headers [ 'host' ] ), headers = request_headers ,
28
32
query_parameters = parse_query_params (str (request .url ))
29
33
)
30
34
async with (aiohttp .ClientSession () as session ):
@@ -35,10 +39,6 @@ async def handle_proxy(request: aiohttp.web.Request):
35
39
log = log .mutate (ProxyLogPhase .REQUEST_BODY_READING )
36
40
proxy_log .put (log )
37
41
data = await request .read ()
38
- request_query_params = request .query_string
39
- if request_query_params :
40
- request_query_params = f'?{ request_query_params } '
41
- destination_url = f'{ destination } { request .path } { request_query_params } '
42
42
print (f'{ request .method } { request .url } -> { destination_url } ...' )
43
43
log = log .mutate (ProxyLogPhase .REQUEST_FORWARD )
44
44
log .forward_destination = destination_url
0 commit comments