1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

Merge pull request #3 from hstefan/actix-web-1.0

Making library compatible with actix-web 1.0
This commit is contained in:
Nikolay Kim 2019-05-19 08:36:37 -07:00 committed by GitHub
commit 5b138af1b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 93 deletions

View File

@ -1,6 +1,5 @@
language: rust language: rust
rust: rust:
- 1.21.0
- stable - stable
- beta - beta
- nightly - nightly

View File

@ -1,5 +1,10 @@
# Changes # Changes
## 0.4.0 (2019-05-18)
* Upgrade to actix-web 1.0.0-rc
* Removed `protobuf` method for `HttpRequest` (use `ProtoBuf` extractor instead)
## 0.3.0 (2019-03-07) ## 0.3.0 (2019-03-07)
* Upgrade to actix-web 0.7.18 * Upgrade to actix-web 0.7.18

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-protobuf" name = "actix-protobuf"
version = "0.3.0" version = "0.4.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"
@ -21,10 +21,10 @@ path = "src/lib.rs"
[dependencies] [dependencies]
bytes = "0.4" bytes = "0.4"
futures = "0.1" futures = "0.1"
failure = "0.1" derive_more = "0.14"
actix = "0.7" actix = "0.8.1"
actix-web = "0.7" actix-web = "1.0.0-rc"
prost = "0.4" prost = "0.4"

View File

@ -10,6 +10,6 @@ env_logger = "*"
prost = "0.4" prost = "0.4"
prost-derive = "0.4" prost-derive = "0.4"
actix = "0.7" actix = "0.8.1"
actix-web = "0.7" actix-web = "1.0.0-rc"
actix-protobuf = { path="../../" } actix-protobuf = { path="../../" }

10
examples/prost-example/client.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
# just start server and run client.py # just start server and run client.py
# wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-python-3.5.1.zip # wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-python-3.5.1.zip
@ -5,12 +6,11 @@
# cd protobuf-3.5.1/python/ # cd protobuf-3.5.1/python/
# python3.6 setup.py install # python3.6 setup.py install
# pip3.6 install --upgrade pip # pip3 install --upgrade pip
# pip3.6 install aiohttp # pip3 install aiohttp
# python3.6 client.py # python3 client.py
#!/usr/bin/env python
import test_pb2 import test_pb2
import traceback import traceback
import sys import sys
@ -48,7 +48,7 @@ async def fetch(session):
obj = test_pb2.MyObj() obj = test_pb2.MyObj()
obj.number = 9 obj.number = 9
obj.name = 'USB' obj.name = 'USB'
async with session.post('http://localhost:8080/', data=obj.SerializeToString(), async with session.post('http://127.0.0.1:8081/', data=obj.SerializeToString(),
headers={"content-type": "application/protobuf"}) as resp: headers={"content-type": "application/protobuf"}) as resp:
print(resp.status) print(resp.status)
data = await resp.read() data = await resp.read()

View File

@ -1,14 +1,14 @@
extern crate bytes;
extern crate actix; extern crate actix;
extern crate actix_web;
extern crate actix_protobuf; extern crate actix_protobuf;
extern crate actix_web;
extern crate bytes;
extern crate env_logger; extern crate env_logger;
extern crate prost; extern crate prost;
#[macro_use] #[macro_use]
extern crate prost_derive; extern crate prost_derive;
use actix_web::*;
use actix_protobuf::*; use actix_protobuf::*;
use actix_web::*;
#[derive(Clone, PartialEq, Message)] #[derive(Clone, PartialEq, Message)]
pub struct MyObj { pub struct MyObj {
@ -18,26 +18,26 @@ pub struct MyObj {
pub name: String, pub name: String,
} }
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> { fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
println!("model: {:?}", msg); println!("model: {:?}", msg);
HttpResponse::Ok().protobuf(msg.0) // <- send response HttpResponse::Ok().protobuf(msg.0) // <- send response
} }
fn main() { fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info"); ::std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
let sys = actix::System::new("prost-example"); let sys = actix::System::new("prost-example");
server::new(|| { HttpServer::new(|| {
App::new() App::new()
.middleware(middleware::Logger::default()) .wrap(middleware::Logger::default())
.resource("/", |r| r.method(http::Method::POST).with(index))}) .service(web::resource("/").route(web::post().to(index)))
.bind("127.0.0.1:8080").unwrap() })
.bind("127.0.0.1:8081")
.unwrap()
.shutdown_timeout(1) .shutdown_timeout(1)
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8081");
let _ = sys.run(); let _ = sys.run();
} }

View File

@ -2,8 +2,7 @@ extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate bytes; extern crate bytes;
extern crate futures; extern crate futures;
#[macro_use] extern crate derive_more;
extern crate failure;
#[cfg(test)] #[cfg(test)]
extern crate http; extern crate http;
@ -13,6 +12,7 @@ extern crate prost;
#[macro_use] extern crate prost_derive; #[macro_use] extern crate prost_derive;
use std::fmt; use std::fmt;
use derive_more::Display;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use bytes::{BytesMut, IntoBuf}; use bytes::{BytesMut, IntoBuf};
@ -23,27 +23,26 @@ use prost::EncodeError as ProtoBufEncodeError;
use futures::{Poll, Future, Stream}; 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, Payload};
use actix_web::error::{Error, PayloadError, ResponseError}; use actix_web::error::{Error, PayloadError, ResponseError};
#[derive(Debug, Display)]
#[derive(Fail, Debug)]
pub enum ProtoBufPayloadError { pub enum ProtoBufPayloadError {
/// Payload size is bigger than 256k /// Payload size is bigger than 256k
#[fail(display="Payload size is bigger than 256k")] #[display(fmt="Payload size is bigger than 256k")]
Overflow, Overflow,
/// Content type error /// Content type error
#[fail(display="Content type error")] #[display(fmt="Content type error")]
ContentType, ContentType,
/// Serialize error /// Serialize error
#[fail(display="ProtoBuf serialize error: {}", _0)] #[display(fmt="ProtoBuf serialize error: {}", _0)]
Serialize(#[cause] ProtoBufEncodeError), Serialize(ProtoBufEncodeError),
/// Deserialize error /// Deserialize error
#[fail(display="ProtoBuf deserialize error: {}", _0)] #[display(fmt="ProtoBuf deserialize error: {}", _0)]
Deserialize(#[cause] ProtoBufDecodeError), Deserialize(ProtoBufDecodeError),
/// Payload error /// Payload error
#[fail(display="Error that occur during reading payload: {}", _0)] #[display(fmt="Error that occur during reading payload: {}", _0)]
Payload(#[cause] PayloadError), Payload(PayloadError),
} }
impl ResponseError for ProtoBufPayloadError { impl ResponseError for ProtoBufPayloadError {
@ -115,27 +114,31 @@ impl Default for ProtoBufConfig {
} }
} }
impl<T, S> FromRequest<S> for ProtoBuf<T> impl<T> FromRequest for ProtoBuf<T>
where T: Message + Default + 'static, S: 'static where T: Message + Default + 'static
{ {
type Config = ProtoBufConfig; type Config = ProtoBufConfig;
type Result = Box<Future<Item=Self, Error=Error>>; type Error = Error;
type Future = Box<Future<Item=Self, Error=Error>>;
#[inline] #[inline]
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let limit = req.app_data::<ProtoBufConfig>().map(|c| c.limit).unwrap_or(262_144);
Box::new( Box::new(
ProtoBufMessage::new(req) ProtoBufMessage::new(req, payload)
.limit(cfg.limit) .limit(limit)
.from_err() .map_err(move |e| {
e.into()
})
.map(ProtoBuf)) .map(ProtoBuf))
} }
} }
impl<T: Message + Default> Responder for ProtoBuf<T> { impl<T: Message + Default> Responder for ProtoBuf<T> {
type Item = HttpResponse;
type Error = Error; type Error = Error;
type Future = Result<HttpResponse, Error>;
fn respond_to<S>(self, _: &HttpRequest<S>) -> Result<HttpResponse, Error> { fn respond_to(self, _: &HttpRequest) -> Self::Future {
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)))
@ -147,18 +150,18 @@ impl<T: Message + Default> Responder for ProtoBuf<T> {
} }
} }
pub struct ProtoBufMessage<T: HttpMessage, U: Message + Default>{ pub struct ProtoBufMessage<T: Message + Default>{
limit: usize, limit: usize,
length: Option<usize>, length: Option<usize>,
stream: Option<T::Stream>, stream: Option<Payload>,
err: Option<ProtoBufPayloadError>, err: Option<ProtoBufPayloadError>,
fut: Option<Box<Future<Item=U, Error=ProtoBufPayloadError>>>, fut: Option<Box<Future<Item=T, Error=ProtoBufPayloadError>>>,
} }
impl<T: HttpMessage, U: Message + Default> ProtoBufMessage<T, U> { impl<T: Message + Default> ProtoBufMessage<T> {
/// Create `ProtoBufMessage` for request. /// Create `ProtoBufMessage` for request.
pub fn new(req: &T) -> Self { pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
if req.content_type() != "application/protobuf" { if req.content_type() != "application/protobuf" {
return ProtoBufMessage { return ProtoBufMessage {
limit: 262_144, limit: 262_144,
@ -181,7 +184,7 @@ impl<T: HttpMessage, U: Message + Default> ProtoBufMessage<T, U> {
ProtoBufMessage { ProtoBufMessage {
limit: 262_144, limit: 262_144,
length: len, length: len,
stream: Some(req.payload()), stream: Some(payload.take()),
fut: None, fut: None,
err: None, err: None,
} }
@ -194,13 +197,12 @@ impl<T: HttpMessage, U: Message + Default> ProtoBufMessage<T, U> {
} }
} }
impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U> impl<T: Message + Default + 'static> Future for ProtoBufMessage<T>
where T: HttpMessage + 'static
{ {
type Item = U; type Item = T;
type Error = ProtoBufPayloadError; type Error = ProtoBufPayloadError;
fn poll(&mut self) -> Poll<U, ProtoBufPayloadError> { fn poll(&mut self) -> Poll<T, ProtoBufPayloadError> {
if let Some(ref mut fut) = self.fut { if let Some(ref mut fut) = self.fut {
return fut.poll(); return fut.poll();
} }
@ -228,7 +230,7 @@ where T: HttpMessage + 'static
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(<T>::decode(&mut body.into_buf())?));
self.fut = Some(Box::new(fut)); self.fut = Some(Box::new(fut));
self.poll() self.poll()
} }
@ -251,30 +253,11 @@ impl ProtoBufResponseBuilder for HttpResponseBuilder {
} }
} }
pub trait ProtoBufHttpMessage {
fn protobuf<T: Message + Default>(&self) -> ProtoBufMessage<Self, T>
where Self: HttpMessage + 'static;
}
impl<S> ProtoBufHttpMessage for HttpRequest<S> {
#[inline]
fn protobuf<T: Message + Default>(&self) -> ProtoBufMessage<Self, T>
where Self: HttpMessage + 'static
{
ProtoBufMessage::new(self)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use http::header; use http::header;
use actix_web::test::TestRequest; use actix_web::test::{block_on, TestRequest};
impl PartialEq for ProtoBufPayloadError { impl PartialEq for ProtoBufPayloadError {
fn eq(&self, other: &ProtoBufPayloadError) -> bool { fn eq(&self, other: &ProtoBufPayloadError) -> bool {
@ -303,33 +286,34 @@ 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(&TestRequest::default().finish()).unwrap(); let req = TestRequest::default().to_http_request();
let resp = protobuf.respond_to(&req).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 = TestRequest::default().finish(); let (req, mut pl) = TestRequest::default().to_http_parts();
let mut protobuf = req.protobuf::<MyObject>(); let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl));
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType); assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::ContentType);
let req = TestRequest::default() let (req, mut pl) = TestRequest::default()
.header( .header(
header::CONTENT_TYPE, header::CONTENT_TYPE,
header::HeaderValue::from_static("application/text"), header::HeaderValue::from_static("application/text"),
).finish(); ).to_http_parts();
let mut protobuf = req.protobuf::<MyObject>(); let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl));
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::ContentType); assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::ContentType);
let req = TestRequest::default() let (req, mut pl) = TestRequest::default()
.header( .header(
header::CONTENT_TYPE, header::CONTENT_TYPE,
header::HeaderValue::from_static("application/protobuf"), header::HeaderValue::from_static("application/protobuf"),
).header( ).header(
header::CONTENT_LENGTH, header::CONTENT_LENGTH,
header::HeaderValue::from_static("10000"), header::HeaderValue::from_static("10000"),
).finish(); ).to_http_parts();
let mut protobuf = req.protobuf::<MyObject>().limit(100); let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl).limit(100));
assert_eq!(protobuf.poll().err().unwrap(), ProtoBufPayloadError::Overflow); assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::Overflow);
} }
} }