(
*,
logger: logging.Logger,
split_raw_response,
trace_suffixes: tuple[str, ...],
url: str,
response: bytes,
)
| 115 | |
| 116 | |
| 117 | def log_response_summary( |
| 118 | *, |
| 119 | logger: logging.Logger, |
| 120 | split_raw_response, |
| 121 | trace_suffixes: tuple[str, ...], |
| 122 | url: str, |
| 123 | response: bytes, |
| 124 | ) -> None: |
| 125 | status, headers, body = split_raw_response(response) |
| 126 | host = (urlparse(url).hostname or "").lower() |
| 127 | |
| 128 | if status < 300 and not should_trace_host(host, trace_suffixes): |
| 129 | return |
| 130 | |
| 131 | location = headers.get("location", "") or "-" |
| 132 | server = headers.get("server", "") or "-" |
| 133 | cf_ray = headers.get("cf-ray", "") or "-" |
| 134 | content_type = headers.get("content-type", "") or "-" |
| 135 | body_len = len(body) |
| 136 | |
| 137 | body_hint = "-" |
| 138 | rate_limited = False |
| 139 | |
| 140 | if ("text" in content_type.lower() or "json" in content_type.lower()) and body: |
| 141 | sample = body[:1200].decode(errors="replace").lower() |
| 142 | if "<title>" in sample and "</title>" in sample: |
| 143 | title = sample.split("<title>", 1)[1].split("</title>", 1)[0] |
| 144 | body_hint = title.strip()[:120] or "-" |
| 145 | elif "captcha" in sample: |
| 146 | body_hint = "captcha" |
| 147 | elif "turnstile" in sample: |
| 148 | body_hint = "turnstile" |
| 149 | elif "loading" in sample: |
| 150 | body_hint = "loading" |
| 151 | |
| 152 | rate_limit_markers = ( |
| 153 | "too many", |
| 154 | "rate limit", |
| 155 | "quota", |
| 156 | "quota exceeded", |
| 157 | "request limit", |
| 158 | "دفعات زیاد", |
| 159 | "بیش از حد", |
| 160 | "سرویس در طول یک روز", |
| 161 | ) |
| 162 | if any(marker in sample for marker in rate_limit_markers): |
| 163 | rate_limited = True |
| 164 | body_hint = "quota_exceeded" |
| 165 | |
| 166 | log_msg = ( |
| 167 | "RESP <- %s status=%s type=%s len=%s server=%s location=%s cf-ray=%s hint=%s" |
| 168 | ) |
| 169 | log_args = ( |
| 170 | host or url[:60], |
| 171 | status, |
| 172 | content_type, |
| 173 | body_len, |
| 174 | server, |
no test coverage detected