-
Notifications
You must be signed in to change notification settings - Fork 126
/
ssl.rs
50 lines (43 loc) · 1.37 KB
/
ssl.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
extern crate tiny_http;
#[cfg(not(any(
feature = "ssl-openssl",
feature = "ssl-rustls",
feature = "ssl-native-tls"
)))]
fn main() {
println!("This example requires one of the supported `ssl-*` features to be enabled");
}
#[cfg(any(
feature = "ssl-openssl",
feature = "ssl-rustls",
feature = "ssl-native-tls"
))]
fn main() {
use tiny_http::{Response, Server};
let server = Server::https(
"0.0.0.0:8000",
tiny_http::SslConfig {
certificate: include_bytes!("ssl-cert.pem").to_vec(),
private_key: include_bytes!("ssl-key.pem").to_vec(),
},
)
.unwrap();
println!(
"Note: connecting to this server will likely give you a warning from your browser \
because the connection is unsecure. This is because the certificate used by this \
example is self-signed. With a real certificate, you wouldn't get this warning."
);
for request in server.incoming_requests() {
assert!(request.secure());
println!(
"received request! method: {:?}, url: {:?}, headers: {:?}",
request.method(),
request.url(),
request.headers()
);
let response = Response::from_string("hello world");
request
.respond(response)
.unwrap_or(println!("Failed to respond to request"));
}
}