|
2 | 2 | from functools import partial |
3 | 3 |
|
4 | 4 | import sentry_sdk |
5 | | -from sentry_sdk._werkzeug import get_host, _get_headers |
| 5 | +from werkzeug.datastructures import EnvironHeaders |
| 6 | +from werkzeug.wsgi import get_host |
6 | 7 | from sentry_sdk.api import continue_trace |
7 | 8 | from sentry_sdk.consts import OP |
8 | 9 | from sentry_sdk.scope import should_send_default_pii |
@@ -62,9 +63,18 @@ def get_request_url(environ, use_x_forwarded_for=False): |
62 | 63 | path_info = environ.get("PATH_INFO", "").lstrip("/") |
63 | 64 | path = f"{script_name}/{path_info}" |
64 | 65 |
|
| 66 | + if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ: |
| 67 | + host = environ["HTTP_X_FORWARDED_HOST"] |
| 68 | + if environ.get("wsgi.url_scheme") == "http" and host.endswith(":80"): |
| 69 | + host = host[:-3] |
| 70 | + elif environ.get("wsgi.url_scheme") == "https" and host.endswith(":443"): |
| 71 | + host = host[:-4] |
| 72 | + else: |
| 73 | + host = get_host(environ) |
| 74 | + |
65 | 75 | return "%s://%s/%s" % ( |
66 | 76 | environ.get("wsgi.url_scheme"), |
67 | | - get_host(environ, use_x_forwarded_for), |
| 77 | + host, |
68 | 78 | wsgi_decoding_dance(path).lstrip("/"), |
69 | 79 | ) |
70 | 80 |
|
@@ -286,7 +296,7 @@ def _make_wsgi_event_processor(environ, use_x_forwarded_for): |
286 | 296 | query_string = environ.get("QUERY_STRING") |
287 | 297 | method = environ.get("REQUEST_METHOD") |
288 | 298 | env = dict(_get_environ(environ)) |
289 | | - headers = _filter_headers(dict(_get_headers(environ))) |
| 299 | + headers = _filter_headers(dict(EnvironHeaders(environ))) |
290 | 300 |
|
291 | 301 | def event_processor(event, hint): |
292 | 302 | # type: (Event, Dict[str, Any]) -> Event |
|
0 commit comments