-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Hyper with Warp for HTTP server
- Use Warp - Optimize clones - HTTPS server - Content-Length limit - HTTP Basic Auth
- Loading branch information
Showing
8 changed files
with
633 additions
and
321 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,55 +1,23 @@ | ||
use bytes::Bytes; | ||
use http_body_util::Full; | ||
use hyper::service::Service; | ||
use hyper::{body::Incoming as IncomingBody, Request, Response}; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::{Arc, Condvar, Mutex}; | ||
use std::sync::{Arc, Mutex, Condvar}; | ||
use std::io::BufRead; | ||
|
||
use warp::http; | ||
|
||
pub struct HttpListener { | ||
pub incoming: std::sync::mpsc::Receiver<HttpRequest>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct HttpRequest { | ||
pub request: Request<IncomingBody>, | ||
pub request_data: HttpRequestData, | ||
pub response: HttpResponse, | ||
} | ||
|
||
pub type HttpResponse = Arc<(Mutex<bool>, Mutex<Option<Response<Full<Bytes>>>>, Condvar)>; | ||
|
||
pub struct HttpService { | ||
pub tx: std::sync::mpsc::SyncSender<HttpRequest>, | ||
} | ||
|
||
impl Service<Request<IncomingBody>> for HttpService { | ||
type Response = Response<Full<Bytes>>; | ||
type Error = hyper::Error; | ||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; | ||
|
||
fn call(&mut self, req: Request<IncomingBody>) -> Self::Future { | ||
// new connection! | ||
// we send the Request info to Prolog | ||
let response = Arc::new((Mutex::new(false), Mutex::new(None), Condvar::new())); | ||
let http_request = HttpRequest { | ||
request: req, | ||
response: Arc::clone(&response), | ||
}; | ||
self.tx.send(http_request).unwrap(); | ||
pub type HttpResponse = Arc<(Mutex<bool>, Mutex<Option<warp::reply::Response>>, Condvar)>; | ||
|
||
// we wait for the Response info from Prolog | ||
{ | ||
let (ready, _response, cvar) = &*response; | ||
let mut ready = ready.lock().unwrap(); | ||
while !*ready { | ||
ready = cvar.wait(ready).unwrap(); | ||
} | ||
} | ||
{ | ||
let (_, response, _) = &*response; | ||
let response = response.lock().unwrap().take(); | ||
let res = response.expect("Data race error in HTTP Server"); | ||
Box::pin(async move { Ok(res) }) | ||
} | ||
} | ||
pub struct HttpRequestData { | ||
pub method: http::Method, | ||
pub headers: http::HeaderMap, | ||
pub path: String, | ||
pub query: String, | ||
pub body: Box<dyn BufRead + Send>, | ||
} |
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
Oops, something went wrong.