mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
Upgrade to actix-web 0.7.18
This commit is contained in:
parent
99e4e1c3ec
commit
def07f58c8
@ -1,5 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## 0.3.0 (2019-03-07)
|
||||||
|
|
||||||
|
* Upgrade to actix-web 0.7.18
|
||||||
|
|
||||||
## 0.2.0 (2018-04-10)
|
## 0.2.0 (2018-04-10)
|
||||||
|
|
||||||
* Provide protobuf extractor
|
* Provide protobuf extractor
|
||||||
|
12
Cargo.toml
12
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-protobuf"
|
name = "actix-protobuf"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
authors = ["kingxsp <jin.hb.zh@outlook.com>"]
|
authors = ["kingxsp <jin.hb.zh@outlook.com>"]
|
||||||
description = "Protobuf support for actix-web framework."
|
description = "Protobuf support for actix-web framework."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -23,14 +23,14 @@ bytes = "0.4"
|
|||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
failure = "0.1"
|
failure = "0.1"
|
||||||
|
|
||||||
actix = "0.5"
|
actix = "0.7"
|
||||||
actix-web = "0.5"
|
actix-web = "0.7"
|
||||||
|
|
||||||
prost = "0.2"
|
prost = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
http = "^0.1.5"
|
http = "^0.1"
|
||||||
prost-derive = "^0.2"
|
prost-derive = "^0.4"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "prost-example"
|
name = "prost-example"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
authors = ["kingxsp <jin.hb.zh@outlook.com>"]
|
authors = ["kingxsp <jin.hb.zh@outlook.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
|
|
||||||
prost = "0.2"
|
prost = "0.4"
|
||||||
prost-derive = "0.2"
|
prost-derive = "0.4"
|
||||||
|
|
||||||
actix = "0.5"
|
actix = "0.7"
|
||||||
actix-web = "0.5"
|
actix-web = "0.7"
|
||||||
actix-protobuf = { path="../../" }
|
actix-protobuf = { path="../../" }
|
@ -10,7 +10,7 @@ extern crate prost_derive;
|
|||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use actix_protobuf::*;
|
use actix_protobuf::*;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Message)]
|
#[derive(Clone, PartialEq, Message)]
|
||||||
pub struct MyObj {
|
pub struct MyObj {
|
||||||
#[prost(int32, tag="1")]
|
#[prost(int32, tag="1")]
|
||||||
pub number: i32,
|
pub number: i32,
|
||||||
|
133
src/lib.rs
133
src/lib.rs
@ -14,14 +14,13 @@ extern crate prost;
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use bytes::{Bytes, BytesMut};
|
|
||||||
use futures::{Poll, Future, Stream};
|
|
||||||
|
|
||||||
use bytes::IntoBuf;
|
use bytes::{BytesMut, IntoBuf};
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
use prost::DecodeError as ProtoBufDecodeError;
|
use prost::DecodeError as ProtoBufDecodeError;
|
||||||
use prost::EncodeError as ProtoBufEncodeError;
|
use prost::EncodeError as ProtoBufEncodeError;
|
||||||
|
|
||||||
|
use futures::{Poll, Future, Stream};
|
||||||
use actix_web::http::header::{CONTENT_TYPE, CONTENT_LENGTH};
|
use actix_web::http::header::{CONTENT_TYPE, CONTENT_LENGTH};
|
||||||
use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse, FromRequest};
|
use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse, FromRequest};
|
||||||
use actix_web::dev::HttpResponseBuilder;
|
use actix_web::dev::HttpResponseBuilder;
|
||||||
@ -125,7 +124,7 @@ impl<T, S> FromRequest<S> for ProtoBuf<T>
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
|
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result {
|
||||||
Box::new(
|
Box::new(
|
||||||
ProtoBufMessage::new(req.clone())
|
ProtoBufMessage::new(req)
|
||||||
.limit(cfg.limit)
|
.limit(cfg.limit)
|
||||||
.from_err()
|
.from_err()
|
||||||
.map(ProtoBuf))
|
.map(ProtoBuf))
|
||||||
@ -136,7 +135,7 @@ impl<T: Message + Default> Responder for ProtoBuf<T> {
|
|||||||
type Item = HttpResponse;
|
type Item = HttpResponse;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
|
fn respond_to<S>(self, _: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
self.0.encode(&mut buf)
|
self.0.encode(&mut buf)
|
||||||
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
|
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
|
||||||
@ -148,22 +147,43 @@ impl<T: Message + Default> Responder for ProtoBuf<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ProtoBufMessage<T, U: Message + Default>{
|
pub struct ProtoBufMessage<T: HttpMessage, U: Message + Default>{
|
||||||
limit: usize,
|
limit: usize,
|
||||||
ct: &'static str,
|
length: Option<usize>,
|
||||||
req: Option<T>,
|
stream: Option<T::Stream>,
|
||||||
|
err: Option<ProtoBufPayloadError>,
|
||||||
fut: Option<Box<Future<Item=U, Error=ProtoBufPayloadError>>>,
|
fut: Option<Box<Future<Item=U, Error=ProtoBufPayloadError>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U: Message + Default> ProtoBufMessage<T, U> {
|
impl<T: HttpMessage, U: Message + Default> ProtoBufMessage<T, U> {
|
||||||
|
|
||||||
/// Create `ProtoBufMessage` for request.
|
/// Create `ProtoBufMessage` for request.
|
||||||
pub fn new(req: T) -> Self {
|
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 {
|
ProtoBufMessage {
|
||||||
limit: 262_144,
|
limit: 262_144,
|
||||||
req: Some(req),
|
length: len,
|
||||||
|
stream: Some(req.payload()),
|
||||||
fut: None,
|
fut: None,
|
||||||
ct: "application/protobuf",
|
err: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,56 +192,45 @@ impl<T, U: Message + Default> ProtoBufMessage<T, U> {
|
|||||||
self.limit = limit;
|
self.limit = limit;
|
||||||
self
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
|
impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
|
||||||
where T: HttpMessage + Stream<Item=Bytes, Error=PayloadError> + 'static
|
where T: HttpMessage + 'static
|
||||||
{
|
{
|
||||||
type Item = U;
|
type Item = U;
|
||||||
type Error = ProtoBufPayloadError;
|
type Error = ProtoBufPayloadError;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<U, ProtoBufPayloadError> {
|
fn poll(&mut self) -> Poll<U, ProtoBufPayloadError> {
|
||||||
if let Some(req) = self.req.take() {
|
if let Some(ref mut fut) = self.fut {
|
||||||
if let Some(len) = req.headers().get(CONTENT_LENGTH) {
|
return fut.poll();
|
||||||
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);
|
if let Some(err) = self.err.take() {
|
||||||
}
|
return Err(err);
|
||||||
}
|
|
||||||
}
|
|
||||||
// check content-type
|
|
||||||
if !self.ct.is_empty() && req.content_type() != self.ct {
|
|
||||||
return Err(ProtoBufPayloadError::ContentType)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let limit = self.limit;
|
let limit = self.limit;
|
||||||
let fut = req.from_err()
|
if let Some(len) = self.length.take() {
|
||||||
.fold(BytesMut::new(), move |mut body, chunk| {
|
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 {
|
if (body.len() + chunk.len()) > limit {
|
||||||
Err(ProtoBufPayloadError::Overflow)
|
Err(ProtoBufPayloadError::Overflow)
|
||||||
} else {
|
} else {
|
||||||
body.extend_from_slice(&chunk);
|
body.extend_from_slice(&chunk);
|
||||||
Ok(body)
|
Ok(body)
|
||||||
}
|
}
|
||||||
})
|
}).and_then(|body| Ok(<U>::decode(&mut body.into_buf())?));
|
||||||
.and_then(|body| Ok(<U>::decode(&mut body.into_buf())?));
|
|
||||||
self.fut = Some(Box::new(fut));
|
self.fut = Some(Box::new(fut));
|
||||||
}
|
self.poll()
|
||||||
|
|
||||||
self.fut.as_mut().expect("ProtoBufBody could not be used second time").poll()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,15 +254,15 @@ impl ProtoBufResponseBuilder for HttpResponseBuilder {
|
|||||||
|
|
||||||
|
|
||||||
pub trait ProtoBufHttpMessage {
|
pub trait ProtoBufHttpMessage {
|
||||||
fn protobuf<T: Message + Default>(self) -> ProtoBufMessage<Self, T>
|
fn protobuf<T: Message + Default>(&self) -> ProtoBufMessage<Self, T>
|
||||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized;
|
where Self: HttpMessage + 'static;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> ProtoBufHttpMessage for HttpRequest<S> {
|
impl<S> ProtoBufHttpMessage for HttpRequest<S> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn protobuf<T: Message + Default>(self) -> ProtoBufMessage<Self, T>
|
fn protobuf<T: Message + Default>(&self) -> ProtoBufMessage<Self, T>
|
||||||
where Self: Stream<Item=Bytes, Error=PayloadError> + Sized
|
where Self: HttpMessage + 'static
|
||||||
{
|
{
|
||||||
ProtoBufMessage::new(self)
|
ProtoBufMessage::new(self)
|
||||||
}
|
}
|
||||||
@ -265,6 +274,7 @@ impl<S> ProtoBufHttpMessage for HttpRequest<S> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use http::header;
|
use http::header;
|
||||||
|
use actix_web::test::TestRequest;
|
||||||
|
|
||||||
impl PartialEq for ProtoBufPayloadError {
|
impl PartialEq for ProtoBufPayloadError {
|
||||||
fn eq(&self, other: &ProtoBufPayloadError) -> bool {
|
fn eq(&self, other: &ProtoBufPayloadError) -> bool {
|
||||||
@ -282,7 +292,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Message)]
|
#[derive(Clone, PartialEq, Message)]
|
||||||
pub struct MyObject {
|
pub struct MyObject {
|
||||||
#[prost(int32, tag="1")]
|
#[prost(int32, tag="1")]
|
||||||
pub number: i32,
|
pub number: i32,
|
||||||
@ -293,27 +303,32 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_protobuf() {
|
fn test_protobuf() {
|
||||||
let protobuf = ProtoBuf(MyObject{number: 9 , name: "test".to_owned()});
|
let protobuf = ProtoBuf(MyObject{number: 9 , name: "test".to_owned()});
|
||||||
let resp = protobuf.respond_to(HttpRequest::default()).unwrap();
|
let resp = protobuf.respond_to(&TestRequest::default().finish()).unwrap();
|
||||||
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/protobuf");
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/protobuf");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_protobuf_message() {
|
fn test_protobuf_message() {
|
||||||
let req = HttpRequest::default();
|
let req = TestRequest::default().finish();
|
||||||
let mut protobuf = req.protobuf::<MyObject>();
|
let mut protobuf = req.protobuf::<MyObject>();
|
||||||
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType);
|
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType);
|
||||||
|
|
||||||
let mut req = HttpRequest::default();
|
let req = TestRequest::default()
|
||||||
req.headers_mut().insert(header::CONTENT_TYPE,
|
.header(
|
||||||
header::HeaderValue::from_static("application/protobuf"));
|
header::CONTENT_TYPE,
|
||||||
let mut protobuf = req.protobuf::<MyObject>().content_type("text/protobuf");
|
header::HeaderValue::from_static("application/text"),
|
||||||
|
).finish();
|
||||||
|
let mut protobuf = req.protobuf::<MyObject>();
|
||||||
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType);
|
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType);
|
||||||
|
|
||||||
let mut req = HttpRequest::default();
|
let req = TestRequest::default()
|
||||||
req.headers_mut().insert(header::CONTENT_TYPE,
|
.header(
|
||||||
header::HeaderValue::from_static("application/json"));
|
header::CONTENT_TYPE,
|
||||||
req.headers_mut().insert(header::CONTENT_LENGTH,
|
header::HeaderValue::from_static("application/protobuf"),
|
||||||
header::HeaderValue::from_static("10000"));
|
).header(
|
||||||
|
header::CONTENT_LENGTH,
|
||||||
|
header::HeaderValue::from_static("10000"),
|
||||||
|
).finish();
|
||||||
let mut protobuf = req.protobuf::<MyObject>().limit(100);
|
let mut protobuf = req.protobuf::<MyObject>().limit(100);
|
||||||
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::Overflow);
|
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::Overflow);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user