Skip to content

Commit 16868c5

Browse files
committed
fix: Dont crash when reading host
1 parent 704c642 commit 16868c5

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

sentry_sdk/integrations/wsgi.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@ def wsgi_decoding_dance(s, charset="utf-8", errors="replace"):
2020

2121
def get_host(environ):
2222
"""Return the host for the given WSGI environment. Yanked from Werkzeug."""
23-
if "HTTP_HOST" in environ:
23+
if environ.get("HTTP_HOST"):
2424
rv = environ["HTTP_HOST"]
2525
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
2626
rv = rv[:-3]
2727
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
2828
rv = rv[:-4]
29-
else:
29+
elif environ.get("SERVER_NAME"):
3030
rv = environ["SERVER_NAME"]
3131
if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in (
3232
("https", "443"),
3333
("http", "80"),
3434
):
3535
rv += ":" + environ["SERVER_PORT"]
36+
else:
37+
# In spite of the WSGI spec, SERVER_NAME might not be present.
38+
rv = "unknown"
3639

3740
return rv
3841

0 commit comments

Comments
 (0)