Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/expect-continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ impl Service for UploadService {
type Body = &'static str;
type Error = Infallible;

fn call(&self, _req: Request<Body>) -> Result<http::Response<Self::Body>, Self::Error> {
fn call(&mut self, _req: Request<Body>) -> Result<http::Response<Self::Body>, Self::Error> {
Ok(Response::builder()
.status(StatusCode::OK)
.body("Thanks for the info!")
.unwrap())
}

fn should_continue(&self, req: &Request<Body>) -> StatusCode {
fn should_continue(&mut self, req: &Request<Body>) -> StatusCode {
match req.headers().typed_get::<headers::ContentLength>() {
Some(len) if len.0 <= self.max_length => StatusCode::CONTINUE,
_ => StatusCode::EXPECTATION_FAILED,
Expand Down
11 changes: 11 additions & 0 deletions examples/single-thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use touche::{Response, Server, StatusCode};

fn main() -> std::io::Result<()> {
let mut counter = 0;
Server::bind("0.0.0.0:4444").serve_single_thread(|_| {
counter += 1;
Response::builder()
.status(StatusCode::OK)
.body(format!("Request count: {}", counter))
})
}
33 changes: 15 additions & 18 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ type IncomingRequest = Request<Body>;
/// type Body = &'static str;
/// type Error = Infallible;
///
/// fn call(&self, _req: Request<Body>) -> Result<http::Response<Self::Body>, Self::Error> {
/// fn call(&mut self, _req: Request<Body>) -> Result<http::Response<Self::Body>, Self::Error> {
/// Ok(Response::builder()
/// .status(StatusCode::OK)
/// .body("Thanks for the info!")
/// .unwrap())
/// }
///
/// fn should_continue(&self, req: &Request<Body>) -> StatusCode {
/// fn should_continue(&mut self, req: &Request<Body>) -> StatusCode {
/// match req.headers().typed_get::<headers::ContentLength>() {
/// Some(len) if len.0 <= self.max_length => StatusCode::CONTINUE,
/// _ => StatusCode::EXPECTATION_FAILED,
Expand All @@ -95,23 +95,23 @@ pub trait Service {
type Body: HttpBody;
type Error: Into<Box<dyn Error + Send + Sync>>;

fn call(&self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error>;
fn call(&mut self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error>;

fn should_continue(&self, _: &IncomingRequest) -> StatusCode {
fn should_continue(&mut self, _: &IncomingRequest) -> StatusCode {
StatusCode::CONTINUE
}
}

impl<F, Body, Err> Service for F
where
F: Fn(IncomingRequest) -> Result<Response<Body>, Err>,
F: FnMut(IncomingRequest) -> Result<Response<Body>, Err>,
Body: HttpBody,
Err: Into<Box<dyn Error + Send + Sync>>,
{
type Body = Body;
type Error = Err;

fn call(&self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error> {
fn call(&mut self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error> {
self(request)
}
}
Expand Down Expand Up @@ -163,9 +163,9 @@ impl Server<'_> {
S: Send + Clone + 'static,
{
for conn in self.incoming {
let app = service.clone();
let mut app = service.clone();
self.thread_pool.execute(move || {
serve(conn, app).ok();
serve(conn, &mut app).ok();
});
}

Expand All @@ -187,13 +187,12 @@ impl Server<'_> {
/// })
/// # }
/// ```
pub fn serve_single_thread<S>(self, service: S) -> io::Result<()>
pub fn serve_single_thread<S>(self, mut service: S) -> io::Result<()>
where
S: Service + Clone,
S: Service,
{
for conn in self.incoming {
let app = service.clone();
serve(conn, app).ok();
serve(conn, &mut service).ok();
}
Ok(())
}
Expand Down Expand Up @@ -221,15 +220,13 @@ impl Server<'_> {
/// ```
pub fn make_service<M>(self, make_service: M) -> io::Result<()>
where
M: MakeService,
M: Clone + 'static,
M: MakeService + 'static,
<M as MakeService>::Service: Send,
{
for conn in self.incoming {
let app = make_service.clone();
if let Ok(handler) = app.call(&conn) {
if let Ok(mut handler) = make_service.call(&conn) {
self.thread_pool.execute(move || {
serve(conn, handler).ok();
serve(conn, &mut handler).ok();
});
}
}
Expand Down Expand Up @@ -404,7 +401,7 @@ where
}
}

fn serve<C: Into<Connection>, A: Service>(stream: C, app: A) -> io::Result<()> {
fn serve<C: Into<Connection>, A: Service>(stream: C, app: &mut A) -> io::Result<()> {
let conn = stream.into();
let mut read_queue = ReadQueue::new(BufReader::new(conn.clone()));

Expand Down
Loading