You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
conduit/src/ruma_wrapper.rs

88 lines
2.6 KiB
Rust

5 years ago
use {
rocket::data::{FromDataSimple, Outcome},
rocket::http::Status,
rocket::response::Responder,
rocket::Request,
rocket::{Data, Outcome::*},
ruma_client_api::error::Error,
std::fmt::Debug,
5 years ago
std::ops::Deref,
std::{
convert::{TryFrom, TryInto},
io::{Cursor, Read},
},
};
const MESSAGE_LIMIT: u64 = 65535;
pub struct Ruma<T>(pub T);
impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T>
where
T::Error: Debug,
{
5 years ago
type Error = ();
fn from_data(request: &Request, data: Data) -> Outcome<Self, Self::Error> {
let mut http_request = http::Request::builder()
.uri(request.uri().to_string())
.method(&*request.method().to_string());
5 years ago
for header in request.headers().iter() {
http_request = http_request.header(header.name.as_str(), &*header.value);
}
let mut handle = data.open().take(MESSAGE_LIMIT);
let mut body = Vec::new();
handle.read_to_end(&mut body).unwrap();
5 years ago
let http_request = http_request.body(body).unwrap();
log::info!("{:?}", http_request);
5 years ago
match T::try_from(http_request) {
Ok(r) => Success(Ruma(r)),
Err(e) => {
log::error!("{:?}", e);
Failure((Status::InternalServerError, ()))
}
5 years ago
}
}
}
impl<T> Deref for Ruma<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct MatrixResult<T>(pub std::result::Result<T, Error>);
impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> {
type Error = T::Error;
fn try_into(self) -> Result<http::Response<Vec<u8>>, T::Error> {
match self.0 {
Ok(t) => t.try_into(),
Err(e) => Ok(e.into()),
}
}
}
impl<'r, T: TryInto<http::Response<Vec<u8>>>> Responder<'r> for MatrixResult<T> {
5 years ago
fn respond_to(self, _: &Request) -> rocket::response::Result<'r> {
let http_response: Result<http::Response<_>, _> = self.try_into();
match http_response {
5 years ago
Ok(http_response) => {
let mut response = rocket::response::Response::build();
response.sized_body(Cursor::new(http_response.body().clone()));
for header in http_response.headers() {
response
.raw_header(header.0.to_string(), header.1.to_str().unwrap().to_owned());
}
response.ok()
}
Err(_) => Err(Status::InternalServerError),
}
}
}