Created
December 11, 2015 01:52
-
-
Save campaul/feeed29ee90e5e725630 to your computer and use it in GitHub Desktop.
Simple HTTP proxy in Rust.
This file contains hidden or 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
/* | |
Simple HTTP proxy in Rust. Hard coded to proxy rust-lang.org. | |
*/ | |
extern crate hyper; | |
use std::io::Read; | |
use hyper::Client; | |
use hyper::header::Connection; | |
use hyper::Server; | |
use hyper::server::Request; | |
use hyper::server::Response; | |
use hyper::uri::RequestUri; | |
fn proxy(proxy_request: Request, proxy_response: Response) { | |
let client = Client::new(); | |
match (proxy_request.method, proxy_request.uri) { | |
(hyper::Get, RequestUri::AbsolutePath(ref path)) => { | |
let uri = "http://rust-lang.org".to_string() + &path; | |
let mut response = client.get(&uri) | |
.header(Connection::close()) | |
.send().unwrap(); | |
let mut body = String::new(); | |
response.read_to_string(&mut body).unwrap(); | |
proxy_response.send(&body.into_bytes()).unwrap(); | |
}, | |
_ => {} | |
} | |
} | |
fn main() { | |
Server::http("127.0.0.1:3000").unwrap().handle(proxy); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, this is helpful!