-
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathio.rs
More file actions
641 lines (600 loc) · 25.4 KB
/
io.rs
File metadata and controls
641 lines (600 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use anyhow::Result;
use futures::{StreamExt, TryStreamExt, sink::SinkExt};
use http_body_util::BodyExt;
use hyper::{
Response, StatusCode, body,
header::{HeaderMap, HeaderName, HeaderValue, SERVER as HK_SERVER},
};
use pyo3::{prelude::*, pybacked::PyBackedBytes, types::PyDict};
use std::{
borrow::Cow,
sync::{Arc, Mutex, atomic},
};
use tokio::{
fs::File,
sync::{Mutex as AsyncMutex, Notify, mpsc, oneshot},
};
use tokio_tungstenite::tungstenite::{Message, protocol::frame as wsframe};
use tokio_util::io::ReaderStream;
use super::{
errors::{UnsupportedASGIMessage, error_flow, error_message},
types::ASGIMessageType,
};
use crate::{
conversion::FutureResultToPy,
http::{HTTPResponse, HTTPResponseBody, HV_SERVER, response_404},
runtime::{
Runtime, RuntimeRef, done_future_into_py, empty_future_into_py, err_future_into_py, future_into_py_futlike,
},
ws::{HyperWebsocket, UpgradeData, WSRxStream, WSTxStream},
};
const EMPTY_BYTES: Cow<[u8]> = Cow::Borrowed(b"");
const EMPTY_STRING: String = String::new();
static WS_SUBPROTO_HNAME: &str = "Sec-WebSocket-Protocol";
#[pyclass(frozen, module = "granian._granian")]
pub(crate) struct ASGIHTTPProtocol {
rt: RuntimeRef,
tx: Mutex<Option<oneshot::Sender<HTTPResponse>>>,
disconnect_guard: Arc<Notify>,
request_body: Arc<AsyncMutex<http_body_util::BodyStream<body::Incoming>>>,
response_started: atomic::AtomicBool,
response_chunked: atomic::AtomicBool,
response_intent: Mutex<Option<(u16, HeaderMap)>>,
body_tx: Mutex<Option<mpsc::UnboundedSender<body::Bytes>>>,
flow_rx_exhausted: Arc<atomic::AtomicBool>,
flow_rx_closed: Arc<atomic::AtomicBool>,
flow_tx_waiter: Arc<Notify>,
sent_response_code: Arc<atomic::AtomicU16>,
}
impl ASGIHTTPProtocol {
pub fn new(
rt: RuntimeRef,
body: hyper::body::Incoming,
tx: oneshot::Sender<HTTPResponse>,
disconnect_guard: Arc<Notify>,
) -> Self {
Self {
rt,
tx: Mutex::new(Some(tx)),
disconnect_guard,
request_body: Arc::new(AsyncMutex::new(http_body_util::BodyStream::new(body))),
response_started: false.into(),
response_chunked: false.into(),
response_intent: Mutex::new(None),
body_tx: Mutex::new(None),
flow_rx_exhausted: Arc::new(atomic::AtomicBool::new(false)),
flow_rx_closed: Arc::new(atomic::AtomicBool::new(false)),
flow_tx_waiter: Arc::new(tokio::sync::Notify::new()),
sent_response_code: Arc::new(atomic::AtomicU16::new(500)),
}
}
#[inline(always)]
fn send_response(&self, status: u16, headers: HeaderMap<HeaderValue>, body: HTTPResponseBody) {
if let Some(tx) = self.tx.lock().unwrap().take() {
let mut res = Response::new(body);
*res.status_mut() = hyper::StatusCode::from_u16(status).unwrap();
*res.headers_mut() = headers;
let _ = tx.send(res);
self.sent_response_code.store(status, atomic::Ordering::Relaxed);
}
}
#[inline]
fn send_body<'p>(
&self,
py: Python<'p>,
tx: &mpsc::UnboundedSender<body::Bytes>,
body: Box<[u8]>,
close: bool,
) -> PyResult<Bound<'p, PyAny>> {
match tx.send(body.into()) {
Ok(()) => {
if close {
self.flow_tx_waiter.notify_one();
}
}
Err(err) => {
if !self.flow_rx_closed.load(atomic::Ordering::Acquire) {
log::info!("ASGI transport error: {err:?}");
}
self.flow_tx_waiter.notify_one();
}
}
empty_future_into_py(py)
}
pub fn tx(&self) -> Option<oneshot::Sender<HTTPResponse>> {
self.tx.lock().unwrap().take()
}
}
#[pymethods]
impl ASGIHTTPProtocol {
fn receive<'p>(&self, py: Python<'p>) -> PyResult<Bound<'p, PyAny>> {
if self.flow_rx_closed.load(atomic::Ordering::Acquire) {
return done_future_into_py(
py,
super::conversion::message_into_py(py, ASGIMessageType::HTTPDisconnect).map(Bound::unbind),
);
}
if self.flow_rx_exhausted.load(atomic::Ordering::Acquire) {
let guard_tx = self.flow_tx_waiter.clone();
let guard_disconnect = self.disconnect_guard.clone();
let disconnected = self.flow_rx_closed.clone();
return future_into_py_futlike(self.rt.clone(), py, async move {
tokio::select! {
() = guard_tx.notified() => {},
() = guard_disconnect.notified() => disconnected.store(true, atomic::Ordering::Release),
}
FutureResultToPy::ASGIMessage(ASGIMessageType::HTTPDisconnect)
});
}
let body_ref = self.request_body.clone();
let guard_tx = self.flow_tx_waiter.clone();
let guard_disconnect = self.disconnect_guard.clone();
let exhausted = self.flow_rx_exhausted.clone();
let disconnected = self.flow_rx_closed.clone();
future_into_py_futlike(self.rt.clone(), py, async move {
let mut bodym = body_ref.lock().await;
let body = &mut *bodym;
let mut more_body = false;
let chunk = tokio::select! {
biased;
frame = body.next() => match frame {
Some(Ok(buf)) => {
more_body = true;
Some(buf.into_data().unwrap_or_default())
}
Some(Err(_)) => None,
_ => Some(body::Bytes::new()),
},
() = guard_disconnect.notified() => {
disconnected.store(true, atomic::Ordering::Release);
None
}
};
if !more_body {
exhausted.store(true, atomic::Ordering::Release);
}
match chunk {
Some(data) => FutureResultToPy::ASGIMessage(ASGIMessageType::HTTPRequestBody((data, more_body))),
_ => {
guard_tx.notify_one();
FutureResultToPy::ASGIMessage(ASGIMessageType::HTTPDisconnect)
}
}
})
}
fn send<'p>(&self, py: Python<'p>, data: &Bound<'p, PyDict>) -> PyResult<Bound<'p, PyAny>> {
match adapt_message_type(py, data) {
Ok(ASGIMessageType::HTTPResponseStart(intent)) => {
if self
.response_started
.compare_exchange(false, true, atomic::Ordering::Relaxed, atomic::Ordering::Relaxed)
.is_err()
{
return error_flow!("Response already started");
}
// NOTE: we could definitely avoid this check, and always start a streamed response
// and thus get rid of the whole `response_chunked` thing.
// But that seems to be ~4% slower when the app actually just want to send 1 msg.
if !intent
.1
.get("content-type")
.is_some_and(|hv| hv.as_bytes().starts_with(b"text/event-stream"))
{
let mut response_intent = self.response_intent.lock().unwrap();
*response_intent = Some(intent);
return empty_future_into_py(py);
}
self.response_chunked.store(true, atomic::Ordering::Relaxed);
let (status, headers) = intent;
let (body_tx, body_rx) = mpsc::unbounded_channel::<body::Bytes>();
let body_stream = http_body_util::StreamBody::new(
tokio_stream::wrappers::UnboundedReceiverStream::new(body_rx)
.map(body::Frame::data)
.map(Result::Ok),
);
*self.body_tx.lock().unwrap() = Some(body_tx.clone());
self.send_response(status, headers, BodyExt::boxed(body_stream));
empty_future_into_py(py)
}
Ok(ASGIMessageType::HTTPResponseBody((body, more))) => {
match (
self.response_started.load(atomic::Ordering::Relaxed),
more,
self.response_chunked.load(atomic::Ordering::Relaxed),
) {
(true, false, false) => match self.response_intent.lock().unwrap().take() {
Some((status, headers)) => {
self.send_response(
status,
headers,
http_body_util::Full::new(body::Bytes::from(body))
.map_err(std::convert::Into::into)
.boxed(),
);
self.flow_tx_waiter.notify_one();
empty_future_into_py(py)
}
_ => error_flow!("Response already finished"),
},
(true, true, false) => match self.response_intent.lock().unwrap().take() {
Some((status, headers)) => {
self.response_chunked.store(true, atomic::Ordering::Relaxed);
let (body_tx, body_rx) = mpsc::unbounded_channel::<body::Bytes>();
let body_stream = http_body_util::StreamBody::new(
tokio_stream::wrappers::UnboundedReceiverStream::new(body_rx)
.map(body::Frame::data)
.map(Result::Ok),
);
*self.body_tx.lock().unwrap() = Some(body_tx.clone());
self.send_response(status, headers, BodyExt::boxed(body_stream));
self.send_body(py, &body_tx, body, false)
}
_ => error_flow!("Response already finished"),
},
(true, true, true) => match &*self.body_tx.lock().unwrap() {
Some(tx) => self.send_body(py, tx, body, false),
_ => error_flow!("Transport not initialized or closed"),
},
(true, false, true) => match self.body_tx.lock().unwrap().take() {
Some(tx) => match body.is_empty() {
false => self.send_body(py, &tx, body, true),
true => {
self.flow_tx_waiter.notify_one();
empty_future_into_py(py)
}
},
_ => error_flow!("Transport not initialized or closed"),
},
_ => error_flow!("Response not started"),
}
}
Ok(ASGIMessageType::HTTPResponseFile(file_path)) => match (
self.response_started.load(atomic::Ordering::Relaxed),
self.tx.lock().unwrap().take(),
) {
(true, Some(tx)) => {
let (status, headers) = self.response_intent.lock().unwrap().take().unwrap();
// FIXME: to store the actual status in case of 404 this should be re-implemented taking
// into account the following async flow (we return empty future to avoid waiting)
self.sent_response_code.store(status, atomic::Ordering::Relaxed);
self.rt.spawn(async move {
let res = match File::open(&file_path).await {
Ok(file) => {
let stream = ReaderStream::with_capacity(file, 131_072);
let stream_body = http_body_util::StreamBody::new(stream.map_ok(body::Frame::data));
let mut res =
Response::new(BodyExt::map_err(stream_body, std::convert::Into::into).boxed());
*res.status_mut() = StatusCode::from_u16(status).unwrap();
*res.headers_mut() = headers;
res
}
Err(_) => {
log::info!("Cannot open file {}", &file_path);
response_404()
}
};
let _ = tx.send(res);
});
empty_future_into_py(py)
}
_ => error_flow!("Response not started"),
},
Err(err) => Err(err.into()),
_ => error_message!(),
}
}
#[getter(sent_response_code)]
fn get_sent_response_code(&self) -> u16 {
self.sent_response_code.load(atomic::Ordering::Relaxed)
}
}
pub(crate) struct WebsocketDetachedTransport {
pub consumed: bool,
rx: Option<WSRxStream>,
tx: Option<WSTxStream>,
closeframe: Option<wsframe::CloseFrame>,
}
impl WebsocketDetachedTransport {
pub fn new(
consumed: bool,
rx: Option<WSRxStream>,
tx: Option<WSTxStream>,
closeframe: Option<wsframe::CloseFrame>,
) -> Self {
Self {
consumed,
rx,
tx,
closeframe,
}
}
pub async fn close(&mut self) {
if let Some(mut tx) = self.tx.take() {
if let Some(frame) = self.closeframe.take() {
_ = tx.send(Message::Close(Some(frame))).await;
}
if let Err(err) = tx.close().await {
log::info!("Failed to close websocket with error {err:?}");
}
}
drop(self.rx.take());
}
}
#[pyclass(frozen, module = "granian._granian")]
pub(crate) struct ASGIWebsocketProtocol {
rt: RuntimeRef,
tx: Mutex<Option<oneshot::Sender<WebsocketDetachedTransport>>>,
websocket: Mutex<Option<HyperWebsocket>>,
upgrade: Mutex<Option<UpgradeData>>,
ws_rx: Arc<AsyncMutex<Option<WSRxStream>>>,
ws_tx: Arc<AsyncMutex<Option<WSTxStream>>>,
init_rx: atomic::AtomicBool,
init_tx: Arc<atomic::AtomicBool>,
init_event: Arc<Notify>,
closed: Arc<atomic::AtomicBool>,
}
impl ASGIWebsocketProtocol {
pub fn new(
rt: RuntimeRef,
tx: oneshot::Sender<WebsocketDetachedTransport>,
websocket: HyperWebsocket,
upgrade: UpgradeData,
) -> Self {
Self {
rt,
tx: Mutex::new(Some(tx)),
websocket: Mutex::new(Some(websocket)),
upgrade: Mutex::new(Some(upgrade)),
ws_rx: Arc::new(AsyncMutex::new(None)),
ws_tx: Arc::new(AsyncMutex::new(None)),
init_rx: false.into(),
init_tx: Arc::new(false.into()),
init_event: Arc::new(Notify::new()),
closed: Arc::new(false.into()),
}
}
#[inline(always)]
fn accept<'p>(&self, py: Python<'p>, subproto: Option<String>) -> PyResult<Bound<'p, PyAny>> {
let upgrade = self.upgrade.lock().unwrap().take();
let websocket = self.websocket.lock().unwrap().take();
let accepted = self.init_tx.clone();
let accept_notify = self.init_event.clone();
let rx = self.ws_rx.clone();
let tx = self.ws_tx.clone();
future_into_py_futlike(self.rt.clone(), py, async move {
if let Some(mut upgrade) = upgrade {
let upgrade_headers = match subproto {
Some(v) => vec![(WS_SUBPROTO_HNAME.to_string(), v)],
_ => vec![],
};
if (upgrade.send(Some(upgrade_headers)).await).is_ok()
&& let Some(websocket) = websocket
&& let Ok(stream) = websocket.await
{
let mut wtx = tx.lock().await;
let mut wrx = rx.lock().await;
let (tx, rx) = stream.split();
*wtx = Some(tx);
*wrx = Some(rx);
drop(wrx);
accepted.store(true, atomic::Ordering::Release);
accept_notify.notify_one();
return FutureResultToPy::None;
}
}
FutureResultToPy::Err(error_flow!("Connection already upgraded"))
})
}
#[inline(always)]
fn send_message<'p>(&self, py: Python<'p>, data: Message) -> PyResult<Bound<'p, PyAny>> {
let transport = self.ws_tx.clone();
let closed = self.closed.clone();
future_into_py_futlike(self.rt.clone(), py, async move {
if let Some(ws) = &mut *(transport.lock().await) {
match ws.send(data).await {
Ok(()) => return FutureResultToPy::None,
_ => {
if closed.load(atomic::Ordering::Acquire) {
log::info!("Attempted to write to a closed websocket");
return FutureResultToPy::None;
}
}
}
}
FutureResultToPy::Err(error_flow!("Transport not initialized or closed"))
})
}
#[inline(always)]
fn close<'p>(&self, py: Python<'p>, frame: Option<wsframe::CloseFrame>) -> PyResult<Bound<'p, PyAny>> {
let closed = self.closed.clone();
let ws_rx = self.ws_rx.clone();
let ws_tx = self.ws_tx.clone();
future_into_py_futlike(self.rt.clone(), py, async move {
if let Some(tx) = ws_tx.lock().await.take() {
closed.store(true, atomic::Ordering::Release);
WebsocketDetachedTransport::new(true, ws_rx.lock().await.take(), Some(tx), frame)
.close()
.await;
}
FutureResultToPy::None
})
}
fn consumed(&self) -> bool {
self.upgrade.lock().unwrap().is_none()
}
pub fn tx(
&self,
) -> (
Option<oneshot::Sender<WebsocketDetachedTransport>>,
WebsocketDetachedTransport,
) {
let mut ws_rx = self.ws_rx.blocking_lock();
let mut ws_tx = self.ws_tx.blocking_lock();
(
self.tx.lock().unwrap().take(),
WebsocketDetachedTransport::new(self.consumed(), ws_rx.take(), ws_tx.take(), None),
)
}
}
#[pymethods]
impl ASGIWebsocketProtocol {
fn receive<'p>(&self, py: Python<'p>) -> PyResult<Bound<'p, PyAny>> {
// if it's the first `receive` call, return the connect message
if self
.init_rx
.compare_exchange(false, true, atomic::Ordering::Relaxed, atomic::Ordering::Relaxed)
.is_ok()
{
return done_future_into_py(
py,
super::conversion::message_into_py(py, ASGIMessageType::WSConnect).map(Bound::unbind),
);
}
let accepted = self.init_tx.clone();
let accepted_ev = self.init_event.clone();
let closed = self.closed.clone();
let transport = self.ws_rx.clone();
future_into_py_futlike(self.rt.clone(), py, async move {
if !accepted.load(atomic::Ordering::Acquire) {
// need to wait for the protocol to send the accept message and init transport
accepted_ev.notified().await;
}
if let Some(ws) = &mut *(transport.lock().await) {
while let Some(recv) = ws.next().await {
match recv {
Ok(Message::Ping(_) | Message::Pong(_)) => {}
Ok(message @ Message::Close(_)) => {
closed.store(true, atomic::Ordering::Release);
return FutureResultToPy::ASGIWSMessage(message);
}
Ok(message) => return FutureResultToPy::ASGIWSMessage(message),
_ => {
// treat any recv error as a disconnection
closed.store(true, atomic::Ordering::Release);
return FutureResultToPy::ASGIWSMessage(Message::Close(None));
}
}
}
}
FutureResultToPy::Err(error_flow!("Transport not initialized or closed"))
})
}
fn send<'p>(&self, py: Python<'p>, data: &Bound<'p, PyDict>) -> PyResult<Bound<'p, PyAny>> {
match adapt_message_type(py, data) {
Ok(ASGIMessageType::WSAccept(subproto)) => self.accept(py, subproto),
Ok(ASGIMessageType::WSClose(frame)) => self.close(py, frame),
Ok(ASGIMessageType::WSMessage(message)) => self.send_message(py, message),
_ => err_future_into_py(py, error_message!()),
}
}
}
#[inline(never)]
fn adapt_message_type(py: Python, message: &Bound<PyDict>) -> Result<ASGIMessageType, UnsupportedASGIMessage> {
match message.get_item(pyo3::intern!(py, "type")) {
Ok(Some(item)) => {
let message_type: &str = item.extract()?;
match message_type {
"http.response.start" => Ok(ASGIMessageType::HTTPResponseStart((
adapt_status_code(py, message)?,
adapt_headers(py, message).map_err(|_| UnsupportedASGIMessage)?,
))),
"http.response.body" => Ok(ASGIMessageType::HTTPResponseBody(adapt_body(py, message))),
"http.response.pathsend" => Ok(ASGIMessageType::HTTPResponseFile(adapt_file(py, message)?)),
"websocket.accept" => {
let subproto: Option<String> = match message.get_item(pyo3::intern!(py, "subprotocol")) {
Ok(Some(item)) => item.extract::<String>().map(Some).unwrap_or(None),
_ => None,
};
Ok(ASGIMessageType::WSAccept(subproto))
}
"websocket.close" => {
let code: wsframe::coding::CloseCode = match message.get_item(pyo3::intern!(py, "code")) {
Ok(Some(item)) => item
.extract::<u16>()
.map(std::convert::Into::into)
.unwrap_or(wsframe::coding::CloseCode::Normal),
_ => wsframe::coding::CloseCode::Normal,
};
let reason: String = match message.get_item(pyo3::intern!(py, "reason")) {
Ok(Some(item)) => item.extract::<String>().unwrap_or(String::new()),
_ => String::new(),
};
Ok(ASGIMessageType::WSClose(Some(wsframe::CloseFrame {
code,
reason: reason.into(),
})))
}
"websocket.send" => Ok(ASGIMessageType::WSMessage(ws_message_into_rs(py, message)?)),
_ => error_message!(),
}
}
_ => error_message!(),
}
}
#[inline(always)]
fn adapt_status_code(py: Python, message: &Bound<PyDict>) -> Result<u16, UnsupportedASGIMessage> {
match message.get_item(pyo3::intern!(py, "status"))? {
Some(item) => Ok(item.extract()?),
_ => error_message!(),
}
}
#[inline(always)]
fn adapt_headers(py: Python, message: &Bound<PyDict>) -> Result<HeaderMap> {
let mut ret = HeaderMap::new();
for headers_item in message
.get_item(pyo3::intern!(py, "headers"))?
.ok_or(UnsupportedASGIMessage)?
.try_iter()?
.flatten()
{
let htup = headers_item.extract::<Vec<PyBackedBytes>>()?;
if htup.len() != 2 {
return error_message!();
}
ret.append(HeaderName::from_bytes(&htup[0])?, HeaderValue::from_bytes(&htup[1])?);
}
ret.entry(HK_SERVER).or_insert(HV_SERVER);
Ok(ret)
}
#[inline(always)]
fn adapt_body(py: Python, message: &Bound<PyDict>) -> (Box<[u8]>, bool) {
let body = message.get_item(pyo3::intern!(py, "body"));
let body = match body {
Ok(Some(ref item)) => item.extract().unwrap_or(EMPTY_BYTES),
_ => EMPTY_BYTES,
};
let more = match message.get_item(pyo3::intern!(py, "more_body")) {
Ok(Some(item)) => item.extract().unwrap_or(false),
_ => false,
};
(body.into(), more)
}
#[inline(always)]
fn adapt_file(py: Python, message: &Bound<PyDict>) -> PyResult<String> {
match message.get_item(pyo3::intern!(py, "path"))? {
Some(item) => item.extract(),
_ => error_message!(),
}
}
#[inline(always)]
fn ws_message_into_rs(py: Python, message: &Bound<PyDict>) -> PyResult<Message> {
match (
message.get_item(pyo3::intern!(py, "bytes"))?,
message.get_item(pyo3::intern!(py, "text"))?,
) {
(Some(item), None) => {
let data: Cow<[u8]> = item.extract().unwrap_or(EMPTY_BYTES);
Ok(data[..].into())
}
(None, Some(item)) => Ok(Message::Text(item.extract::<String>().unwrap_or(EMPTY_STRING).into())),
(Some(itemb), Some(itemt)) => match (itemb.is_none(), itemt.is_none()) {
(false, true) => {
let data: Box<[u8]> = itemb.extract::<Cow<[u8]>>()?.into();
Ok(Message::Binary(body::Bytes::from(data)))
}
(true, false) => Ok(itemt.extract::<String>()?.into()),
_ => error_message!(),
},
_ => error_message!(),
}
}