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

Switch to rust-url #23

Merged
merged 1 commit into from
Jul 21, 2014
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
42 changes: 22 additions & 20 deletions src/http/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ let response = match request.read_response() {

*/

use url;
use url::Url;
use method::Method;
use std::io::{IoError, IoResult};
Expand Down Expand Up @@ -106,23 +105,24 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
}

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

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

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

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

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

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

try!(self.headers.write_all(self.stream.get_mut_ref()));
self.headers_written = true;
Expand Down
2 changes: 1 addition & 1 deletion src/http/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ impl HeaderConvertible for uint {

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

fn http_value(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#![feature(phase)]

#[phase(plugin, link)] extern crate log;
extern crate url;
extern crate url = "url_";
extern crate time;
extern crate collections;
extern crate debug;
Expand Down
8 changes: 4 additions & 4 deletions src/http/server/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fn test_read_request_line() {
tt!("FOO /\n" => Ok((ExtensionMethod(String::from_str("FOO")), AbsolutePath(String::from_str("/")), (0, 9))));
tt!("get http://example.com/ HTTP/42.17\r\n"
=> Ok((ExtensionMethod(String::from_str("get")),
AbsoluteUri(from_str("http://example.com/").unwrap()),
AbsoluteUri(Url::parse("http://example.com/").unwrap()),
(42, 17))));

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