MCPcopy
hub / github.com/encode/starlette / handle_request

Method handle_request

starlette/testclient.py:225–373  ·  view source on GitHub ↗
(self, request: httpx.Request)

Source from the content-addressed store, hash-verified

223 self.client = client
224
225 def handle_request(self, request: httpx.Request) -> httpx.Response:
226 scheme = request.url.scheme
227 netloc = request.url.netloc.decode(encoding="ascii")
228 path = request.url.path
229 raw_path = request.url.raw_path
230 query = request.url.query.decode(encoding="ascii")
231
232 default_port = {"http": 80, "ws": 80, "https": 443, "wss": 443}[scheme]
233
234 if ":" in netloc:
235 host, port_string = netloc.split(":", 1)
236 port = int(port_string)
237 else:
238 host = netloc
239 port = default_port
240
241 # Include the 'host' header.
242 if "host" in request.headers:
243 headers: list[tuple[bytes, bytes]] = []
244 elif port == default_port: # pragma: no cover
245 headers = [(b"host", host.encode())]
246 else: # pragma: no cover
247 headers = [(b"host", (f"{host}:{port}").encode())]
248
249 # Include other request headers.
250 headers += [(key.lower().encode(), value.encode()) for key, value in request.headers.multi_items()]
251
252 scope: dict[str, Any]
253
254 if scheme in {"ws", "wss"}:
255 subprotocol = request.headers.get("sec-websocket-protocol", None)
256 if subprotocol is None:
257 subprotocols: Sequence[str] = []
258 else:
259 subprotocols = [value.strip() for value in subprotocol.split(",")]
260 scope = {
261 "type": "websocket",
262 "path": unquote(path),
263 "raw_path": raw_path.split(b"?", 1)[0],
264 "root_path": self.root_path,
265 "scheme": scheme,
266 "query_string": query.encode(),
267 "headers": headers,
268 "client": self.client,
269 "server": [host, port],
270 "subprotocols": subprotocols,
271 "state": self.app_state.copy(),
272 "extensions": {"websocket.http.response": {}},
273 }
274 session = WebSocketTestSession(self.app, scope, self.portal_factory)
275 raise _Upgrade(session)
276
277 scope = {
278 "type": "http",
279 "http_version": "1.1",
280 "method": request.method,
281 "path": unquote(path),
282 "raw_path": raw_path.split(b"?", 1)[0],

Callers

nothing calls this directly

Calls 6

_UpgradeClass · 0.85
decodeMethod · 0.80
multi_itemsMethod · 0.80
readMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected