协议阅读 - ASGI 如何处理 HTTP 和 WebSocket 数据

查看原文

这篇 spec 规定了 ASGI 如何处理 HTTP 和 Websocket 数据。

  • HTTP:
    • http connection scope 包含的信息:type=http, http_version=1.0/1.1/2, method, scheme, path, query_string, root_path, headers, client=[host, port], server=[host, port]
    • http request: type=http.request, body, more_body=true/false.
    • restart start: type=http.response.start, status, headers
    • response body: type=http.response.body, body, more_body
    • disconnect: type=http.disconnect
  • WebSocket:
    • scope: 跟 http 差不多,type=websocket, scheme=ws/wss, subprotocols, ...
    • Connection: type=websocket.connect
    • Accept: type=websocket.accept, subprotocol
    • Receive: type=websocket.receive, bytes, text
    • Send: type=websocket.send, bytes, text
    • Disconnect: type=websocket.disconnect, code
    • Close: type=websocket.close, code
  • ASGI 提供了一套与 WSGI 字段一一对应的关系。