Skip to content

Commit

Permalink
Feat #437 urlencode 和 urldecode 支持指定编码
Browse files Browse the repository at this point in the history
  • Loading branch information
a76yyyy committed Jul 15, 2023
1 parent 17198d7 commit 94c7a48
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
39 changes: 38 additions & 1 deletion libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
b2a_qp, b2a_uu, crc32, crc_hqx)
from email.mime.text import MIMEText
from hashlib import sha1
from typing import Any, Iterable, Mapping, Tuple, Union
from urllib import parse as urllib_parse

import charset_normalizer
Expand All @@ -30,7 +31,7 @@
from faker import Faker
from jinja2.filters import do_float, do_int
from jinja2.runtime import Undefined
from jinja2.utils import generate_lorem_ipsum
from jinja2.utils import generate_lorem_ipsum, url_quote
from requests.utils import get_encoding_from_headers
from tornado import gen, httpclient

Expand Down Expand Up @@ -338,6 +339,41 @@ def conver2unicode(value, html_unescape=False):
tmp = html.unescape(tmp)
return tmp

def urlencode_with_encoding(
value: Union[str, Mapping[str, Any], Iterable[Tuple[str, Any]]],
encoding: str = "utf-8",
for_qs: bool = False,
) -> str:
"""Quote data for use in a URL path or query using UTF-8.
Basic wrapper around :func:`urllib.parse.quote` when given a
string, or :func:`urllib.parse.urlencode` for a dict or iterable.
:param value: Data to quote. A string will be quoted directly. A
dict or iterable of ``(key, value)`` pairs will be joined as a
query string.
:param encoding: The encoding to use for quoted strings.
:param for_qs: If ``True``, quote ``/`` as ``%2F``. If ``False``,
leave slashes unquoted. Defaults to ``False``.
When given a string, "/" is not quoted. HTTP servers treat "/" and
"%2F" equivalently in paths. If you need quoted slashes, use the
``|replace("/", "%2F")`` filter.
.. versionadded:: 2.7
"""
if isinstance(value, str) or not isinstance(value, Iterable):
return url_quote(value, charset=encoding, for_qs=for_qs)

if isinstance(value, dict):
items: Iterable[Tuple[str, Any]] = value.items()
else:
items = value # type: ignore

return "&".join(
f"{url_quote(k, for_qs=True)}={url_quote(v, for_qs=True)}" for k, v in items
)

def to_bool(value):
''' return a bool for the arg '''
if value is None or isinstance(value, bool):
Expand Down Expand Up @@ -802,6 +838,7 @@ def _aes_decrypt(word:str, key:str, mode='CBC', iv:str=None, input_format='base6
'bool': to_bool,
'utf8': utf8,
'unicode': conver2unicode,
'urlencode': urlencode_with_encoding,
'quote_chinese': quote_chinese,
# binascii
'b2a_hex': b2a_hex,
Expand Down
7 changes: 6 additions & 1 deletion web/handlers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ async def post(self):
Rtv = {}
try:
content = self.get_argument("content", "")
Rtv[u"转换后"] = urllib.parse.unquote(content)
encoding = self.get_argument("encoding", "utf-8")
unquote_plus = self.get_argument("unquote_plus", "false")
if strtobool(unquote_plus):
Rtv[u"转换后"] = urllib.parse.unquote_plus(content, encoding=encoding)
else:
Rtv[u"转换后"] = urllib.parse.unquote(content, encoding=encoding)
Rtv[u"状态"] = "200"
except Exception as e:
Rtv[u"状态"] = str(e)
Expand Down
2 changes: 1 addition & 1 deletion web/static/coffee/har/entry_editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ define (require, exports, module) ->
headers: [],
cookies: [],
postData: {
text: "content="
text: "unquote_plus=false&encoding=utf-8&content="
}
},
response: {},
Expand Down
2 changes: 1 addition & 1 deletion web/static/har/entry_editor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions web/tpl/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ <h2>
<a href="/util/urldecode" target="_blank" rel="nofollow">api://util/urldecode</a>
<button class="btn hljs-button" data-clipboard-target="#api_url3">复制</button>
</td>
<td>content</td>
<td>unquote_plus,encoding,content</td>
<td></td>
<td>要转码的内容</td>
<td>unquote_plus: 是否将加号(+)解码为空格( ), <br>默认为 False, 仅在 encoding 为 utf-8 时生效. <br>encoding: 指定编码, 默认为 utf-8<br>content:要转码的 urlencoded 内容, <br>如果 content 为非 utf-8 编码的 urlencoded 字符串, 请再次进行 urlencode 编码后再传入</td>

<td id="api_exam3" class="showbut">
<div class="autowrap">
Expand Down Expand Up @@ -341,6 +341,13 @@ <h2>
<td class="autowrap">123%E4%B8%AD%E6%96%87QAQ&amp;amp;/:</td>
</tr>

<tr>
<td>urlencode(value,encoding="utf-8", for_qs=False)</td>
<td>将 value 字符串进行url编码, <br>encoding: 编码, 默认为 utf-8, <br>for_qs: 是否将斜杠 / 编码为 %2F, 将 %20 替换为 +, 默认为 False</td>
<td>{{ urlencode("http://www.baidu.com?name=张三&age=18 ") }}</td>
<td class="autowrap">http%3A//www.baidu.com%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D18%20</td>
</tr>

<tr>
<td>b2a_hex(data[, sep[, bytes_per_sep=1]])</td>
<td>将字节流 data 转为十六进制表示, <br>sep: 分隔符, 默认为空<br>bytes_per_sep: 每隔多少个字节添加一个分隔符, 默认为1</td>
Expand Down Expand Up @@ -746,13 +753,6 @@ <h2>
<td class="autowrap">123 456</td>
</tr>

<tr>
<td>urlencode</td>
<td>将字符串进行url编码</td>
<td>{{ "http://www.baidu.com?name=张三&age=18" | urlencode }}</td>
<td class="autowrap">http%3A//www.baidu.com%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D18</td>
</tr>

<tr>
<td>upper</td>
<td>将字符串转为大写</td>
Expand Down

0 comments on commit 94c7a48

Please sign in to comment.