Skip to content

Commit ee5f05e

Browse files
committed
Remove http-service
1 parent 00cb0f3 commit ee5f05e

File tree

6 files changed

+37
-80
lines changed

6 files changed

+37
-80
lines changed

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
2424

2525
[features]
2626
default = ["h1-server"]
27-
h1-server = ["http-service-h1"]
27+
h1-server = ["async-h1"]
2828
docs = ["unstable"]
2929
unstable = []
3030

3131
[dependencies]
3232
async-sse = "2.1.0"
3333
http-types = "1.0.1"
34-
http-service = "0.5.0"
35-
http-service-h1 = { version = "0.1.0", optional = true }
34+
async-h1 = { version = "1.1.2", optional = true }
3635
route-recognizer = "0.1.13"
3736
serde = "1.0.102"
3837
serde_json = "1.0.41"
@@ -54,7 +53,6 @@ basic-cookies = "0.1.3"
5453
bytes = "0.4.12"
5554
futures-fs = "0.0.5"
5655
futures-util = { version = "0.3.0", features = ["compat"] }
57-
http-service-mock = "0.5.0"
5856
juniper = "0.14.1"
5957
mime = "0.3.14"
6058
mime_guess = "2.0.1"

src/middleware.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
33
use std::sync::Arc;
44

5-
#[doc(inline)]
6-
pub use http_service::HttpService;
7-
85
use crate::endpoint::DynEndpoint;
96
use crate::utils::BoxFuture;
107
use crate::Request;

src/request.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
use cookie::Cookie;
2-
use http_types::{
3-
headers::{HeaderName, HeaderValue},
4-
Method, StatusCode, Url, Version,
5-
};
62
use route_recognizer::Params;
73
use serde::Deserialize;
84

@@ -13,6 +9,8 @@ use std::pin::Pin;
139
use std::{str::FromStr, sync::Arc};
1410

1511
use crate::cookies::CookieData;
12+
use crate::http::headers::{HeaderName, HeaderValue};
13+
use crate::http::{self, Method, StatusCode, Url, Version};
1614
use crate::Response;
1715

1816
/// An HTTP request.
@@ -25,7 +23,7 @@ use crate::Response;
2523
#[derive(Debug)]
2624
pub struct Request<State> {
2725
pub(crate) state: Arc<State>,
28-
pub(crate) request: http_service::Request,
26+
pub(crate) request: http::Request,
2927
pub(crate) route_params: Vec<Params>,
3028
}
3129

src/response.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ use async_std::io::prelude::*;
22
use std::convert::TryFrom;
33

44
use cookie::Cookie;
5-
use http_service::Body;
6-
use http_types::{
7-
headers::{HeaderName, HeaderValue},
8-
StatusCode,
9-
};
105
use mime::Mime;
116
use serde::Serialize;
127

8+
use crate::http::headers::{HeaderName, HeaderValue};
9+
use crate::http::{self, Body, StatusCode};
10+
1311
#[derive(Debug)]
1412
pub(crate) enum CookieEvent {
1513
Added(Cookie<'static>),
@@ -19,7 +17,7 @@ pub(crate) enum CookieEvent {
1917
/// An HTTP response
2018
#[derive(Debug)]
2119
pub struct Response {
22-
pub(crate) res: http_service::Response,
20+
pub(crate) res: http::Response,
2321
// tracking here
2422
pub(crate) cookie_events: Vec<CookieEvent>,
2523
}
@@ -246,14 +244,14 @@ impl Response {
246244
}
247245
}
248246

249-
impl Into<http_service::Response> for Response {
250-
fn into(self) -> http_service::Response {
247+
impl Into<http::Response> for Response {
248+
fn into(self) -> http::Response {
251249
self.res
252250
}
253251
}
254252

255-
impl From<http_service::Response> for Response {
256-
fn from(res: http_service::Response) -> Self {
253+
impl From<http::Response> for Response {
254+
fn from(res: http::Response) -> Self {
257255
Self {
258256
res,
259257
cookie_events: vec![],

src/security/cors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::utils::BoxFuture;
22
use http_types::headers::HeaderValue;
33
use http_types::{headers, Method, StatusCode};
44

5+
use crate::http;
56
use crate::middleware::{Middleware, Next};
67
use crate::{Request, Result};
78

@@ -167,7 +168,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CorsMiddleware {
167168
return Ok(self.build_preflight_response(&origins).into());
168169
}
169170

170-
let mut response: http_service::Response = next.run(req).await?.into();
171+
let mut response: http::Response = next.run(req).await?.into();
171172
response
172173
.insert_header(
173174
headers::ACCESS_CONTROL_ALLOW_ORIGIN,

src/server.rs

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
//! An HTTP server
22
3-
use async_std::future::Future;
43
use async_std::io;
54
use async_std::net::ToSocketAddrs;
5+
use async_std::prelude::*;
66
use async_std::sync::Arc;
7-
use async_std::task::{Context, Poll};
8-
use http_service::HttpService;
7+
use async_std::task;
98

109
use std::fmt::Debug;
11-
use std::pin::Pin;
1210

1311
use crate::cookies;
1412
use crate::log;
@@ -281,14 +279,31 @@ impl<State: Send + Sync + 'static> Server<State> {
281279

282280
/// Asynchronously serve the app at the given address.
283281
#[cfg(feature = "h1-server")]
284-
pub async fn listen(self, addr: impl ToSocketAddrs) -> std::io::Result<()> {
282+
pub async fn listen(self, addr: impl ToSocketAddrs) -> io::Result<()> {
285283
let listener = async_std::net::TcpListener::bind(addr).await?;
286284

287285
let addr = format!("http://{}", listener.local_addr()?);
288286
log::info!("Server is listening on: {}", addr);
289-
let mut server = http_service_h1::Server::new(addr, listener.incoming(), self);
287+
let mut incoming = listener.incoming();
290288

291-
server.run().await
289+
while let Some(stream) = incoming.next().await {
290+
let stream = stream?;
291+
let addr = addr.clone();
292+
let this = self.clone();
293+
task::spawn(async move {
294+
let res = async_h1::accept(&addr, stream, |req| async {
295+
let res = this.respond(req).await;
296+
let res = res.map_err(|_| io::Error::from(io::ErrorKind::Other))?;
297+
Ok(res)
298+
})
299+
.await;
300+
301+
if let Err(err) = res {
302+
log::error!("async-h1 error", { error: err.to_string() });
303+
}
304+
});
305+
}
306+
Ok(())
292307
}
293308

294309
/// Respond to a `Request` with a `Response`.
@@ -352,56 +367,6 @@ impl<State> Clone for Server<State> {
352367
}
353368
}
354369

355-
#[derive(Debug)]
356-
pub struct ReadyFuture;
357-
358-
impl Future for ReadyFuture {
359-
type Output = io::Result<()>;
360-
361-
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
362-
Poll::Ready(Ok(()))
363-
}
364-
}
365-
366-
impl<State: Sync + Send + 'static> HttpService for Server<State> {
367-
type Connection = ();
368-
type ConnectionFuture = ReadyFuture;
369-
type ConnectionError = io::Error;
370-
type ResponseFuture = BoxFuture<'static, Result<http_service::Response, http_types::Error>>;
371-
type ResponseError = http_types::Error;
372-
373-
fn connect(&self) -> Self::ConnectionFuture {
374-
ReadyFuture {}
375-
}
376-
377-
fn respond(&self, _conn: (), req: http_service::Request) -> Self::ResponseFuture {
378-
let req = Request::new(self.state.clone(), req, Vec::new());
379-
let service = self.clone();
380-
Box::pin(async move {
381-
match service.call(req).await {
382-
Ok(value) => {
383-
let res = value.into();
384-
// We assume that if an error was manually cast to a
385-
// Response that we actually want to send the body to the
386-
// client. At this point we don't scrub the message.
387-
Ok(res)
388-
}
389-
Err(err) => {
390-
let mut res = http_types::Response::new(err.status());
391-
res.set_content_type(http_types::mime::PLAIN);
392-
// Only send the message if it is a non-500 range error. All
393-
// errors default to 500 by default, so sending the error
394-
// body is opt-in at the call site.
395-
if !res.status().is_server_error() {
396-
res.set_body(err.to_string());
397-
}
398-
Ok(res)
399-
}
400-
}
401-
})
402-
}
403-
}
404-
405370
impl<State: Sync + Send + 'static, InnerState: Sync + Send + 'static> Endpoint<State>
406371
for Server<InnerState>
407372
{

0 commit comments

Comments
 (0)