Skip to content

Commit

Permalink
replace uses of url_encode with urllib.parse.urlencode
Browse files Browse the repository at this point in the history
need a wrapper to handle MultiDict and drop None
  • Loading branch information
davidism committed Mar 2, 2023
1 parent 47cf613 commit 6613fb9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/simplewiki/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from os import path
from urllib.parse import quote
from urllib.parse import urlencode

import creoleparser
from genshi import Stream
from genshi.template import TemplateLoader
from werkzeug.local import Local
from werkzeug.local import LocalManager
from werkzeug.urls import url_encode
from werkzeug.utils import cached_property
from werkzeug.wrappers import Request as BaseRequest
from werkzeug.wrappers import Response as BaseResponse
Expand Down Expand Up @@ -60,7 +60,7 @@ def href(*args, **kw):
for idx, arg in enumerate(args):
result.append(f"{'/' if idx else ''}{quote(arg)}")
if kw:
result.append(f"?{url_encode(kw)}")
result.append(f"?{urlencode(kw)}")
return "".join(result)


Expand Down
16 changes: 13 additions & 3 deletions src/werkzeug/_urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import annotations

import typing as t
from urllib.parse import quote
from urllib.parse import urlencode

from werkzeug.datastructures import iter_multi_items

# RFC 3986 characters, excluding &= which appear in query string
_always_safe_rfc3986 = "$!'()*+,;"
_rfc3986 = "$!'()*+,;"


def _quote(
Expand All @@ -12,5 +16,11 @@ def _quote(
encoding: str | None = None,
errors: str | None = None,
) -> str:
safe = f"{_always_safe_rfc3986}{safe}"
return quote(string, safe=safe, encoding=encoding, errors=errors)
return quote(string, safe=f"{_rfc3986}{safe}", encoding=encoding, errors=errors)


def _urlencode(
query: t.Mapping[str, str] | t.Iterable[tuple[str, str]], encoding: str = "utf-8"
):
items = [x for x in iter_multi_items(query) if x[1] is not None]
return urlencode(items, safe=f"{_rfc3986}/", encoding=encoding)
4 changes: 2 additions & 2 deletions src/werkzeug/routing/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from .._internal import _to_str
from .._internal import _wsgi_decoding_dance
from .._urls import _quote
from .._urls import _urlencode
from ..datastructures import ImmutableDict
from ..datastructures import MultiDict
from ..exceptions import BadHost
from ..exceptions import HTTPException
from ..exceptions import MethodNotAllowed
from ..exceptions import NotFound
from ..urls import url_encode
from ..urls import url_join
from ..wsgi import get_host
from .converters import DEFAULT_CONVERTERS
Expand Down Expand Up @@ -737,7 +737,7 @@ def get_default_redirect(

def encode_query_args(self, query_args: t.Union[t.Mapping[str, t.Any], str]) -> str:
if not isinstance(query_args, str):
return url_encode(query_args, self.map.charset)
return _urlencode(query_args, encoding=self.map.charset)
return query_args

def make_redirect_url(
Expand Down
15 changes: 8 additions & 7 deletions src/werkzeug/routing/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from .._internal import _to_bytes
from .._urls import _quote
from ..urls import url_encode
from .._urls import _urlencode
from ..datastructures import iter_multi_items
from .converters import ValidationError

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -575,12 +576,12 @@ def get_converter(
return self.map.converters[converter_name](self.map, *args, **kwargs)

def _encode_query_vars(self, query_vars: t.Mapping[str, t.Any]) -> str:
return url_encode(
query_vars,
charset=self.map.charset,
sort=self.map.sort_parameters,
key=self.map.sort_key,
)
items = iter_multi_items(query_vars)

if self.map.sort_parameters:
items = sorted(items, key=self.map.sort_key)

return _urlencode(items, encoding=self.map.charset)

def _parse_rule(self, rule: str) -> t.Iterable[RulePart]:
content = ""
Expand Down
6 changes: 3 additions & 3 deletions src/werkzeug/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ._internal import _make_encode_wrapper
from ._internal import _wsgi_decoding_dance
from ._internal import _wsgi_encoding_dance
from ._urls import _urlencode
from .datastructures import Authorization
from .datastructures import CallbackDict
from .datastructures import CombinedMultiDict
Expand All @@ -36,7 +37,6 @@
from .sansio.multipart import MultipartEncoder
from .sansio.multipart import Preamble
from .urls import iri_to_uri
from .urls import url_encode
from .urls import url_fix
from .utils import cached_property
from .utils import get_content_type
Expand Down Expand Up @@ -667,7 +667,7 @@ def query_string(self) -> str:
"""
if self._query_string is None:
if self._args is not None:
return url_encode(self._args, charset=self.charset)
return _urlencode(self._args, encoding=self.charset)
return ""
return self._query_string

Expand Down Expand Up @@ -760,7 +760,7 @@ def get_environ(self) -> "WSGIEnvironment":
)
content_type = f'{mimetype}; boundary="{boundary}"'
elif mimetype == "application/x-www-form-urlencoded":
form_encoded = url_encode(self.form, charset=self.charset).encode("ascii")
form_encoded = _urlencode(self.form, encoding=self.charset).encode("ascii")
content_length = len(form_encoded)
input_stream = BytesIO(form_encoded)
else:
Expand Down

0 comments on commit 6613fb9

Please sign in to comment.