Build ASGI HTTP scope from parsed request.
(self, request, sockname, peername)
| 1123 | return self.worker.alive and self.cfg.keepalive |
| 1124 | |
| 1125 | def _build_http_scope(self, request, sockname, peername): |
| 1126 | """Build ASGI HTTP scope from parsed request.""" |
| 1127 | # Use pre-computed bytes headers if available (fast path) |
| 1128 | # Fall back to conversion for HTTP/2 requests |
| 1129 | headers_bytes = getattr(request, 'headers_bytes', None) |
| 1130 | if isinstance(headers_bytes, list): |
| 1131 | headers = list(headers_bytes) # Copy to avoid mutation |
| 1132 | else: |
| 1133 | headers = [] |
| 1134 | for name, value in request.headers: |
| 1135 | headers.append((name.lower().encode("latin-1"), value.encode("latin-1"))) |
| 1136 | |
| 1137 | server = _normalize_sockaddr(sockname) |
| 1138 | client = _normalize_sockaddr(peername) |
| 1139 | |
| 1140 | scope = { |
| 1141 | "type": "http", |
| 1142 | "asgi": {"version": "3.0", "spec_version": "2.4"}, |
| 1143 | "http_version": f"{request.version[0]}.{request.version[1]}", |
| 1144 | "method": request.method, |
| 1145 | "scheme": request.scheme, |
| 1146 | "path": request.path, |
| 1147 | "raw_path": request.raw_path if request.raw_path else b"", |
| 1148 | "query_string": request.query.encode("latin-1") if request.query else b"", |
| 1149 | "root_path": self.cfg.root_path or "", |
| 1150 | "headers": headers, |
| 1151 | "server": server, |
| 1152 | "client": client, |
| 1153 | } |
| 1154 | |
| 1155 | # Add state dict for lifespan sharing |
| 1156 | if hasattr(self.worker, 'state'): |
| 1157 | scope["state"] = self.worker.state |
| 1158 | |
| 1159 | # Add HTTP/2 priority extension if available |
| 1160 | if hasattr(request, 'priority_weight'): |
| 1161 | scope["extensions"] = { |
| 1162 | "http.response.priority": { |
| 1163 | "weight": request.priority_weight, |
| 1164 | "depends_on": request.priority_depends_on, |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return scope |
| 1169 | |
| 1170 | def _build_environ(self, request, sockname, peername): |
| 1171 | """Build minimal WSGI-like environ dict for access logging.""" |