refactor(hyper): Update to rust-url 1.0

BREAKING CHANGE: The re-exported Url type has breaking changes.
This commit is contained in:
Simon Sapin
2016-04-22 01:14:08 +02:00
committed by Sean McArthur
parent 4bdf52a482
commit 8fa7a98968
9 changed files with 54 additions and 54 deletions

View File

@@ -22,7 +22,7 @@ time = "0.1"
traitobject = "0.0.1" traitobject = "0.0.1"
typeable = "0.1" typeable = "0.1"
unicase = "1.0" unicase = "1.0"
url = "0.5" url = "1.0"
[dependencies.cookie] [dependencies.cookie]
version = "0.2" version = "0.2"

View File

@@ -62,14 +62,13 @@ use std::fmt;
use std::time::Duration; use std::time::Duration;
use url::UrlParser; use url::Url;
use url::ParseError as UrlError; use url::ParseError as UrlError;
use header::{Headers, Header, HeaderFormat}; use header::{Headers, Header, HeaderFormat};
use header::{ContentLength, Location}; use header::{ContentLength, Location};
use method::Method; use method::Method;
use net::{NetworkConnector, NetworkStream}; use net::{NetworkConnector, NetworkStream};
use {Url};
use Error; use Error;
pub use self::pool::Pool; pub use self::pool::Pool;
@@ -264,7 +263,7 @@ impl<'a> RequestBuilder<'a> {
loop { loop {
let message = { let message = {
let (host, port) = try!(get_host_and_port(&url)); 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)); let mut req = try!(Request::with_message(method.clone(), url.clone(), message));
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter())); headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
@@ -292,7 +291,7 @@ impl<'a> RequestBuilder<'a> {
// punching borrowck here // punching borrowck here
let loc = match res.headers.get::<Location>() { let loc = match res.headers.get::<Location>() {
Some(&Location(ref loc)) => { Some(&Location(ref loc)) => {
Some(UrlParser::new().base_url(&url).parse(&loc[..])) Some(url.join(loc))
} }
None => { None => {
debug!("no Location header"); debug!("no Location header");
@@ -439,13 +438,13 @@ impl Default for RedirectPolicy {
} }
} }
fn get_host_and_port(url: &Url) -> ::Result<(String, u16)> { fn get_host_and_port(url: &Url) -> ::Result<(&str, u16)> {
let host = match url.serialize_host() { let host = match url.host_str() {
Some(host) => host, Some(host) => host,
None => return Err(Error::Uri(UrlError::EmptyHost)) None => return Err(Error::Uri(UrlError::EmptyHost))
}; };
trace!("host={:?}", host); trace!("host={:?}", host);
let port = match url.port_or_default() { let port = match url.port_or_known_default() {
Some(port) => port, Some(port) => port,
None => return Err(Error::Uri(UrlError::InvalidPort)) None => return Err(Error::Uri(UrlError::InvalidPort))
}; };
@@ -498,7 +497,7 @@ mod tests {
#[test] #[test]
fn test_redirect_followif() { fn test_redirect_followif() {
fn follow_if(url: &Url) -> bool { 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); let mut client = Client::with_connector(MockRedirectPolicy);
client.set_redirect_policy(RedirectPolicy::FollowIf(follow_if)); client.set_redirect_policy(RedirectPolicy::FollowIf(follow_if));

View File

@@ -61,12 +61,14 @@ impl Request<Fresh> {
/// properly initialized by the caller (e.g. a TCP connection's already established). /// properly initialized by the caller (e.g. a TCP connection's already established).
pub fn with_message(method: Method, url: Url, message: Box<HttpMessage>) pub fn with_message(method: Method, url: Url, message: Box<HttpMessage>)
-> ::Result<Request<Fresh>> { -> ::Result<Request<Fresh>> {
let (host, port) = try!(get_host_and_port(&url));
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.set(Host { {
hostname: host, let (host, port) = try!(get_host_and_port(&url));
port: Some(port), headers.set(Host {
}); hostname: host.to_owned(),
port: Some(port),
});
}
Ok(Request { Ok(Request {
method: method, method: method,
@@ -89,8 +91,10 @@ impl Request<Fresh> {
-> ::Result<Request<Fresh>> where -> ::Result<Request<Fresh>> where
C: NetworkConnector<Stream=S>, C: NetworkConnector<Stream=S>,
S: Into<Box<NetworkStream + Send>> { S: Into<Box<NetworkStream + Send>> {
let (host, port) = try!(get_host_and_port(&url)); let stream = {
let stream = try!(connector.connect(&*host, port, &*url.scheme)).into(); 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))) Request::with_message(method, url, Box::new(Http11Message::with_stream(stream)))
} }
@@ -223,7 +227,8 @@ mod tests {
let mut req = Request::with_connector( let mut req = Request::with_connector(
Post, url, &mut MockConnector Post, url, &mut MockConnector
).unwrap(); ).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)); req.headers_mut().set(ContentLength(body.len() as u64));
let bytes = run_request(req); let bytes = run_request(req);
let s = from_utf8(&bytes[..]).unwrap(); let s = from_utf8(&bytes[..]).unwrap();

View File

@@ -12,7 +12,7 @@ use unicase::UniCase;
use url::percent_encoding; use url::percent_encoding;
use header::{Header, HeaderFormat, parsing}; use header::{Header, HeaderFormat, parsing};
use header::parsing::parse_extended_value; use header::parsing::{parse_extended_value, HTTP_VALUE};
use header::shared::Charset; use header::shared::Charset;
/// The implied disposition of the content of the HTTP body /// The implied disposition of the content of the HTTP body
@@ -184,8 +184,7 @@ impl fmt::Display for ContentDisposition {
}; };
try!(write!(f, "'")); try!(write!(f, "'"));
try!(f.write_str( try!(f.write_str(
&*percent_encoding::percent_encode( &percent_encoding::percent_encode(bytes, HTTP_VALUE).to_string()))
bytes, percent_encoding::HTTP_VALUE_ENCODE_SET)))
} }
}, },
DispositionParam::Ext(ref k, ref v) => try!(write!(f, "; {}=\"{}\"", k, v)), DispositionParam::Ext(ref k, ref v) => try!(write!(f, "; {}=\"{}\"", k, v)),

View File

@@ -118,7 +118,7 @@ pub fn parse_extended_value(val: &str) -> ::Result<ExtendedValue> {
// Interpret the third piece as a sequence of value characters // Interpret the third piece as a sequence of value characters
let value: Vec<u8> = match parts.next() { let value: Vec<u8> = match parts.next() {
None => return Err(::Error::Header), 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 { Ok(ExtendedValue {
@@ -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 { impl Display for ExtendedValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let encoded_value = let encoded_value =
percent_encoding::percent_encode(&self.value[..], percent_encoding::percent_encode(&self.value[..], HTTP_VALUE);
percent_encoding::HTTP_VALUE_ENCODE_SET);
if let Some(ref lang) = self.language_tag { if let Some(ref lang) = self.language_tag {
write!(f, "{}'{}'{}", self.charset, lang, encoded_value) write!(f, "{}'{}'{}", self.charset, lang, encoded_value)
} else { } else {

View File

@@ -7,6 +7,7 @@ use std::net::Shutdown;
use std::time::Duration; use std::time::Duration;
use httparse; use httparse;
use url::Position as UrlPosition;
use buffer::BufReader; use buffer::BufReader;
use Error; use Error;
@@ -142,24 +143,22 @@ impl HttpMessage for Http11Message {
}; };
let mut stream = BufWriter::new(stream); let mut stream = BufWriter::new(stream);
let mut uri = head.url.serialize_path().unwrap(); {
if let Some(ref q) = head.url.query { let uri = &head.url[UrlPosition::BeforePath..UrlPosition::AfterQuery];
uri.push('?');
uri.push_str(&q[..]);
}
let version = version::HttpVersion::Http11; let version = version::HttpVersion::Http11;
debug!("request line: {:?} {:?} {:?}", head.method, uri, version); debug!("request line: {:?} {:?} {:?}", head.method, uri, version);
match write!(&mut stream, "{} {} {}{}", match write!(&mut stream, "{} {} {}{}",
head.method, uri, version, LINE_ENDING) { head.method, uri, version, LINE_ENDING) {
Err(e) => { Err(e) => {
res = Err(From::from(e)); res = Err(From::from(e));
// TODO What should we do if the BufWriter doesn't wanna // TODO What should we do if the BufWriter doesn't wanna
// relinquish the stream? // relinquish the stream?
return Stream::Idle(stream.into_inner().ok().unwrap()); return Stream::Idle(stream.into_inner().ok().unwrap());
}, },
Ok(_) => {}, Ok(_) => {},
}; };
}
let stream = { let stream = {
let write_headers = |mut stream: BufWriter<Box<NetworkStream + Send>>, head: &RequestHead| { let write_headers = |mut stream: BufWriter<Box<NetworkStream + Send>>, head: &RequestHead| {

View File

@@ -15,7 +15,7 @@ use http::{
}; };
use net::{NetworkStream, NetworkConnector}; use net::{NetworkStream, NetworkConnector};
use net::{HttpConnector, HttpStream}; use net::{HttpConnector, HttpStream};
use url::Url; use url::Position as UrlPosition;
use header::Headers; use header::Headers;
use header; use header;
@@ -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. /// 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. /// Adapts the `Headers` into a list of octet string pairs.
@@ -374,7 +364,7 @@ impl<S> HttpMessage for Http2Message<S> where S: CloneableStream {
let (RequestHead { headers, method, url }, body) = (request.head, request.body); let (RequestHead { headers, method, url }, body) = (request.head, request.body);
let method = method.as_ref().as_bytes(); 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 extra_headers = prepare_headers(headers);
let body = prepare_body(body); let body = prepare_body(body);

View File

@@ -130,7 +130,7 @@
extern crate rustc_serialize as serialize; extern crate rustc_serialize as serialize;
extern crate time; extern crate time;
extern crate url; #[macro_use] extern crate url;
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
extern crate openssl; extern crate openssl;
#[cfg(feature = "security-framework")] #[cfg(feature = "security-framework")]

View File

@@ -56,7 +56,7 @@ impl FromStr for RequestUri {
fn from_str(s: &str) -> Result<RequestUri, Error> { fn from_str(s: &str) -> Result<RequestUri, Error> {
let bytes = s.as_bytes(); let bytes = s.as_bytes();
if bytes == [] { if bytes == [] {
Err(Error::Uri(UrlError::InvalidCharacter)) Err(Error::Uri(UrlError::RelativeUrlWithoutBase))
} else if bytes == b"*" { } else if bytes == b"*" {
Ok(RequestUri::Star) Ok(RequestUri::Star)
} else if bytes.starts_with(b"/") { } else if bytes.starts_with(b"/") {