jetforce/examples/http_proxy.py

41 lines
1.2 KiB
Python
Raw Normal View History

"""
This is an example of setting up a Gemini server to proxy requests to other
protocols. This application will accept HTTP URLs, download and render them
locally using the `w3m` tool, and render the output to the client as plain text.
"""
import asyncio
import subprocess
import jetforce
2019-08-23 00:53:02 +02:00
from jetforce import Response, Status
2019-08-23 00:53:02 +02:00
app = jetforce.JetforceApplication()
2019-08-23 00:53:02 +02:00
@app.route(scheme="https", strict_hostname=False)
@app.route(scheme="http", strict_hostname=False)
def proxy_request(request):
command = [b"w3m", b"-dump", request.url.encode()]
try:
out = subprocess.run(command, stdout=subprocess.PIPE)
out.check_returncode()
except Exception:
return Response(Status.CGI_ERROR, "Failed to load URL")
else:
return Response(Status.SUCCESS, "text/plain", out.stdout)
if __name__ == "__main__":
2019-08-23 00:53:02 +02:00
args = jetforce.command_line_parser().parse_args()
ssl_context = jetforce.make_ssl_context(
args.hostname, args.certfile, args.keyfile, args.cafile, args.capath
)
server = jetforce.GeminiServer(
host=args.host,
port=args.port,
ssl_context=ssl_context,
hostname=args.hostname,
app=app,
)
asyncio.run(server.run())