mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 10:27:42 +02:00
add protobuf feature
This commit is contained in:
44
src/error.rs
44
src/error.rs
@ -15,8 +15,6 @@ use http::{header, StatusCode, Error as HttpError};
|
||||
use http::uri::InvalidUriBytes;
|
||||
use http_range::HttpRangeParseError;
|
||||
use serde_json::error::Error as JsonError;
|
||||
use prost::EncodeError as ProtoBufEncodeError;
|
||||
use prost::DecodeError as ProtoBufDecodeError;
|
||||
pub use url::ParseError as UrlParseError;
|
||||
|
||||
// re-exports
|
||||
@ -109,10 +107,6 @@ impl From<failure::Error> for Error {
|
||||
/// `InternalServerError` for `JsonError`
|
||||
impl ResponseError for JsonError {}
|
||||
|
||||
/// `InternalServerError` for `ProtoBufEncodeError` `ProtoBufDecodeError`
|
||||
impl ResponseError for ProtoBufEncodeError {}
|
||||
impl ResponseError for ProtoBufDecodeError {}
|
||||
|
||||
/// `InternalServerError` for `UrlParseError`
|
||||
impl ResponseError for UrlParseError {}
|
||||
|
||||
@ -456,44 +450,6 @@ impl From<JsonError> for JsonPayloadError {
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
/// Deserialize error
|
||||
#[fail(display="Json deserialize error: {}", _0)]
|
||||
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 => httpcodes::HttpPayloadTooLarge.into(),
|
||||
_ => httpcodes::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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors which can occur when attempting to interpret a segment string as a
|
||||
/// valid path segment.
|
||||
#[derive(Fail, Debug, PartialEq)]
|
||||
|
@ -4,7 +4,6 @@ use bytes::{Bytes, BytesMut};
|
||||
use futures::{Future, Stream, Poll};
|
||||
use http_range::HttpRange;
|
||||
use serde::de::DeserializeOwned;
|
||||
use prost::Message;
|
||||
use mime::Mime;
|
||||
use url::form_urlencoded;
|
||||
use encoding::all::UTF_8;
|
||||
@ -13,7 +12,6 @@ use encoding::label::encoding_from_whatwg_label;
|
||||
use http::{header, HeaderMap};
|
||||
|
||||
use json::JsonBody;
|
||||
use protobuf::ProtoBufBody;
|
||||
use header::Header;
|
||||
use multipart::Multipart;
|
||||
use error::{ParseError, ContentTypeError,
|
||||
@ -211,12 +209,6 @@ pub trait HttpMessage {
|
||||
JsonBody::new(self)
|
||||
}
|
||||
|
||||
fn protobuf<T: Message + Default>(self) -> ProtoBufBody<Self, T>
|
||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized
|
||||
{
|
||||
ProtoBufBody::new(self)
|
||||
}
|
||||
|
||||
/// Return stream to http payload processes as multipart.
|
||||
///
|
||||
/// Content-type: multipart/form-data;
|
||||
|
@ -11,7 +11,6 @@ use http::{StatusCode, Version, HeaderMap, HttpTryFrom, Error as HttpError};
|
||||
use http::header::{self, HeaderName, HeaderValue};
|
||||
use serde_json;
|
||||
use serde::Serialize;
|
||||
use prost::Message;
|
||||
|
||||
use body::Body;
|
||||
use error::Error;
|
||||
@ -509,22 +508,6 @@ impl HttpResponseBuilder {
|
||||
Ok(self.body(body)?)
|
||||
}
|
||||
|
||||
pub fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
|
||||
let mut body = Vec::new();
|
||||
value.encode(&mut body)?;
|
||||
|
||||
let contains = if let Some(parts) = parts(&mut self.response, &self.err) {
|
||||
parts.headers.contains_key(header::CONTENT_TYPE)
|
||||
} else {
|
||||
true
|
||||
};
|
||||
if !contains {
|
||||
self.header(header::CONTENT_TYPE, "application/protobuf");
|
||||
}
|
||||
|
||||
Ok(self.body(body)?)
|
||||
}
|
||||
|
||||
/// Set an empty body and generate `HttpResponse`
|
||||
///
|
||||
/// `HttpResponseBuilder` can not be used after this call.
|
||||
|
11
src/lib.rs
11
src/lib.rs
@ -78,7 +78,6 @@ extern crate url;
|
||||
extern crate libc;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate prost;
|
||||
extern crate flate2;
|
||||
extern crate brotli2;
|
||||
extern crate encoding;
|
||||
@ -89,6 +88,9 @@ extern crate h2 as http2;
|
||||
extern crate trust_dns_resolver;
|
||||
#[macro_use] extern crate actix;
|
||||
|
||||
#[cfg(feature="protobuf")]
|
||||
extern crate prost;
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
@ -112,7 +114,6 @@ mod httprequest;
|
||||
mod httpresponse;
|
||||
mod info;
|
||||
mod json;
|
||||
mod protobuf;
|
||||
mod route;
|
||||
mod router;
|
||||
mod resource;
|
||||
@ -120,6 +121,11 @@ mod param;
|
||||
mod payload;
|
||||
mod pipeline;
|
||||
|
||||
#[cfg(feature="protobuf")]
|
||||
mod protobuf;
|
||||
#[cfg(feature="protobuf")]
|
||||
pub use protobuf::{ProtoBuf, ProtoBufBody};
|
||||
|
||||
pub mod client;
|
||||
pub mod fs;
|
||||
pub mod ws;
|
||||
@ -134,7 +140,6 @@ pub mod server;
|
||||
pub use error::{Error, Result, ResponseError};
|
||||
pub use body::{Body, Binary};
|
||||
pub use json::Json;
|
||||
pub use protobuf::ProtoBuf;
|
||||
pub use application::Application;
|
||||
pub use httpmessage::HttpMessage;
|
||||
pub use httprequest::HttpRequest;
|
||||
|
@ -1,16 +1,60 @@
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{Poll, Future, Stream};
|
||||
use http::header::CONTENT_LENGTH;
|
||||
use http::header::{CONTENT_TYPE, CONTENT_LENGTH};
|
||||
|
||||
use bytes::IntoBuf;
|
||||
use prost::Message;
|
||||
use prost::EncodeError as ProtoBufEncodeError;
|
||||
use prost::DecodeError as ProtoBufDecodeError;
|
||||
|
||||
use error::{Error, ProtoBufPayloadError, PayloadError};
|
||||
use error::{Error, PayloadError, ResponseError};
|
||||
use handler::Responder;
|
||||
use httpmessage::HttpMessage;
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
use httpresponse::{HttpResponse, HttpResponseBuilder};
|
||||
use httpcodes::{HttpBadRequest, HttpPayloadTooLarge};
|
||||
|
||||
#[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,
|
||||
/// Deserialize error
|
||||
#[fail(display="Json deserialize error: {}", _0)]
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
/// `InternalServerError` for `ProtoBufEncodeError` `ProtoBufDecodeError`
|
||||
impl ResponseError for ProtoBufEncodeError {}
|
||||
impl ResponseError for ProtoBufDecodeError {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ProtoBuf<T: Message>(pub T);
|
||||
@ -32,9 +76,6 @@ impl<T: Message> Responder for ProtoBuf<T> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
pub struct ProtoBufBody<T, U: Message + Default>{
|
||||
limit: usize,
|
||||
ct: &'static str,
|
||||
@ -110,4 +151,16 @@ impl<T, U: Message + Default + 'static> Future for ProtoBufBody<T, U>
|
||||
|
||||
self.fut.as_mut().expect("ProtoBufBody could not be used second time").poll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl HttpResponseBuilder {
|
||||
|
||||
pub 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)?;
|
||||
Ok(self.body(body)?)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user