Skip to content

Commit

Permalink
Merge pull request #358 from ocefpaf/always_quote
Browse files Browse the repository at this point in the history
Always quote params in queries
  • Loading branch information
ocefpaf authored Sep 18, 2024
2 parents 8668d42 + dae2469 commit 252011b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ repos:
- id: add-trailing-comma

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
rev: v0.6.5
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
Expand All @@ -73,7 +73,7 @@ repos:
- id: nb-strip-paths

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.1
rev: 2.2.4
hooks:
- id: pyproject-fmt

Expand Down
16 changes: 15 additions & 1 deletion erddapy/core/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
OptionalList = list[str] | tuple[str] | None


def quote_url(url: str) -> str:
"""Quote URL args for modern ERDDAP servers."""
# We should always quote for queries.
if "?" in url and not "searchFor":
base, unquoted = url.split("?")
url = f"{base}?{parse.quote_plus(unquoted)}"
return url


def _sort_url(url: str) -> str:
"""Return a URL with sorted variables and constraints for hashing."""
parts = parse.urlparse(url)
Expand All @@ -48,7 +57,12 @@ def _sort_url(url: str) -> str:
def _urlopen(url: str, auth: tuple | None = None, **kwargs: dict) -> BinaryIO:
if "timeout" not in kwargs:
kwargs["timeout"] = 60
response = httpx.get(url, follow_redirects=True, auth=auth, **kwargs)
response = httpx.get(
quote_url(url),
follow_redirects=True,
auth=auth,
**kwargs,
)
try:
response.raise_for_status()
except httpx.HTTPError as err:
Expand Down

0 comments on commit 252011b

Please sign in to comment.