Responses as a server writes them (Effect's HttpServerResponse.toWeb
and fromWeb, with a WireResponse in the place of a Web
Response): a status, a reason, header lines (set-cookie once per
cookie) and the body's bytes.
A raw body that is a WebResponse is sent as that response, with
the outer response's headers merged over its own.
Types#
| Type | Definition | Description |
|---|---|---|
WireBody | type WireBody = bytes | Stream[bytes, object] | None | The bytes, a stream of them, or no body. |
Classes#
Functions#
cancel_body#
cancel_body(body) -> NoneClose the async iterable of a raw body left unsent (Effect's
cancelResponseBody, Web ReadableStream.cancel).
| Parameter | Type | Description |
|---|---|---|
body | _body.HttpBody |
def cancel_body(body: _body.HttpBody) -> None:
"""Close the async iterable of a raw body left unsent (Effect's
``cancelResponseBody``, Web ``ReadableStream.cancel``)."""
if isinstance(body, _body.Raw):
_cancel(body.body)raw_bytes#
raw_bytes(value) -> bytesThe bytes of a raw body that is bytes or str.
| Parameter | Type | Description |
|---|---|---|
value | object |
Returns bytes
def raw_bytes(value: object) -> bytes:
"""The bytes of a raw body that is ``bytes`` or ``str``."""
if isinstance(value, (bytes, bytearray)):
return bytes(value)
if isinstance(value, str):
return text_encode(value)
raise TypeError(f"a raw body of type {type(value).__name__} cannot be written")to_wire#
to_wire(response, *, without_body=False) -> WireResponseresponse as written; without_body (a HEAD request) and
the 204, 205 and 304 statuses leave out its body, and 204 and 205 its
content-length.
| Parameter | Type | Description |
|---|---|---|
response | HttpServerResponse | |
without_body | bool = False |
Returns WireResponse
def to_wire(response: HttpServerResponse, *, without_body: bool = False) -> WireResponse:
"""``response`` as written; ``without_body`` (a ``HEAD`` request) and
the 204, 205 and 304 statuses leave out its body, and 204 and 205 its
``content-length``."""
headers = _response_headers(response)
body = response.body
outer_omits_body = omits_body(response)
no_body = outer_omits_body or without_body
if isinstance(body, _body.Raw) and isinstance(body.body, WebResponse):
raw = body.body
# HEAD keeps the raw status; an explicit bodyless status replaces it.
status, status_text = (
(response.status, response.status_text or "")
if outer_omits_body
else (raw.status, raw.status_text)
)
merged = _merge(list(raw.header_lines), headers)
if omits_content_length(status):
merged = _without(merged, "content-length")
return WireResponse(status, status_text, tuple(merged), None if no_body else _web_body(raw))
status_text = response.status_text or ""
if no_body:
cancel_body(body)
if omits_content_length(response.status):
headers = _without(headers, "content-length")
return WireResponse(response.status, status_text, tuple(headers), None)
wire_body: WireBody
match body:
case _body.Empty():
wire_body = None
case _body.Bytes():
wire_body = body.body
case _body.Raw():
wire_body = _raw_body(body.body)
case _body.FormData():
wire_body, content_type = _form_data.encode(body)
headers = _merge(headers, [("content-type", content_type)])
case _body.Stream():
wire_body = body.stream
return WireResponse(response.status, status_text, tuple(headers), wire_body)from_wire#
from_wire(wire) -> HttpServerResponseA server response of wire: its cookies parsed from
set-cookie, its body streamed.
| Parameter | Type | Description |
|---|---|---|
wire | WireResponse |
Returns HttpServerResponse
def from_wire(wire: WireResponse) -> HttpServerResponse:
"""A server response of ``wire``: its cookies parsed from
``set-cookie``, its body streamed."""
headers = Headers(dict(_without(_header_lines(wire.headers), "set-cookie")))
response = HttpServerResponse(
wire.status,
wire.status_text or None,
headers,
from_set_cookie(wire.get_set_cookie()),
)
if wire.body is None:
return response
chunks: Stream[bytes, object]
chunks = stream_succeed(wire.body) if isinstance(wire.body, bytes) else wire.body
return response.set_body(
_body.stream(
chunks,
wire.get("content-type"),
parse_content_length(wire.get("content-length")),
)
)with_body#
with_body(wire, body) -> WireResponse| Parameter | Type | Description |
|---|---|---|
wire | WireResponse | |
body | WireBody |
Returns WireResponse
def with_body(wire: WireResponse, body: WireBody) -> WireResponse:
return replace(wire, body=body)