mirror of
https://github.com/actix/actix-web.git
synced 2024-12-17 21:56:38 +00:00
rename BinaryBody
This commit is contained in:
parent
f2520d2d79
commit
c565965865
6 changed files with 103 additions and 104 deletions
|
@ -9,6 +9,5 @@ path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
#actix = "0.3"
|
actix = "^0.3.1"
|
||||||
actix = { git = "https://github.com/actix/actix.git" }
|
|
||||||
actix-web = { path = "../../" }
|
actix-web = { path = "../../" }
|
||||||
|
|
|
@ -24,6 +24,6 @@ serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
|
||||||
#actix = "0.3"
|
actix = "^0.3.1"
|
||||||
actix = { git = "https://github.com/actix/actix.git" }
|
#actix = { git = "https://github.com/actix/actix.git" }
|
||||||
actix-web = { path = "../../" }
|
actix-web = { path = "../../" }
|
||||||
|
|
190
src/body.rs
190
src/body.rs
|
@ -11,7 +11,7 @@ pub enum Body {
|
||||||
/// Empty response. `Content-Length` header is set to `0`
|
/// Empty response. `Content-Length` header is set to `0`
|
||||||
Empty,
|
Empty,
|
||||||
/// Specific response body.
|
/// Specific response body.
|
||||||
Binary(BinaryBody),
|
Binary(Binary),
|
||||||
/// Streaming response body with specified length.
|
/// Streaming response body with specified length.
|
||||||
Length(u64),
|
Length(u64),
|
||||||
/// Unspecified streaming response. Developer is responsible for setting
|
/// Unspecified streaming response. Developer is responsible for setting
|
||||||
|
@ -24,7 +24,7 @@ pub enum Body {
|
||||||
/// Represents various types of binary body.
|
/// Represents various types of binary body.
|
||||||
/// `Content-Length` header is set to length of the body.
|
/// `Content-Length` header is set to length of the body.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum BinaryBody {
|
pub enum Binary {
|
||||||
/// Bytes body
|
/// Bytes body
|
||||||
Bytes(Bytes),
|
Bytes(Bytes),
|
||||||
/// Static slice
|
/// Static slice
|
||||||
|
@ -60,143 +60,143 @@ impl Body {
|
||||||
|
|
||||||
/// Create body from slice (copy)
|
/// Create body from slice (copy)
|
||||||
pub fn from_slice(s: &[u8]) -> Body {
|
pub fn from_slice(s: &[u8]) -> Body {
|
||||||
Body::Binary(BinaryBody::Bytes(Bytes::from(s)))
|
Body::Binary(Binary::Bytes(Bytes::from(s)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<T> for Body where T: Into<BinaryBody>{
|
impl<T> From<T> for Body where T: Into<Binary>{
|
||||||
fn from(b: T) -> Body {
|
fn from(b: T) -> Body {
|
||||||
Body::Binary(b.into())
|
Body::Binary(b.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BinaryBody {
|
impl Binary {
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.len() == 0
|
self.len() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
match *self {
|
match *self {
|
||||||
BinaryBody::Bytes(ref bytes) => bytes.len(),
|
Binary::Bytes(ref bytes) => bytes.len(),
|
||||||
BinaryBody::Slice(slice) => slice.len(),
|
Binary::Slice(slice) => slice.len(),
|
||||||
BinaryBody::SharedBytes(ref bytes) => bytes.len(),
|
Binary::SharedBytes(ref bytes) => bytes.len(),
|
||||||
BinaryBody::ArcSharedBytes(ref bytes) => bytes.len(),
|
Binary::ArcSharedBytes(ref bytes) => bytes.len(),
|
||||||
BinaryBody::SharedString(ref s) => s.len(),
|
Binary::SharedString(ref s) => s.len(),
|
||||||
BinaryBody::ArcSharedString(ref s) => s.len(),
|
Binary::ArcSharedString(ref s) => s.len(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create binary body from slice
|
/// Create binary body from slice
|
||||||
pub fn from_slice(s: &[u8]) -> BinaryBody {
|
pub fn from_slice(s: &[u8]) -> Binary {
|
||||||
BinaryBody::Bytes(Bytes::from(s))
|
Binary::Bytes(Bytes::from(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static str> for BinaryBody {
|
impl From<&'static str> for Binary {
|
||||||
fn from(s: &'static str) -> BinaryBody {
|
fn from(s: &'static str) -> Binary {
|
||||||
BinaryBody::Slice(s.as_ref())
|
Binary::Slice(s.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&'static [u8]> for BinaryBody {
|
impl From<&'static [u8]> for Binary {
|
||||||
fn from(s: &'static [u8]) -> BinaryBody {
|
fn from(s: &'static [u8]) -> Binary {
|
||||||
BinaryBody::Slice(s)
|
Binary::Slice(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<u8>> for BinaryBody {
|
impl From<Vec<u8>> for Binary {
|
||||||
fn from(vec: Vec<u8>) -> BinaryBody {
|
fn from(vec: Vec<u8>) -> Binary {
|
||||||
BinaryBody::Bytes(Bytes::from(vec))
|
Binary::Bytes(Bytes::from(vec))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for BinaryBody {
|
impl From<String> for Binary {
|
||||||
fn from(s: String) -> BinaryBody {
|
fn from(s: String) -> Binary {
|
||||||
BinaryBody::Bytes(Bytes::from(s))
|
Binary::Bytes(Bytes::from(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a String> for BinaryBody {
|
impl<'a> From<&'a String> for Binary {
|
||||||
fn from(s: &'a String) -> BinaryBody {
|
fn from(s: &'a String) -> Binary {
|
||||||
BinaryBody::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s)))
|
Binary::Bytes(Bytes::from(AsRef::<[u8]>::as_ref(&s)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Bytes> for BinaryBody {
|
impl From<Bytes> for Binary {
|
||||||
fn from(s: Bytes) -> BinaryBody {
|
fn from(s: Bytes) -> Binary {
|
||||||
BinaryBody::Bytes(s)
|
Binary::Bytes(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BytesMut> for BinaryBody {
|
impl From<BytesMut> for Binary {
|
||||||
fn from(s: BytesMut) -> BinaryBody {
|
fn from(s: BytesMut) -> Binary {
|
||||||
BinaryBody::Bytes(s.freeze())
|
Binary::Bytes(s.freeze())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Rc<Bytes>> for BinaryBody {
|
impl From<Rc<Bytes>> for Binary {
|
||||||
fn from(body: Rc<Bytes>) -> BinaryBody {
|
fn from(body: Rc<Bytes>) -> Binary {
|
||||||
BinaryBody::SharedBytes(body)
|
Binary::SharedBytes(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Rc<Bytes>> for BinaryBody {
|
impl<'a> From<&'a Rc<Bytes>> for Binary {
|
||||||
fn from(body: &'a Rc<Bytes>) -> BinaryBody {
|
fn from(body: &'a Rc<Bytes>) -> Binary {
|
||||||
BinaryBody::SharedBytes(Rc::clone(body))
|
Binary::SharedBytes(Rc::clone(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Arc<Bytes>> for BinaryBody {
|
impl From<Arc<Bytes>> for Binary {
|
||||||
fn from(body: Arc<Bytes>) -> BinaryBody {
|
fn from(body: Arc<Bytes>) -> Binary {
|
||||||
BinaryBody::ArcSharedBytes(body)
|
Binary::ArcSharedBytes(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Arc<Bytes>> for BinaryBody {
|
impl<'a> From<&'a Arc<Bytes>> for Binary {
|
||||||
fn from(body: &'a Arc<Bytes>) -> BinaryBody {
|
fn from(body: &'a Arc<Bytes>) -> Binary {
|
||||||
BinaryBody::ArcSharedBytes(Arc::clone(body))
|
Binary::ArcSharedBytes(Arc::clone(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Rc<String>> for BinaryBody {
|
impl From<Rc<String>> for Binary {
|
||||||
fn from(body: Rc<String>) -> BinaryBody {
|
fn from(body: Rc<String>) -> Binary {
|
||||||
BinaryBody::SharedString(body)
|
Binary::SharedString(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Rc<String>> for BinaryBody {
|
impl<'a> From<&'a Rc<String>> for Binary {
|
||||||
fn from(body: &'a Rc<String>) -> BinaryBody {
|
fn from(body: &'a Rc<String>) -> Binary {
|
||||||
BinaryBody::SharedString(Rc::clone(body))
|
Binary::SharedString(Rc::clone(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Arc<String>> for BinaryBody {
|
impl From<Arc<String>> for Binary {
|
||||||
fn from(body: Arc<String>) -> BinaryBody {
|
fn from(body: Arc<String>) -> Binary {
|
||||||
BinaryBody::ArcSharedString(body)
|
Binary::ArcSharedString(body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Arc<String>> for BinaryBody {
|
impl<'a> From<&'a Arc<String>> for Binary {
|
||||||
fn from(body: &'a Arc<String>) -> BinaryBody {
|
fn from(body: &'a Arc<String>) -> Binary {
|
||||||
BinaryBody::ArcSharedString(Arc::clone(body))
|
Binary::ArcSharedString(Arc::clone(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsRef<[u8]> for BinaryBody {
|
impl AsRef<[u8]> for Binary {
|
||||||
fn as_ref(&self) -> &[u8] {
|
fn as_ref(&self) -> &[u8] {
|
||||||
match *self {
|
match *self {
|
||||||
BinaryBody::Bytes(ref bytes) => bytes.as_ref(),
|
Binary::Bytes(ref bytes) => bytes.as_ref(),
|
||||||
BinaryBody::Slice(slice) => slice,
|
Binary::Slice(slice) => slice,
|
||||||
BinaryBody::SharedBytes(ref bytes) => bytes.as_ref(),
|
Binary::SharedBytes(ref bytes) => bytes.as_ref(),
|
||||||
BinaryBody::ArcSharedBytes(ref bytes) => bytes.as_ref(),
|
Binary::ArcSharedBytes(ref bytes) => bytes.as_ref(),
|
||||||
BinaryBody::SharedString(ref s) => s.as_bytes(),
|
Binary::SharedString(ref s) => s.as_bytes(),
|
||||||
BinaryBody::ArcSharedString(ref s) => s.as_bytes(),
|
Binary::ArcSharedString(ref s) => s.as_bytes(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BinaryBody> for Frame {
|
impl From<Binary> for Frame {
|
||||||
fn from(b: BinaryBody) -> Frame {
|
fn from(b: Binary) -> Frame {
|
||||||
Frame::Payload(Some(b))
|
Frame::Payload(Some(b))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -207,70 +207,70 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_str() {
|
fn test_static_str() {
|
||||||
assert_eq!(BinaryBody::from("test").len(), 4);
|
assert_eq!(Binary::from("test").len(), 4);
|
||||||
assert_eq!(BinaryBody::from("test").as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from("test").as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_bytes() {
|
fn test_static_bytes() {
|
||||||
assert_eq!(BinaryBody::from(b"test".as_ref()).len(), 4);
|
assert_eq!(Binary::from(b"test".as_ref()).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(b"test".as_ref()).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(b"test".as_ref()).as_ref(), "test".as_bytes());
|
||||||
assert_eq!(BinaryBody::from_slice(b"test".as_ref()).len(), 4);
|
assert_eq!(Binary::from_slice(b"test".as_ref()).len(), 4);
|
||||||
assert_eq!(BinaryBody::from_slice(b"test".as_ref()).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from_slice(b"test".as_ref()).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vec() {
|
fn test_vec() {
|
||||||
assert_eq!(BinaryBody::from(Vec::from("test")).len(), 4);
|
assert_eq!(Binary::from(Vec::from("test")).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(Vec::from("test")).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(Vec::from("test")).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bytes() {
|
fn test_bytes() {
|
||||||
assert_eq!(BinaryBody::from(Bytes::from("test")).len(), 4);
|
assert_eq!(Binary::from(Bytes::from("test")).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(Bytes::from("test")).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(Bytes::from("test")).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rc_bytes() {
|
fn test_rc_bytes() {
|
||||||
let b = Rc::new(Bytes::from("test"));
|
let b = Rc::new(Bytes::from("test"));
|
||||||
assert_eq!(BinaryBody::from(b.clone()).len(), 4);
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
||||||
assert_eq!(BinaryBody::from(&b).len(), 4);
|
assert_eq!(Binary::from(&b).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ref_string() {
|
fn test_ref_string() {
|
||||||
let b = Rc::new("test".to_owned());
|
let b = Rc::new("test".to_owned());
|
||||||
assert_eq!(BinaryBody::from(&b).len(), 4);
|
assert_eq!(Binary::from(&b).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rc_string() {
|
fn test_rc_string() {
|
||||||
let b = Rc::new("test".to_owned());
|
let b = Rc::new("test".to_owned());
|
||||||
assert_eq!(BinaryBody::from(b.clone()).len(), 4);
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
||||||
assert_eq!(BinaryBody::from(&b).len(), 4);
|
assert_eq!(Binary::from(&b).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arc_bytes() {
|
fn test_arc_bytes() {
|
||||||
let b = Arc::new(Bytes::from("test"));
|
let b = Arc::new(Bytes::from("test"));
|
||||||
assert_eq!(BinaryBody::from(b.clone()).len(), 4);
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
||||||
assert_eq!(BinaryBody::from(&b).len(), 4);
|
assert_eq!(Binary::from(&b).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arc_string() {
|
fn test_arc_string() {
|
||||||
let b = Arc::new("test".to_owned());
|
let b = Arc::new("test".to_owned());
|
||||||
assert_eq!(BinaryBody::from(b.clone()).len(), 4);
|
assert_eq!(Binary::from(b.clone()).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(b.clone()).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(b.clone()).as_ref(), "test".as_bytes());
|
||||||
assert_eq!(BinaryBody::from(&b).len(), 4);
|
assert_eq!(Binary::from(&b).len(), 4);
|
||||||
assert_eq!(BinaryBody::from(&b).as_ref(), "test".as_bytes());
|
assert_eq!(Binary::from(&b).as_ref(), "test".as_bytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCel
|
||||||
Envelope, ToEnvelope, RemoteEnvelope};
|
Envelope, ToEnvelope, RemoteEnvelope};
|
||||||
|
|
||||||
use task::{IoContext, DrainFut};
|
use task::{IoContext, DrainFut};
|
||||||
use body::BinaryBody;
|
use body::Binary;
|
||||||
use route::{Route, Frame};
|
use route::{Route, Frame};
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ impl<A> HttpContext<A> where A: Actor<Context=Self> + Route {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write payload
|
/// Write payload
|
||||||
pub fn write<B: Into<BinaryBody>>(&mut self, data: B) {
|
pub fn write<B: Into<Binary>>(&mut self, data: B) {
|
||||||
self.stream.push_back(Frame::Payload(Some(data.into())))
|
self.stream.push_back(Frame::Payload(Some(data.into())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ pub mod multipart;
|
||||||
pub mod middlewares;
|
pub mod middlewares;
|
||||||
pub use encoding::ContentEncoding;
|
pub use encoding::ContentEncoding;
|
||||||
pub use error::ParseError;
|
pub use error::ParseError;
|
||||||
pub use body::{Body, BinaryBody};
|
pub use body::{Body, Binary};
|
||||||
pub use application::{Application, ApplicationBuilder};
|
pub use application::{Application, ApplicationBuilder};
|
||||||
pub use httprequest::{HttpRequest, UrlEncoded};
|
pub use httprequest::{HttpRequest, UrlEncoded};
|
||||||
pub use httpresponse::{HttpResponse, HttpResponseBuilder};
|
pub use httpresponse::{HttpResponse, HttpResponseBuilder};
|
||||||
|
|
|
@ -8,7 +8,7 @@ use http::{header, Version};
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
|
|
||||||
use task::{Task, DrainFut};
|
use task::{Task, DrainFut};
|
||||||
use body::BinaryBody;
|
use body::Binary;
|
||||||
use context::HttpContext;
|
use context::HttpContext;
|
||||||
use resource::Reply;
|
use resource::Reply;
|
||||||
use payload::Payload;
|
use payload::Payload;
|
||||||
|
@ -21,7 +21,7 @@ use httpcodes::HTTPExpectationFailed;
|
||||||
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
|
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
|
||||||
pub enum Frame {
|
pub enum Frame {
|
||||||
Message(HttpResponse),
|
Message(HttpResponse),
|
||||||
Payload(Option<BinaryBody>),
|
Payload(Option<Binary>),
|
||||||
Drain(Rc<RefCell<DrainFut>>),
|
Drain(Rc<RefCell<DrainFut>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue