Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 0deff5b

Browse files
committed
Improve the conduit::Body type
The `WriteBody` trait was only used to support sending files via `conduit-static` and child process standard output via `conduit-git-http-backend`. The git functionality is only used by crates.io in development mode, so I have focused on file handling functionality. The new structure allows the blocking `civet` server to continue using `io::copy` while `conduit-hyper` can turn the blocking `File` into an async `tokio::fs::File` and free the background thread to handle other requests. For now, `conduit-git-http-backend` will fall back to buffering the response into a `Vec<u8>` rather than streaming chunks of data as it arrives from the child process. This seems like an acceptable tradeoff given the functionality is only used when doing local development within a docker container.
1 parent e932958 commit 0deff5b

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "conduit"
4-
version = "0.9.0-alpha.1"
4+
version = "0.9.0-alpha.2"
55
authors = ["[email protected]",
66
"Alex Crichton <[email protected]>"]
77
description = "Common HTTP server interface"

example/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ version = "0.0.1"
44
authors = ["Example User <[email protected]>"]
55

66
[dependencies]
7-
civet = "0.12.0-alpha.2"
8-
conduit = "0.9.0-alpha.1"
9-
conduit-router = "0.9.0-alpha.1"
7+
civet = "0.12.0-alpha.3"
8+
conduit = "0.9.0-alpha.2"
9+
conduit-router = "0.9.0-alpha.2"

example/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ extern crate conduit_router;
55
use std::sync::mpsc::channel;
66

77
use civet::{Config, Server};
8-
use conduit::{static_to_body, vec_to_body, HttpResult, RequestExt, Response};
8+
use conduit::{Body, HttpResult, RequestExt, Response};
99
use conduit_router::{RequestParams, RouteBuilder};
1010

1111
fn name(req: &mut dyn RequestExt) -> HttpResult {
1212
let name = req.params().find("name").unwrap();
1313
let bytes = format!("Hello {}!", name).into_bytes();
14-
Response::builder().body(vec_to_body(bytes))
14+
Response::builder().body(Body::from_vec(bytes))
1515
}
1616

1717
fn hello(_req: &mut dyn RequestExt) -> HttpResult {
18-
Response::builder().body(static_to_body(b"Hello world!"))
18+
Response::builder().body(Body::from_static(b"Hello world!"))
1919
}
2020

2121
fn main() {

src/lib.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,39 @@
33
extern crate http;
44

55
use std::error::Error;
6-
use std::io::{self, prelude::*, Cursor};
6+
use std::fs::File;
7+
use std::io::Read;
78
use std::net::SocketAddr;
89

910
pub use http::{header, HeaderMap, Method, Request, Response, StatusCode, Version};
1011

1112
pub use self::typemap::TypeMap;
1213
mod typemap;
1314

14-
pub type Body = Box<dyn WriteBody>;
1515
pub type ResponseResult<Error> = Result<Response<Body>, Error>;
1616
pub type HttpResult = ResponseResult<http::Error>;
1717

1818
pub type BoxError = Box<dyn Error + Send>;
1919
pub type HandlerResult = Result<Response<Body>, BoxError>;
2020

21-
pub fn static_to_body(bytes: &'static [u8]) -> Body {
22-
Box::new(bytes)
21+
pub enum Body {
22+
Static(&'static [u8]),
23+
Owned(Vec<u8>),
24+
File(File),
2325
}
2426

25-
pub fn vec_to_body(bytes: Vec<u8>) -> Body {
26-
Box::new(Cursor::new(bytes))
27+
impl Body {
28+
pub fn empty() -> Self {
29+
Self::from_static(b"")
30+
}
31+
32+
pub fn from_static(bytes: &'static [u8]) -> Self {
33+
Self::Static(bytes)
34+
}
35+
36+
pub fn from_vec(bytes: Vec<u8>) -> Self {
37+
Self::Owned(bytes)
38+
}
2739
}
2840

2941
pub fn box_error<E: Error + Send + 'static>(error: E) -> BoxError {
@@ -104,19 +116,3 @@ where
104116
(*self)(request).map_err(box_error)
105117
}
106118
}
107-
108-
/// A trait which writes the response body out to a `Write`r.
109-
///
110-
/// This is implemented for all `Read`ers.
111-
pub trait WriteBody {
112-
fn write_body(&mut self, out: &mut dyn Write) -> io::Result<u64>;
113-
}
114-
115-
impl<R> WriteBody for R
116-
where
117-
R: Read,
118-
{
119-
fn write_body(&mut self, out: &mut dyn Write) -> io::Result<u64> {
120-
io::copy(self, out)
121-
}
122-
}

0 commit comments

Comments
 (0)