Skip to content

refactor(hyper): Split hyper into multiple crates #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/target
/Cargo.lock
/hypernet/target
/hypernet/Cargo.lock
/hyperprotocol/target
/hyperprotocol/Cargo.lock
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ cache:
directories:
- target

script:
- cargo build
- cargo test
- cargo bench
script: ./script.sh

after_success: |
[ $TRAVIS_BRANCH = master ] &&
Expand Down
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ authors = ["Sean McArthur <sean.monstar@gmail.com>",
keywords = ["http", "hyper", "hyperium"]

[dependencies]
cookie = "*"
httparse = "*"
log = ">= 0.2.0"
mime = "*"
num_cpus = "*"
openssl = "*"
rustc-serialize = "*"
time = "*"
unicase = "*"
num_cpus = "*"
url = "*"

[dependencies.hypernet]
path = "hypernet"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly confident this will prevent uploading to crates.io. We will have to upload and depend on hypernet and hyperprotocol as normal dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably true.

If we split hyper consequently there will be no "hyper" crate only "hyperclient" and "hyperserver". I have no idea what the current crate will be useful for.


[dependencies.hyperprotocol]
path = "hyperprotocol"

[dev-dependencies]
env_logger = "*"

[dev-dependencies.hypernet]
path = "hypernet"
features = ["mock"]
1 change: 0 additions & 1 deletion benches/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,3 @@ fn bench_mock_hyper(b: &mut test::Bencher) {
.read_to_string(&mut s).unwrap()
});
}

34 changes: 34 additions & 0 deletions benches/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![deny(warnings)]
#![feature(test)]
extern crate hyper;
extern crate test;

use test::Bencher;
use std::io::{Read, Write};

use hyper::method::Method::Get;
use hyper::server::{Request, Response};

static PHRASE: &'static [u8] = b"Benchmarking hyper vs others!";

fn request(url: hyper::Url) {
let req = hyper::client::Request::new(Get, url).unwrap();
let mut s = String::new();
req.start().unwrap().send().unwrap().read_to_string(&mut s).unwrap();
}

fn hyper_handle(_: Request, res: Response) {
let mut res = res.start().unwrap();
res.write_all(PHRASE).unwrap();
res.end().unwrap();
}

#[bench]
fn bench_hyper(b: &mut Bencher) {
let server = hyper::Server::http(hyper_handle);
let mut listener = server.listen("127.0.0.1").unwrap();

let url = hyper::Url::parse(&*format!("http://{}", listener.socket)).unwrap();
b.iter(|| request(url.clone()));
listener.close().unwrap();
}
13 changes: 13 additions & 0 deletions hypernet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]

name = "hypernet"
version = "0.0.1"
authors = ["Sean McArthur <sean.monstar@gmail.com>",
"Jonathan Reem <jonathan.reem@gmail.com>"]

[dependencies]
log = ">= 0.2.0"
openssl = "*"

[features]
mock = []
14 changes: 13 additions & 1 deletion src/net.rs → hypernet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#![feature(core, io)]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
#![cfg_attr(test, feature(box_syntax))]
//! A collection of traits abstracting over Listeners and Streams.

#[macro_use]
extern crate log;
extern crate openssl;

use std::any::{Any, TypeId};
use std::fmt;
use std::io::{self, Read, Write};
Expand Down Expand Up @@ -347,9 +356,12 @@ fn lift_ssl_error(ssl: SslError) -> io::Error {
}
}

#[cfg(any(test, feature="mock"))]
pub mod mock;

#[cfg(test)]
mod tests {
use mock::MockStream;
use super::mock::MockStream;
use super::NetworkStream;

#[test]
Expand Down
16 changes: 13 additions & 3 deletions src/mock.rs → hypernet/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Contains various utility types and macros useful for testing hyper clients.
use std::fmt;
use std::io::{self, Read, Write, Cursor};
use std::net::SocketAddr;

use net::{NetworkStream, NetworkConnector};
use super::{NetworkStream, NetworkConnector};

/// A `NetworkStream` compatible stream that writes into memory, and reads from memory.
pub struct MockStream {
/// Data readable from stream.
pub read: Cursor<Vec<u8>>,
/// Data written to the stream.
pub write: Vec<u8>,
}

Expand All @@ -31,13 +35,15 @@ impl PartialEq for MockStream {
}

impl MockStream {
/// Creates a new empty mock stream.
pub fn new() -> MockStream {
MockStream {
read: Cursor::new(vec![]),
write: vec![],
}
}

/// Creates a new stream with data that can be read from the stream.
pub fn with_input(input: &[u8]) -> MockStream {
MockStream {
read: Cursor::new(input.to_vec()),
Expand Down Expand Up @@ -68,6 +74,8 @@ impl NetworkStream for MockStream {
}
}

/// A `NetworkConnector` which creates `MockStream` instances exclusively.
/// It may be useful to intercept writes.
pub struct MockConnector;

impl NetworkConnector for MockConnector {
Expand All @@ -78,7 +86,10 @@ impl NetworkConnector for MockConnector {
}
}

/// new connectors must be created if you wish to intercept requests.
/// This macro maps host URLs to a respective reply, which is given in plain-text.
/// It ignores, but stores, everything that is written to it. However, the stored
/// values are not accessible just yet.
#[macro_export]
macro_rules! mock_connector (
($name:ident {
$($url:expr => $res:expr)*
Expand Down Expand Up @@ -111,4 +122,3 @@ macro_rules! mock_connector (

)
);

17 changes: 17 additions & 0 deletions hyperprotocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]

name = "hyperprotocol"
version = "0.0.1"
authors = ["Sean McArthur <sean.monstar@gmail.com>",
"Jonathan Reem <jonathan.reem@gmail.com>",
"Pyfisch <pyfisch@gmail.com>"]

[dependencies]
cookie = "*"
httparse = "*"
log = ">= 0.2.0"
mime = "*"
rustc-serialize = "*"
time = "*"
unicase = "*"
url = "*"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use header::QualityItem;
/// Example:
///
/// ```
/// # use hyper::header::Headers;
/// # use hyper::header::Accept;
/// # use hyper::header::qitem;
/// use hyper::mime::Mime;
/// use hyper::mime::TopLevel::Text;
/// use hyper::mime::SubLevel::{Html, Xml};
/// # use hyperprotocol::header::Headers;
/// # use hyperprotocol::header::Accept;
/// # use hyperprotocol::header::qitem;
/// use hyperprotocol::mime::Mime;
/// use hyperprotocol::mime::TopLevel::Text;
/// use hyperprotocol::mime::SubLevel::{Html, Xml};
/// # let mut headers = Headers::new();
/// headers.set(Accept(vec![
/// qitem(Mime(Text, Html, vec![])),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate url;

use std::fmt::{self};
use std::fmt;
use std::str;

use url;

use header;

/// The `Access-Control-Allow-Origin` response header,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/header/mod.rs → hyperprotocol/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Headers {
/// Example:
///
/// ```
/// # use hyper::header::Headers;
/// # use hyperprotocol::header::Headers;
/// # let mut headers = Headers::new();
/// let raw_content_type = headers.get_raw("content-type");
/// ```
Expand All @@ -162,7 +162,7 @@ impl Headers {
/// Example:
///
/// ```
/// # use hyper::header::Headers;
/// # use hyperprotocol::header::Headers;
/// # let mut headers = Headers::new();
/// headers.set_raw("content-length", vec![b"5".to_vec()]);
/// ```
Expand Down Expand Up @@ -190,8 +190,8 @@ impl Headers {
/// Example:
///
/// ```
/// # use hyper::header::Headers;
/// # use hyper::header::ContentType;
/// # use hyperprotocol::header::Headers;
/// # use hyperprotocol::header::ContentType;
/// # let mut headers = Headers::new();
/// let has_type = headers.has::<ContentType>();
/// ```
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions hyperprotocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![feature(core, box_syntax, unsafe_destructor, into_cow, convert)]
#![cfg_attr(test, feature(test))]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]

//! # Hyperprotocol
//! Hyperprotol contains the high-level semantics of HTTP. The provided code is used by both client
//! and server.

extern crate cookie;
extern crate httparse;
#[macro_use]
extern crate log;
extern crate mime as mime_crate;
extern crate rustc_serialize as serialize;
extern crate time;
extern crate unicase;
extern crate url;

#[cfg(test)]
extern crate test;

pub use mime_crate as mime;

pub mod error;
pub mod header;
pub mod method;
pub mod status;
pub mod version;
File renamed without changes.
12 changes: 6 additions & 6 deletions src/status.rs → hyperprotocol/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::cmp::Ordering;
/// ```rust
/// #![feature(core)]
/// # use std::num::FromPrimitive;
/// # use hyper::status::StatusCode;
/// # use hyperprotocol::status::StatusCode;
/// let statusopt: Option<StatusCode> = FromPrimitive::from_u16(137u16);
/// assert_eq!(statusopt.unwrap().class().default_code(), StatusCode::Continue);
/// ```
Expand Down Expand Up @@ -357,7 +357,7 @@ impl Copy for StatusCode {}
/// Formats the status code, *including* the canonical reason.
///
/// ```rust
/// # use hyper::status::StatusCode::{ImATeapot, Unregistered};
/// # use hyperprotocol::status::StatusCode::{ImATeapot, Unregistered};
/// assert_eq!(format!("{}", ImATeapot), "418 I'm a teapot");
/// assert_eq!(format!("{}", Unregistered(123)),
/// "123 <unknown status code>");
Expand All @@ -367,8 +367,8 @@ impl Copy for StatusCode {}
///
/// ```rust
/// #![feature(core)]
/// # use hyperprotocol::status::StatusCode::{ImATeapot, Unregistered};
/// # use std::num::ToPrimitive;
/// # use hyper::status::StatusCode::{ImATeapot, Unregistered};
/// assert_eq!(format!("{}", ImATeapot.to_u16().unwrap()), "418");
/// assert_eq!(format!("{}", Unregistered(123).to_u16().unwrap()), "123");
/// ```
Expand Down Expand Up @@ -696,8 +696,8 @@ impl StatusClass {
/// example, this will produce `BadRequest` (400):
///
/// ```rust
/// # use hyper::status::StatusClass::ClientError;
/// # use hyper::status::StatusCode::BadRequest;
/// # use hyperprotocol::status::StatusClass::ClientError;
/// # use hyperprotocol::status::StatusCode::BadRequest;
/// assert_eq!(ClientError.default_code(), BadRequest);
/// ```
///
Expand All @@ -721,7 +721,7 @@ impl StatusClass {
/// This is demonstrated thusly:
///
/// ```rust
/// # use hyper::status::StatusCode::{Unregistered, BadRequest};
/// # use hyperprotocol::status::StatusCode::{Unregistered, BadRequest};
/// // Suppose we have received this status code.
/// // You will never directly create an unregistered status code.
/// let status = Unregistered(471);
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cargo build
cargo test
cargo bench

cd hypernet
cargo test
cargo bench

cd ../hyperprotocol
cargo test
cargo bench
Loading