From f214af6b6bf66da5913278bdb26670a9c4c1a171 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 6 Sep 2013 15:30:43 -0700 Subject: [PATCH 1/2] Add preliminary DNS resolution Surely this could be a lot smarter. In particular it just fails on several types of errors. --- src/libhttp/client/request.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/libhttp/client/request.rs b/src/libhttp/client/request.rs index 8498114..41bbe9b 100644 --- a/src/libhttp/client/request.rs +++ b/src/libhttp/client/request.rs @@ -1,7 +1,8 @@ use extra::url::Url; use method::Method; use std::rt::io::{Reader, Writer}; -use std::rt::io::net::ip::SocketAddr; +use std::rt::io::net::get_host_addresses; +use std::rt::io::net::ip::{SocketAddr, Ipv4Addr}; use std::rt::io::net::tcp::TcpStream; use buffer::BufferedStream; use headers::request::HeaderCollection; @@ -70,10 +71,39 @@ impl RequestWriter { }, }; + let remote_addr = url_to_socket_addr(&url); + info!("using ip address %s for %s", remote_addr.to_str(), url.host); + + fn url_to_socket_addr(url: &Url) -> SocketAddr { + // Just grab the first IPv4 address + let addrs = get_host_addresses(url.host); + // TODO: Error handling + let addrs = addrs.unwrap(); + let addr = do addrs.move_iter().find |&a| { + match a { + Ipv4Addr(*) => true, + _ => false + } + }; + + // TODO: Error handling + let addr = addr.unwrap(); + + let port = url.port.clone().unwrap_or_default(~"80"); + let port = FromStr::from_str(port); + // TODO: Error handling + let port = port.unwrap(); + + SocketAddr { + ip: addr, + port: port + } + } + let mut request = RequestWriter { stream: None, headers_written: false, - remote_addr: None, + remote_addr: Some(remote_addr), headers: ~HeaderCollection::new(), method: method, url: url, From a195a9ee8c06b8bc4373a38ab1fa34d6abd50be8 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 6 Sep 2013 15:32:27 -0700 Subject: [PATCH 2/2] Make the Expires header parse as a plain string Google is sending these headers with value '-1', which doesn't parse. --- src/libhttp/headers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libhttp/headers/mod.rs b/src/libhttp/headers/mod.rs index e445a74..0d5e6b5 100644 --- a/src/libhttp/headers/mod.rs +++ b/src/libhttp/headers/mod.rs @@ -1089,6 +1089,6 @@ headers_mod! { 24, "Content-MD5", "Content-Md5", ContentMd5, content_md5, ~str; 25, "Content-Range", "Content-Range", ContentRange, content_range, ~str; 26, "Content-Type", "Content-Type", ContentType, content_type, headers::content_type::MediaType; - 27, "Expires", "Expires", Expires, expires, extra::time::Tm; + 27, "Expires", "Expires", Expires, expires, ~str; // TODO: Should be Tm 28, "Last-Modified", "Last-Modified", LastModified, last_modified, extra::time::Tm; }