use std::ops::Deref;
use actix_web::{
dev::Payload,
http::header::{self, HeaderValue},
FromRequest, HttpMessage, HttpRequest,
};
use futures::future::ok;
pub struct IsPlaintextRequest(pub bool);
impl Deref for IsPlaintextRequest {
type Target = bool;
fn deref(&self) -> &bool {
&self.0
}
}
impl FromRequest for IsPlaintextRequest {
type Error = actix_web::Error;
type Future = futures::future::Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
if req.content_type() == "text/plain" {
return ok(IsPlaintextRequest(true));
}
match req
.headers()
.get(header::USER_AGENT)
.and_then(|u| u.to_str().unwrap().split('/').next())
{
None | Some("Wget" | "curl" | "HTTPie") => ok(IsPlaintextRequest(true)),
_ => ok(IsPlaintextRequest(false)),
}
}
}
pub struct HostHeader(pub Option<HeaderValue>);
impl FromRequest for HostHeader {
type Error = actix_web::Error;
type Future = futures::future::Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
ok(Self(req.headers().get(header::HOST).cloned()))
}
}