Skip to content

Commit 1cbd877

Browse files
committed
Simplify.
1 parent a516087 commit 1cbd877

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,28 @@ def _add_request_attributes(span, environ):
4848
host = environ.get("HTTP_HOST")
4949
if not host:
5050
host = environ["SERVER_NAME"]
51-
if environ["wsgi.url_scheme"] == "https":
52-
if environ.get("SERVER_PORT", "443") != "443":
53-
host += ":" + environ["SERVER_PORT"]
54-
elif environ.get("SERVER_PORT", "80") != "80":
55-
host += ":" + environ["SERVER_PORT"]
56-
57-
# NOTE: Nonstandard, may (not) include port
51+
port = environ.get("SERVER_PORT")
52+
if port and (
53+
port != "80"
54+
and environ["wsgi.url_scheme"] == "http"
55+
or port != "443"
56+
):
57+
host += ":" + port
58+
59+
# NOTE: Nonstandard
5860
span.set_attribute("http.host", host)
5961

6062
url = environ.get("REQUEST_URI") or environ.get("RAW_URI")
6163

62-
if url: # We got something, but is absolute?
63-
# The simplistic ``"://" in url` is not sufficient,
64-
# as that could be contained in the query string.
65-
try:
66-
urlparts = urlparse(url)
67-
except Exception: # pylint:disable=broad-except
64+
if url:
65+
if url[0] == "/":
66+
# We assume that no scheme-relative URLs will be in url here.
67+
# After all, if a request is made to http://myserver//foo, we may get
68+
# //foo which looks like scheme-relative but isn't.
69+
url = environ["wsgi.url_scheme"] + "://" + host + url
70+
elif not url.startswith(environ["wsgi.url_scheme"] + ":"):
71+
# Something fishy is in RAW_URL. Let's fall back to request_uri()
6872
url = wsgiref_util.request_uri(environ)
69-
else:
70-
if url.startswith("//"): # Scheme-relative URL
71-
url = url[2:]
72-
if (
73-
not urlparts.netloc and urlparts.scheme
74-
): # E.g., "http:///?"
75-
scheme, path = url.split("://", 1)
76-
url = scheme + "://" + host + path
77-
elif not urlparts.netloc or not urlparts.scheme:
78-
scheme = environ["wsgi.url_scheme"] + "://"
79-
if not urlparts.netloc:
80-
url = host + url
81-
url = scheme + url
8273
else:
8374
url = wsgiref_util.request_uri(environ)
8475

ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,32 +219,40 @@ def test_request_attributes_with_partial_raw_uri_and_nonstandard_port(
219219
self.environ["SERVER_PORT"] = "8080"
220220
self.validate_url("http://127.0.0.1:8080/?")
221221

222+
def test_https_uri_port(self):
223+
del self.environ["HTTP_HOST"]
224+
self.environ["SERVER_PORT"] = "443"
225+
self.environ["wsgi.url_scheme"] = "https"
226+
self.validate_url("https://127.0.0.1/")
227+
228+
self.environ["SERVER_PORT"] = "8080"
229+
self.validate_url("https://127.0.0.1:8080/")
230+
231+
self.environ["SERVER_PORT"] = "80"
232+
self.validate_url("https://127.0.0.1:80/")
233+
222234
def test_request_attributes_with_nonstandard_port_and_no_host(self):
223235
del self.environ["HTTP_HOST"]
224236
self.environ["SERVER_PORT"] = "8080"
225237
self.validate_url("http://127.0.0.1:8080/")
226238

239+
self.environ["SERVER_PORT"] = "443"
240+
self.validate_url("http://127.0.0.1:443/")
241+
227242
def test_request_attributes_with_nonstandard_port(self):
228243
self.environ["HTTP_HOST"] += ":8080"
229244
self.validate_url("http://127.0.0.1:8080/")
230245

231-
def test_request_attributes_with_scheme_relative_raw_uri(self):
246+
def test_request_attributes_with_faux_scheme_relative_raw_uri(self):
232247
self.environ["RAW_URI"] = "//127.0.0.1/?"
233-
self.validate_url("http://127.0.0.1/?")
234-
235-
def test_request_attributes_with_netlocless_raw_uri(self):
236-
self.environ["RAW_URI"] = "http:///?"
237-
self.validate_url("http://127.0.0.1/?")
248+
self.validate_url("http://127.0.0.1//127.0.0.1/?")
238249

239250
def test_request_attributes_with_pathless_raw_uri(self):
251+
self.environ["PATH_INFO"] = ""
240252
self.environ["RAW_URI"] = "http://hello"
241253
self.environ["HTTP_HOST"] = "hello"
242254
self.validate_url("http://hello")
243255

244-
def test_request_attributes_with_strange_raw_uri(self):
245-
self.environ["RAW_URI"] = "http://?"
246-
self.validate_url("http://127.0.0.1?")
247-
248256
def test_request_attributes_with_full_request_uri(self):
249257
self.environ["HTTP_HOST"] = "127.0.0.1:8080"
250258
self.environ["REQUEST_URI"] = "http://127.0.0.1:8080/?foo=bar#top"

0 commit comments

Comments
 (0)