Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit e95cdae

Browse files
committed
Merge pull request #23 from servo/servo-rust-url
Switch to rust-url
2 parents f81e3f2 + fed75f3 commit e95cdae

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

Diff for: src/http/client/request.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ let response = match request.read_response() {
3636
3737
*/
3838

39-
use url;
4039
use url::Url;
4140
use method::Method;
4241
use std::io::{IoError, IoResult};
@@ -106,23 +105,24 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
106105
}
107106

108107
pub fn new_request(method: Method, url: Url, use_ssl: bool, auto_detect_ssl: bool) -> IoResult<RequestWriter<S>> {
109-
let host = match url.port {
110-
None => Host {
111-
name: url.host.clone(),
112-
port: None,
113-
},
114-
Some(ref p) => Host {
115-
name: url.host.clone(),
116-
port: Some(from_str(p.as_slice()).expect("You didn’t aught to give a bad port!")),
108+
let host = Host {
109+
name: url.domain().unwrap().to_string(),
110+
port: {
111+
let port = url.port().unwrap();
112+
if port.is_empty() {
113+
None
114+
} else {
115+
Some(from_str(port).expect("You didn’t aught to give a bad port!"))
116+
}
117117
},
118118
};
119119

120-
let remote_addr = try!(url_to_socket_addr(&url));
121-
info!("using ip address {} for {}", remote_addr.to_str(), url.host.as_slice());
120+
let remote_addr = try!(url_to_socket_addr(&url, &host));
121+
info!("using ip address {} for {}", remote_addr.to_str(), host.name.as_slice());
122122

123-
fn url_to_socket_addr(url: &Url) -> IoResult<SocketAddr> {
123+
fn url_to_socket_addr(url: &Url, host: &Host) -> IoResult<SocketAddr> {
124124
// Just grab the first IPv4 address
125-
let addrs = try!(get_host_addresses(url.host.as_slice()));
125+
let addrs = try!(get_host_addresses(host.name.as_slice()));
126126
let addr = addrs.move_iter().find(|&a| {
127127
match a {
128128
Ipv4Addr(..) => true,
@@ -134,8 +134,8 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
134134
let addr = addr.unwrap();
135135

136136
// Default to 80, using the port specified or 443 if the protocol is HTTPS.
137-
let port = match url.port {
138-
Some(ref p) => from_str(p.as_slice()).expect("You didn’t aught to give a bad port!"),
137+
let port = match host.port {
138+
Some(p) => p,
139139
// FIXME: case insensitivity?
140140
None => if url.scheme.as_slice() == "https" { 443 } else { 80 },
141141
};
@@ -186,7 +186,8 @@ impl<S: Connecter + Reader + Writer = super::NetworkStream> RequestWriter<S> {
186186

187187
self.stream = match self.remote_addr {
188188
Some(addr) => {
189-
let stream = try!(Connecter::connect(addr, self.url.host.as_slice(), self.use_ssl));
189+
let stream = try!(Connecter::connect(
190+
addr, self.headers.host.as_ref().unwrap().name.as_slice(), self.use_ssl));
190191
Some(BufferedStream::new(stream))
191192
},
192193
None => fail!("connect() called before remote_addr was set"),
@@ -217,12 +218,13 @@ impl<S: Connecter + Reader + Writer = super::NetworkStream> RequestWriter<S> {
217218

218219
// Write the Request-Line (RFC2616 §5.1)
219220
// TODO: get to the point where we can say HTTP/1.1 with good conscience
221+
let (question_mark, query) = match self.url.query {
222+
Some(ref query) => ("?", query.as_slice()),
223+
None => ("", "")
224+
};
220225
try!(write!(self.stream.get_mut_ref() as &mut Writer,
221226
"{} {}{}{} HTTP/1.0\r\n",
222-
self.method.to_str(),
223-
if self.url.path.len() > 0 { self.url.path.as_slice() } else { "/" },
224-
if self.url.query.len() > 0 { "?" } else { "" },
225-
url::query_to_str(&self.url.query)));
227+
self.method.to_str(), self.url.serialize_path().unwrap(), question_mark, query));
226228

227229
try!(self.headers.write_all(self.stream.get_mut_ref()));
228230
self.headers_written = true;

Diff for: src/http/headers/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl HeaderConvertible for uint {
623623

624624
impl HeaderConvertible for Url {
625625
fn from_stream<R: Reader>(reader: &mut HeaderValueByteIterator<R>) -> Option<Url> {
626-
from_str(reader.collect_to_string().as_slice())
626+
Url::parse(reader.collect_to_string().as_slice()).ok()
627627
}
628628

629629
fn http_value(&self) -> String {

Diff for: src/http/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#![feature(phase)]
1616

1717
#[phase(plugin, link)] extern crate log;
18-
extern crate url;
18+
extern crate url = "url_";
1919
extern crate time;
2020
extern crate collections;
2121
extern crate debug;

Diff for: src/http/server/request.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn test_read_request_line() {
181181
tt!("FOO /\n" => Ok((ExtensionMethod(String::from_str("FOO")), AbsolutePath(String::from_str("/")), (0, 9))));
182182
tt!("get http://example.com/ HTTP/42.17\r\n"
183183
=> Ok((ExtensionMethod(String::from_str("get")),
184-
AbsoluteUri(from_str("http://example.com/").unwrap()),
184+
AbsoluteUri(Url::parse("http://example.com/").unwrap()),
185185
(42, 17))));
186186

187187
// Now for some failing cases.
@@ -270,9 +270,9 @@ impl RequestUri {
270270
Some(AbsolutePath(request_uri))
271271
} else if request_uri.as_slice().contains("/") {
272272
// An authority can't have a slash in it
273-
match from_str(request_uri.as_slice()) {
274-
Some(url) => Some(AbsoluteUri(url)),
275-
None => None,
273+
match Url::parse(request_uri.as_slice()) {
274+
Ok(url) => Some(AbsoluteUri(url)),
275+
Err(_) => None,
276276
}
277277
} else {
278278
// TODO: parse authority with extra::net::url

0 commit comments

Comments
 (0)