1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/examples/protobuf/src/protobuf.rs

170 lines
5.2 KiB
Rust
Raw Normal View History

2018-03-09 03:05:13 +01:00
use bytes::{Bytes, BytesMut};
use futures::{Poll, Future, Stream};
use bytes::IntoBuf;
use prost::Message;
2018-03-09 05:56:18 +01:00
use prost::DecodeError as ProtoBufDecodeError;
2018-03-09 14:29:06 +01:00
use prost::EncodeError as ProtoBufEncodeError;
2018-03-31 03:54:38 +02:00
use actix_web::http::header::{CONTENT_TYPE, CONTENT_LENGTH};
2018-03-09 14:29:06 +01:00
use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse};
use actix_web::dev::HttpResponseBuilder;
use actix_web::error::{Error, PayloadError, ResponseError};
use actix_web::httpcodes::{HttpBadRequest, HttpPayloadTooLarge};
2018-03-09 03:05:13 +01:00
2018-03-09 05:56:18 +01:00
#[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,
2018-03-09 14:29:06 +01:00
/// Serialize error
#[fail(display="ProtoBud serialize error: {}", _0)]
Serialize(#[cause] ProtoBufEncodeError),
2018-03-09 05:56:18 +01:00
/// Deserialize error
2018-03-09 14:29:06 +01:00
#[fail(display="ProtoBud deserialize error: {}", _0)]
2018-03-09 05:56:18 +01: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 {
ProtoBufPayloadError::Overflow => HttpPayloadTooLarge.into(),
_ => HttpBadRequest.into(),
}
}
}
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)
}
}
2018-03-09 03:05:13 +01:00
#[derive(Debug)]
pub struct ProtoBuf<T: Message>(pub T);
impl<T: Message> Responder for ProtoBuf<T> {
type Item = HttpResponse;
type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
let mut buf = Vec::new();
self.0.encode(&mut buf)
2018-03-09 14:29:06 +01:00
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
.and_then(|()| {
2018-03-09 03:05:13 +01:00
Ok(HttpResponse::Ok()
2018-03-09 14:29:06 +01:00
.content_type("application/protobuf")
2018-03-09 03:05:13 +01:00
.body(buf)
.into())
})
}
}
2018-03-09 14:29:06 +01:00
pub struct ProtoBufMessage<T, U: Message + Default>{
2018-03-09 03:05:13 +01:00
limit: usize,
ct: &'static str,
req: Option<T>,
fut: Option<Box<Future<Item=U, Error=ProtoBufPayloadError>>>,
}
2018-03-09 14:29:06 +01:00
impl<T, U: Message + Default> ProtoBufMessage<T, U> {
2018-03-09 03:05:13 +01:00
2018-03-09 14:29:06 +01:00
/// Create `ProtoBufMessage` for request.
2018-03-09 03:05:13 +01:00
pub fn new(req: T) -> Self {
2018-03-09 14:29:06 +01:00
ProtoBufMessage{
2018-03-09 03:05:13 +01:00
limit: 262_144,
req: Some(req),
fut: None,
ct: "application/protobuf",
}
}
/// Change max size of payload. By default max size is 256Kb
pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit;
self
}
/// Set allowed content type.
///
/// By default *application/protobuf* content type is used. Set content type
/// to empty string if you want to disable content type check.
pub fn content_type(mut self, ct: &'static str) -> Self {
self.ct = ct;
self
}
}
2018-03-09 14:29:06 +01:00
impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
2018-03-09 03:05:13 +01:00
where T: HttpMessage + Stream<Item=Bytes, Error=PayloadError> + 'static
{
type Item = U;
type Error = ProtoBufPayloadError;
fn poll(&mut self) -> Poll<U, ProtoBufPayloadError> {
if let Some(req) = self.req.take() {
if let Some(len) = req.headers().get(CONTENT_LENGTH) {
if let Ok(s) = len.to_str() {
if let Ok(len) = s.parse::<usize>() {
if len > self.limit {
return Err(ProtoBufPayloadError::Overflow);
}
} else {
return Err(ProtoBufPayloadError::Overflow);
}
}
}
// check content-type
if !self.ct.is_empty() && req.content_type() != self.ct {
return Err(ProtoBufPayloadError::ContentType)
}
let limit = self.limit;
let fut = req.from_err()
.fold(BytesMut::new(), 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.fut.as_mut().expect("ProtoBufBody could not be used second time").poll()
}
2018-03-09 05:56:18 +01:00
}
2018-03-09 14:29:06 +01:00
pub trait ProtoBufResponseBuilder {
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error>;
}
impl ProtoBufResponseBuilder for HttpResponseBuilder {
2018-03-09 05:56:18 +01:00
2018-03-09 14:29:06 +01:00
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
2018-03-09 05:56:18 +01:00
self.header(CONTENT_TYPE, "application/protobuf");
let mut body = Vec::new();
2018-03-09 14:29:06 +01:00
value.encode(&mut body).map_err(|e| ProtoBufPayloadError::Serialize(e))?;
2018-03-09 05:56:18 +01:00
Ok(self.body(body)?)
}
}