2020-03-28 22:08:59 +00:00
|
|
|
use {
|
|
|
|
rocket::data::{FromDataSimple, Outcome},
|
|
|
|
rocket::http::Status,
|
|
|
|
rocket::response::Responder,
|
|
|
|
rocket::Outcome::*,
|
|
|
|
rocket::Request,
|
|
|
|
rocket::State,
|
2020-03-29 00:29:47 +00:00
|
|
|
ruma_api::{
|
|
|
|
error::{FromHttpRequestError, FromHttpResponseError},
|
|
|
|
Endpoint, Outgoing,
|
|
|
|
},
|
2020-03-28 22:08:59 +00:00
|
|
|
ruma_client_api::error::Error,
|
2020-03-29 19:05:20 +00:00
|
|
|
ruma_identifiers::UserId,
|
2020-03-28 22:08:59 +00:00
|
|
|
std::ops::Deref,
|
|
|
|
std::{
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
io::{Cursor, Read},
|
|
|
|
},
|
2020-02-15 21:42:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MESSAGE_LIMIT: u64 = 65535;
|
|
|
|
|
2020-03-28 22:08:59 +00:00
|
|
|
/// This struct converts rocket requests into ruma structs by converting them into http requests
|
|
|
|
/// first.
|
2020-03-29 19:05:20 +00:00
|
|
|
#[derive(Debug)]
|
2020-03-29 00:29:47 +00:00
|
|
|
pub struct Ruma<T: Outgoing> {
|
|
|
|
body: T::Incoming,
|
2020-03-29 19:05:20 +00:00
|
|
|
pub user_id: Option<UserId>,
|
2020-03-29 22:10:15 +00:00
|
|
|
pub json_body: serde_json::Value,
|
2020-03-28 17:50:02 +00:00
|
|
|
}
|
2020-03-28 22:08:59 +00:00
|
|
|
|
2020-03-29 00:29:47 +00:00
|
|
|
impl<T: Endpoint> FromDataSimple for Ruma<T>
|
2020-02-18 21:07:57 +00:00
|
|
|
where
|
2020-03-29 00:29:47 +00:00
|
|
|
// We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet.
|
|
|
|
// See https://github.com/rust-lang/rust/issues/54149
|
|
|
|
<T as Outgoing>::Incoming: TryFrom<http::Request<Vec<u8>>, Error = FromHttpRequestError>,
|
|
|
|
<T::Response as Outgoing>::Incoming: TryFrom<
|
|
|
|
http::Response<Vec<u8>>,
|
|
|
|
Error = FromHttpResponseError<<T as Endpoint>::ResponseError>,
|
|
|
|
>,
|
2020-02-18 21:07:57 +00:00
|
|
|
{
|
2020-03-29 19:05:20 +00:00
|
|
|
type Error = (); // TODO: Better error handling
|
2020-02-15 21:42:21 +00:00
|
|
|
|
2020-03-28 22:08:59 +00:00
|
|
|
fn from_data(request: &Request, data: rocket::Data) -> Outcome<Self, Self::Error> {
|
2020-03-29 19:05:20 +00:00
|
|
|
let user_id = if T::METADATA.requires_authentication {
|
|
|
|
let data = request.guard::<State<crate::Data>>().unwrap();
|
|
|
|
|
|
|
|
// Get token from header or query value
|
|
|
|
let token = match request
|
|
|
|
.headers()
|
|
|
|
.get_one("Authorization")
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
.or_else(|| request.get_query_value("access_token").and_then(|r| r.ok()))
|
|
|
|
{
|
|
|
|
// TODO: M_MISSING_TOKEN
|
|
|
|
None => return Failure((Status::Unauthorized, ())),
|
|
|
|
Some(token) => token,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if token is valid
|
|
|
|
match data.user_from_token(&token) {
|
|
|
|
// TODO: M_UNKNOWN_TOKEN
|
|
|
|
None => return Failure((Status::Unauthorized, ())),
|
|
|
|
Some(user_id) => Some(user_id),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-02-18 21:07:57 +00:00
|
|
|
let mut http_request = http::Request::builder()
|
|
|
|
.uri(request.uri().to_string())
|
|
|
|
.method(&*request.method().to_string());
|
2020-02-15 21:42:21 +00:00
|
|
|
for header in request.headers().iter() {
|
|
|
|
http_request = http_request.header(header.name.as_str(), &*header.value);
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:07:57 +00:00
|
|
|
let mut handle = data.open().take(MESSAGE_LIMIT);
|
|
|
|
let mut body = Vec::new();
|
|
|
|
handle.read_to_end(&mut body).unwrap();
|
|
|
|
|
2020-03-29 22:10:15 +00:00
|
|
|
let http_request = http_request.body(body.clone()).unwrap();
|
2020-02-18 21:07:57 +00:00
|
|
|
log::info!("{:?}", http_request);
|
2020-03-29 22:10:15 +00:00
|
|
|
|
2020-03-29 00:29:47 +00:00
|
|
|
match T::Incoming::try_from(http_request) {
|
2020-03-29 22:10:15 +00:00
|
|
|
Ok(t) => Success(Ruma {
|
|
|
|
body: t,
|
|
|
|
user_id,
|
|
|
|
// TODO: Can we avoid parsing it again?
|
|
|
|
json_body: serde_json::from_slice(&body)
|
|
|
|
.expect("Ruma already parsed it successfuly"),
|
|
|
|
}),
|
2020-02-18 21:07:57 +00:00
|
|
|
Err(e) => {
|
|
|
|
log::error!("{:?}", e);
|
|
|
|
Failure((Status::InternalServerError, ()))
|
|
|
|
}
|
2020-02-15 21:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 00:29:47 +00:00
|
|
|
impl<T: Outgoing> Deref for Ruma<T> {
|
|
|
|
type Target = T::Incoming;
|
2020-02-15 21:42:21 +00:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-03-28 17:50:02 +00:00
|
|
|
&self.body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:08:59 +00:00
|
|
|
/// This struct converts ruma responses into rocket http responses.
|
2020-02-18 21:07:57 +00:00
|
|
|
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> {
|
2020-02-15 21:42:21 +00:00
|
|
|
fn respond_to(self, _: &Request) -> rocket::response::Result<'r> {
|
2020-02-18 21:07:57 +00:00
|
|
|
let http_response: Result<http::Response<_>, _> = self.try_into();
|
|
|
|
match http_response {
|
2020-02-15 21:42:21 +00:00
|
|
|
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());
|
|
|
|
}
|
2020-03-28 17:50:02 +00:00
|
|
|
|
|
|
|
response.raw_header("Access-Control-Allow-Origin", "*");
|
|
|
|
response.raw_header(
|
|
|
|
"Access-Control-Allow-Methods",
|
|
|
|
"GET, POST, PUT, DELETE, OPTIONS",
|
|
|
|
);
|
|
|
|
response.raw_header(
|
|
|
|
"Access-Control-Allow-Headers",
|
|
|
|
"Origin, X-Requested-With, Content-Type, Accept, Authorization",
|
|
|
|
);
|
2020-02-15 21:42:21 +00:00
|
|
|
response.ok()
|
|
|
|
}
|
|
|
|
Err(_) => Err(Status::InternalServerError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|