weft-server 0.1.1__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {weft_server-0.1.1 → weft_server-0.2.0}/Cargo.lock +1 -1
- {weft_server-0.1.1 → weft_server-0.2.0}/Cargo.toml +9 -1
- {weft_server-0.1.1 → weft_server-0.2.0}/PKG-INFO +1 -1
- {weft_server-0.1.1 → weft_server-0.2.0}/src/asgi.rs +340 -53
- {weft_server-0.1.1 → weft_server-0.2.0}/src/cache.rs +98 -7
- {weft_server-0.1.1 → weft_server-0.2.0}/src/compress.rs +50 -23
- {weft_server-0.1.1 → weft_server-0.2.0}/src/core.rs +43 -22
- {weft_server-0.1.1 → weft_server-0.2.0}/src/fdpoll.rs +16 -2
- {weft_server-0.1.1 → weft_server-0.2.0}/src/h2c.rs +39 -10
- {weft_server-0.1.1 → weft_server-0.2.0}/src/h3c.rs +64 -48
- {weft_server-0.1.1 → weft_server-0.2.0}/src/http.rs +76 -28
- {weft_server-0.1.1 → weft_server-0.2.0}/src/interned.rs +3 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/src/lib.rs +57 -12
- {weft_server-0.1.1 → weft_server-0.2.0}/src/limit.rs +56 -2
- {weft_server-0.1.1 → weft_server-0.2.0}/src/metrics.rs +35 -11
- {weft_server-0.1.1 → weft_server-0.2.0}/src/ops.rs +158 -31
- {weft_server-0.1.1 → weft_server-0.2.0}/src/staticf.rs +168 -17
- {weft_server-0.1.1 → weft_server-0.2.0}/src/tls.rs +190 -52
- {weft_server-0.1.1 → weft_server-0.2.0}/src/types.rs +87 -6
- {weft_server-0.1.1 → weft_server-0.2.0}/src/winaccept.rs +8 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/src/ws.rs +69 -52
- {weft_server-0.1.1 → weft_server-0.2.0}/src/wsgi.rs +445 -43
- {weft_server-0.1.1 → weft_server-0.2.0}/src/wt.rs +40 -15
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/cli.py +41 -6
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/config.py +86 -4
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/contrib/__init__.py +11 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/lifespan.py +11 -4
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/loop.py +32 -1
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/server.py +69 -28
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/worker.py +132 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/LICENSE +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/README.md +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/build.rs +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/pyproject.toml +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/src/py.rs +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/__init__.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/__main__.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/_alloc.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/_gc.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/_weft.pyi +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/contrib/asgi.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/contrib/django.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/contrib/fastapi.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/contrib/uploads.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/log.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/py.typed +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/webtransport.py +0 -0
- {weft_server-0.1.1 → weft_server-0.2.0}/weft/wsgi.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "weft"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "Weft is an ASGI and WSGI server for FastAPI, Flask and the rest. It is written in Rust on Tokio and runs inside the Python thread that runs your application."
|
|
5
5
|
license = "MIT"
|
|
6
6
|
edition = "2024"
|
|
@@ -54,6 +54,13 @@ zstd = "0.13"
|
|
|
54
54
|
[target.'cfg(unix)'.dependencies]
|
|
55
55
|
libc = "0.2"
|
|
56
56
|
|
|
57
|
+
# Python loads the extension with dlopen, and the BSD loaders keep only a
|
|
58
|
+
# small reserve of static TLS for that: mimalloc's default initial-exec TLS
|
|
59
|
+
# makes the import fail ("No space available for static Thread Local
|
|
60
|
+
# Storage"). Local-dynamic TLS is allocated on demand instead.
|
|
61
|
+
[target.'cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly"))'.dependencies]
|
|
62
|
+
mimalloc = { version = "0.1.49", default-features = false, features = ["local_dynamic_tls"], optional = true }
|
|
63
|
+
|
|
57
64
|
[target.'cfg(windows)'.dependencies]
|
|
58
65
|
windows-sys = { version = "0.61", features = [
|
|
59
66
|
"Win32_Foundation",
|
|
@@ -61,6 +68,7 @@ windows-sys = { version = "0.61", features = [
|
|
|
61
68
|
"Win32_Security",
|
|
62
69
|
"Win32_System_IO",
|
|
63
70
|
"Win32_System_Threading",
|
|
71
|
+
"Win32_System_WindowsProgramming",
|
|
64
72
|
] }
|
|
65
73
|
|
|
66
74
|
[build-dependencies]
|
|
@@ -94,9 +94,9 @@ fn parse_len(v: &[u8]) -> Option<u64> {
|
|
|
94
94
|
|
|
95
95
|
/// Parses one request head from `rbuf` and starts the application on it.
|
|
96
96
|
pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut BytesMut) -> Parsed {
|
|
97
|
-
let mut headers = [
|
|
98
|
-
let mut req = httparse::Request::new(&mut
|
|
99
|
-
let n = match req.
|
|
97
|
+
let mut headers = [std::mem::MaybeUninit::uninit(); MAX_HEADERS];
|
|
98
|
+
let mut req = httparse::Request::new(&mut []);
|
|
99
|
+
let n = match req.parse_with_uninit_headers(rbuf, &mut headers) {
|
|
100
100
|
Ok(httparse::Status::Complete(n)) if n > ctx.max_head => return Parsed::Reject(431),
|
|
101
101
|
Ok(httparse::Status::Complete(n)) => n,
|
|
102
102
|
Ok(httparse::Status::Partial) => return Parsed::Partial,
|
|
@@ -163,8 +163,14 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
163
163
|
if (http11 && hosts == 0) || hosts > 1 {
|
|
164
164
|
return Parsed::Reject(400);
|
|
165
165
|
}
|
|
166
|
-
let
|
|
167
|
-
let
|
|
166
|
+
let raw_method = req.method.unwrap_or("GET");
|
|
167
|
+
let method_owned: std::borrow::Cow<'_, str> =
|
|
168
|
+
if raw_method.bytes().any(|b| b.is_ascii_lowercase()) {
|
|
169
|
+
std::borrow::Cow::Owned(raw_method.to_ascii_uppercase())
|
|
170
|
+
} else {
|
|
171
|
+
std::borrow::Cow::Borrowed(raw_method)
|
|
172
|
+
};
|
|
173
|
+
let method = method_owned.as_ref();
|
|
168
174
|
if let Some(path) = &ctx.health
|
|
169
175
|
&& matches!(method, "GET" | "HEAD")
|
|
170
176
|
&& !te
|
|
@@ -190,7 +196,9 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
190
196
|
st.out.extend_from_slice(b"\r\n");
|
|
191
197
|
}
|
|
192
198
|
st.out.extend_from_slice(b"\r\n");
|
|
193
|
-
|
|
199
|
+
if crate::metrics::enabled() {
|
|
200
|
+
st.started = tokio::time::Instant::now();
|
|
201
|
+
}
|
|
194
202
|
st.log_response(if draining { 503 } else { 200 });
|
|
195
203
|
return Parsed::Answered(keep);
|
|
196
204
|
}
|
|
@@ -204,7 +212,11 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
204
212
|
let key = meta
|
|
205
213
|
.client
|
|
206
214
|
.as_deref()
|
|
207
|
-
.map(|c|
|
|
215
|
+
.map(|c| {
|
|
216
|
+
c.parse::<std::net::IpAddr>()
|
|
217
|
+
.map(crate::limit::ip_key)
|
|
218
|
+
.unwrap_or_else(|_| crate::limit::name_key(c.as_bytes()))
|
|
219
|
+
})
|
|
208
220
|
.or_else(|| (!conn.unix).then(|| crate::limit::ip_key(conn.peer.ip())));
|
|
209
221
|
if let Some(wait) = crate::limit::wait(rate, key)
|
|
210
222
|
&& wait > 0
|
|
@@ -223,7 +235,19 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
223
235
|
} else {
|
|
224
236
|
Vec::new()
|
|
225
237
|
};
|
|
226
|
-
let
|
|
238
|
+
let proto = {
|
|
239
|
+
let st = conn.st.borrow();
|
|
240
|
+
if st.h3 {
|
|
241
|
+
3
|
|
242
|
+
} else if st.h2 {
|
|
243
|
+
2
|
|
244
|
+
} else if http11 {
|
|
245
|
+
1
|
|
246
|
+
} else {
|
|
247
|
+
0
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
let log = crate::ops::log_enabled().then(|| log_req(ctx, &req, method, proto));
|
|
227
251
|
let head_only = method == "HEAD";
|
|
228
252
|
rbuf.advance(n);
|
|
229
253
|
let mut st = conn.st.borrow_mut();
|
|
@@ -244,7 +268,7 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
244
268
|
&& matches!(method, "GET" | "HEAD")
|
|
245
269
|
&& !te
|
|
246
270
|
&& length.unwrap_or(0) == 0
|
|
247
|
-
&& let Some(opened) =
|
|
271
|
+
&& let Some(mut opened) =
|
|
248
272
|
crate::staticf::open(&ctx.static_dirs, target, req.headers, ctx.compress_static)
|
|
249
273
|
{
|
|
250
274
|
let (status, _) = opened.precondition(req.headers);
|
|
@@ -261,7 +285,19 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
261
285
|
} else {
|
|
262
286
|
Vec::new()
|
|
263
287
|
};
|
|
264
|
-
let
|
|
288
|
+
let proto = {
|
|
289
|
+
let st = conn.st.borrow();
|
|
290
|
+
if st.h3 {
|
|
291
|
+
3
|
|
292
|
+
} else if st.h2 {
|
|
293
|
+
2
|
|
294
|
+
} else if http11 {
|
|
295
|
+
1
|
|
296
|
+
} else {
|
|
297
|
+
0
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
let log = crate::ops::log_enabled().then(|| log_req(ctx, &req, method, proto));
|
|
265
301
|
let head_only = method == "HEAD";
|
|
266
302
|
rbuf.advance(n);
|
|
267
303
|
let mut st = conn.st.borrow_mut();
|
|
@@ -334,8 +370,7 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
334
370
|
if let Some((Some(key), _, variant, true)) = cache_prep.as_ref()
|
|
335
371
|
&& !chunked
|
|
336
372
|
&& length.unwrap_or(0) == 0
|
|
337
|
-
&& let Some(mut hit) =
|
|
338
|
-
crate::cache::lookup(key, 0, None).or_else(|| crate::cache::lookup(key, *variant, None))
|
|
373
|
+
&& let Some(mut hit) = crate::cache::lookup_any(key, *variant, None)
|
|
339
374
|
{
|
|
340
375
|
let etag = crate::cache::stored_etag(&hit.head);
|
|
341
376
|
if !etag.is_empty() && crate::cache::inm_matches(req.headers, &etag) {
|
|
@@ -354,7 +389,19 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
354
389
|
} else {
|
|
355
390
|
Vec::new()
|
|
356
391
|
};
|
|
357
|
-
let
|
|
392
|
+
let proto = {
|
|
393
|
+
let st = conn.st.borrow();
|
|
394
|
+
if st.h3 {
|
|
395
|
+
3
|
|
396
|
+
} else if st.h2 {
|
|
397
|
+
2
|
|
398
|
+
} else if http11 {
|
|
399
|
+
1
|
|
400
|
+
} else {
|
|
401
|
+
0
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
let log = crate::ops::log_enabled().then(|| log_req(ctx, &req, method, proto));
|
|
358
405
|
let head_only = method == "HEAD" || hit.status == 304;
|
|
359
406
|
rbuf.advance(n);
|
|
360
407
|
let mut st = conn.st.borrow_mut();
|
|
@@ -375,6 +422,10 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
375
422
|
if ctx.wsgi.is_some() && upgrade && to_websocket {
|
|
376
423
|
return Parsed::Reject(501);
|
|
377
424
|
}
|
|
425
|
+
let is_h2_or_h3 = {
|
|
426
|
+
let st = conn.st.borrow();
|
|
427
|
+
st.h2 || st.h3
|
|
428
|
+
};
|
|
378
429
|
if upgrade
|
|
379
430
|
&& to_websocket
|
|
380
431
|
&& http11
|
|
@@ -382,7 +433,7 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
382
433
|
&& ws_version
|
|
383
434
|
&& let Some(key) = ws_key.filter(|k| !k.is_empty())
|
|
384
435
|
{
|
|
385
|
-
if !ctx.ws.enabled {
|
|
436
|
+
if is_h2_or_h3 || !ctx.ws.enabled {
|
|
386
437
|
return Parsed::Reject(501);
|
|
387
438
|
}
|
|
388
439
|
let started = unsafe { crate::ws::start(sh, ctx, conn, &req, key, &meta) };
|
|
@@ -427,7 +478,16 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
427
478
|
};
|
|
428
479
|
}
|
|
429
480
|
if crate::ops::log_enabled() {
|
|
430
|
-
|
|
481
|
+
let proto = if st.h3 {
|
|
482
|
+
3
|
|
483
|
+
} else if st.h2 {
|
|
484
|
+
2
|
|
485
|
+
} else if http11 {
|
|
486
|
+
1
|
|
487
|
+
} else {
|
|
488
|
+
0
|
|
489
|
+
};
|
|
490
|
+
st.log = Some(log_req(ctx, &req, method, proto));
|
|
431
491
|
}
|
|
432
492
|
st.seq
|
|
433
493
|
};
|
|
@@ -445,9 +505,25 @@ pub fn dispatch(sh: &Shared, ctx: &Rc<AppCtx>, conn: &Rc<Conn>, rbuf: &mut Bytes
|
|
|
445
505
|
}
|
|
446
506
|
let started = unsafe { start(sh, ctx, conn, &req, http11, seq, &meta) };
|
|
447
507
|
rbuf.advance(n);
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
508
|
+
match started {
|
|
509
|
+
Ok(true) => {
|
|
510
|
+
let st = conn.st.borrow();
|
|
511
|
+
if !st.disconnected
|
|
512
|
+
&& !st.h2
|
|
513
|
+
&& !st.h3
|
|
514
|
+
&& st.resp.phase == Phase::Done
|
|
515
|
+
&& st.send_file.is_none()
|
|
516
|
+
&& st.body.done
|
|
517
|
+
{
|
|
518
|
+
let keep = !st.resp.close && !sh.closing();
|
|
519
|
+
return Parsed::Answered(keep);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
Ok(false) => {}
|
|
523
|
+
Err(_) => {
|
|
524
|
+
unsafe { py::report_exception(ctx.report.as_ref().map(PyRef::ptr)) };
|
|
525
|
+
conn.st.borrow_mut().write_error(500);
|
|
526
|
+
}
|
|
451
527
|
}
|
|
452
528
|
Parsed::Dispatched
|
|
453
529
|
}
|
|
@@ -499,7 +575,7 @@ fn log_req(
|
|
|
499
575
|
ctx: &AppCtx,
|
|
500
576
|
req: &httparse::Request<'_, '_>,
|
|
501
577
|
method: &str,
|
|
502
|
-
|
|
578
|
+
proto: u8,
|
|
503
579
|
) -> Box<crate::ops::LogReq> {
|
|
504
580
|
let target = req.path.unwrap_or("/").as_bytes();
|
|
505
581
|
let mut line = Vec::with_capacity(method.len() + 1 + target.len());
|
|
@@ -522,7 +598,7 @@ fn log_req(
|
|
|
522
598
|
started: std::time::Instant::now(),
|
|
523
599
|
line,
|
|
524
600
|
method_len: method.len(),
|
|
525
|
-
|
|
601
|
+
proto,
|
|
526
602
|
trace,
|
|
527
603
|
})
|
|
528
604
|
}
|
|
@@ -597,14 +673,18 @@ unsafe fn start(
|
|
|
597
673
|
http11: bool,
|
|
598
674
|
seq: u32,
|
|
599
675
|
meta: &Meta,
|
|
600
|
-
) -> PResult<
|
|
676
|
+
) -> PResult<bool> {
|
|
601
677
|
let i = s();
|
|
602
678
|
unsafe {
|
|
603
679
|
let scope = PyRef::own(PyDict_Copy(ctx.scope_proto.ptr()))?;
|
|
604
680
|
let d = scope.ptr();
|
|
605
|
-
let
|
|
681
|
+
let (is_h2, is_h3) = {
|
|
682
|
+
let st = conn.st.borrow();
|
|
683
|
+
(st.h2, st.h3)
|
|
684
|
+
};
|
|
685
|
+
let ver = if is_h3 {
|
|
606
686
|
i.v3
|
|
607
|
-
} else if
|
|
687
|
+
} else if is_h2 {
|
|
608
688
|
i.v2
|
|
609
689
|
} else if http11 {
|
|
610
690
|
i.v1_1
|
|
@@ -612,9 +692,9 @@ unsafe fn start(
|
|
|
612
692
|
i.v1_0
|
|
613
693
|
};
|
|
614
694
|
py::dict_set(d, i.http_version, PyRef::borrow(ver))?;
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
{
|
|
695
|
+
// Only HTTP/3 can carry WebTransport; HTTP/1 and HTTP/2 keep the
|
|
696
|
+
// default extensions from scope_proto.
|
|
697
|
+
if is_h3 && let Some(ext) = &ctx.h3_extensions {
|
|
618
698
|
py::dict_set(d, i.extensions, ext.clone())?;
|
|
619
699
|
}
|
|
620
700
|
py::dict_set(d, i.method, method_str(req.method.unwrap_or("GET"))?)?;
|
|
@@ -624,13 +704,13 @@ unsafe fn start(
|
|
|
624
704
|
d,
|
|
625
705
|
i.scheme,
|
|
626
706
|
if secure {
|
|
627
|
-
|
|
707
|
+
PyRef::borrow(i.https)
|
|
628
708
|
} else {
|
|
629
709
|
PyRef::borrow(i.http)
|
|
630
710
|
},
|
|
631
711
|
)?;
|
|
632
712
|
}
|
|
633
|
-
launch(sh, ctx, conn, d, receive_vc, send_vc, seq)
|
|
713
|
+
launch(sh, ctx, conn, d, receive_vc, send_vc, seq, ctx.eager)
|
|
634
714
|
}
|
|
635
715
|
}
|
|
636
716
|
|
|
@@ -711,6 +791,9 @@ pub unsafe fn fill_request(
|
|
|
711
791
|
}
|
|
712
792
|
|
|
713
793
|
/// Calls the application with `scope` and wraps the coroutine in a task.
|
|
794
|
+
/// When `eager` is true (HTTP on Python 3.12+), runs the task eagerly and
|
|
795
|
+
/// returns `Ok(true)` if it finished inline without suspending.
|
|
796
|
+
#[allow(clippy::too_many_arguments)]
|
|
714
797
|
pub unsafe fn launch(
|
|
715
798
|
sh: &Shared,
|
|
716
799
|
ctx: &AppCtx,
|
|
@@ -719,24 +802,53 @@ pub unsafe fn launch(
|
|
|
719
802
|
receive_vc: vectorcallfunc,
|
|
720
803
|
send_vc: vectorcallfunc,
|
|
721
804
|
seq: u32,
|
|
722
|
-
|
|
805
|
+
eager: bool,
|
|
806
|
+
) -> PResult<bool> {
|
|
723
807
|
let i = s();
|
|
724
808
|
unsafe {
|
|
725
809
|
let t = types();
|
|
726
810
|
let receive = types::chan(&t.receive, receive_vc, conn.slot, conn.generation, seq)?;
|
|
727
811
|
let send = types::chan(&t.send, send_vc, conn.slot, conn.generation, seq)?;
|
|
728
812
|
let coro = py::call(ctx.app.ptr(), &[d, receive.ptr(), send.ptr()])?;
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
813
|
+
#[cfg(Py_3_12)]
|
|
814
|
+
let task = if eager {
|
|
815
|
+
py::call_kw(
|
|
816
|
+
ctx.task_type.ptr(),
|
|
817
|
+
&[coro.ptr(), ctx.loop_.ptr(), Py_True()],
|
|
818
|
+
1,
|
|
819
|
+
ctx.kw_task.ptr(),
|
|
820
|
+
)?
|
|
821
|
+
} else {
|
|
822
|
+
py::call_kw(
|
|
823
|
+
ctx.task_type.ptr(),
|
|
824
|
+
&[coro.ptr(), ctx.loop_.ptr()],
|
|
825
|
+
1,
|
|
826
|
+
ctx.kw_loop.ptr(),
|
|
827
|
+
)?
|
|
828
|
+
};
|
|
829
|
+
#[cfg(not(Py_3_12))]
|
|
830
|
+
let task = {
|
|
831
|
+
let _ = eager;
|
|
832
|
+
py::call_kw(
|
|
833
|
+
ctx.task_type.ptr(),
|
|
834
|
+
&[coro.ptr(), ctx.loop_.ptr()],
|
|
835
|
+
1,
|
|
836
|
+
ctx.kw_loop.ptr(),
|
|
837
|
+
)?
|
|
838
|
+
};
|
|
839
|
+
#[cfg(Py_3_12)]
|
|
840
|
+
if eager {
|
|
841
|
+
let is_done = py::call_method(i.done, &[task.ptr()])?;
|
|
842
|
+
if py::truthy(is_done.ptr())? {
|
|
843
|
+
finish_task_for(sh, ctx, conn.slot, conn.generation, seq, task.ptr(), true);
|
|
844
|
+
return Ok(true);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
735
847
|
let done = types::chan(&t.done, done_vc, conn.slot, conn.generation, seq)?;
|
|
736
848
|
py::call_method(i.add_done_callback, &[task.ptr(), done.ptr()])?;
|
|
737
849
|
}
|
|
738
850
|
sh.py_scheduled();
|
|
739
|
-
Ok(
|
|
851
|
+
Ok(false)
|
|
740
852
|
}
|
|
741
853
|
|
|
742
854
|
// --- messages ------------------------------------------------------------------
|
|
@@ -890,14 +1002,41 @@ pub unsafe fn worker<'a>() -> PResult<(&'a Shared, Rc<AppCtx>)> {
|
|
|
890
1002
|
Ok((sh, ctx))
|
|
891
1003
|
}
|
|
892
1004
|
|
|
1005
|
+
const TOKEN_CHAR: [bool; 256] = {
|
|
1006
|
+
let mut t = [false; 256];
|
|
1007
|
+
let mut b = 0usize;
|
|
1008
|
+
while b < 256 {
|
|
1009
|
+
let c = b as u8;
|
|
1010
|
+
t[b] = c.is_ascii_alphanumeric()
|
|
1011
|
+
|| matches!(
|
|
1012
|
+
c,
|
|
1013
|
+
b'!' | b'#'
|
|
1014
|
+
| b'$'
|
|
1015
|
+
| b'%'
|
|
1016
|
+
| b'&'
|
|
1017
|
+
| b'\''
|
|
1018
|
+
| b'*'
|
|
1019
|
+
| b'+'
|
|
1020
|
+
| b'-'
|
|
1021
|
+
| b'.'
|
|
1022
|
+
| b'^'
|
|
1023
|
+
| b'_'
|
|
1024
|
+
| b'`'
|
|
1025
|
+
| b'|'
|
|
1026
|
+
| b'~'
|
|
1027
|
+
);
|
|
1028
|
+
b += 1;
|
|
1029
|
+
}
|
|
1030
|
+
t
|
|
1031
|
+
};
|
|
1032
|
+
|
|
893
1033
|
pub fn valid_name(n: &[u8]) -> bool {
|
|
894
|
-
!n.is_empty()
|
|
895
|
-
&& n.iter()
|
|
896
|
-
.all(|&c| c.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&c))
|
|
1034
|
+
!n.is_empty() && n.iter().all(|&c| TOKEN_CHAR[c as usize])
|
|
897
1035
|
}
|
|
898
1036
|
|
|
899
1037
|
pub fn valid_value(v: &[u8]) -> bool {
|
|
900
|
-
|
|
1038
|
+
v.iter()
|
|
1039
|
+
.all(|&c| c == b'\t' || (0x20..=0x7e).contains(&c) || c >= 0x80)
|
|
901
1040
|
}
|
|
902
1041
|
|
|
903
1042
|
/// Appends `name: value\r\n` lines for an ASGI header iterable, noting the
|
|
@@ -911,6 +1050,11 @@ pub unsafe fn encode_headers(
|
|
|
911
1050
|
skip: Option<fn(&[u8]) -> bool>,
|
|
912
1051
|
) -> PResult<()> {
|
|
913
1052
|
unsafe {
|
|
1053
|
+
if (PyList_CheckExact(headers) != 0 && PyList_GET_SIZE(headers) == 0)
|
|
1054
|
+
|| (PyTuple_CheckExact(headers) != 0 && PyTuple_GET_SIZE(headers) == 0)
|
|
1055
|
+
{
|
|
1056
|
+
return Ok(());
|
|
1057
|
+
}
|
|
914
1058
|
let it = PyRef::own(PyObject_GetIter(headers))?;
|
|
915
1059
|
loop {
|
|
916
1060
|
let item = PyIter_Next(it.ptr());
|
|
@@ -978,9 +1122,10 @@ pub unsafe fn encode_headers(
|
|
|
978
1122
|
17 if eq_ci(n, b"transfer-encoding") => {
|
|
979
1123
|
info.chunked = tokens(v).any(|t| eq_ci(t, b"chunked"));
|
|
980
1124
|
if info.chunked {
|
|
981
|
-
// Framing is ours to write
|
|
1125
|
+
// Framing is ours to write; State::write_head emits
|
|
1126
|
+
// `transfer-encoding: chunked` only when HTTP/1.1 framing
|
|
1127
|
+
// actually uses it.
|
|
982
1128
|
out.truncate(start);
|
|
983
|
-
out.extend_from_slice(b"transfer-encoding: chunked\r\n");
|
|
984
1129
|
continue;
|
|
985
1130
|
}
|
|
986
1131
|
}
|
|
@@ -1005,6 +1150,7 @@ unsafe fn send(c: &ChanObj, msg: *mut PyObject) -> PResult<PyRef> {
|
|
|
1005
1150
|
match ty {
|
|
1006
1151
|
b"http.response.start" => http_start(&ctx, c, msg, conn.as_deref()),
|
|
1007
1152
|
b"http.response.body" => http_body(sh, &ctx, c, msg, conn.as_deref()),
|
|
1153
|
+
b"http.response.pathsend" => http_pathsend(sh, &ctx, c, msg, conn.as_deref()),
|
|
1008
1154
|
b"http.response.informational" => {
|
|
1009
1155
|
informational(sh, &ctx, c, msg, conn.as_deref(), false)
|
|
1010
1156
|
}
|
|
@@ -1102,7 +1248,11 @@ pub unsafe fn http_body(
|
|
|
1102
1248
|
let Some(conn) = conn else {
|
|
1103
1249
|
return Ok(ctx.done.clone());
|
|
1104
1250
|
};
|
|
1105
|
-
let date =
|
|
1251
|
+
let date = if ctx.date_header {
|
|
1252
|
+
sh.date()
|
|
1253
|
+
} else {
|
|
1254
|
+
[0u8; 29]
|
|
1255
|
+
};
|
|
1106
1256
|
let mut st = conn.st.borrow_mut();
|
|
1107
1257
|
if st.disconnected || (st.rejected && st.seq == c.seq) {
|
|
1108
1258
|
return Ok(ctx.done.clone());
|
|
@@ -1137,6 +1287,113 @@ pub unsafe fn http_body(
|
|
|
1137
1287
|
}
|
|
1138
1288
|
}
|
|
1139
1289
|
|
|
1290
|
+
/// ASGI `http.response.pathsend` extension: streams a file directly from disk
|
|
1291
|
+
/// using `staticf::SendFile` without reading the file into Python memory.
|
|
1292
|
+
unsafe fn http_pathsend(
|
|
1293
|
+
sh: &Shared,
|
|
1294
|
+
ctx: &AppCtx,
|
|
1295
|
+
c: &ChanObj,
|
|
1296
|
+
msg: *mut PyObject,
|
|
1297
|
+
conn: Option<&Conn>,
|
|
1298
|
+
) -> PResult<PyRef> {
|
|
1299
|
+
unsafe {
|
|
1300
|
+
let i = s();
|
|
1301
|
+
let path_obj = py::dict_get(msg, i.path)?
|
|
1302
|
+
.ok_or_else(|| py::raise(PyExc_KeyError, "'http.response.pathsend' has no 'path'"))?;
|
|
1303
|
+
let fspath = PyRef::own(PyOS_FSPath(path_obj.ptr()))?;
|
|
1304
|
+
let path_str = if PyUnicode_Check(fspath.ptr()) != 0 {
|
|
1305
|
+
std::str::from_utf8(py::str_view(fspath.ptr())?)
|
|
1306
|
+
.map_err(|_| py::raise(PyExc_ValueError, "path must be valid UTF-8"))?
|
|
1307
|
+
.to_owned()
|
|
1308
|
+
} else {
|
|
1309
|
+
return Err(py::type_error(
|
|
1310
|
+
"'http.response.pathsend' path must be str or PathLike",
|
|
1311
|
+
));
|
|
1312
|
+
};
|
|
1313
|
+
let file = std::fs::File::open(&path_str)
|
|
1314
|
+
.map_err(|e| py::raise(PyExc_OSError, &format!("{path_str}: {e}")))?;
|
|
1315
|
+
let meta = file
|
|
1316
|
+
.metadata()
|
|
1317
|
+
.map_err(|e| py::raise(PyExc_OSError, &format!("{path_str}: {e}")))?;
|
|
1318
|
+
if !meta.is_file() {
|
|
1319
|
+
return Err(py::raise(
|
|
1320
|
+
PyExc_OSError,
|
|
1321
|
+
&format!("{path_str}: not a regular file"),
|
|
1322
|
+
));
|
|
1323
|
+
}
|
|
1324
|
+
let size = meta.len();
|
|
1325
|
+
let Some(conn) = conn else {
|
|
1326
|
+
return Ok(ctx.done.clone());
|
|
1327
|
+
};
|
|
1328
|
+
let date = if ctx.date_header {
|
|
1329
|
+
sh.date()
|
|
1330
|
+
} else {
|
|
1331
|
+
[0u8; 29]
|
|
1332
|
+
};
|
|
1333
|
+
let mut st = conn.st.borrow_mut();
|
|
1334
|
+
if st.disconnected || (st.rejected && st.seq == c.seq) {
|
|
1335
|
+
return Ok(ctx.done.clone());
|
|
1336
|
+
}
|
|
1337
|
+
if st.seq != c.seq || st.resp.phase == Phase::Done {
|
|
1338
|
+
drop(st);
|
|
1339
|
+
return Err(py::runtime_error(
|
|
1340
|
+
"Unexpected ASGI message 'http.response.pathsend': the response has already completed",
|
|
1341
|
+
));
|
|
1342
|
+
}
|
|
1343
|
+
if st.resp.phase == Phase::Idle {
|
|
1344
|
+
drop(st);
|
|
1345
|
+
return Err(py::runtime_error(
|
|
1346
|
+
"Expected ASGI message 'http.response.start', but got 'http.response.pathsend'",
|
|
1347
|
+
));
|
|
1348
|
+
}
|
|
1349
|
+
if st.resp.phase == Phase::Body {
|
|
1350
|
+
drop(st);
|
|
1351
|
+
return Err(py::runtime_error(
|
|
1352
|
+
"Unexpected ASGI message 'http.response.pathsend' after 'http.response.body'",
|
|
1353
|
+
));
|
|
1354
|
+
}
|
|
1355
|
+
// The file is the whole body: its size is the framing, whatever
|
|
1356
|
+
// framing the start message asked for.
|
|
1357
|
+
if let Some(n) = st.resp.info.length
|
|
1358
|
+
&& n != size
|
|
1359
|
+
{
|
|
1360
|
+
drop(st);
|
|
1361
|
+
return Err(py::raise(
|
|
1362
|
+
PyExc_ValueError,
|
|
1363
|
+
&format!("{path_str}: content-length {n} does not match the file size {size}"),
|
|
1364
|
+
));
|
|
1365
|
+
}
|
|
1366
|
+
st.resp.info.length = Some(size);
|
|
1367
|
+
st.resp.info.chunked = false;
|
|
1368
|
+
// The body bypasses the capture the response cache reads from.
|
|
1369
|
+
st.cache_ok = false;
|
|
1370
|
+
st.cache_key = None;
|
|
1371
|
+
let stopping = sh.closing();
|
|
1372
|
+
// `pathsend` streams raw file bytes directly via `SendFile`.
|
|
1373
|
+
st.accepted = crate::compress::Coding::Identity;
|
|
1374
|
+
st.write_head(size as usize, false, ctx, &date, stopping);
|
|
1375
|
+
st.resp.phase = Phase::Done;
|
|
1376
|
+
let no_body =
|
|
1377
|
+
st.head_req || st.resp.status < 200 || st.resp.status == 204 || st.resp.status == 304;
|
|
1378
|
+
if !no_body && size > 0 {
|
|
1379
|
+
st.send_file = Some(Box::new(crate::staticf::SendFile {
|
|
1380
|
+
file,
|
|
1381
|
+
remaining: size,
|
|
1382
|
+
}));
|
|
1383
|
+
crate::staticf::pump(&mut st);
|
|
1384
|
+
}
|
|
1385
|
+
if crate::http::flush_st(conn, &mut st).is_err() {
|
|
1386
|
+
st.disconnected = true;
|
|
1387
|
+
st.out.clear();
|
|
1388
|
+
st.send_file = None;
|
|
1389
|
+
}
|
|
1390
|
+
let queued = st.out.len();
|
|
1391
|
+
drop(st);
|
|
1392
|
+
conn.notify(sh);
|
|
1393
|
+
backpressure(sh, ctx, conn, queued)
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1140
1397
|
/// Fields that describe a connection or a body, which a 1xx has neither of.
|
|
1141
1398
|
fn interim_forbidden(n: &[u8]) -> bool {
|
|
1142
1399
|
[
|
|
@@ -1343,6 +1600,21 @@ unsafe fn receive(c: &ChanObj) -> PResult<PyRef> {
|
|
|
1343
1600
|
unsafe fn task_done(c: &ChanObj, task: *mut PyObject) -> PResult<PyRef> {
|
|
1344
1601
|
unsafe {
|
|
1345
1602
|
let (sh, ctx) = worker()?;
|
|
1603
|
+
finish_task_for(sh, &ctx, c.slot, c.generation, c.seq, task, false);
|
|
1604
|
+
Ok(py::none())
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
unsafe fn finish_task_for(
|
|
1609
|
+
sh: &Shared,
|
|
1610
|
+
ctx: &AppCtx,
|
|
1611
|
+
slot: u32,
|
|
1612
|
+
generation: u32,
|
|
1613
|
+
seq: u32,
|
|
1614
|
+
task: *mut PyObject,
|
|
1615
|
+
inline: bool,
|
|
1616
|
+
) {
|
|
1617
|
+
unsafe {
|
|
1346
1618
|
let report = ctx.report.as_ref().map(PyRef::ptr);
|
|
1347
1619
|
let failed = match py::call_method(s().exception, &[task]) {
|
|
1348
1620
|
Ok(exc) if py::is_none(exc.ptr()) => false,
|
|
@@ -1360,9 +1632,9 @@ unsafe fn task_done(c: &ChanObj, task: *mut PyObject) -> PResult<PyRef> {
|
|
|
1360
1632
|
true
|
|
1361
1633
|
}
|
|
1362
1634
|
};
|
|
1363
|
-
if let Some(conn) = sh.conn(
|
|
1635
|
+
if let Some(conn) = sh.conn(slot, generation) {
|
|
1364
1636
|
let mut st = conn.st.borrow_mut();
|
|
1365
|
-
if st.seq ==
|
|
1637
|
+
if st.seq == seq && st.ws.is_some() {
|
|
1366
1638
|
crate::ws::task_finished(&mut st, failed, sh.stopping.get());
|
|
1367
1639
|
if !st.disconnected && crate::http::flush_st(&conn, &mut st).is_err() {
|
|
1368
1640
|
st.disconnected = true;
|
|
@@ -1370,12 +1642,12 @@ unsafe fn task_done(c: &ChanObj, task: *mut PyObject) -> PResult<PyRef> {
|
|
|
1370
1642
|
}
|
|
1371
1643
|
drop(st);
|
|
1372
1644
|
conn.notify(sh);
|
|
1373
|
-
} else if st.seq ==
|
|
1645
|
+
} else if st.seq == seq && st.wt.is_some() {
|
|
1374
1646
|
crate::wt::task_finished(&mut st, failed);
|
|
1375
1647
|
drop(st);
|
|
1376
1648
|
crate::wt::wake_recv(&conn);
|
|
1377
1649
|
conn.notify(sh);
|
|
1378
|
-
} else if st.seq ==
|
|
1650
|
+
} else if st.seq == seq && !st.disconnected {
|
|
1379
1651
|
match st.resp.phase {
|
|
1380
1652
|
Phase::Idle => {
|
|
1381
1653
|
if !failed {
|
|
@@ -1393,14 +1665,29 @@ unsafe fn task_done(c: &ChanObj, task: *mut PyObject) -> PResult<PyRef> {
|
|
|
1393
1665
|
}
|
|
1394
1666
|
Phase::Done => {}
|
|
1395
1667
|
}
|
|
1396
|
-
|
|
1397
|
-
st.
|
|
1398
|
-
st.
|
|
1668
|
+
let batched = !inline
|
|
1669
|
+
&& !st.out.is_empty()
|
|
1670
|
+
&& st.resp.phase == Phase::Done
|
|
1671
|
+
&& !st.resp.close
|
|
1672
|
+
&& !st.h2
|
|
1673
|
+
&& !st.h3
|
|
1674
|
+
&& !sh.closing()
|
|
1675
|
+
&& st.send_file.is_none();
|
|
1676
|
+
if batched {
|
|
1677
|
+
drop(st);
|
|
1678
|
+
crate::http::flush_soon(sh, &conn);
|
|
1679
|
+
conn.notify(sh);
|
|
1680
|
+
} else {
|
|
1681
|
+
if !st.out.is_empty() && crate::http::flush_st(&conn, &mut st).is_err() {
|
|
1682
|
+
st.disconnected = true;
|
|
1683
|
+
st.out.clear();
|
|
1684
|
+
}
|
|
1685
|
+
drop(st);
|
|
1686
|
+
if !inline {
|
|
1687
|
+
conn.notify(sh);
|
|
1688
|
+
}
|
|
1399
1689
|
}
|
|
1400
|
-
drop(st);
|
|
1401
|
-
conn.notify(sh);
|
|
1402
1690
|
}
|
|
1403
1691
|
}
|
|
1404
|
-
Ok(py::none())
|
|
1405
1692
|
}
|
|
1406
1693
|
}
|