Skip to content

Instantly share code, notes, and snippets.

@pfalabella
Last active August 29, 2015 14:02
Show Gist options
  • Save pfalabella/862c80572409b25ea04d to your computer and use it in GitHub Desktop.
Save pfalabella/862c80572409b25ea04d to your computer and use it in GitHub Desktop.
// 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