Skip to content

Update DefaultStacIO to fix parsing ascii in urls #1566

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ python -m pip install 'pystac[orjson]'
```

If you would like to use a custom `RetryStacIO` class for automatically retrying
network requests when reading with PySTAC, you'll need
network requests when reading with PySTAC, or if you have non-ASCII characters in
your urls you'll need
[`urllib3`](https://urllib3.readthedocs.io/en/stable/):

```shell
Expand Down
21 changes: 16 additions & 5 deletions pystac/stac_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,9 @@ def read_text_from_href(self, href: str) -> str:
"""Reads file as a UTF-8 string.

If ``href`` has a "scheme" (e.g. if it starts with "https://") then this will
use :func:`urllib.request.urlopen` to open the file and read the contents;
otherwise, :func:`open` will be used to open a local file.
use :func:`urllib.request.urlopen` (or func:`urllib3.request` if available)
to open the file and read the contents; otherwise, :func:`open` will be used
to open a local file.

Args:

Expand All @@ -297,9 +298,19 @@ def read_text_from_href(self, href: str) -> str:
if _is_url(href):
try:
logger.debug(f"GET {href} Headers: {self.headers}")
req = Request(href, headers=self.headers)
with urlopen(req) as f:
href_contents = f.read().decode("utf-8")
if HAS_URLLIB3:
with urllib3.request(
"GET",
href,
headers=self.headers,
preload_content=False, # type: ignore
) as f:
href_contents = f.read().decode("utf-8")
else:
req = Request(href, headers=self.headers)
with urlopen(req) as f:
href_contents = f.read().decode("utf-8")

except HTTPError as e:
raise Exception(f"Could not read uri {href}") from e
else:
Expand Down
Loading