mirror of
https://github.com/YGGverse/titanit.git
synced 2026-03-31 09:05:45 +00:00
draft gemini request handler
This commit is contained in:
parent
580dbdb4a7
commit
d33c678e3f
1 changed files with 41 additions and 14 deletions
47
src/main.rs
47
src/main.rs
|
|
@ -60,7 +60,38 @@ fn handle(argument: Arc<Argument>, peer: SocketAddr, stream: &mut TlsStream<TcpS
|
|||
match Request::from_bytes(&input[..l]) {
|
||||
Ok(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) => {
|
||||
// init memory pool
|
||||
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
|
||||
let mut input = vec![0; argument.chunk];
|
||||
match stream.read(&mut input) {
|
||||
Ok(0) => {
|
||||
println!(
|
||||
Ok(0) => println!(
|
||||
"[{}] [warning] [{peer}] Connection closed by peer",
|
||||
now()
|
||||
)
|
||||
}
|
||||
),
|
||||
Ok(l) => {
|
||||
data.extend(&input[..l]);
|
||||
|
||||
|
|
@ -91,12 +120,10 @@ fn handle(argument: Arc<Argument>, peer: SocketAddr, stream: &mut TlsStream<TcpS
|
|||
.into_bytes(),
|
||||
stream,
|
||||
|result| match result {
|
||||
Ok(()) => {
|
||||
println!(
|
||||
Ok(()) => println!(
|
||||
"[{}] [warning] [{peer}] {MESSAGE}",
|
||||
now()
|
||||
)
|
||||
}
|
||||
),
|
||||
Err(e) => {
|
||||
println!("[{}] [error] [{peer}] {e}", now())
|
||||
}
|
||||
|
|
@ -277,7 +304,7 @@ fn send(data: &[u8], stream: &mut TlsStream<TcpStream>, callback: impl FnOnce(Re
|
|||
callback((|| {
|
||||
stream.write_all(data)?;
|
||||
stream.flush()?;
|
||||
// Close connection gracefully
|
||||
// close connection gracefully
|
||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections
|
||||
stream.shutdown()?;
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue