|
2 | 2 |
|
3 | 3 | from sentry_sdk.hub import Hub, _should_send_default_pii
|
4 | 4 | from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
|
5 |
| -from sentry_sdk._compat import reraise, implements_iterator |
| 5 | +from sentry_sdk._compat import PY2, reraise, implements_iterator |
6 | 6 | from sentry_sdk.integrations._wsgi_common import _filter_headers
|
7 | 7 |
|
8 | 8 |
|
| 9 | +if PY2: |
| 10 | + |
| 11 | + def wsgi_decoding_dance(s, charset="utf-8", errors="replace"): |
| 12 | + return s.decode(charset, errors) |
| 13 | + |
| 14 | + |
| 15 | +else: |
| 16 | + |
| 17 | + def wsgi_decoding_dance(s, charset="utf-8", errors="replace"): |
| 18 | + return s.encode("latin1").decode(charset, errors) |
| 19 | + |
| 20 | + |
| 21 | +def get_host(environ): |
| 22 | + """Return the host for the given WSGI environment. Yanked from Werkzeug.""" |
| 23 | + if "HTTP_HOST" in environ: |
| 24 | + rv = environ["HTTP_HOST"] |
| 25 | + if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"): |
| 26 | + rv = rv[:-3] |
| 27 | + elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"): |
| 28 | + rv = rv[:-4] |
| 29 | + else: |
| 30 | + rv = environ["SERVER_NAME"] |
| 31 | + if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in ( |
| 32 | + ("https", "443"), |
| 33 | + ("http", "80"), |
| 34 | + ): |
| 35 | + rv += ":" + environ["SERVER_PORT"] |
| 36 | + |
| 37 | + return rv |
| 38 | + |
| 39 | + |
| 40 | +def get_request_url(environ): |
| 41 | + """Return the absolute URL without query string for the given WSGI |
| 42 | + environment.""" |
| 43 | + return "%s://%s/%s" % ( |
| 44 | + environ.get("wsgi.url_scheme"), |
| 45 | + get_host(environ), |
| 46 | + wsgi_decoding_dance(environ.get("PATH_INFO") or "").lstrip("/"), |
| 47 | + ) |
| 48 | + |
| 49 | + |
9 | 50 | class SentryWsgiMiddleware(object):
|
10 | 51 | __slots__ = ("app",)
|
11 | 52 |
|
@@ -139,6 +180,9 @@ def event_processor(event, hint):
|
139 | 180 | if "ip_address" not in user_info:
|
140 | 181 | user_info["ip_address"] = get_client_ip(environ)
|
141 | 182 |
|
| 183 | + if "url" not in request_info: |
| 184 | + request_info["url"] = get_request_url(environ) |
| 185 | + |
142 | 186 | if "query_string" not in request_info:
|
143 | 187 | request_info["query_string"] = environ.get("QUERY_STRING")
|
144 | 188 |
|
|
0 commit comments