maj/src/response.rs

267 lines
7.3 KiB
Rust
Raw Normal View History

2020-07-28 00:50:42 +00:00
use crate::{gemini, StatusCode};
2020-07-24 02:25:35 +00:00
use num::FromPrimitive;
2020-07-31 17:38:33 +00:00
use std::fmt;
2020-07-28 00:50:42 +00:00
use std::io::{self, prelude::*, ErrorKind};
2020-07-24 02:25:35 +00:00
/// A Gemini response as specified in [the spec](https://gemini.circumlunar.space/docs/specification.html).
#[derive(Default)]
pub struct Response {
2020-07-25 14:56:46 +00:00
pub status: StatusCode,
pub meta: String,
pub body: Vec<u8>,
2020-07-24 02:25:35 +00:00
}
2020-07-31 17:38:33 +00:00
#[derive(thiserror::Error, Debug)]
pub struct ResponseStatusError {
status: StatusCode,
meta: String,
}
impl fmt::Display for ResponseStatusError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:?} ({}): {}",
self.status, self.status as u8, self.meta
)
}
}
2020-07-28 00:50:42 +00:00
impl Response {
2020-07-31 16:16:15 +00:00
pub fn with_body(meta: String, body: Vec<u8>) -> Response {
Response {
status: StatusCode::Success,
meta: meta,
body: body,
}
}
2020-07-28 00:50:42 +00:00
pub fn gemini(body: Vec<u8>) -> Response {
Response {
status: StatusCode::Success,
meta: "text/gemini".to_string(),
body: body,
}
}
pub fn render(body: Vec<gemini::Node>) -> Response {
let mut buf: Vec<u8> = vec![];
gemini::render(body, &mut buf).unwrap();
Response {
status: StatusCode::Success,
meta: "text/gemini".to_string(),
body: buf,
}
}
pub fn perm_redirect(to: String) -> Response {
Response {
status: StatusCode::PermanentRedirect,
meta: to,
body: vec![],
}
}
pub fn no_proxy() -> Response {
Response {
status: StatusCode::ProxyRequestRefused,
meta: "Wrong host".to_string(),
body: vec![],
}
}
pub fn not_found() -> Response {
Response {
status: StatusCode::NotFound,
meta: "Not found".to_string(),
body: vec![],
}
}
2020-07-28 01:03:50 +00:00
pub fn input<T: Into<String>>(msg: T) -> Response {
Response {
status: StatusCode::Input,
meta: msg.into(),
body: vec![],
}
}
pub fn need_cert<T: Into<String>>(msg: T) -> Response {
Response {
status: StatusCode::ClientCertificateRequired,
meta: msg.into(),
body: vec![],
}
}
2020-07-28 00:50:42 +00:00
}
2020-07-24 02:25:35 +00:00
/// The parser state.
#[derive(Debug)]
enum State {
ReadStatusCode { data: Vec<u8> },
ReadWhitespace,
ReadMeta { data: Vec<u8> },
ReadBody { data: Vec<u8> },
}
2020-07-25 15:39:23 +00:00
/// Response error.
2020-07-24 02:25:35 +00:00
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("unexpected end of file found while parsing response")]
EOF,
#[error("I/O error")]
IO(#[from] std::io::Error),
#[error("invalid status code character {0}")]
InvalidStatusCode(u8),
#[error("UTF-8 error: {0}")]
Utf8(#[from] std::str::Utf8Error),
#[error("Number parsing error: {0}")]
NumParse(#[from] std::num::ParseIntError),
#[error("None found when none should not be found")]
NoneFound,
2020-07-25 14:56:46 +00:00
2020-07-25 15:39:23 +00:00
#[error("Response meta is too long")]
ResponseMetaTooLong,
2020-07-24 02:25:35 +00:00
}
impl Response {
pub fn parse(inp: &mut impl Read) -> Result<Response, Error> {
let mut state = State::ReadStatusCode { data: vec![] };
let mut buf = [0; 1];
let mut result = Response::default();
loop {
match inp.read(&mut buf) {
Ok(n) => {
if n == 0 {
if let State::ReadBody { data } = state {
result.body = data;
return Ok(result);
}
2020-07-27 22:14:45 +00:00
panic!("got here: {}, {:?}", n, state);
2020-07-24 02:25:35 +00:00
}
2020-07-25 14:56:46 +00:00
}
Err(why) => {
if why.kind() == ErrorKind::ConnectionAborted {
if let State::ReadBody { data } = state {
result.body = data;
return Ok(result);
}
}
return Err(Error::IO(why));
2020-07-24 02:25:35 +00:00
}
}
match &mut state {
State::ReadStatusCode { data } => match buf[0] as char {
'1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '0' => {
data.push(buf[0]);
}
' ' | '\t' => {
let status_code: &str = std::str::from_utf8(data)?;
let status_code: u8 = status_code.parse()?;
result.status = StatusCode::from_u8(status_code).ok_or(Error::NoneFound)?;
state = State::ReadWhitespace;
}
foo => return Err(Error::InvalidStatusCode(foo as u8)),
},
State::ReadWhitespace => match buf[0] as char {
' ' | '\t' => {}
_ => {
state = State::ReadMeta { data: vec![buf[0]] };
}
},
State::ReadMeta { data } => match buf[0] as char {
'\r' => {}
'\n' => {
result.meta = std::str::from_utf8(data)?.to_string();
state = State::ReadBody { data: vec![] };
}
2020-07-25 15:39:23 +00:00
_ => {
if data.len() == 1024 {
2020-07-28 00:50:42 +00:00
return Err(Error::ResponseMetaTooLong);
2020-07-25 15:39:23 +00:00
}
data.push(buf[0]);
}
2020-07-24 02:25:35 +00:00
},
State::ReadBody { data } => data.push(buf[0]),
}
}
}
2020-07-25 15:39:23 +00:00
pub fn write(self, out: &mut impl Write) -> io::Result<()> {
write!(out, "{} {}\r\n", self.status.num(), self.meta)?;
out.write(&self.body)?;
Ok(())
}
2020-07-24 02:25:35 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
#[test]
2020-07-24 02:32:30 +00:00
fn success() -> Result<(), Error> {
let _ = pretty_env_logger::try_init();
2020-07-24 02:25:35 +00:00
let mut fin = std::fs::File::open("./testdata/simple_response.txt")?;
2020-07-24 02:32:30 +00:00
let resp = Response::parse(&mut fin)?;
assert_eq!(resp.meta, "text/gemini".to_string());
assert_eq!(resp.status, StatusCode::Success);
Ok(())
}
#[test]
fn error() -> Result<(), Error> {
let _ = pretty_env_logger::try_init();
let mut fin = std::fs::File::open("./testdata/error_response.txt")?;
let resp = Response::parse(&mut fin)?;
assert_eq!(resp.status, StatusCode::PermanentFailure);
Ok(())
}
#[test]
fn not_found() -> Result<(), Error> {
let _ = pretty_env_logger::try_init();
let mut fin = std::fs::File::open("./testdata/notfound_response.txt")?;
let resp = Response::parse(&mut fin)?;
assert_eq!(resp.status, StatusCode::NotFound);
2020-07-24 02:25:35 +00:00
Ok(())
}
2020-07-25 15:39:23 +00:00
#[test]
fn meta_too_long() {
let _ = pretty_env_logger::try_init();
let mut fin = std::fs::File::open("./testdata/meta_too_long.txt").unwrap();
match Response::parse(&mut fin) {
Ok(_) => panic!("wanted error but didn't get one"),
2020-07-28 00:50:42 +00:00
Err(why) => {
if let ResponseError::ResponseMetaTooLong = why {
println!("ok");
} else {
panic!("wanted ResponseError::ResponseMetaTooLong")
}
}
2020-07-25 15:39:23 +00:00
}
}
2020-07-24 02:25:35 +00:00
}