Skip to content

Commit

Permalink
WSGI: Fix port 80 always appended in http.host (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 authored and c24t committed Sep 25, 2019
1 parent 77adc46 commit e14076d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ def _add_request_attributes(span, environ):
if not host:
host = environ["SERVER_NAME"]
port = environ["SERVER_PORT"]
scheme = environ["wsgi.url_scheme"]
if (
port != "80"
and environ["wsgi.url_scheme"] == "http"
or port != "443"
scheme == "http"
and port != "80"
or scheme == "https"
and port != "443"
):
host += ":" + port

Expand Down
16 changes: 13 additions & 3 deletions ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ def validate_url(self, expected_url):
self.assertIn("http.url", attrs)
self.assertEqual(attrs["http.url"], expected_url)
self.assertIn("http.host", attrs)
self.assertEqual(
attrs["http.host"], urlparse(attrs["http.url"]).netloc
)
self.assertEqual(attrs["http.host"], urlparse(expected_url).netloc)

def test_request_attributes_with_partial_raw_uri(self):
self.environ["RAW_URI"] = "/#top"
Expand All @@ -231,6 +229,18 @@ def test_https_uri_port(self):
self.environ["SERVER_PORT"] = "80"
self.validate_url("https://127.0.0.1:80/")

def test_http_uri_port(self):
del self.environ["HTTP_HOST"]
self.environ["SERVER_PORT"] = "80"
self.environ["wsgi.url_scheme"] = "http"
self.validate_url("http://127.0.0.1/")

self.environ["SERVER_PORT"] = "8080"
self.validate_url("http://127.0.0.1:8080/")

self.environ["SERVER_PORT"] = "443"
self.validate_url("http://127.0.0.1:443/")

def test_request_attributes_with_nonstandard_port_and_no_host(self):
del self.environ["HTTP_HOST"]
self.environ["SERVER_PORT"] = "8080"
Expand Down

0 comments on commit e14076d

Please sign in to comment.