maj-async-std #4

Merged
cadey merged 11 commits from maj-async-std into main 2020-07-28 01:36:02 +00:00
1 changed files with 21 additions and 12 deletions
Showing only changes of commit 8495c5ab0d - Show all commits

View File

@ -55,8 +55,9 @@ where
let h = h.clone(); let h = h.clone();
let acceptor = acceptor.clone(); let acceptor = acceptor.clone();
let addr = stream.peer_addr().unwrap(); let addr = stream.peer_addr().unwrap();
let port = port.clone();
task::spawn(handle_request(h, stream, acceptor, addr)); task::spawn(handle_request(h, stream, acceptor, addr, port));
} }
Ok(()) Ok(())
@ -68,12 +69,25 @@ async fn handle_request(
stream: TcpStream, stream: TcpStream,
acceptor: Arc<TlsAcceptor>, acceptor: Arc<TlsAcceptor>,
addr: SocketAddr, addr: SocketAddr,
port: u16,
) -> Result { ) -> Result {
// Perform handshake. // Perform handshake.
let mut stream = acceptor.clone().accept(stream).await?; let mut stream = acceptor.clone().accept(stream).await?;
match parse_request(&mut stream).await { match parse_request(&mut stream).await {
Ok(url) => { Ok(url) => {
if let Some(u_port) = url.port() {
if port != u_port {
let _ = respond(&mut stream, "53", &["Cannot proxy"]).await;
return Ok(());
}
}
if url.scheme() != "gemini" {
let _ = respond(&mut stream, "53", &["Cannot proxy outside geminispace"]).await;
Err(RequestParsingError::InvalidScheme(url.scheme().to_string()))?
}
let req = Request { let req = Request {
url: url, url: url,
certs: None, certs: None,
@ -82,7 +96,7 @@ async fn handle_request(
} }
Err(e) => { Err(e) => {
respond(&mut stream, "59", &["Invalid request."]).await?; respond(&mut stream, "59", &["Invalid request."]).await?;
return Err(e); log::error!("error from {}: {:?}", addr, e);
} }
} }
Ok(()) Ok(())
@ -128,9 +142,6 @@ async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
}; };
// Validate the URL. TODO: Check the hostname and port. // Validate the URL. TODO: Check the hostname and port.
if url.scheme() != "gemini" {
Err(RequestParsingError::InvalidScheme(url.scheme().to_string()))?
}
Ok(url) Ok(url)
} }
@ -141,18 +152,16 @@ where
let u = req.url.clone(); let u = req.url.clone();
match h.handle(req).await { match h.handle(req).await {
Ok(resp) => { Ok(resp) => {
stream let _ = stream
.write(format!("{} {}\r\n", resp.status as u8, resp.meta).as_bytes()) .write(format!("{} {}\r\n", resp.status as u8, resp.meta).as_bytes())
.await .await;
.unwrap(); let _ = stream.write(&resp.body).await;
stream.write(&resp.body).await.unwrap();
log::info!("{}: {} {} {:?}", addr, u, resp.meta, resp.status); log::info!("{}: {} {} {:?}", addr, u, resp.meta, resp.status);
} }
Err(why) => { Err(why) => {
stream let _ = stream
.write(format!("{} {:?}\r\n", StatusCode::PermanentFailure as u8, why).as_bytes()) .write(format!("{} {:?}\r\n", StatusCode::PermanentFailure as u8, why).as_bytes())
.await .await;
.unwrap();
log::error!("{}: {}: {:?}", addr, u, why); log::error!("{}: {}: {:?}", addr, u, why);
} }
}; };