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:
@ -1,5 +1,12 @@
|
||||
"""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:
|
||||
|
||||
- `scheme://host/path`
|
||||
@ -7,13 +14,6 @@ This parser will work with naked and wrapped URL strings:
|
||||
- `<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):
|
||||
|
||||
$ echo "http://example.com/test/path?q1=foo&q2=bar#frag" | jc --url
|
||||
@ -43,7 +43,7 @@ Schema:
|
||||
"username": string or null,
|
||||
"password": string or null,
|
||||
"hostname": string or null,
|
||||
"port": integer or null,
|
||||
"port": integer or null, # [1]
|
||||
"encoded": {
|
||||
"url": string,
|
||||
"scheme": string,
|
||||
@ -57,7 +57,7 @@ Schema:
|
||||
"username": string or null,
|
||||
"password": string or null,
|
||||
"hostname": string or null,
|
||||
"port": string or null,
|
||||
"port": integer or null, # [1]
|
||||
},
|
||||
"decoded": {
|
||||
"url": string,
|
||||
@ -72,13 +72,16 @@ Schema:
|
||||
"username": string or null,
|
||||
"password": 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
|
||||
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:
|
||||
|
||||
% 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)
|
||||
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:
|
||||
if normalized.port:
|
||||
normalized_port = normalized.port
|
||||
encoded_port = quote(str(normalized.port), safe=NETLOC_SAFE)
|
||||
decoded_port = unquote(str(normalized.port))
|
||||
encoded_port = int(quote(str(normalized.port), safe=NETLOC_SAFE))
|
||||
decoded_port = int(unquote(str(normalized.port)))
|
||||
|
||||
except ValueError:
|
||||
# Non-integer decoded port values can also cause a ValueError
|
||||
# try unquoting, otherwise set to None if it can't be converted
|
||||
try:
|
||||
if unquoted_parts.port:
|
||||
normalized_port = unquote(str(unquoted_parts.port)) # type: ignore
|
||||
encoded_port = quote(str(unquoted_parts.port), safe=NETLOC_SAFE)
|
||||
decoded_port = unquote(str(unquoted_parts.port))
|
||||
normalized_port = int(unquote(str(unquoted_parts.port)))
|
||||
encoded_port = int(quote(str(unquoted_parts.port), safe=NETLOC_SAFE))
|
||||
decoded_port = int(unquote(str(unquoted_parts.port)))
|
||||
|
||||
except ValueError:
|
||||
if not quiet:
|
||||
jc.utils.warning_message(['Unable to convert invalid port value. Setting to null.'])
|
||||
|
||||
normalized_port = None
|
||||
encoded_port = None
|
||||
decoded_port = None
|
||||
|
Reference in New Issue
Block a user