You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
use std::io::prelude::*;
|
|
use std::net::{TcpListener, TcpStream};
|
|
|
|
fn main() {
|
|
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
|
|
|
for stream in listener.incoming() {
|
|
let stream = stream.unwrap();
|
|
|
|
handle_connection(stream);
|
|
}
|
|
}
|
|
|
|
fn handle_connection(mut stream: TcpStream) {
|
|
// make it 8k?
|
|
// https://stackoverflow.com/questions/686217/maximum-on-http-header-values
|
|
let mut buffer = [0; 1024];
|
|
stream.read(&mut buffer).unwrap();
|
|
|
|
let get = b"GET / HTTP/1.1\r\n";
|
|
|
|
let (status_line, contents) = if buffer.starts_with(get) {
|
|
(
|
|
"HTTP/1.1 200 OK",
|
|
stream.peer_addr().unwrap().ip().to_string(),
|
|
)
|
|
} else {
|
|
(
|
|
"HTTP/1.1 404 NOT FOUND",
|
|
"<html>\n<head><title>404 Not Found</title></head>\n\
|
|
<body bgcolor=\"white\">\n<center><h1>404 Not Found</h1></center>\n\
|
|
<hr>\n</body>\n</html>"
|
|
.to_string(),
|
|
)
|
|
};
|
|
|
|
// add date header formatted to rfc2822?
|
|
let response = format!(
|
|
"{}\r\n\
|
|
Access-control-allow-origin: *\r\n\
|
|
Content-type: text/plain; charset=utf-8\r\n\
|
|
Content-Lengh: {}\r\n\r\n\
|
|
{}",
|
|
status_line,
|
|
contents.len(),
|
|
contents
|
|
);
|
|
|
|
stream.write(response.as_bytes()).unwrap();
|
|
stream.flush().unwrap();
|
|
}
|