Skip to content

Commit

Permalink
refactor(hyper): Update to rust-url 1.0
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The re-exported Url type has breaking changes.
  • Loading branch information
SimonSapin authored and seanmonstar committed Apr 21, 2016
1 parent 4bdf52a commit 8fa7a98
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ time = "0.1"
traitobject = "0.0.1"
typeable = "0.1"
unicase = "1.0"
url = "0.5"
url = "1.0"

[dependencies.cookie]
version = "0.2"
Expand Down
15 changes: 7 additions & 8 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ use std::fmt;

use std::time::Duration;

use url::UrlParser;
use url::Url;
use url::ParseError as UrlError;

use header::{Headers, Header, HeaderFormat};
use header::{ContentLength, Location};
use method::Method;
use net::{NetworkConnector, NetworkStream};
use {Url};
use Error;

pub use self::pool::Pool;
Expand Down Expand Up @@ -264,7 +263,7 @@ impl<'a> RequestBuilder<'a> {
loop {
let message = {
let (host, port) = try!(get_host_and_port(&url));
try!(client.protocol.new_message(&host, port, &*url.scheme))
try!(client.protocol.new_message(&host, port, url.scheme()))
};
let mut req = try!(Request::with_message(method.clone(), url.clone(), message));
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
Expand Down Expand Up @@ -292,7 +291,7 @@ impl<'a> RequestBuilder<'a> {
// punching borrowck here
let loc = match res.headers.get::<Location>() {
Some(&Location(ref loc)) => {
Some(UrlParser::new().base_url(&url).parse(&loc[..]))
Some(url.join(loc))
}
None => {
debug!("no Location header");
Expand Down Expand Up @@ -439,13 +438,13 @@ impl Default for RedirectPolicy {
}
}

fn get_host_and_port(url: &Url) -> ::Result<(String, u16)> {
let host = match url.serialize_host() {
fn get_host_and_port(url: &Url) -> ::Result<(&str, u16)> {
let host = match url.host_str() {
Some(host) => host,
None => return Err(Error::Uri(UrlError::EmptyHost))
};
trace!("host={:?}", host);
let port = match url.port_or_default() {
let port = match url.port_or_known_default() {
Some(port) => port,
None => return Err(Error::Uri(UrlError::InvalidPort))
};
Expand Down Expand Up @@ -498,7 +497,7 @@ mod tests {
#[test]
fn test_redirect_followif() {
fn follow_if(url: &Url) -> bool {
!url.serialize().contains("127.0.0.3")
!url.as_str().contains("127.0.0.3")
}
let mut client = Client::with_connector(MockRedirectPolicy);
client.set_redirect_policy(RedirectPolicy::FollowIf(follow_if));
Expand Down
21 changes: 13 additions & 8 deletions src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ impl Request<Fresh> {
/// properly initialized by the caller (e.g. a TCP connection's already established).
pub fn with_message(method: Method, url: Url, message: Box<HttpMessage>)
-> ::Result<Request<Fresh>> {
let (host, port) = try!(get_host_and_port(&url));
let mut headers = Headers::new();
headers.set(Host {
hostname: host,
port: Some(port),
});
{
let (host, port) = try!(get_host_and_port(&url));
headers.set(Host {
hostname: host.to_owned(),
port: Some(port),
});
}

Ok(Request {
method: method,
Expand All @@ -89,8 +91,10 @@ impl Request<Fresh> {
-> ::Result<Request<Fresh>> where
C: NetworkConnector<Stream=S>,
S: Into<Box<NetworkStream + Send>> {
let (host, port) = try!(get_host_and_port(&url));
let stream = try!(connector.connect(&*host, port, &*url.scheme)).into();
let stream = {
let (host, port) = try!(get_host_and_port(&url));
try!(connector.connect(host, port, url.scheme())).into()
};

Request::with_message(method, url, Box::new(Http11Message::with_stream(stream)))
}
Expand Down Expand Up @@ -223,7 +227,8 @@ mod tests {
let mut req = Request::with_connector(
Post, url, &mut MockConnector
).unwrap();
let body = form_urlencoded::serialize(vec!(("q","value")).into_iter());
let mut body = String::new();
form_urlencoded::Serializer::new(&mut body).append_pair("q", "value");
req.headers_mut().set(ContentLength(body.len() as u64));
let bytes = run_request(req);
let s = from_utf8(&bytes[..]).unwrap();
Expand Down
5 changes: 2 additions & 3 deletions src/header/common/content_disposition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use unicase::UniCase;
use url::percent_encoding;

use header::{Header, HeaderFormat, parsing};
use header::parsing::parse_extended_value;
use header::parsing::{parse_extended_value, HTTP_VALUE};
use header::shared::Charset;

/// The implied disposition of the content of the HTTP body
Expand Down Expand Up @@ -184,8 +184,7 @@ impl fmt::Display for ContentDisposition {
};
try!(write!(f, "'"));
try!(f.write_str(
&*percent_encoding::percent_encode(
bytes, percent_encoding::HTTP_VALUE_ENCODE_SET)))
&percent_encoding::percent_encode(bytes, HTTP_VALUE).to_string()))
}
},
DispositionParam::Ext(ref k, ref v) => try!(write!(f, "; {}=\"{}\"", k, v)),
Expand Down
14 changes: 11 additions & 3 deletions src/header/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn parse_extended_value(val: &str) -> ::Result<ExtendedValue> {
// Interpret the third piece as a sequence of value characters
let value: Vec<u8> = match parts.next() {
None => return Err(::Error::Header),
Some(v) => percent_encoding::percent_decode(v.as_bytes()),
Some(v) => percent_encoding::percent_decode(v.as_bytes()).collect(),
};

Ok(ExtendedValue {
Expand All @@ -128,11 +128,19 @@ pub fn parse_extended_value(val: &str) -> ::Result<ExtendedValue> {
})
}

define_encode_set! {
/// This encode set is used for HTTP header values and is defined at
/// https://tools.ietf.org/html/rfc5987#section-3.2
pub HTTP_VALUE = [percent_encoding::SIMPLE_ENCODE_SET] | {
' ', '"', '%', '\'', '(', ')', '*', ',', '/', ':', ';', '<', '-', '>', '?',
'[', '\\', ']', '{', '}'
}
}

impl Display for ExtendedValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let encoded_value =
percent_encoding::percent_encode(&self.value[..],
percent_encoding::HTTP_VALUE_ENCODE_SET);
percent_encoding::percent_encode(&self.value[..], HTTP_VALUE);
if let Some(ref lang) = self.language_tag {
write!(f, "{}'{}'{}", self.charset, lang, encoded_value)
} else {
Expand Down
33 changes: 16 additions & 17 deletions src/http/h1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::net::Shutdown;
use std::time::Duration;

use httparse;
use url::Position as UrlPosition;

use buffer::BufReader;
use Error;
Expand Down Expand Up @@ -142,25 +143,23 @@ impl HttpMessage for Http11Message {
};
let mut stream = BufWriter::new(stream);

let mut uri = head.url.serialize_path().unwrap();
if let Some(ref q) = head.url.query {
uri.push('?');
uri.push_str(&q[..]);
{
let uri = &head.url[UrlPosition::BeforePath..UrlPosition::AfterQuery];

let version = version::HttpVersion::Http11;
debug!("request line: {:?} {:?} {:?}", head.method, uri, version);
match write!(&mut stream, "{} {} {}{}",
head.method, uri, version, LINE_ENDING) {
Err(e) => {
res = Err(From::from(e));
// TODO What should we do if the BufWriter doesn't wanna
// relinquish the stream?
return Stream::Idle(stream.into_inner().ok().unwrap());
},
Ok(_) => {},
};
}

let version = version::HttpVersion::Http11;
debug!("request line: {:?} {:?} {:?}", head.method, uri, version);
match write!(&mut stream, "{} {} {}{}",
head.method, uri, version, LINE_ENDING) {
Err(e) => {
res = Err(From::from(e));
// TODO What should we do if the BufWriter doesn't wanna
// relinquish the stream?
return Stream::Idle(stream.into_inner().ok().unwrap());
},
Ok(_) => {},
};

let stream = {
let write_headers = |mut stream: BufWriter<Box<NetworkStream + Send>>, head: &RequestHead| {
debug!("headers={:?}", head.headers);
Expand Down
14 changes: 2 additions & 12 deletions src/http/h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use http::{
};
use net::{NetworkStream, NetworkConnector};
use net::{HttpConnector, HttpStream};
use url::Url;
use url::Position as UrlPosition;
use header::Headers;

use header;
Expand Down Expand Up @@ -262,16 +262,6 @@ impl<S> Read for Http2Message<S> where S: CloneableStream {
}
}

/// A helper function that prepares the path of a request by extracting it from the given `Url`.
fn prepare_path(url: Url) -> Vec<u8> {
let mut uri = url.serialize_path().unwrap();
if let Some(ref q) = url.query {
uri.push('?');
uri.push_str(&q[..]);
}
uri.into_bytes()
}

/// A helper function that prepares the headers that should be sent in an HTTP/2 message.
///
/// Adapts the `Headers` into a list of octet string pairs.
Expand Down Expand Up @@ -374,7 +364,7 @@ impl<S> HttpMessage for Http2Message<S> where S: CloneableStream {
let (RequestHead { headers, method, url }, body) = (request.head, request.body);

let method = method.as_ref().as_bytes();
let path = prepare_path(url);
let path = url[UrlPosition::BeforePath..UrlPosition::AfterQuery].as_bytes();
let extra_headers = prepare_headers(headers);
let body = prepare_body(body);

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
extern crate rustc_serialize as serialize;
extern crate time;
extern crate url;
#[macro_use] extern crate url;
#[cfg(feature = "openssl")]
extern crate openssl;
#[cfg(feature = "security-framework")]
Expand Down
2 changes: 1 addition & 1 deletion src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl FromStr for RequestUri {
fn from_str(s: &str) -> Result<RequestUri, Error> {
let bytes = s.as_bytes();
if bytes == [] {
Err(Error::Uri(UrlError::InvalidCharacter))
Err(Error::Uri(UrlError::RelativeUrlWithoutBase))
} else if bytes == b"*" {
Ok(RequestUri::Star)
} else if bytes.starts_with(b"/") {
Expand Down

0 comments on commit 8fa7a98

Please sign in to comment.