Skip to content

Fix HTTPFileSystem isdir downloads the whole file issue #1889

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

Merged
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
26 changes: 18 additions & 8 deletions fsspec/implementations/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,24 @@ async def _ls_real(self, url, detail=True, **kwargs):
session = await self.set_session()
async with session.get(self.encode_url(url), **self.kwargs) as r:
self._raise_not_found_for_status(r, url)
try:
text = await r.text()
if self.simple_links:
links = ex2.findall(text) + [u[2] for u in ex.findall(text)]
else:
links = [u[2] for u in ex.findall(text)]
except UnicodeDecodeError:
links = [] # binary, not HTML

if "Content-Type" in r.headers:
mimetype = r.headers["Content-Type"].partition(";")[0]
else:
mimetype = None

if mimetype in ("text/html", None):
try:
text = await r.text(errors="ignore")
if self.simple_links:
links = ex2.findall(text) + [u[2] for u in ex.findall(text)]
else:
links = [u[2] for u in ex.findall(text)]
except UnicodeDecodeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://www.w3schools.com/html/html_charset.asp , although utf8 and ascii are far dominant, other encodings are allowed and still in use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That exception block was already there in the original code. While I'm certainly not an expert on Python's string decoding, it seems like UnicodeDecodeError is being thrown in any case where a string or sequence of bytes can't be decoded according to the given encoding: https://wiki.python.org/moin/UnicodeDecodeError so the catch seems appropriate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using errors='ignore' should be the right thing to do here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stated purpose of catching the exception is to determine if the content is binary or HTML, would ignoring errors make sense in that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data could be text, with HTML and links, but not UTF8

links = [] # binary, not HTML
else:
links = []

out = set()
parts = urlparse(url)
for l in links:
Expand Down
5 changes: 5 additions & 0 deletions fsspec/implementations/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def test_glob_return_subfolders(server):


def test_isdir(server):
h = fsspec.filesystem("http", headers={"give_mimetype": "true"})
assert h.isdir(server.address + "/index/")
assert not h.isdir(server.realfile)
assert not h.isdir(server.address + "doesnotevenexist")

h = fsspec.filesystem("http")
assert h.isdir(server.address + "/index/")
assert not h.isdir(server.realfile)
Expand Down
Loading