Skip to content

Commit 9224ab9

Browse files
committed
Improve state management
1 parent 232c834 commit 9224ab9

File tree

8 files changed

+579
-325
lines changed

8 files changed

+579
-325
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"build:debug": "napi build --platform --features napi-support",
2929
"prepublishOnly": "napi prepublish -t npm",
3030
"lint": "oxlint",
31-
"test": "node --test test/**.test.mjs",
31+
"test": "node --test test/**/*.test.mjs",
3232
"universal": "napi universal",
3333
"version": "napi version"
3434
}

src/asgi/http.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use http_handler::{Request, RequestExt, Version};
12
use pyo3::exceptions::PyValueError;
23
use pyo3::prelude::*;
34
use pyo3::types::{PyAny, PyDict};
4-
use http_handler::{Request, RequestExt, Version};
55
use std::net::SocketAddr;
66

77
use crate::asgi::{AsgiInfo, HttpMethod, HttpVersion};
@@ -70,24 +70,18 @@ impl TryFrom<&Request> for HttpConnectionScope {
7070
fn try_from(request: &Request) -> Result<Self, Self::Error> {
7171
// Extract HTTP version
7272
let http_version = match request.version() {
73-
Version::HTTP_09 => HttpVersion::V1_0, // fallback for HTTP/0.9
7473
Version::HTTP_10 => HttpVersion::V1_0,
7574
Version::HTTP_11 => HttpVersion::V1_1,
7675
Version::HTTP_2 => HttpVersion::V2_0,
7776
Version::HTTP_3 => HttpVersion::V2_0, // treat HTTP/3 as HTTP/2 for ASGI
78-
_ => HttpVersion::V1_1, // default fallback
77+
_ => HttpVersion::V1_1, // default fallback
7978
};
8079

8180
// Extract method
82-
let method = request.method().try_into()
83-
.map_err(PyValueError::new_err)?;
81+
let method = request.method().try_into().map_err(PyValueError::new_err)?;
8482

8583
// Extract scheme from URI or default to http
86-
let scheme = request
87-
.uri()
88-
.scheme_str()
89-
.unwrap_or("http")
90-
.to_string();
84+
let scheme = request.uri().scheme_str().unwrap_or("http").to_string();
9185

9286
// Extract path
9387
let path = request.uri().path().to_string();
@@ -96,11 +90,7 @@ impl TryFrom<&Request> for HttpConnectionScope {
9690
let raw_path = path.clone();
9791

9892
// Extract query string
99-
let query_string = request
100-
.uri()
101-
.query()
102-
.unwrap_or("")
103-
.to_string();
93+
let query_string = request.uri().query().unwrap_or("").to_string();
10494

10595
// Extract root path from DocumentRoot extension
10696
let root_path = request
@@ -322,11 +312,9 @@ impl<'py> FromPyObject<'py> for HttpSendMessage {
322312
})?
323313
.extract()?;
324314

325-
let headers_py = dict
326-
.get_item("headers")?
327-
.ok_or_else(|| {
328-
PyValueError::new_err("Missing 'headers' key in HTTP response start message")
329-
})?;
315+
let headers_py = dict.get_item("headers")?.ok_or_else(|| {
316+
PyValueError::new_err("Missing 'headers' key in HTTP response start message")
317+
})?;
330318

331319
// Convert headers from list of lists to vec of tuples
332320
let mut headers: Vec<(String, String)> = Vec::new();
@@ -395,9 +383,12 @@ pub enum HttpSendException {
395383
#[cfg(test)]
396384
mod tests {
397385
use super::*;
398-
use http_handler::{RequestExt, extensions::DocumentRoot};
399386
use http_handler::{Method, Version, request::Builder};
400-
use std::{net::{IpAddr, Ipv4Addr, SocketAddr}, path::PathBuf};
387+
use http_handler::{RequestExt, extensions::DocumentRoot};
388+
use std::{
389+
net::{IpAddr, Ipv4Addr, SocketAddr},
390+
path::PathBuf,
391+
};
401392

402393
macro_rules! dict_get {
403394
($dict:expr, $key:expr) => {
@@ -439,10 +430,13 @@ mod tests {
439430

440431
// Set document root extension
441432
let doc_root = PathBuf::from("/var/www/html");
442-
request.set_document_root(DocumentRoot { path: doc_root.clone() });
433+
request.set_document_root(DocumentRoot {
434+
path: doc_root.clone(),
435+
});
443436

444437
// Convert to ASGI scope
445-
let scope: HttpConnectionScope = (&request).try_into()
438+
let scope: HttpConnectionScope = (&request)
439+
.try_into()
446440
.expect("Failed to convert request to HttpConnectionScope");
447441

448442
// Verify HTTP version
@@ -494,7 +488,8 @@ mod tests {
494488
.body(bytes::BytesMut::new())
495489
.unwrap();
496490

497-
let scope: HttpConnectionScope = (&request).try_into()
491+
let scope: HttpConnectionScope = (&request)
492+
.try_into()
498493
.expect("Failed to convert request to HttpConnectionScope");
499494

500495
assert_eq!(scope.http_version, HttpVersion::V1_1);
@@ -520,7 +515,8 @@ mod tests {
520515
.body(bytes::BytesMut::new())
521516
.unwrap();
522517

523-
let scope: HttpConnectionScope = (&request).try_into()
518+
let scope: HttpConnectionScope = (&request)
519+
.try_into()
524520
.expect("Failed to convert request to HttpConnectionScope");
525521

526522
assert_eq!(scope.http_version, HttpVersion::V2_0);

0 commit comments

Comments
 (0)