Skip to content

Commit 8025264

Browse files
authored
RequestInfo: Replace custom struct with Request<Bytes> (#33)
1 parent 8d9a8b0 commit 8025264

File tree

2 files changed

+13
-47
lines changed

2 files changed

+13
-47
lines changed

src/adaptor.rs

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,25 @@ use std::net::SocketAddr;
1414

1515
use conduit::{Extensions, HeaderMap, Host, Method, RequestExt, Scheme, StartInstant, Version};
1616
use http::request::Parts as HttpParts;
17+
use http::Request;
1718
use hyper::body::Bytes;
1819

19-
/// Owned data consumed by the background thread
20-
///
21-
/// `ConduitRequest` cannot be sent between threads, so the needed request data
22-
/// is extracted from hyper on a core thread and taken by the background thread.
23-
pub(crate) struct RequestInfo(Option<(Parts, Bytes)>);
24-
25-
impl RequestInfo {
26-
/// Save the request info that can be sent between threads
27-
pub(crate) fn new(parts: HttpParts, body: Bytes) -> Self {
28-
let tuple = (Parts(parts), body);
29-
Self(Some(tuple))
30-
}
31-
32-
/// Take back the request info
33-
///
34-
/// Call this from the background thread to obtain ownership of the `Send` data.
35-
///
36-
/// # Panics
37-
///
38-
/// Panics if called more than once on a value.
39-
fn take(&mut self) -> (Parts, Bytes) {
40-
self.0.take().expect("called take multiple times")
41-
}
42-
}
43-
44-
#[derive(Debug)]
45-
pub(crate) struct Parts(HttpParts);
46-
47-
impl Parts {
48-
fn headers(&self) -> &HeaderMap {
49-
&self.0.headers
50-
}
51-
}
52-
5320
pub(crate) struct ConduitRequest {
54-
parts: Parts,
21+
parts: HttpParts,
5522
path: String,
5623
remote_addr: SocketAddr,
5724
body: Cursor<Bytes>,
5825
}
5926

6027
impl ConduitRequest {
61-
pub(crate) fn new(info: &mut RequestInfo, remote_addr: SocketAddr, now: StartInstant) -> Self {
62-
let (mut parts, body) = info.take();
63-
let path = parts.0.uri.path().as_bytes();
28+
pub(crate) fn new(request: Request<Bytes>, remote_addr: SocketAddr, now: StartInstant) -> Self {
29+
let (mut parts, body) = request.into_parts();
30+
let path = parts.uri.path().as_bytes();
6431
let path = percent_encoding::percent_decode(path)
6532
.decode_utf8_lossy()
6633
.into_owned();
6734

68-
parts.0.extensions.insert(now);
35+
parts.extensions.insert(now);
6936

7037
Self {
7138
parts,
@@ -76,7 +43,7 @@ impl ConduitRequest {
7643
}
7744

7845
fn parts(&self) -> &HttpParts {
79-
&self.parts.0
46+
&self.parts
8047
}
8148
}
8249

@@ -95,7 +62,7 @@ impl RequestExt for ConduitRequest {
9562
}
9663

9764
fn headers(&self) -> &HeaderMap {
98-
self.parts.headers()
65+
&self.parts.headers
9966
}
10067

10168
/// Returns the length of the buffered body
@@ -121,19 +88,18 @@ impl RequestExt for ConduitRequest {
12188
}
12289

12390
fn extensions(&self) -> &Extensions {
124-
&self.parts.0.extensions
91+
&self.parts.extensions
12592
}
12693

12794
fn mut_extensions(&mut self) -> &mut Extensions {
128-
&mut self.parts.0.extensions
95+
&mut self.parts.extensions
12996
}
13097

13198
/// Returns the value of the `Host` header
13299
///
133100
/// If the header is not present or is invalid UTF-8, then the empty string is returned
134101
fn host(&self) -> Host<'_> {
135102
let host = self
136-
.parts
137103
.headers()
138104
.get(http::header::HOST)
139105
.map(|h| h.to_str().unwrap_or(""))

src/service/blocking_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::adaptor::{ConduitRequest, RequestInfo};
1+
use crate::adaptor::ConduitRequest;
22
use crate::file_stream::FileStream;
33
use crate::service::ServiceError;
44
use crate::{ConduitResponse, HyperResponse};
@@ -32,11 +32,11 @@ impl<H: Handler> BlockingHandler<H> {
3232
let now = StartInstant::now();
3333

3434
let full_body = hyper::body::to_bytes(body).await?;
35-
let mut request_info = RequestInfo::new(parts, full_body);
35+
let request = Request::from_parts(parts, full_body);
3636

3737
let handler = self.handler.clone();
3838
tokio::task::spawn_blocking(move || {
39-
let mut request = ConduitRequest::new(&mut request_info, remote_addr, now);
39+
let mut request = ConduitRequest::new(request, remote_addr, now);
4040
handler
4141
.call(&mut request)
4242
.map(conduit_into_hyper)

0 commit comments

Comments
 (0)