Skip to content

Commit

Permalink
Introduce rudimentary http error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmark committed Aug 21, 2022
1 parent 2517b1a commit 81d4b40
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import requests
import argparse
from flask import Flask, request, session, g
from flask import Flask, request, session, g, abort
from html_utils import transcode_html

app = Flask(__name__)
session = requests.Session()

HTTP_ERRORS = (403, 404, 500, 503, 504)
ERROR_HEADER = "[[Macproxy Encountered an Error]]"

@app.route("/", defaults={"path": ""}, methods=["GET"])
@app.route("/<path:path>", methods=["GET"])
def get(path):
Expand All @@ -25,7 +28,13 @@ def get(path):
}
if app.config["USER_AGENT"]:
headers["User-Agent"] = app.config["USER_AGENT"]
resp = session.get(url, params=request.args, headers=headers)
try:
resp = session.get(url, params=request.args, headers=headers)
except Exception as e:
return abort(500, ERROR_HEADER + str(e))

if resp.status_code in HTTP_ERRORS:
return abort(resp.status_code)
if "content-type" in resp.headers.keys():
g.content_type = resp.headers["Content-Type"]
if resp.headers["Content-Type"].startswith("text/html"):
Expand All @@ -51,7 +60,13 @@ def post(path):
}
if app.config["USER_AGENT"]:
headers["User-Agent"] = app.config["USER_AGENT"]
resp = session.post(url, data=request.form, headers=headers, allow_redirects=True)
try:
resp = session.post(url, data=request.form, headers=headers, allow_redirects=True)
except Exception as e:
return abort(500, ERROR_HEADER + str(e))

if resp.status_code in HTTP_ERRORS:
return abort(resp.status_code)
if "content-type" in resp.headers.keys():
g.content_type = resp.headers["Content-Type"]
if resp.headers["Content-Type"].startswith("text/html"):
Expand Down

0 comments on commit 81d4b40

Please sign in to comment.