Browse Source

Refactor geminiresponse

master
Garrit Franke 4 years ago
parent
commit
b4cc0e1314
  1. 37
      src/gemini.rs
  2. 35
      src/main.rs

37
src/gemini.rs

@ -26,3 +26,40 @@ impl GeminiRequest {
fn parse_path(req: &str) -> Option<&str> {
req.split("\r\n").next()
}
pub struct GeminiResonse {
pub status: [u8; 2],
pub meta: Vec<u8>,
pub body: Option<Vec<u8>>,
}
impl GeminiResonse {
pub fn new() -> Self {
GeminiResonse {
status: [b'2', b'0'],
meta: "text/gemini; charset=utf-8".as_bytes().to_vec(),
body: None,
}
}
pub fn build(&self) -> Vec<u8> {
let mut buf: Vec<u8> = Vec::new();
// 20 SUCESS status
buf.extend(&self.status);
// <Space>
buf.push(0x20);
// <Meta>
buf.extend(&self.meta);
buf.extend("\r\n".as_bytes());
if let Some(body) = &self.body {
buf.extend(body);
}
buf
}
}

35
src/main.rs

@ -58,18 +58,7 @@ fn handle_client(mut stream: TlsStream<TcpStream>) {
}
let request = gemini::GeminiRequest::from_string(&raw_request).unwrap();
let mut response: Vec<u8> = Vec::new();
// 20 SUCESS status
response.extend("20".as_bytes());
// <Space>
response.push(0x20);
// <Meta>
response.extend("text/gemini".as_bytes());
response.extend("\r\n".as_bytes());
let mut response = gemini::GeminiResonse::new();
match request.file_path() {
Some(file_path) => {
@ -77,18 +66,28 @@ fn handle_client(mut stream: TlsStream<TcpStream>) {
match File::open(file_path) {
Ok(mut file) => {
if let Err(e) = file.read_to_end(&mut response) {
let mut body_buf = Vec::new();
if let Err(e) = file.read_to_end(&mut body_buf) {
println!("Could not read file {}", e);
}
if let Err(e) = stream.write(&response) {
println!("Could not write to stream: {}", e);
}
response.body = Some(body_buf);
}
Err(e) => {
println!("Error ({}): {}", request.file_path().unwrap_or(""), e);
let file_path = request.file_path().unwrap_or("");
println!("Error ({}): {}", file_path, e);
response.status = [b'5', b'1'];
response.meta = format!("Resource not found: {}", file_path).into();
}
}
}
None => println!("No file found in request"),
None => {
println!("No file found in request");
response.status = [b'5', b'1']
}
}
if let Err(e) = stream.write(&response.build()) {
println!("Could not write to stream: {}", e);
}
}

Loading…
Cancel
Save