112 lines
2.2 KiB
Rust
112 lines
2.2 KiB
Rust
use std::{
|
|
collections::*,
|
|
convert::{TryFrom},
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
// Hyper doesn't seem to make it easy to de/ser requests
|
|
// and responses and stuff like that, so I do it by hand here.
|
|
|
|
pub enum Error {
|
|
Unsupported,
|
|
InvalidHeaderName,
|
|
}
|
|
|
|
impl From <hyper::header::InvalidHeaderName> for Error {
|
|
fn from (_x: hyper::header::InvalidHeaderName) -> Self {
|
|
Self::InvalidHeaderName
|
|
}
|
|
}
|
|
|
|
#[derive (Deserialize, Serialize)]
|
|
pub enum Method {
|
|
Get,
|
|
Head,
|
|
}
|
|
|
|
impl TryFrom <hyper::Method> for Method {
|
|
type Error = Error;
|
|
|
|
fn try_from (x: hyper::Method) -> Result <Self, Error> {
|
|
match x {
|
|
hyper::Method::GET => Ok (Self::Get),
|
|
hyper::Method::HEAD => Ok (Self::Head),
|
|
_ => Err (Error::Unsupported),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive (Deserialize, Serialize)]
|
|
pub struct RequestParts {
|
|
pub id: String,
|
|
pub method: Method,
|
|
|
|
// Technically URIs are subtle and complex but I don't care
|
|
pub uri: String,
|
|
|
|
// Technically Hyper has headers in a multi-map
|
|
// but I don't feel like doing that right now.
|
|
|
|
// Headers are _kinda_ ASCII but they can also be binary.
|
|
// HTTP is nutty.
|
|
pub headers: HashMap <String, Vec <u8>>,
|
|
}
|
|
|
|
#[derive (Deserialize, Serialize)]
|
|
pub enum StatusCode {
|
|
Ok,
|
|
NotFound,
|
|
PartialContent,
|
|
}
|
|
|
|
impl Default for StatusCode {
|
|
fn default () -> Self {
|
|
Self::Ok
|
|
}
|
|
}
|
|
|
|
impl From <StatusCode> for hyper::StatusCode {
|
|
fn from (x: StatusCode) -> Self {
|
|
match x {
|
|
StatusCode::Ok => Self::OK,
|
|
StatusCode::NotFound => Self::NOT_FOUND,
|
|
StatusCode::PartialContent => Self::PARTIAL_CONTENT,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive (Default, Deserialize, Serialize)]
|
|
pub struct ResponseParts {
|
|
pub status_code: StatusCode,
|
|
|
|
// Technically Hyper has headers in a multi-map
|
|
// but I don't feel like doing that right now.
|
|
// We promise not to need this feature.
|
|
|
|
pub headers: HashMap <String, Vec <u8>>,
|
|
}
|
|
|
|
#[derive (Default)]
|
|
pub struct Response {
|
|
pub parts: ResponseParts,
|
|
pub body: Option <reqwest::Body>,
|
|
}
|
|
|
|
impl Response {
|
|
pub fn status_code (&mut self, c: StatusCode) -> &mut Self {
|
|
self.parts.status_code = c;
|
|
self
|
|
}
|
|
|
|
pub fn header (&mut self, k: String, v: Vec <u8>) -> &mut Self {
|
|
self.parts.headers.insert (k, v);
|
|
self
|
|
}
|
|
|
|
pub fn body (&mut self, b: reqwest::Body) -> &mut Self {
|
|
self.body = Some (b);
|
|
self
|
|
}
|
|
}
|