| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from multiprocessing.synchronize import Event as EventType |
| 4 |
from types import TracebackType |
| 5 |
from typing import ( |
| 6 |
Any, |
| 7 |
Awaitable, |
| 8 |
Callable, |
| 9 |
Dict, |
| 10 |
Iterable, |
| 11 |
Literal, |
| 12 |
Optional, |
| 13 |
Protocol, |
| 14 |
Tuple, |
| 15 |
Type, |
| 16 |
TypedDict, |
| 17 |
Union, |
| 18 |
) |
| 19 |
|
| 20 |
import h2.events |
| 21 |
import h11 |
| 22 |
|
| 23 |
from .config import Config, Sockets |
| 24 |
|
| 25 |
H11SendableEvent = Union[h11.Data, h11.EndOfMessage, h11.InformationalResponse, h11.Response] |
| 26 |
|
| 27 |
WorkerFunc = Callable[[Config, Optional[Sockets], Optional[EventType]], None] |
| 28 |
|
| 29 |
|
| 30 |
class ASGIVersions(TypedDict, total=False): |
| 31 |
spec_version: str |
| 32 |
version: Union[Literal["2.0"], Literal["3.0"]] |
| 33 |
|
| 34 |
|
| 35 |
class HTTPScope(TypedDict): |
| 36 |
type: Literal["http"] |
| 37 |
asgi: ASGIVersions |
| 38 |
http_version: str |
| 39 |
method: str |
| 40 |
scheme: str |
| 41 |
path: str |
| 42 |
raw_path: bytes |
| 43 |
query_string: bytes |
| 44 |
root_path: str |
| 45 |
headers: Iterable[Tuple[bytes, bytes]] |
| 46 |
client: Optional[Tuple[str, int]] |
| 47 |
server: Optional[Tuple[str, Optional[int]]] |
| 48 |
extensions: Dict[str, dict] |
| 49 |
|
| 50 |
|
| 51 |
class WebsocketScope(TypedDict): |
| 52 |
type: Literal["websocket"] |
| 53 |
asgi: ASGIVersions |
| 54 |
http_version: str |
| 55 |
scheme: str |
| 56 |
path: str |
| 57 |
raw_path: bytes |
| 58 |
query_string: bytes |
| 59 |
root_path: str |
| 60 |
headers: Iterable[Tuple[bytes, bytes]] |
| 61 |
client: Optional[Tuple[str, int]] |
| 62 |
server: Optional[Tuple[str, Optional[int]]] |
| 63 |
subprotocols: Iterable[str] |
| 64 |
extensions: Dict[str, dict] |
| 65 |
|
| 66 |
|
| 67 |
class LifespanScope(TypedDict): |
| 68 |
type: Literal["lifespan"] |
| 69 |
asgi: ASGIVersions |
| 70 |
|
| 71 |
|
| 72 |
WWWScope = Union[HTTPScope, WebsocketScope] |
| 73 |
Scope = Union[HTTPScope, WebsocketScope, LifespanScope] |
| 74 |
|
| 75 |
|
| 76 |
class HTTPRequestEvent(TypedDict): |
| 77 |
type: Literal["http.request"] |
| 78 |
body: bytes |
| 79 |
more_body: bool |
| 80 |
|
| 81 |
|
| 82 |
class HTTPResponseStartEvent(TypedDict): |
| 83 |
type: Literal["http.response.start"] |
| 84 |
status: int |
| 85 |
headers: Iterable[Tuple[bytes, bytes]] |
| 86 |
|
| 87 |
|
| 88 |
class HTTPResponseBodyEvent(TypedDict): |
| 89 |
type: Literal["http.response.body"] |
| 90 |
body: bytes |
| 91 |
more_body: bool |
| 92 |
|
| 93 |
|
| 94 |
class HTTPServerPushEvent(TypedDict): |
| 95 |
type: Literal["http.response.push"] |
| 96 |
path: str |
| 97 |
headers: Iterable[Tuple[bytes, bytes]] |
| 98 |
|
| 99 |
|
| 100 |
class HTTPEarlyHintEvent(TypedDict): |
| 101 |
type: Literal["http.response.early_hint"] |
| 102 |
links: Iterable[bytes] |
| 103 |
|
| 104 |
|
| 105 |
class HTTPDisconnectEvent(TypedDict): |
| 106 |
type: Literal["http.disconnect"] |
| 107 |
|
| 108 |
|
| 109 |
class WebsocketConnectEvent(TypedDict): |
| 110 |
type: Literal["websocket.connect"] |
| 111 |
|
| 112 |
|
| 113 |
class WebsocketAcceptEvent(TypedDict): |
| 114 |
type: Literal["websocket.accept"] |
| 115 |
subprotocol: Optional[str] |
| 116 |
headers: Iterable[Tuple[bytes, bytes]] |
| 117 |
|
| 118 |
|
| 119 |
class WebsocketReceiveEvent(TypedDict): |
| 120 |
type: Literal["websocket.receive"] |
| 121 |
bytes: Optional[bytes] |
| 122 |
text: Optional[str] |
| 123 |
|
| 124 |
|
| 125 |
class WebsocketSendEvent(TypedDict): |
| 126 |
type: Literal["websocket.send"] |
| 127 |
bytes: Optional[bytes] |
| 128 |
text: Optional[str] |
| 129 |
|
| 130 |
|
| 131 |
class WebsocketResponseStartEvent(TypedDict): |
| 132 |
type: Literal["websocket.http.response.start"] |
| 133 |
status: int |
| 134 |
headers: Iterable[Tuple[bytes, bytes]] |
| 135 |
|
| 136 |
|
| 137 |
class WebsocketResponseBodyEvent(TypedDict): |
| 138 |
type: Literal["websocket.http.response.body"] |
| 139 |
body: bytes |
| 140 |
more_body: bool |
| 141 |
|
| 142 |
|
| 143 |
class WebsocketDisconnectEvent(TypedDict): |
| 144 |
type: Literal["websocket.disconnect"] |
| 145 |
code: int |
| 146 |
|
| 147 |
|
| 148 |
class WebsocketCloseEvent(TypedDict): |
| 149 |
type: Literal["websocket.close"] |
| 150 |
code: int |
| 151 |
reason: Optional[str] |
| 152 |
|
| 153 |
|
| 154 |
class LifespanStartupEvent(TypedDict): |
| 155 |
type: Literal["lifespan.startup"] |
| 156 |
|
| 157 |
|
| 158 |
class LifespanShutdownEvent(TypedDict): |
| 159 |
type: Literal["lifespan.shutdown"] |
| 160 |
|
| 161 |
|
| 162 |
class LifespanStartupCompleteEvent(TypedDict): |
| 163 |
type: Literal["lifespan.startup.complete"] |
| 164 |
|
| 165 |
|
| 166 |
class LifespanStartupFailedEvent(TypedDict): |
| 167 |
type: Literal["lifespan.startup.failed"] |
| 168 |
message: str |
| 169 |
|
| 170 |
|
| 171 |
class LifespanShutdownCompleteEvent(TypedDict): |
| 172 |
type: Literal["lifespan.shutdown.complete"] |
| 173 |
|
| 174 |
|
| 175 |
class LifespanShutdownFailedEvent(TypedDict): |
| 176 |
type: Literal["lifespan.shutdown.failed"] |
| 177 |
message: str |
| 178 |
|
| 179 |
|
| 180 |
ASGIReceiveEvent = Union[ |
| 181 |
HTTPRequestEvent, |
| 182 |
HTTPDisconnectEvent, |
| 183 |
WebsocketConnectEvent, |
| 184 |
WebsocketReceiveEvent, |
| 185 |
WebsocketDisconnectEvent, |
| 186 |
LifespanStartupEvent, |
| 187 |
LifespanShutdownEvent, |
| 188 |
] |
| 189 |
|
| 190 |
|
| 191 |
ASGISendEvent = Union[ |
| 192 |
HTTPResponseStartEvent, |
| 193 |
HTTPResponseBodyEvent, |
| 194 |
HTTPServerPushEvent, |
| 195 |
HTTPEarlyHintEvent, |
| 196 |
HTTPDisconnectEvent, |
| 197 |
WebsocketAcceptEvent, |
| 198 |
WebsocketSendEvent, |
| 199 |
WebsocketResponseStartEvent, |
| 200 |
WebsocketResponseBodyEvent, |
| 201 |
WebsocketCloseEvent, |
| 202 |
LifespanStartupCompleteEvent, |
| 203 |
LifespanStartupFailedEvent, |
| 204 |
LifespanShutdownCompleteEvent, |
| 205 |
LifespanShutdownFailedEvent, |
| 206 |
] |
| 207 |
|
| 208 |
|
| 209 |
ASGIReceiveCallable = Callable[[], Awaitable[ASGIReceiveEvent]] |
| 210 |
ASGISendCallable = Callable[[ASGISendEvent], Awaitable[None]] |
| 211 |
|
| 212 |
ASGIFramework = Callable[ |
| 213 |
[ |
| 214 |
Scope, |
| 215 |
ASGIReceiveCallable, |
| 216 |
ASGISendCallable, |
| 217 |
], |
| 218 |
Awaitable[None], |
| 219 |
] |
| 220 |
WSGIFramework = Callable[[dict, Callable], Iterable[bytes]] |
| 221 |
Framework = Union[ASGIFramework, WSGIFramework] |
| 222 |
|
| 223 |
|
| 224 |
class H2SyncStream(Protocol): |
| 225 |
scope: dict |
| 226 |
|
| 227 |
def data_received(self, data: bytes) -> None: |
| 228 |
... |
| 229 |
|
| 230 |
def ended(self) -> None: |
| 231 |
... |
| 232 |
|
| 233 |
def reset(self) -> None: |
| 234 |
... |
| 235 |
|
| 236 |
def close(self) -> None: |
| 237 |
... |
| 238 |
|
| 239 |
async def handle_request( |
| 240 |
self, |
| 241 |
event: h2.events.RequestReceived, |
| 242 |
scheme: str, |
| 243 |
client: Tuple[str, int], |
| 244 |
server: Tuple[str, int], |
| 245 |
) -> None: |
| 246 |
... |
| 247 |
|
| 248 |
|
| 249 |
class H2AsyncStream(Protocol): |
| 250 |
scope: dict |
| 251 |
|
| 252 |
async def data_received(self, data: bytes) -> None: |
| 253 |
... |
| 254 |
|
| 255 |
async def ended(self) -> None: |
| 256 |
... |
| 257 |
|
| 258 |
async def reset(self) -> None: |
| 259 |
... |
| 260 |
|
| 261 |
async def close(self) -> None: |
| 262 |
... |
| 263 |
|
| 264 |
async def handle_request( |
| 265 |
self, |
| 266 |
event: h2.events.RequestReceived, |
| 267 |
scheme: str, |
| 268 |
client: Tuple[str, int], |
| 269 |
server: Tuple[str, int], |
| 270 |
) -> None: |
| 271 |
... |
| 272 |
|
| 273 |
|
| 274 |
class Event(Protocol): |
| 275 |
def __init__(self) -> None: |
| 276 |
... |
| 277 |
|
| 278 |
async def clear(self) -> None: |
| 279 |
... |
| 280 |
|
| 281 |
async def set(self) -> None: |
| 282 |
... |
| 283 |
|
| 284 |
async def wait(self) -> None: |
| 285 |
... |
| 286 |
|
| 287 |
def is_set(self) -> bool: |
| 288 |
... |
| 289 |
|
| 290 |
|
| 291 |
class WorkerContext(Protocol): |
| 292 |
event_class: Type[Event] |
| 293 |
terminated: Event |
| 294 |
|
| 295 |
@staticmethod |
| 296 |
async def sleep(wait: Union[float, int]) -> None: |
| 297 |
... |
| 298 |
|
| 299 |
@staticmethod |
| 300 |
def time() -> float: |
| 301 |
... |
| 302 |
|
| 303 |
|
| 304 |
class TaskGroup(Protocol): |
| 305 |
async def spawn_app( |
| 306 |
self, |
| 307 |
app: AppWrapper, |
| 308 |
config: Config, |
| 309 |
scope: Scope, |
| 310 |
send: Callable[[Optional[ASGISendEvent]], Awaitable[None]], |
| 311 |
) -> Callable[[ASGIReceiveEvent], Awaitable[None]]: |
| 312 |
... |
| 313 |
|
| 314 |
def spawn(self, func: Callable, *args: Any) -> None: |
| 315 |
... |
| 316 |
|
| 317 |
async def __aenter__(self) -> TaskGroup: |
| 318 |
... |
| 319 |
|
| 320 |
async def __aexit__(self, exc_type: type, exc_value: BaseException, tb: TracebackType) -> None: |
| 321 |
... |
| 322 |
|
| 323 |
|
| 324 |
class ResponseSummary(TypedDict): |
| 325 |
status: int |
| 326 |
headers: Iterable[Tuple[bytes, bytes]] |
| 327 |
|
| 328 |
|
| 329 |
class AppWrapper(Protocol): |
| 330 |
async def __call__( |
| 331 |
self, |
| 332 |
scope: Scope, |
| 333 |
receive: ASGIReceiveCallable, |
| 334 |
send: ASGISendCallable, |
| 335 |
sync_spawn: Callable, |
| 336 |
call_soon: Callable, |
| 337 |
) -> None: |
| 338 |
... |