Plume/plume-models/src/headers.rs
fdb-hiroshima 8a4702df92 Add unit tests for main model parts (#310)
Add tests for following models:
- Blog
- Instance
- Media
- User
2018-11-24 12:44:17 +01:00

30 lines
858 B
Rust

use rocket::request::{self, FromRequest, Request};
use rocket::{
http::{Header, HeaderMap},
Outcome,
};
pub struct Headers<'r>(pub HeaderMap<'r>);
impl<'a, 'r> FromRequest<'a, 'r> for Headers<'r> {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> {
let mut headers = HeaderMap::new();
for header in request.headers().clone().into_iter() {
headers.add(header);
}
let ori = request.uri();
let uri = if let Some(query) = ori.query() {
format!("{}?{}", ori.path(), query)
} else {
ori.path().to_owned()
};
headers.add(Header::new(
"(request-target)",
format!("{} {}", request.method().as_str().to_lowercase(), uri),
));
Outcome::Success(Headers(headers))
}
}