Skip to content
Merged
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
17 changes: 7 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ authors = ["Sean McArthur <sean.monstar@gmail.com>",
"Jonathan Reem <jonathan.reem@gmail.com>"]

[dependencies]
url = "*"
openssl = "*"
mime = "*"
unsafe-any = "*"
cookie = "*"
time = "*"
mucell = "*"
log = "*"
mime = "*"
mucell = "*"
openssl = "*"
rustc-serialize = "*"

[dev-dependencies]
curl = "*"

time = "*"
unicase = "*"
unsafe-any = "*"
url = "*"
18 changes: 1 addition & 17 deletions benches/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(macro_rules)]
extern crate curl;
#![allow(unstable)]
extern crate hyper;

extern crate test;
Expand Down Expand Up @@ -31,21 +30,6 @@ fn handle(_r: Request, res: Response) {
try_return!(res.end());
}

#[bench]
fn bench_curl(b: &mut test::Bencher) {
let mut listening = listen();
let s = format!("http://{}/", listening.socket);
let url = s.as_slice();
b.iter(|| {
curl::http::handle()
.get(url)
.header("X-Foo", "Bar")
.exec()
.unwrap()
});
listening.close().unwrap();
}

#[derive(Clone)]
struct Foo;

Expand Down
21 changes: 3 additions & 18 deletions benches/client_mock_tcp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(default_type_params)]
extern crate curl;
#![allow(unstable)]
extern crate hyper;

extern crate test;
Expand Down Expand Up @@ -49,21 +48,6 @@ impl Writer for MockStream {
}
}

#[bench]
fn bench_mock_curl(b: &mut test::Bencher) {
let mut cwd = os::getcwd().unwrap();
cwd.push("README.md");
let s = format!("file://{}", cwd.container_as_str().unwrap());
let url = s.as_slice();
b.iter(|| {
curl::http::handle()
.get(url)
.header("X-Foo", "Bar")
.exec()
.unwrap()
});
}

#[derive(Clone)]
struct Foo;

Expand All @@ -90,7 +74,8 @@ impl net::NetworkStream for MockStream {

struct MockConnector;

impl net::NetworkConnector<MockStream> for MockConnector {
impl net::NetworkConnector for MockConnector {
type Stream = MockStream;
fn connect(&mut self, _: &str, _: u16, _: &str) -> IoResult<MockStream> {
Ok(MockStream::new())
}
Expand Down
1 change: 1 addition & 0 deletions benches/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unstable)]
extern crate hyper;
extern crate test;

Expand Down
1 change: 1 addition & 0 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unstable)]
extern crate hyper;

use std::os;
Expand Down
1 change: 1 addition & 0 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unstable)]
extern crate hyper;

use std::io::net::ip::Ipv4Addr;
Expand Down
3 changes: 2 additions & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unstable)]
extern crate hyper;
#[macro_use] extern crate log;

Expand All @@ -24,7 +25,7 @@ fn echo(mut req: Request, mut res: Response) {
(&Get, "/") | (&Get, "/echo") => {
let out = b"Try POST /echo";

res.headers_mut().set(ContentLength(out.len()));
res.headers_mut().set(ContentLength(out.len() as u64));
let mut res = try_return!(res.start());
try_return!(res.write(out));
try_return!(res.end());
Expand Down
34 changes: 17 additions & 17 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use openssl::ssl::VerifyCallback;
use header::{Headers, Header, HeaderFormat};
use header::common::{ContentLength, Location};
use method::Method;
use net::{NetworkConnector, NetworkStream, HttpConnector};
use net::{NetworkConnector, HttpConnector};
use status::StatusClass::Redirection;
use {Url, Port, HttpResult};
use HttpError::HttpUriError;
Expand Down Expand Up @@ -63,8 +63,7 @@ impl Client<HttpConnector> {

}

#[old_impl_check]
impl<C: NetworkConnector<S>, S: NetworkStream> Client<C> {
impl<C: NetworkConnector> Client<C> {

/// Create a new client with a specific connector.
pub fn with_connector(connector: C) -> Client<C> {
Expand All @@ -80,33 +79,33 @@ impl<C: NetworkConnector<S>, S: NetworkStream> Client<C> {
}

/// Execute a Get request.
pub fn get<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C, S> {
pub fn get<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C> {
self.request(Method::Get, url)
}

/// Execute a Head request.
pub fn head<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C, S> {
pub fn head<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C> {
self.request(Method::Head, url)
}

/// Execute a Post request.
pub fn post<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C, S> {
pub fn post<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C> {
self.request(Method::Post, url)
}

/// Execute a Put request.
pub fn put<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C, S> {
pub fn put<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C> {
self.request(Method::Put, url)
}

/// Execute a Delete request.
pub fn delete<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C, S> {
pub fn delete<U: IntoUrl>(&mut self, url: U) -> RequestBuilder<U, C> {
self.request(Method::Delete, url)
}


/// Build a new request using this Client.
pub fn request<U: IntoUrl>(&mut self, method: Method, url: U) -> RequestBuilder<U, C, S> {
pub fn request<U: IntoUrl>(&mut self, method: Method, url: U) -> RequestBuilder<U, C> {
RequestBuilder {
client: self,
method: method,
Expand All @@ -121,30 +120,30 @@ impl<C: NetworkConnector<S>, S: NetworkStream> Client<C> {
///
/// One of these will be built for you if you use one of the convenience
/// methods, such as `get()`, `post()`, etc.
pub struct RequestBuilder<'a, U: IntoUrl, C: NetworkConnector<S> + 'a, S: NetworkStream> {
pub struct RequestBuilder<'a, U: IntoUrl, C: NetworkConnector + 'a> {
client: &'a mut Client<C>,
url: U,
headers: Option<Headers>,
method: Method,
body: Option<Body<'a>>,
}

impl<'a, U: IntoUrl, C: NetworkConnector<S>, S: NetworkStream> RequestBuilder<'a, U, C, S> {
impl<'a, U: IntoUrl, C: NetworkConnector> RequestBuilder<'a, U, C> {

/// Set a request body to be sent.
pub fn body<B: IntoBody<'a>>(mut self, body: B) -> RequestBuilder<'a, U, C, S> {
pub fn body<B: IntoBody<'a>>(mut self, body: B) -> RequestBuilder<'a, U, C> {
self.body = Some(body.into_body());
self
}

/// Add additional headers to the request.
pub fn headers(mut self, headers: Headers) -> RequestBuilder<'a, U, C, S> {
pub fn headers(mut self, headers: Headers) -> RequestBuilder<'a, U, C> {
self.headers = Some(headers);
self
}

/// Add an individual new header to the request.
pub fn header<H: Header + HeaderFormat>(mut self, header: H) -> RequestBuilder<'a, U, C, S> {
pub fn header<H: Header + HeaderFormat>(mut self, header: H) -> RequestBuilder<'a, U, C> {
{
let mut headers = match self.headers {
Some(ref mut h) => h,
Expand Down Expand Up @@ -243,15 +242,16 @@ pub enum Body<'a> {
/// A Reader does not necessarily know it's size, so it is chunked.
ChunkedBody(&'a mut (Reader + 'a)),
/// For Readers that can know their size, like a `File`.
SizedBody(&'a mut (Reader + 'a), usize),
SizedBody(&'a mut (Reader + 'a), u64),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this really be u64 and not usize?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It could be from an fstat. usize on 32bit system will limit you to files less than 4gb.

/// A String has a size, and uses Content-Length.
BufBody(&'a [u8] , usize),
}

impl<'a> Body<'a> {
fn size(&self) -> Option<usize> {
fn size(&self) -> Option<u64> {
match *self {
Body::SizedBody(_, len) | Body::BufBody(_, len) => Some(len),
Body::SizedBody(_, len) => Some(len),
Body::BufBody(_, len) => Some(len as u64),
_ => None
}
}
Expand Down
49 changes: 8 additions & 41 deletions src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::io::{BufferedWriter, IoResult};

use url::Url;

use method;
use method::Method::{Get, Post, Delete, Put, Patch, Head, Options};
use method::{self, Method};
use header::Headers;
use header::common::{self, Host};
use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
Expand Down Expand Up @@ -46,11 +45,14 @@ impl Request<Fresh> {
}

/// Create a new client request with a specific underlying NetworkStream.
pub fn with_connector<C: NetworkConnector<S>, S: NetworkStream>(method: method::Method, url: Url, connector: &mut C) -> HttpResult<Request<Fresh>> {
debug!("{:?} {:?}", method, url);
pub fn with_connector<C, S>(method: method::Method, url: Url, connector: &mut C)
-> HttpResult<Request<Fresh>> where
C: NetworkConnector<Stream=S>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a where clause instead, it's much cleaner. (for all these params)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see how it much cleaner, it looks the same to me except one is before the parens, one is after. But I moved it cause people seem to like it.

S: NetworkStream + Send {
debug!("{} {}", method, url);
let (host, port) = try!(get_host_and_port(&url));

let stream: S = try!(connector.connect(&host[], port, &*url.scheme));
let stream = try!(connector.connect(&*host, port, &*url.scheme));
let stream = ThroughWriter(BufferedWriter::new(box stream as Box<NetworkStream + Send>));

let mut headers = Headers::new();
Expand All @@ -68,41 +70,6 @@ impl Request<Fresh> {
})
}

/// Create a new GET request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn get(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Get, url) }

/// Create a new POST request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn post(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Post, url) }

/// Create a new DELETE request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn delete(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Delete, url) }

/// Create a new PUT request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn put(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Put, url) }

/// Create a new PATCH request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn patch(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Patch, url) }

/// Create a new HEAD request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn head(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Head, url) }

/// Create a new OPTIONS request.
#[inline]
#[deprecated = "use hyper::Client"]
pub fn options(url: Url) -> HttpResult<Request<Fresh>> { Request::new(Options, url) }

/// Consume a Fresh Request, writing the headers and method,
/// returning a Streaming Request.
pub fn start(mut self) -> HttpResult<Request<Streaming>> {
Expand All @@ -119,7 +86,7 @@ impl Request<Fresh> {


let stream = match self.method {
Get | Head => {
Method::Get | Method::Head => {
debug!("headers [\n{:?}]", self.headers);
try!(write!(&mut self.body, "{}{}", self.headers, LINE_ENDING));
EmptyWriter(self.body.unwrap())
Expand Down
15 changes: 5 additions & 10 deletions src/header/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl HeaderFormat for CacheControl {
}

/// CacheControl contains a list of these directives.
#[derive(PartialEq, Clone)]
#[derive(PartialEq, Clone, Show)]
pub enum CacheDirective {
/// "no-cache"
NoCache,
Expand All @@ -47,11 +47,11 @@ pub enum CacheDirective {

// request directives
/// "max-age=delta"
MaxAge(usize),
MaxAge(u32),
/// "max-stale=delta"
MaxStale(usize),
MaxStale(u32),
/// "min-fresh=delta"
MinFresh(usize),
MinFresh(u32),

// response directives
/// "must-revalidate"
Expand All @@ -63,7 +63,7 @@ pub enum CacheDirective {
/// "proxy-revalidate"
ProxyRevalidate,
/// "s-maxage=delta"
SMaxAge(usize),
SMaxAge(u32),

/// Extension directives. Optionally include an argument.
Extension(String, Option<String>)
Expand Down Expand Up @@ -95,11 +95,6 @@ impl fmt::String for CacheDirective {
}
}

impl fmt::Show for CacheDirective {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.to_string().fmt(fmt)
}
}
impl FromStr for CacheDirective {
fn from_str(s: &str) -> Option<CacheDirective> {
use self::CacheDirective::*;
Expand Down
16 changes: 3 additions & 13 deletions src/header/common/content_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use header::shared::util::from_one_raw_str;
///
/// Simply a wrapper around a `usize`.
#[derive(Copy, Clone, PartialEq, Show)]
pub struct ContentLength(pub usize);
pub struct ContentLength(pub u64);

deref!(ContentLength => usize);
deref!(ContentLength => u64);

impl Header for ContentLength {
fn header_name(_: Option<ContentLength>) -> &'static str {
Expand All @@ -23,17 +23,7 @@ impl Header for ContentLength {

impl HeaderFormat for ContentLength {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let ContentLength(ref value) = *self;
write!(fmt, "{}", value)
}
}

impl ContentLength {
/// Returns the wrapped length.
#[deprecated = "use Deref instead"]
#[inline]
pub fn len(&self) -> usize {
**self
fmt::String::fmt(&self.0, fmt)
}
}

Expand Down
Loading