8000 fix: Expose URL in wsgi middleware · etherscan-io/sentry-python@84a9a4b · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 84a9a4b

Browse files
committed
fix: Expose URL in wsgi middleware
1 parent 9f15a40 commit 84a9a4b

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

sentry_sdk/integrations/_wsgi_common.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ def extract_into_event(self, event):
1616

1717
content_length = self.content_length()
1818
request_info = event.setdefault("request", {})
19-
try:
20-
request_info["url"] = self.url()
21-
except Exception:
22-
# Django sometimes crashes with KeyError trying to build an
23-
# absolute URL
24-
pass
2519

2620
if _should_send_default_pii():
2721
request_info["cookies"] = dict(self.cookies())
@@ -56,9 +50,6 @@ def content_length(self):
5650
except ValueError:
5751
return 0
5852

59-
def url(self):
60-
raise NotImplementedError()
61-
6253
def cookies(self):
6354
raise NotImplementedError()
6455

sentry_sdk/integrations/django/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ def _got_request_exception(request=None, **kwargs):
159159

160160

161161
class DjangoRequestExtractor(RequestExtractor):
162-
def url(self):
163-
return self.request.build_absolute_uri(self.request.path)
164-
165162
def env(self):
166163
return self.request.META
167164

sentry_sdk/integrations/flask.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ def _request_started(sender, **kwargs):
8585

8686

8787
class FlaskRequestExtractor(RequestExtractor):
88-
def url(self):
89-
return "%s://%s%s" % (self.request.scheme, self.request.host, self.request.path)
90-
9188
def env(self):
9289
return self.request.environ
9390

sentry_sdk/integrations/wsgi.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,51 @@
22

33
from sentry_sdk.hub import Hub, _should_send_default_pii
44
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
66
from sentry_sdk.integrations._wsgi_common import _filter_headers
77

88

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+
950
class SentryWsgiMiddleware(object):
1051
__sl 6D4E ots__ = ("app",)
1152

@@ -139,6 +180,9 @@ def event_processor(event, hint):
139180
if "ip_address" not in user_info:
140181
user_info["ip_address"] = get_client_ip(environ)
141182

183+
if "url" not in request_info:
184+
request_info["url"] = get_request_url(environ)
185+
142186
if "query_string" not in request_info:
143187
request_info["query_string"] = environ.get("QUERY_STRING")
144188

tests/integrations/wsgi/test_wsgi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_basic(sentry_init, crashing_app, capture_events):
3030
"headers": {"Content-Length": "0", "Content-Type": "", "Host": "localhost"},
3131
"method": "GET",
3232
"query_string": "",
33+
"url": "http://localhost/",
3334
}
3435

3536

0 commit comments

Comments
 (0)
0