-
-
Save divarvel/4c59b15d6921f79884b7638905faa4e9 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
¬ clementd_win git:(master) x cargo run | |
Compiling clementd_win v0.1.0 (file:///Users/clementd/Projects/perso/clementd_win) | |
Finished dev [unoptimized + debuginfo] target(s) in 1.72 secs | |
Running `target/debug/clementd_win` | |
ERROR:clementd_win: Redirecting Builder { head: Some(Parts { status: 301, version: HTTP/1.1, headers: {"location": "https://clementd.win"} }), err: None } |
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
curl http://localhost:8080 -v | |
* Rebuilt URL to: http://localhost:8080/ | |
* Trying ::1... | |
* TCP_NODELAY set | |
* Connection failed | |
* connect to ::1 port 8080 failed: Connection refused | |
* Trying 127.0.0.1... | |
* TCP_NODELAY set | |
* Connected to localhost (127.0.0.1) port 8080 (#0) | |
> GET / HTTP/1.1 | |
> Host: localhost:8080 | |
> User-Agent: curl/7.54.0 | |
> Accept: */* | |
> | |
< HTTP/1.1 301 Moved Permanently | |
* no chunk, no close, no size. Assume close to signal end | |
< | |
* Closing connection 0 |
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
#[macro_use] | |
extern crate log; | |
extern crate env_logger; | |
extern crate http; | |
extern crate simple_server; | |
use simple_server::Server; | |
use http::header; | |
use http::header::HeaderValue; | |
use http::StatusCode; | |
fn main() { | |
env_logger::init().unwrap(); | |
let host = "0.0.0.0"; | |
let port = "8080"; | |
let server = Server::new(|request, mut response| { | |
info!("Request received. {} {}", request.method(), request.uri()); | |
let http_value = &HeaderValue::from_static("http"); | |
let x = request.headers().get("X-Forwarded-Proto").unwrap_or(http_value); | |
let expected = HeaderValue::from_static("https"); | |
if x == expected { | |
Ok(response.body("Avoid air travel this week".as_bytes())?) | |
} else { | |
response.status(StatusCode::MOVED_PERMANENTLY); | |
response.header(header::LOCATION, "https://clementd.win".as_bytes()); | |
error!("Redirecting {:?}", response); | |
Ok(response.body("".as_bytes())?) | |
} | |
}); | |
server.listen(host, port); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment