Skip to content

Commit 98e19be

Browse files
authored
If a HEAD request raises an exception fallback to a GET (#185)
* If a HEAD request raises an exception fallback to a GET * Fix it better
1 parent 7534de3 commit 98e19be

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

parfive/tests/test_downloader.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,3 +869,16 @@ def test_invalid_server_checksum(httpserver, tmpdir, caplog, checksum):
869869

870870
assert len(f.errors) == 0
871871
assert "Got invalid checksum:" in caplog.messages[0]
872+
873+
874+
@pytest.mark.parametrize(
875+
"url", ["https://gong2.nso.edu/oQR/zqs/201912/mrzqs191231/mrzqs191231t2304c2225_011.fits.gz"]
876+
)
877+
@pytest.mark.allow_hosts(True)
878+
def test_problematic_http_urls(url, tmpdir):
879+
"""
880+
This test checks that certain URLs which have caused trouble continue to work.
881+
"""
882+
res = Downloader.simple_download([url], path=tmpdir)
883+
assert len(res) == 1, res.errors
884+
assert not res.errors

parfive/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,14 @@ async def session_head_or_get(session: aiohttp.ClientSession, url: str, **kwargs
333333
Try and make a HEAD request to the resource and fallback to a get
334334
request if that fails.
335335
"""
336-
async with session.head(url, **kwargs) as resp:
337-
if resp.status == 200:
338-
yield resp
339-
return
336+
try:
337+
async with session.head(url, **kwargs) as resp:
338+
if resp.status == 200:
339+
yield resp
340+
return
341+
# Catch the situation where the server just ignores the HEAD request
342+
except aiohttp.client_exceptions.ServerDisconnectedError:
343+
pass
340344

341345
async with session.get(url, **kwargs) as resp:
342346
yield resp

0 commit comments

Comments
 (0)