Skip to content

Commit f734e77

Browse files
mariodevbeniwohli
authored andcommitted
Add support for ipv6 address format when parsing url [#648] (#649)
* Add support for ipv6 address format when parsing url * Refactored url parsing to leverage ParseResult object
1 parent 62f12e3 commit f734e77

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

elasticapm/utils/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,21 @@ def is_master_process():
102102

103103

104104
def get_url_dict(url):
105-
scheme, netloc, path, params, query, fragment = compat.urlparse.urlparse(url)
106-
if ":" in netloc:
107-
hostname, port = netloc.split(":")
108-
else:
109-
hostname, port = (netloc, None)
105+
parse_result = compat.urlparse.urlparse(url)
106+
110107
url_dict = {
111108
"full": encoding.keyword_field(url),
112-
"protocol": scheme + ":",
113-
"hostname": encoding.keyword_field(hostname),
114-
"pathname": encoding.keyword_field(path),
109+
"protocol": parse_result.scheme + ":",
110+
"hostname": encoding.keyword_field(parse_result.hostname),
111+
"pathname": encoding.keyword_field(parse_result.path),
115112
}
113+
114+
port = None if parse_result.port is None else str(parse_result.port)
115+
116116
if port:
117117
url_dict["port"] = port
118-
if query:
119-
url_dict["search"] = encoding.keyword_field("?" + query)
118+
if parse_result.query:
119+
url_dict["search"] = encoding.keyword_field("?" + parse_result.query)
120120
return url_dict
121121

122122

tests/utils/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ def test_get_url_dict():
8989
"search": "?de",
9090
"full": "https://example.com:443/a/b/c?de",
9191
},
92+
"https://[::ffff:a9fe:a9fe]/a/b/c?de": {
93+
"protocol": "https:",
94+
"hostname": "::ffff:a9fe:a9fe",
95+
"pathname": "/a/b/c",
96+
"search": "?de",
97+
"full": "https://[::ffff:a9fe:a9fe]/a/b/c?de",
98+
},
99+
"http://[::ffff:a9fe:a9fe]:80/a/b/c?de": {
100+
"protocol": "http:",
101+
"hostname": "::ffff:a9fe:a9fe",
102+
"port": "80",
103+
"pathname": "/a/b/c",
104+
"search": "?de",
105+
"full": "http://[::ffff:a9fe:a9fe]:80/a/b/c?de",
106+
},
92107
}
93108
for url, expected in data.items():
94109
assert get_url_dict(url) == expected

0 commit comments

Comments
 (0)