draft gemini request handler

This commit is contained in:
yggverse 2025-02-22 06:24:58 +02:00
parent 580dbdb4a7
commit d33c678e3f

View file

@ -60,7 +60,38 @@ fn handle(argument: Arc<Argument>, peer: SocketAddr, stream: &mut TlsStream<TcpS
match Request::from_bytes(&input[..l]) { match Request::from_bytes(&input[..l]) {
Ok(request) => { Ok(request) => {
match request { match request {
Request::Gemini(gemini) => println!("request: {}", gemini.url), // @TODO Request::Gemini(gemini) => {
match storage::Item::from_url(&gemini.url.as_str(), &argument.directory)
{
Ok(item) => send(
&response::success::Default {
mime: "text/gemini".to_string(),
//data: item.file.read(vec![1000]),
}
.into_bytes(),
stream,
|result| match result {
Ok(()) => println!(
"[{}] [error] [{peer}] Request: {}",
now(),
gemini.url
),
Err(e) => println!("[{}] [error] [{peer}] {e}", now()),
},
),
Err(e) => send(
&response::failure::permanent::NotFound {
message: Some("Not found".to_string()),
}
.into_bytes(),
stream,
|result| match result {
Ok(()) => println!("[{}] [error] [{peer}] {e}", now()),
Err(e) => println!("[{}] [error] [{peer}] {e}", now()),
},
),
}
}
Request::Titan(titan) => { Request::Titan(titan) => {
// init memory pool // init memory pool
let mut data: Vec<u8> = Vec::with_capacity(titan.size); let mut data: Vec<u8> = Vec::with_capacity(titan.size);
@ -68,12 +99,10 @@ fn handle(argument: Arc<Argument>, peer: SocketAddr, stream: &mut TlsStream<TcpS
// read data bytes // read data bytes
let mut input = vec![0; argument.chunk]; let mut input = vec![0; argument.chunk];
match stream.read(&mut input) { match stream.read(&mut input) {
Ok(0) => { Ok(0) => println!(
println!(
"[{}] [warning] [{peer}] Connection closed by peer", "[{}] [warning] [{peer}] Connection closed by peer",
now() now()
) ),
}
Ok(l) => { Ok(l) => {
data.extend(&input[..l]); data.extend(&input[..l]);
@ -91,12 +120,10 @@ fn handle(argument: Arc<Argument>, peer: SocketAddr, stream: &mut TlsStream<TcpS
.into_bytes(), .into_bytes(),
stream, stream,
|result| match result { |result| match result {
Ok(()) => { Ok(()) => println!(
println!(
"[{}] [warning] [{peer}] {MESSAGE}", "[{}] [warning] [{peer}] {MESSAGE}",
now() now()
) ),
}
Err(e) => { Err(e) => {
println!("[{}] [error] [{peer}] {e}", now()) println!("[{}] [error] [{peer}] {e}", now())
} }
@ -277,7 +304,7 @@ fn send(data: &[u8], stream: &mut TlsStream<TcpStream>, callback: impl FnOnce(Re
callback((|| { callback((|| {
stream.write_all(data)?; stream.write_all(data)?;
stream.flush()?; stream.flush()?;
// Close connection gracefully // close connection gracefully
// https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections // https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections
stream.shutdown()?; stream.shutdown()?;
Ok(()) Ok(())