|
2 | 2 | import traceback |
3 | 3 | from urllib.parse import urlparse |
4 | 4 |
|
5 | | -from django.conf import settings |
6 | | - |
7 | | -ALLOWED_CONTENT_TYPES = [ |
8 | | - "application/json", |
9 | | - "multipart/form-data", |
10 | | - "text/html", |
11 | | - "text/plain", |
12 | | - "", |
13 | | - None, |
14 | | -] |
| 5 | +from .utils import get_encoding, is_content_admissible_for_saving |
15 | 6 |
|
16 | 7 |
|
17 | 8 | class DatabaseOutgoingRequestsHandler(logging.Handler): |
18 | 9 | def emit(self, record): |
19 | | - from .models import OutgoingRequestsLogConfig |
| 10 | + from .models import OutgoingRequestsLog, OutgoingRequestsLogConfig |
20 | 11 |
|
21 | 12 | config = OutgoingRequestsLogConfig.get_solo() |
22 | 13 |
|
23 | | - if config.save_to_db or settings.LOG_OUTGOING_REQUESTS_DB_SAVE: |
24 | | - from .models import OutgoingRequestsLog |
25 | | - |
26 | | - trace = None |
| 14 | + if config.save_logs_enabled: |
| 15 | + trace = "" |
27 | 16 |
|
28 | 17 | # skip requests not coming from the library requests |
29 | 18 | if not record or not record.getMessage() == "Outgoing request": |
30 | 19 | return |
31 | 20 |
|
32 | | - # skip requests with non-allowed content |
33 | | - request_content_type = record.req.headers.get("Content-Type", "") |
34 | | - response_content_type = record.res.headers.get("Content-Type", "") |
35 | | - |
36 | | - if not ( |
37 | | - request_content_type in ALLOWED_CONTENT_TYPES |
38 | | - and response_content_type in ALLOWED_CONTENT_TYPES |
39 | | - ): |
40 | | - return |
41 | | - |
42 | | - safe_req_headers = record.req.headers.copy() |
| 21 | + scrubbed_req_headers = record.req.headers.copy() |
43 | 22 |
|
44 | | - if "Authorization" in safe_req_headers: |
45 | | - safe_req_headers["Authorization"] = "***hidden***" |
| 23 | + if "Authorization" in scrubbed_req_headers: |
| 24 | + scrubbed_req_headers["Authorization"] = "***hidden***" |
46 | 25 |
|
47 | 26 | if record.exc_info: |
48 | 27 | trace = traceback.format_exc() |
49 | 28 |
|
50 | 29 | parsed_url = urlparse(record.req.url) |
51 | 30 | kwargs = { |
52 | 31 | "url": record.req.url, |
53 | | - "hostname": parsed_url.hostname, |
| 32 | + "hostname": parsed_url.netloc, |
54 | 33 | "params": parsed_url.params, |
55 | 34 | "status_code": record.res.status_code, |
56 | 35 | "method": record.req.method, |
57 | | - "req_content_type": record.req.headers.get("Content-Type", ""), |
58 | | - "res_content_type": record.res.headers.get("Content-Type", ""), |
59 | 36 | "timestamp": record.requested_at, |
60 | 37 | "response_ms": int(record.res.elapsed.total_seconds() * 1000), |
61 | | - "req_headers": self.format_headers(safe_req_headers), |
| 38 | + "req_headers": self.format_headers(scrubbed_req_headers), |
62 | 39 | "res_headers": self.format_headers(record.res.headers), |
63 | 40 | "trace": trace, |
| 41 | + "req_content_type": "", |
| 42 | + "res_content_type": "", |
| 43 | + "req_body": b"", |
| 44 | + "res_body": b"", |
| 45 | + "req_body_encoding": "", |
| 46 | + "res_body_encoding": record.res.encoding, |
64 | 47 | } |
65 | 48 |
|
66 | | - if config.save_body or settings.LOG_OUTGOING_REQUESTS_SAVE_BODY: |
67 | | - kwargs["req_body"] = (record.req.body,) |
68 | | - kwargs["res_body"] = (record.res.json(),) |
| 49 | + if config.save_body_enabled: |
| 50 | + # check request |
| 51 | + if is_content_admissible_for_saving(record.req, config): |
| 52 | + kwargs["req_content_type"] = record.req.headers.get("Content-Type") |
| 53 | + kwargs["req_body"] = record.req.body or b"" |
| 54 | + kwargs["req_body_encoding"] = get_encoding(record.req) |
| 55 | + |
| 56 | + # check response |
| 57 | + if is_content_admissible_for_saving(record.res, config): |
| 58 | + kwargs["res_content_type"] = record.res.headers.get("Content-Type") |
| 59 | + kwargs["res_body"] = record.res.content or b"" |
69 | 60 |
|
70 | 61 | OutgoingRequestsLog.objects.create(**kwargs) |
71 | 62 |
|
|
0 commit comments