From 90912b9190d2f8902590a32c256654bfbbae0006 Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Fri, 12 Mar 2021 13:42:19 +0100 Subject: [PATCH 1/2] FrontendApp: forward HTTP status of CDX backend to allow clients to handle errors more easily --- pywb/apps/frontendapp.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pywb/apps/frontendapp.py b/pywb/apps/frontendapp.py index c3ece2940..69382df2f 100644 --- a/pywb/apps/frontendapp.py +++ b/pywb/apps/frontendapp.py @@ -404,10 +404,12 @@ def serve_cdx(self, environ, coll='$root'): try: res = requests.get(cdx_url, stream=True) + status_line = '{} {}'.format(res.status_code, res.reason) content_type = res.headers.get('Content-Type') return WbResponse.bin_stream(StreamIter(res.raw), - content_type=content_type) + content_type=content_type, + status=status_line) except Exception as e: return WbResponse.text_response('Error: ' + str(e), status='400 Bad Request') From 0eb475df9d68f73a7deb8728e79a50e65bfa529f Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Mon, 22 Mar 2021 11:42:50 +0100 Subject: [PATCH 2/2] WarcServer: keep the HTTP status lines short - append the exception message only if the status isn't a string (WbException and inherited classes already have nice status string) - avoid overlong status lines, eg. HTTP/1.1 404 Not Found No Captures found for: https://very-long.url/... --- pywb/warcserver/basewarcserver.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pywb/warcserver/basewarcserver.py b/pywb/warcserver/basewarcserver.py index 0ef5f448e..f1d6b01b2 100644 --- a/pywb/warcserver/basewarcserver.py +++ b/pywb/warcserver/basewarcserver.py @@ -141,6 +141,9 @@ def send_error(self, errs, start_response, out_headers['ResErrors'] = res[0] message = message.encode('utf-8') - message = str(status) + ' ' + message + if isinstance(status, str): + message = status + else: + message = str(status) + ' ' + message start_response(message, list(out_headers.items())) return res