Skip to content

Commit a708407

Browse files
committed
Simplify.
1 parent 8aee33c commit a708407

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

+17-26
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,28 @@ def _add_request_attributes(span, environ):
5050
host = environ.get("HTTP_HOST")
5151
if not host:
5252
host = environ["SERVER_NAME"]
53-
if environ["wsgi.url_scheme"] == "https":
54-
if environ.get("SERVER_PORT", "443") != "443":
55-
host += ":" + environ["SERVER_PORT"]
56-
elif environ.get("SERVER_PORT", "80") != "80":
57-
host += ":" + environ["SERVER_PORT"]
58-
59-
# NOTE: Nonstandard, may (not) include port
53+
port = environ.get("SERVER_PORT")
54+
if port and (
55+
port != "80"
56+
and environ["wsgi.url_scheme"] == "http"
57+
or port != "443"
58+
):
59+
host += ":" + port
60+
61+
# NOTE: Nonstandard
6062
span.set_attribute("http.host", host)
6163

6264
url = environ.get("REQUEST_URI") or environ.get("RAW_URI")
6365

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

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

+18-10
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)