Compute the effective origin for the current HTTP/WebSocket request.
(scope: dict[str, Any])
| 166 | |
| 167 | |
| 168 | def _get_request_origin(scope: dict[str, Any]) -> Optional[str]: |
| 169 | """Compute the effective origin for the current HTTP/WebSocket request.""" |
| 170 | forwarded = _get_scope_header(scope, b"forwarded") |
| 171 | if forwarded is not None: |
| 172 | proto = None |
| 173 | host = None |
| 174 | for element in forwarded.split(",", 1)[0].split(";"): |
| 175 | if "=" not in element: |
| 176 | continue |
| 177 | name, value = element.split("=", 1) |
| 178 | if name.strip().lower() == "proto": |
| 179 | proto = _strip_optional_quotes(value.strip()) |
| 180 | elif name.strip().lower() == "host": |
| 181 | host = _strip_optional_quotes(value.strip()) |
| 182 | if proto is not None and host is not None: |
| 183 | return f"{_normalize_origin_scheme(proto)}://{host}" |
| 184 | |
| 185 | host = _get_scope_header(scope, b"x-forwarded-host") |
| 186 | if host is None: |
| 187 | host = _get_scope_header(scope, b"host") |
| 188 | if host is None: |
| 189 | return None |
| 190 | |
| 191 | proto = _get_scope_header(scope, b"x-forwarded-proto") |
| 192 | if proto is None: |
| 193 | proto = scope.get("scheme", "http") |
| 194 | return f"{_normalize_origin_scheme(proto)}://{host}" |
| 195 | |
| 196 | |
| 197 | def _is_request_origin_allowed( |
no test coverage detected