Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle 404 from the remotes #691

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions examples/http-proxy/http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datetime import datetime, timedelta
import functools
import pathlib
import re
import shlex
import sys

Expand Down Expand Up @@ -49,8 +50,8 @@ async def request_handler(cluster: Cluster, request: web.Request):

instance: HttpService = cluster.instances[request_count % len(cluster.instances)]
request_count += 1
response = await instance.handle_request(request.path_qs)
return web.Response(text=response)
response, status = await instance.handle_request(request.path_qs)
return web.Response(text=response, status=status)


async def run_local_server(cluster: Cluster, port: int):
Expand Down Expand Up @@ -96,9 +97,20 @@ async def start(self):
"-c",
f"echo {shlex.quote(msg)} > /usr/share/nginx/html/index.html",
)
script.run("/bin/rm", "/var/log/nginx/access.log", "/var/log/nginx/error.log")
script.run("/usr/sbin/nginx"),
yield script

async def shutdown(self):
# grab the remote HTTP server's logs on shutdown
# we don't need to display them in any way since the result of those commands
# is written into the example's logfile alongside all other results

script = self._ctx.new_script()
script.run("/bin/cat", "/var/log/nginx/access.log")
script.run("/bin/cat", "/var/log/nginx/error.log")
yield script
johny-b marked this conversation as resolved.
Show resolved Hide resolved

# we don't need to implement `run` since, after the service is started,
# all communication is performed through the VPN

Expand All @@ -110,23 +122,32 @@ async def handle_request(self, query_string: str):
instance_ws = self.network_node.get_websocket_uri(80)
app_key = self.cluster._engine._api_config.app_key

print(f"{TEXT_COLOR_GREEN}sending a remote request to {self}{TEXT_COLOR_DEFAULT}")
print(
f"{TEXT_COLOR_GREEN}sending a remote request '{query_string}' to {self}{TEXT_COLOR_DEFAULT}"
)
ws_session = aiohttp.ClientSession()
async with ws_session.ws_connect(
instance_ws, headers={"Authorization": f"Bearer {app_key}"}
) as ws:
await ws.send_str(f"GET {query_string} HTTP/1.0\n\n")
await ws.send_str(f"GET {query_string} HTTP/1.0\r\n\r\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

headers = await ws.__anext__()
status = int(re.match("^HTTP/1.1 (\d+)", headers.data.decode("ascii")).group(1))
johny-b marked this conversation as resolved.
Show resolved Hide resolved
print(f"{TEXT_COLOR_GREEN}remote headers: {headers.data} {TEXT_COLOR_DEFAULT}")
content = await ws.__anext__()
data: bytes = content.data
print(f"{TEXT_COLOR_GREEN}remote content: {data} {TEXT_COLOR_DEFAULT}")

response_text = data.decode("utf-8")
print(f"{TEXT_COLOR_GREEN}local response: {response_text}{TEXT_COLOR_DEFAULT}")
if status == 200:
content = await ws.__anext__()
data: bytes = content.data
print(f"{TEXT_COLOR_GREEN}remote content: {data} {TEXT_COLOR_DEFAULT}")

response_text = data.decode("utf-8")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, just a stupid question.
Why do we decode headers with ascii and decode content with utf-8?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I'm unsure if headers are utf-8-encoded ...

else:
response_text = None
print(
f"{TEXT_COLOR_GREEN}local response ({status}): {response_text}{TEXT_COLOR_DEFAULT}"
)

await ws_session.close()
return response_text
return response_text, status


# ######## Main application code which spawns the Golem service and the local HTTP server
Expand Down