Last active
August 29, 2015 14:02
-
-
Save pfalabella/862c80572409b25ea04d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://rosettacode.org/wiki/HTTP | |
use std::io::net::tcp::TcpStream; | |
use std::io; | |
#[cfg(not(test))] | |
fn main() { | |
let target = "www.rust-lang.org"; | |
// Create a socket. Mutable so we can write to it. | |
let mut socket = TcpStream::connect(target, 80); | |
// Write to the socket as bytes. | |
let _ = write!(socket, "GET / HTTP/1.1\nHost: {}\n\n", target); | |
static BUF_SIZE:uint=1024*8; | |
let mut buf=[0u8,..BUF_SIZE]; | |
// Read any response and write it to stodout(). | |
let mut out = io::stdout(); | |
loop { | |
match socket.read(buf) { | |
Ok(num_bytes) => { | |
let r=out.write(buf.slice_to(num_bytes)); | |
if r.is_err() || num_bytes < BUF_SIZE { break; } | |
}, | |
Err(e) => { | |
println!("{}", e); | |
break; | |
} | |
}; | |
}; | |
drop(socket); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment