1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

fix port integer handling

This commit is contained in:
Kelly Brazil
2022-07-22 11:36:23 -07:00
parent 551f4b097d
commit a296ac2e8f

View File

@ -1,5 +1,12 @@
"""jc - JSON Convert URL string parser """jc - JSON Convert URL string parser
Normalized, Encoded, and Decoded versions of the original URL and URL parts
are included in the output. Encoding and Decoding is best effort.
> Note: Do not use the Encoded fields for a URL that has already been
> Encoded. Similarly, do not use the Decoded fields for a URL that has
> already been Decoded.
This parser will work with naked and wrapped URL strings: This parser will work with naked and wrapped URL strings:
- `scheme://host/path` - `scheme://host/path`
@ -7,13 +14,6 @@ This parser will work with naked and wrapped URL strings:
- `<scheme://host/path>` - `<scheme://host/path>`
- `<URL:scheme://host/path>` - `<URL:scheme://host/path>`
Normalized, Encoded, and Decoded versions of the original URL and URL parts
are included in the output.
> Note: Do not use the encoded fields for a URL that has already been
> encoded. Similarly, do not use the decoded fields for a URL that has
> already been decoded.
Usage (cli): Usage (cli):
$ echo "http://example.com/test/path?q1=foo&q2=bar#frag" | jc --url $ echo "http://example.com/test/path?q1=foo&q2=bar#frag" | jc --url
@ -43,7 +43,7 @@ Schema:
"username": string or null, "username": string or null,
"password": string or null, "password": string or null,
"hostname": string or null, "hostname": string or null,
"port": integer or null, "port": integer or null, # [1]
"encoded": { "encoded": {
"url": string, "url": string,
"scheme": string, "scheme": string,
@ -57,7 +57,7 @@ Schema:
"username": string or null, "username": string or null,
"password": string or null, "password": string or null,
"hostname": string or null, "hostname": string or null,
"port": string or null, "port": integer or null, # [1]
}, },
"decoded": { "decoded": {
"url": string, "url": string,
@ -72,13 +72,16 @@ Schema:
"username": string or null, "username": string or null,
"password": string or null, "password": string or null,
"hostname": string or null, "hostname": string or null,
"port": string or null, "port": integer or null, # [1]
} }
} }
[0] Duplicate query-keys will have their values consolidated into the [0] Duplicate query-keys will have their values consolidated into the
array of query-values array of query-values
[1] Invalid port values will be converted to null/None and a warning
message will be printed to `STDERR` if quiet=False
Examples: Examples:
% echo "http://example.com/test/path?q1=foo&q1=bar&q2=baz#frag" \\ % echo "http://example.com/test/path?q1=foo&q1=bar&q2=baz#frag" \\
@ -340,22 +343,26 @@ def parse(
encoded_hostname = quote(normalized.hostname, safe=NETLOC_SAFE) encoded_hostname = quote(normalized.hostname, safe=NETLOC_SAFE)
decoded_hostname = unquote(normalized.hostname) decoded_hostname = unquote(normalized.hostname)
# handle port differently since an encoded port can cause a ValueError # handle port differently since an encoded port can cause a ValueError if it's not an integer
try: try:
if normalized.port: if normalized.port:
normalized_port = normalized.port normalized_port = normalized.port
encoded_port = quote(str(normalized.port), safe=NETLOC_SAFE) encoded_port = int(quote(str(normalized.port), safe=NETLOC_SAFE))
decoded_port = unquote(str(normalized.port)) decoded_port = int(unquote(str(normalized.port)))
except ValueError: except ValueError:
# Non-integer decoded port values can also cause a ValueError # Non-integer decoded port values can also cause a ValueError
# try unquoting, otherwise set to None if it can't be converted
try: try:
if unquoted_parts.port: if unquoted_parts.port:
normalized_port = unquote(str(unquoted_parts.port)) # type: ignore normalized_port = int(unquote(str(unquoted_parts.port)))
encoded_port = quote(str(unquoted_parts.port), safe=NETLOC_SAFE) encoded_port = int(quote(str(unquoted_parts.port), safe=NETLOC_SAFE))
decoded_port = unquote(str(unquoted_parts.port)) decoded_port = int(unquote(str(unquoted_parts.port)))
except ValueError: except ValueError:
if not quiet:
jc.utils.warning_message(['Unable to convert invalid port value. Setting to null.'])
normalized_port = None normalized_port = None
encoded_port = None encoded_port = None
decoded_port = None decoded_port = None