-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
516 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::{convert::Infallible, future::Future, net::SocketAddr, thread}; | ||
|
||
use bytes::Bytes; | ||
use futures::stream::StreamExt; | ||
use http_body_util::BodyExt; | ||
use hyper::{ | ||
body::{Body, Incoming}, | ||
server::conn, | ||
service, Request, Response, | ||
}; | ||
use hyper_util::rt::{TokioIo, TokioTimer}; | ||
use tokio::{net::TcpListener, runtime}; | ||
|
||
pub struct ServerHandle; | ||
|
||
pub fn start_server<S, F, B>(addr: SocketAddr, serve: S) -> ServerHandle | ||
where | ||
S: Fn(Request<Incoming>) -> F + Send + Sync + 'static, | ||
F: Future<Output = Response<B>> + Send, | ||
B: Body<Data = Bytes, Error = Infallible> + Send + 'static, | ||
{ | ||
let serving = async move { | ||
let listener = TcpListener::bind(addr).await.unwrap(); | ||
|
||
loop { | ||
let (stream, _) = listener.accept().await.unwrap(); | ||
|
||
let service = | ||
service::service_fn(|request| async { Ok::<_, Infallible>(serve(request).await) }); | ||
|
||
// SELECT benchmark doesn't read the whole body, so ignore possible errors. | ||
let _ = conn::http1::Builder::new() | ||
.timer(TokioTimer::new()) | ||
.serve_connection(TokioIo::new(stream), service) | ||
.await; | ||
} | ||
}; | ||
|
||
thread::spawn(move || { | ||
runtime::Builder::new_current_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap() | ||
.block_on(serving); | ||
}); | ||
|
||
ServerHandle | ||
} | ||
|
||
pub async fn skip_incoming(request: Request<Incoming>) { | ||
let mut body = request.into_body().into_data_stream(); | ||
|
||
// Read and skip all frames. | ||
while let Some(result) = body.next().await { | ||
result.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
use hyper::{ | ||
client::{connect::Connect, ResponseFuture}, | ||
Body, Request, | ||
}; | ||
use hyper::Request; | ||
use hyper_util::client::legacy::{connect::Connect, Client, ResponseFuture}; | ||
use sealed::sealed; | ||
|
||
use crate::request_body::RequestBody; | ||
|
||
/// A trait for underlying HTTP client. | ||
/// | ||
/// Firstly, now it is implemented only for `hyper_util::client::legacy::Client`, | ||
/// it's impossible to use another HTTP client. | ||
/// | ||
/// Secondly, although it's stable in terms of semver, it will be changed in the future | ||
/// (e.g. to support more runtimes, not only tokio). Thus, prefer to open a feature | ||
/// request instead of implementing this trait manually. | ||
#[sealed] | ||
pub trait HttpClient: Send + Sync + 'static { | ||
fn _request(&self, req: Request<Body>) -> ResponseFuture; | ||
fn request(&self, req: Request<RequestBody>) -> ResponseFuture; | ||
} | ||
|
||
#[sealed] | ||
impl<C> HttpClient for hyper::Client<C> | ||
impl<C> HttpClient for Client<C, RequestBody> | ||
where | ||
C: Connect + Clone + Send + Sync + 'static, | ||
{ | ||
fn _request(&self, req: Request<Body>) -> ResponseFuture { | ||
fn request(&self, req: Request<RequestBody>) -> ResponseFuture { | ||
self.request(req) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.