Initial commit

This commit is contained in:
timokoesters 2020-02-15 22:42:21 +01:00
commit 6264628c11
5 changed files with 1029 additions and 0 deletions

26
src/main.rs Normal file
View file

@ -0,0 +1,26 @@
#![feature(proc_macro_hygiene, decl_macro)]
mod ruma_wrapper;
use {
rocket::{get, post, routes},
ruma_client_api::r0::account::register,
ruma_wrapper::Ruma,
std::convert::TryInto,
};
#[post("/_matrix/client/r0/register", data = "<body>")]
fn register_route(body: Ruma<register::Request>) -> Ruma<register::Response> {
Ruma(register::Response {
access_token: "42".to_owned(),
home_server: "deprecated".to_owned(),
user_id: "@yourrequestedid:homeserver.com".try_into().unwrap(),
device_id: body.device_id.clone().unwrap_or_default(),
})
}
fn main() {
pretty_env_logger::init();
rocket::ignite()
.mount("/", routes![register_route])
.launch();
}

63
src/ruma_wrapper.rs Normal file
View file

@ -0,0 +1,63 @@
use {
rocket::data::{FromDataSimple, Outcome},
rocket::http::Status,
rocket::response::Responder,
rocket::Request,
rocket::{Data, Outcome::*},
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> {
type Error = ();
fn from_data(request: &Request, data: Data) -> Outcome<Self, Self::Error> {
let mut handle = data.open().take(MESSAGE_LIMIT);
let mut body = Vec::new();
handle.read_to_end(&mut body).unwrap();
dbg!(&body);
let mut http_request = http::Request::builder().uri(request.uri().to_string());
for header in request.headers().iter() {
http_request = http_request.header(header.name.as_str(), &*header.value);
}
let http_request = http_request.body(body).unwrap();
match T::try_from(http_request) {
Ok(r) => Success(Ruma(r)),
Err(_) => Failure((Status::InternalServerError, ())),
}
}
}
impl<T> Deref for Ruma<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'r, T: TryInto<http::Response<Vec<u8>>>> Responder<'r> for Ruma<T> {
fn respond_to(self, _: &Request) -> rocket::response::Result<'r> {
match self.0.try_into() {
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),
}
}
}