1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-02 10:59:03 +01:00
actix-extras/src/lib.rs

320 lines
9.2 KiB
Rust
Raw Normal View History

2018-03-13 22:13:21 +08:00
extern crate actix;
extern crate actix_web;
extern crate bytes;
extern crate futures;
extern crate derive_more;
2018-03-13 22:13:21 +08:00
#[cfg(test)]
extern crate http;
extern crate prost;
#[cfg(test)]
#[macro_use] extern crate prost_derive;
2018-04-10 12:40:11 -07:00
use std::fmt;
use derive_more::Display;
2018-04-10 12:40:11 -07:00
use std::ops::{Deref, DerefMut};
2018-03-21 15:43:32 -07:00
2019-03-07 14:19:57 +08:00
use bytes::{BytesMut, IntoBuf};
2018-03-21 15:43:32 -07:00
use prost::Message;
use prost::DecodeError as ProtoBufDecodeError;
use prost::EncodeError as ProtoBufEncodeError;
2019-03-07 14:19:57 +08:00
use futures::{Poll, Future, Stream};
2018-04-10 12:40:11 -07:00
use actix_web::http::header::{CONTENT_TYPE, CONTENT_LENGTH};
use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse, FromRequest};
use actix_web::dev::{HttpResponseBuilder, Payload};
2018-03-21 15:43:32 -07:00
use actix_web::error::{Error, PayloadError, ResponseError};
#[derive(Debug, Display)]
2018-03-21 15:43:32 -07:00
pub enum ProtoBufPayloadError {
/// Payload size is bigger than 256k
#[display(fmt="Payload size is bigger than 256k")]
2018-03-21 15:43:32 -07:00
Overflow,
/// Content type error
#[display(fmt="Content type error")]
2018-03-21 15:43:32 -07:00
ContentType,
/// Serialize error
#[display(fmt="ProtoBuf serialize error: {}", _0)]
Serialize(ProtoBufEncodeError),
2018-03-21 15:43:32 -07:00
/// Deserialize error
#[display(fmt="ProtoBuf deserialize error: {}", _0)]
Deserialize(ProtoBufDecodeError),
2018-03-21 15:43:32 -07:00
/// Payload error
#[display(fmt="Error that occur during reading payload: {}", _0)]
Payload(PayloadError),
2018-03-21 15:43:32 -07:00
}
impl ResponseError for ProtoBufPayloadError {
fn error_response(&self) -> HttpResponse {
match *self {
2018-04-10 12:40:11 -07:00
ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(),
_ => HttpResponse::BadRequest().into(),
2018-03-21 15:43:32 -07:00
}
}
}
impl From<PayloadError> for ProtoBufPayloadError {
fn from(err: PayloadError) -> ProtoBufPayloadError {
ProtoBufPayloadError::Payload(err)
}
}
impl From<ProtoBufDecodeError> for ProtoBufPayloadError {
fn from(err: ProtoBufDecodeError) -> ProtoBufPayloadError {
ProtoBufPayloadError::Deserialize(err)
}
}
pub struct ProtoBuf<T: Message>(pub T);
2018-04-10 12:40:11 -07:00
impl<T: Message> Deref for ProtoBuf<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T: Message> DerefMut for ProtoBuf<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T: Message> fmt::Debug for ProtoBuf<T> where T: fmt::Debug {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ProtoBuf: {:?}", self.0)
}
}
impl<T: Message> fmt::Display for ProtoBuf<T> where T: fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
pub struct ProtoBufConfig {
limit: usize,
}
impl ProtoBufConfig {
/// Change max size of payload. By default max size is 256Kb
pub fn limit(&mut self, limit: usize) -> &mut Self {
self.limit = limit;
self
}
}
impl Default for ProtoBufConfig {
fn default() -> Self {
ProtoBufConfig{limit: 262_144}
}
}
impl<T> FromRequest for ProtoBuf<T>
where T: Message + Default + 'static
2018-04-10 12:40:11 -07:00
{
type Config = ProtoBufConfig;
type Error = Error;
type Future = Box<Future<Item=Self, Error=Error>>;
2018-04-10 12:40:11 -07:00
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let limit = req.app_data::<ProtoBufConfig>().map(|c| c.limit).unwrap_or(262_144);
2018-04-10 12:40:11 -07:00
Box::new(
ProtoBufMessage::new(req, payload)
.limit(limit)
.map_err(move |e| {
e.into()
})
2018-04-10 12:40:11 -07:00
.map(ProtoBuf))
}
}
impl<T: Message + Default> Responder for ProtoBuf<T> {
2018-03-21 15:43:32 -07:00
type Error = Error;
type Future = Result<HttpResponse, Error>;
2018-03-21 15:43:32 -07:00
fn respond_to(self, _: &HttpRequest) -> Self::Future {
2018-03-21 15:43:32 -07:00
let mut buf = Vec::new();
self.0.encode(&mut buf)
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
.and_then(|()| {
Ok(HttpResponse::Ok()
.content_type("application/protobuf")
2018-04-10 12:40:11 -07:00
.body(buf))
2018-03-21 15:43:32 -07:00
})
}
}
pub struct ProtoBufMessage<T: Message + Default>{
2018-03-21 15:43:32 -07:00
limit: usize,
2019-03-07 14:19:57 +08:00
length: Option<usize>,
stream: Option<Payload>,
2019-03-07 14:19:57 +08:00
err: Option<ProtoBufPayloadError>,
fut: Option<Box<Future<Item=T, Error=ProtoBufPayloadError>>>,
2018-03-21 15:43:32 -07:00
}
impl<T: Message + Default> ProtoBufMessage<T> {
2018-03-21 15:43:32 -07:00
/// Create `ProtoBufMessage` for request.
pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
2019-03-07 14:19:57 +08:00
if req.content_type() != "application/protobuf" {
return ProtoBufMessage {
limit: 262_144,
length: None,
stream: None,
fut: None,
err: Some(ProtoBufPayloadError::ContentType),
};
}
let mut len = None;
if let Some(l) = req.headers().get(CONTENT_LENGTH) {
if let Ok(s) = l.to_str() {
if let Ok(l) = s.parse::<usize>() {
len = Some(l)
}
}
}
ProtoBufMessage {
2018-03-21 15:43:32 -07:00
limit: 262_144,
2019-03-07 14:19:57 +08:00
length: len,
stream: Some(payload.take()),
2018-03-21 15:43:32 -07:00
fut: None,
2019-03-07 14:19:57 +08:00
err: None,
2018-03-21 15:43:32 -07:00
}
}
/// Change max size of payload. By default max size is 256Kb
pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit;
self
}
}
impl<T: Message + Default + 'static> Future for ProtoBufMessage<T>
2018-03-21 15:43:32 -07:00
{
type Item = T;
2018-03-21 15:43:32 -07:00
type Error = ProtoBufPayloadError;
fn poll(&mut self) -> Poll<T, ProtoBufPayloadError> {
2019-03-07 14:19:57 +08:00
if let Some(ref mut fut) = self.fut {
return fut.poll();
}
2018-03-21 15:43:32 -07:00
2019-03-07 14:19:57 +08:00
if let Some(err) = self.err.take() {
return Err(err);
2018-03-21 15:43:32 -07:00
}
2019-03-07 14:19:57 +08:00
let limit = self.limit;
if let Some(len) = self.length.take() {
if len > limit {
return Err(ProtoBufPayloadError::Overflow);
}
}
let fut = self
.stream
.take()
.expect("ProtoBufMessage could not be used second time")
.from_err()
.fold(BytesMut::with_capacity(8192), move |mut body, chunk| {
if (body.len() + chunk.len()) > limit {
Err(ProtoBufPayloadError::Overflow)
} else {
body.extend_from_slice(&chunk);
Ok(body)
}
}).and_then(|body| Ok(<T>::decode(&mut body.into_buf())?));
2019-03-07 14:19:57 +08:00
self.fut = Some(Box::new(fut));
self.poll()
2018-03-21 15:43:32 -07:00
}
}
pub trait ProtoBufResponseBuilder {
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error>;
}
impl ProtoBufResponseBuilder for HttpResponseBuilder {
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
self.header(CONTENT_TYPE, "application/protobuf");
let mut body = Vec::new();
value.encode(&mut body).map_err(ProtoBufPayloadError::Serialize)?;
2018-04-10 12:40:11 -07:00
Ok(self.body(body))
2018-03-21 15:43:32 -07:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::header;
use actix_web::test::{block_on, TestRequest};
2018-03-21 15:43:32 -07:00
impl PartialEq for ProtoBufPayloadError {
fn eq(&self, other: &ProtoBufPayloadError) -> bool {
match *self {
ProtoBufPayloadError::Overflow => match *other {
ProtoBufPayloadError::Overflow => true,
_ => false,
},
ProtoBufPayloadError::ContentType => match *other {
ProtoBufPayloadError::ContentType => true,
_ => false,
},
_ => false,
}
}
}
2019-03-07 14:19:57 +08:00
#[derive(Clone, PartialEq, Message)]
2018-03-21 15:43:32 -07:00
pub struct MyObject {
#[prost(int32, tag="1")]
pub number: i32,
#[prost(string, tag="2")]
pub name: String,
}
#[test]
fn test_protobuf() {
let protobuf = ProtoBuf(MyObject{number: 9 , name: "test".to_owned()});
let req = TestRequest::default().to_http_request();
let resp = protobuf.respond_to(&req).unwrap();
2018-03-21 15:43:32 -07:00
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/protobuf");
}
#[test]
fn test_protobuf_message() {
let (req, mut pl) = TestRequest::default().to_http_parts();
let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl));
assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::ContentType);
2018-03-21 15:43:32 -07:00
let (req, mut pl) = TestRequest::default()
2019-03-07 14:19:57 +08:00
.header(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/text"),
).to_http_parts();
let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl));
assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::ContentType);
2018-03-21 15:43:32 -07:00
let (req, mut pl) = TestRequest::default()
2019-03-07 14:19:57 +08:00
.header(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/protobuf"),
).header(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("10000"),
).to_http_parts();
let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl).limit(100));
assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::Overflow);
2018-03-21 15:43:32 -07:00
}
}