Skip to content

Commit

Permalink
feat(http): use the bytes crate for Chunk and internally
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Mar 1, 2017
1 parent cf7cc50 commit 65b3e08
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 599 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ include = [
]

[dependencies]
base64 = "0.4.0"
base64 = "0.4"
bytes = "0.4"
futures = "0.1.7"
futures-cpupool = "0.1"
httparse = "1.0"
Expand Down
6 changes: 3 additions & 3 deletions src/header/common/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ mod tests {
use header::Header;

use http::{ServerTransaction, Http1Transaction};
use http::buf::MemBuf;
use bytes::BytesMut;

use mime::Mime;
use mime::TopLevel::Text;
Expand Down Expand Up @@ -1018,7 +1018,7 @@ mod tests {

let expected_link = Link::new(vec![first_link, second_link, third_link]);

let raw = MemBuf::from(b"GET /super_short_uri/and_whatever HTTP/1.1\r\nHost: \
let mut raw = BytesMut::from(b"GET /super_short_uri/and_whatever HTTP/1.1\r\nHost: \
hyper.rs\r\nAccept: a lot of things\r\nAccept-Charset: \
utf8\r\nAccept-Encoding: *\r\nLink: </TheBook/chapter2>; \
rel=\"previous\"; title*=UTF-8'de'letztes%20Kapitel, \
Expand All @@ -1029,7 +1029,7 @@ mod tests {
rel=\"previous\"; rev=next; title=\"previous chapter\"\
\r\n\r\n".to_vec());

let (mut res, _) = ServerTransaction::parse(&raw).unwrap().unwrap();
let (mut res, _) = ServerTransaction::parse(&mut raw).unwrap().unwrap();

let link = res.headers.remove::<Link>().unwrap();

Expand Down
6 changes: 3 additions & 3 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use self::internals::{Item, VecMap, Entry};
pub use self::shared::*;
pub use self::common::*;
pub use self::raw::Raw;
use http::buf::MemSlice;
use bytes::Bytes;

mod common;
mod internals;
Expand Down Expand Up @@ -611,8 +611,8 @@ impl<'a> Extend<HeaderView<'a>> for Headers {
}
}

impl<'a> Extend<(&'a str, MemSlice)> for Headers {
fn extend<I: IntoIterator<Item=(&'a str, MemSlice)>>(&mut self, iter: I) {
impl<'a> Extend<(&'a str, Bytes)> for Headers {
fn extend<I: IntoIterator<Item=(&'a str, Bytes)>>(&mut self, iter: I) {
for (name, value) in iter {
let name = HeaderName(UniCase(maybe_literal(name)));
//let trim = header.value.iter().rev().take_while(|&&x| x == b' ').count();
Expand Down
16 changes: 8 additions & 8 deletions src/header/raw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::fmt;
use http::buf::MemSlice;
use bytes::Bytes;

/// A raw header value.
#[derive(Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -72,7 +72,7 @@ enum Lines {
enum Line {
Static(&'static [u8]),
Owned(Vec<u8>),
Shared(MemSlice),
Shared(Bytes),
}

fn eq<A: AsRef<[u8]>, B: AsRef<[u8]>>(a: &[A], b: &[B]) -> bool {
Expand Down Expand Up @@ -152,9 +152,9 @@ impl<'a> From<&'a [u8]> for Raw {
}
}

impl From<MemSlice> for Raw {
impl From<Bytes> for Raw {
#[inline]
fn from(val: MemSlice) -> Raw {
fn from(val: Bytes) -> Raw {
Raw(Lines::One(Line::Shared(val)))
}
}
Expand All @@ -166,9 +166,9 @@ impl From<Vec<u8>> for Line {
}
}

impl From<MemSlice> for Line {
impl From<Bytes> for Line {
#[inline]
fn from(val: MemSlice) -> Line {
fn from(val: Bytes) -> Line {
Line::Shared(val)
}
}
Expand All @@ -183,11 +183,11 @@ impl AsRef<[u8]> for Line {
}
}

pub fn parsed(val: MemSlice) -> Raw {
pub fn parsed(val: Bytes) -> Raw {
Raw(Lines::One(From::from(val)))
}

pub fn push(raw: &mut Raw, val: MemSlice) {
pub fn push(raw: &mut Raw, val: Bytes) {
raw.push_line(Line::from(val));
}

Expand Down
14 changes: 10 additions & 4 deletions src/http/body.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::convert::From;

use tokio_proto;
use http::Chunk;
use bytes::Bytes;
use futures::{Poll, Stream};
use futures::sync::mpsc;
use tokio_proto;

use http::Chunk;

pub type TokioBody = tokio_proto::streaming::Body<Chunk, ::Error>;

Expand Down Expand Up @@ -58,6 +58,12 @@ impl From<Chunk> for Body {
}
}

impl From<Bytes> for Body {
fn from (bytes: Bytes) -> Body {
Body(TokioBody::from(Chunk::from(bytes)))
}
}

impl From<Vec<u8>> for Body {
fn from (vec: Vec<u8>) -> Body {
Body(TokioBody::from(Chunk::from(vec)))
Expand Down
Loading

0 comments on commit 65b3e08

Please sign in to comment.