Skip to content

Instantly share code, notes, and snippets.

@campaul
Created December 11, 2015 01:52
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save campaul/feeed29ee90e5e725630 to your computer and use it in GitHub Desktop.
Save campaul/feeed29ee90e5e725630 to your computer and use it in GitHub Desktop.
Simple HTTP proxy in Rust.
/*
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);
}
@stuartweir
Copy link

Cool, this is helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment