Skip to content

Commit a72a8e4

Browse files
committed
Merge pull request chris-morgan#12 from brson/master
Basic DNS and a parser workaround
2 parents 48bc01b + a195a9e commit a72a8e4

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/libhttp/client/request.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use extra::url::Url;
22
use method::Method;
33
use std::rt::io::{Reader, Writer};
4-
use std::rt::io::net::ip::SocketAddr;
4+
use std::rt::io::net::get_host_addresses;
5+
use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
56
use std::rt::io::net::tcp::TcpStream;
67
use buffer::BufferedStream;
78
use headers::request::HeaderCollection;
@@ -70,10 +71,39 @@ impl<S: Reader + Writer> RequestWriter<S> {
7071
},
7172
};
7273

74+
let remote_addr = url_to_socket_addr(&url);
75+
info!("using ip address %s for %s", remote_addr.to_str(), url.host);
76+
77+
fn url_to_socket_addr(url: &Url) -> SocketAddr {
78+
// Just grab the first IPv4 address
79+
let addrs = get_host_addresses(url.host);
80+
// TODO: Error handling
81+
let addrs = addrs.unwrap();
82+
let addr = do addrs.move_iter().find |&a| {
83+
match a {
84+
Ipv4Addr(*) => true,
85+
_ => false
86+
}
87+
};
88+
89+
// TODO: Error handling
90+
let addr = addr.unwrap();
91+
92+
let port = url.port.clone().unwrap_or_default(~"80");
93+
let port = FromStr::from_str(port);
94+
// TODO: Error handling
95+
let port = port.unwrap();
96+
97+
SocketAddr {
98+
ip: addr,
99+
port: port
100+
}
101+
}
102+
73103
let mut request = RequestWriter {
74104
stream: None,
75105
headers_written: false,
76-
remote_addr: None,
106+
remote_addr: Some(remote_addr),
77107
headers: ~HeaderCollection::new(),
78108
method: method,
79109
url: url,

src/libhttp/headers/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,6 @@ headers_mod! {
10891089
24, "Content-MD5", "Content-Md5", ContentMd5, content_md5, ~str;
10901090
25, "Content-Range", "Content-Range", ContentRange, content_range, ~str;
10911091
26, "Content-Type", "Content-Type", ContentType, content_type, headers::content_type::MediaType;
1092-
27, "Expires", "Expires", Expires, expires, extra::time::Tm;
1092+
27, "Expires", "Expires", Expires, expires, ~str; // TODO: Should be Tm
10931093
28, "Last-Modified", "Last-Modified", LastModified, last_modified, extra::time::Tm;
10941094
}

0 commit comments

Comments
 (0)