Skip to content

Commit

Permalink
Merge pull request #19 from smartondev/17-request-url-ws-fix
Browse files Browse the repository at this point in the history
fix: fix request url in request log #17
  • Loading branch information
kamarton authored May 25, 2024
2 parents ae62119 + d567cc4 commit 4f8bc9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
15 changes: 13 additions & 2 deletions http_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ def get_mime_type_from_content_type(content_type: str | None) -> str | None:
return content_type.split(';')[0].strip()


def get_host_from_url(url: str) -> str:
return urlparse(url).hostname
def get_host_with_port_from_url(url: str) -> str:
parsed = urlparse(url)
port = ''
if parsed.port is not None:
port = f':{parsed.port}'
return f'{parsed.hostname}{port}'


def url_replace_host(url: str, new_host: str | None) -> str:
if new_host is None:
return url
parsed = urlparse(url)
return parsed._replace(netloc=new_host).geturl()


def parse_query_params(url: str) -> list[tuple[str, str]] | None:
Expand Down
14 changes: 7 additions & 7 deletions proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from multidict import CIMultiDict

from environment import Environment
from http_helper import parse_query_params, get_host_from_url, http_remove_not_forwardable_headers
from http_helper import parse_query_params, get_host_with_port_from_url, http_remove_not_forwardable_headers, url_replace_host
from proxy_log import ProxyLog, ProxyLogPhase, HttpHeaders, ContentStorage, RequestEntry


Expand All @@ -16,15 +16,19 @@ async def handle_proxy(request: aiohttp.web.Request):
try:
environment: Environment = request.app.get('environment')
destination = environment.PROXY_DESTINATION
host_from_destination = get_host_from_url(destination)
host_from_destination = get_host_with_port_from_url(destination)
forward_headers = CIMultiDict(request.headers)
if environment.PROXY_OVERRIDE_HOST_HEADER:
forward_headers['host'] = host_from_destination
request_headers = HttpHeaders.create_by(forward_headers)
http_remove_not_forwardable_headers(forward_headers)
request_query_params = request.query_string
if request_query_params:
request_query_params = f'?{request_query_params}'
destination_url = f'{destination}{request.path}{request_query_params}'
log = proxy_log.new_entry(
method=request.method,
url=str(request.url), headers=request_headers,
url=url_replace_host(str(destination_url), forward_headers['host']), headers=request_headers,
query_parameters=parse_query_params(str(request.url))
)
async with (aiohttp.ClientSession() as session):
Expand All @@ -35,10 +39,6 @@ async def handle_proxy(request: aiohttp.web.Request):
log = log.mutate(ProxyLogPhase.REQUEST_BODY_READING)
proxy_log.put(log)
data = await request.read()
request_query_params = request.query_string
if request_query_params:
request_query_params = f'?{request_query_params}'
destination_url = f'{destination}{request.path}{request_query_params}'
print(f'{request.method} {request.url} -> {destination_url}...')
log = log.mutate(ProxyLogPhase.REQUEST_FORWARD)
log.forward_destination = destination_url
Expand Down

0 comments on commit 4f8bc9a

Please sign in to comment.