1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-22 23:05:56 +01:00
actix-extras/src/lib.rs

336 lines
9.3 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;
#[macro_use]
extern crate failure;
#[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 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};
2018-03-21 15:43:32 -07:00
use actix_web::dev::HttpResponseBuilder;
use actix_web::error::{Error, PayloadError, ResponseError};
#[derive(Fail, Debug)]
pub enum ProtoBufPayloadError {
/// Payload size is bigger than 256k
#[fail(display="Payload size is bigger than 256k")]
Overflow,
/// Content type error
#[fail(display="Content type error")]
ContentType,
/// Serialize error
#[fail(display="ProtoBuf serialize error: {}", _0)]
2018-03-21 15:43:32 -07:00
Serialize(#[cause] ProtoBufEncodeError),
/// Deserialize error
#[fail(display="ProtoBuf deserialize error: {}", _0)]
2018-03-21 15:43:32 -07:00
Deserialize(#[cause] ProtoBufDecodeError),
/// Payload error
#[fail(display="Error that occur during reading payload: {}", _0)]
Payload(#[cause] PayloadError),
}
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, S> FromRequest<S> for ProtoBuf<T>
where T: Message + Default + 'static, S: 'static
{
type Config = ProtoBufConfig;
type Result = Box<Future<Item=Self, Error=Error>>;
#[inline]
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
Box::new(
2019-03-07 14:19:57 +08:00
ProtoBufMessage::new(req)
2018-04-10 12:40:11 -07:00
.limit(cfg.limit)
.from_err()
.map(ProtoBuf))
}
}
impl<T: Message + Default> Responder for ProtoBuf<T> {
2018-03-21 15:43:32 -07:00
type Item = HttpResponse;
type Error = Error;
2019-03-07 14:19:57 +08:00
fn respond_to<S>(self, _: &HttpRequest<S>) -> Result<HttpResponse, Error> {
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
})
}
}
2019-03-07 14:19:57 +08:00
pub struct ProtoBufMessage<T: HttpMessage, U: 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<T::Stream>,
err: Option<ProtoBufPayloadError>,
2018-03-21 15:43:32 -07:00
fut: Option<Box<Future<Item=U, Error=ProtoBufPayloadError>>>,
}
2019-03-07 14:19:57 +08:00
impl<T: HttpMessage, U: Message + Default> ProtoBufMessage<T, U> {
2018-03-21 15:43:32 -07:00
/// Create `ProtoBufMessage` for request.
2019-03-07 14:19:57 +08:00
pub fn new(req: &T) -> Self {
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(req.payload()),
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, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
2019-03-07 14:19:57 +08:00
where T: HttpMessage + 'static
2018-03-21 15:43:32 -07:00
{
type Item = U;
type Error = ProtoBufPayloadError;
fn poll(&mut self) -> Poll<U, 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(<U>::decode(&mut body.into_buf())?));
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
}
}
pub trait ProtoBufHttpMessage {
2019-03-07 14:19:57 +08:00
fn protobuf<T: Message + Default>(&self) -> ProtoBufMessage<Self, T>
where Self: HttpMessage + 'static;
2018-03-21 15:43:32 -07:00
}
impl<S> ProtoBufHttpMessage for HttpRequest<S> {
#[inline]
2019-03-07 14:19:57 +08:00
fn protobuf<T: Message + Default>(&self) -> ProtoBufMessage<Self, T>
where Self: HttpMessage + 'static
2018-03-21 15:43:32 -07:00
{
ProtoBufMessage::new(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::header;
2019-03-07 14:19:57 +08:00
use actix_web::test::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()});
2019-03-07 14:19:57 +08:00
let resp = protobuf.respond_to(&TestRequest::default().finish()).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() {
2019-03-07 14:19:57 +08:00
let req = TestRequest::default().finish();
2018-03-21 15:43:32 -07:00
let mut protobuf = req.protobuf::<MyObject>();
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType);
2019-03-07 14:19:57 +08:00
let req = TestRequest::default()
.header(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/text"),
).finish();
let mut protobuf = req.protobuf::<MyObject>();
2018-03-21 15:43:32 -07:00
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType);
2019-03-07 14:19:57 +08:00
let req = TestRequest::default()
.header(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/protobuf"),
).header(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("10000"),
).finish();
2018-03-21 15:43:32 -07:00
let mut protobuf = req.protobuf::<MyObject>().limit(100);
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::Overflow);
}
}