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 CDXException and respond with HTTP 400 Bad Request #626

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion pywb/apps/frontendapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
19 changes: 17 additions & 2 deletions pywb/warcserver/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from warcio.recordloader import ArchiveLoadFailed

from pywb.warcserver.index.cdxobject import CDXException
from pywb.warcserver.index.fuzzymatcher import FuzzyMatcher
from pywb.warcserver.resource.responseloader import WARCPathLoader, LiveWebLoader, VideoLoader

Expand Down Expand Up @@ -96,13 +97,27 @@ def __call__(self, params):
content_type, res = handler(cdx_iter, fields, params)
out_headers = {'Content-Type': content_type}

def check_str(lines):
first_line = None
try:
# raise exceptions early so that they can be handled properly
first_line = next(res)
except StopIteration:
pass
except CDXException as e:
errs = dict(last_exc=e)
return None, None, errs

def check_str(first_line, lines):
if first_line is not None:
if isinstance(first_line, six.text_type):
first_line = first_line.encode('utf-8')
yield first_line
for line in lines:
if isinstance(line, six.text_type):
line = line.encode('utf-8')
yield line

return out_headers, check_str(res), errs
return out_headers, check_str(first_line, res), errs


#=============================================================================
Expand Down
8 changes: 8 additions & 0 deletions tests/test_zipnum_auto_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ def test_paged_index_query(self):
assert lines[2] == {"urlkey": "org,iana)/_css/2013.1/fonts/opensans-regular.ttf 20140126200654", "part": "zipnum", "offset": 1692, "length": 235, "lineno": 7}
assert lines[3] == {"urlkey": "org,iana)/_css/2013.1/fonts/opensans-regular.ttf 20140126200816", "part": "zipnum", "offset": 1927, "length": 231, "lineno": 8}

def test_paged_index_query_out_of_range(self):
res = self.testapp.get(
'/testzip/cdx?url=http://iana.org/domains/&matchType=domain&output=json&showPagedIndex=true&pageSize=4&page=10',
expect_errors=True)

assert res.status_code == 400
assert res.json == {"message": "Page 10 invalid: First Page is 0, Last Page is 9"}