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.
 
 
 
shock/src/main.rs

43 lines
1.3 KiB

use std::{
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
str,
};
#[path = "./index.rs"]
mod index;
#[path = "./style.rs"]
mod style;
fn main() {
let listener = TcpListener::bind("0.0.0.0:7878").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&mut stream);
let request_line = buf_reader.lines().next().unwrap().unwrap();
let mut content = "<html><head><strong>Error 404</strong><br /></head><body><img src=\"https://http.cat/404\"></img><br /><p>Not Found</p></body></html>";
let mut status_line = "HTTP/1.1 404 NOT FOUND";
if request_line == "GET / HTTP/1.1" {
status_line = "HTTP/1.1 200 OK";
content = "<html><head>Sean's Got A Webserver</head><body><p>EVERYBODY RUN</p></body></html>";
}else if request_line == "GET /index.html HTTP/1.1" {
status_line = "HTTP/1.1 200 OK";
content = index::INDEX;
}else if request_line == "GET /style.css HTTP/1.1" {
status_line = "HTTP/1.1 200 OK";
content = style::STYLE;
}
let status_line = "HTTP/1.1 200 OK";
let length = content.len();
let response = format!(
"{}\r\nContent-Length: {}\r\n\r\n{}"
, status_line, length, content);
stream.write_all(response.as_bytes());
}