Add date field but only on Unix hosts. Sorry friens who use Windows!

This commit is contained in:
abbie 2021-12-22 20:30:53 +00:00
parent 14ee095313
commit 3d0f673e19
No known key found for this signature in database
GPG key ID: 04DDE463F9200F87

View file

@ -2,6 +2,7 @@ use std::io::{Write, BufReader, BufRead};
use std::net::{TcpListener, TcpStream};
use std::fs;
use std::string::{String};
use std::process::Command;
fn detect_media_type(filename: String) -> String {
@ -111,6 +112,16 @@ fn get_page(filename: String) -> String {
}
}
fn grab_time() -> String{
let output = Command::new("date")
.arg("+'%a, %d %b %Y %H:%m:%S %Z'")
.output()
.expect("failed to execute process");
return String::from_utf8_lossy(&output.stdout).to_string();
}
fn process_request(request: Vec<u8>) -> String {
let mut input = String::from_utf8_lossy(&request).to_string();
@ -187,8 +198,10 @@ fn serve(mut stream: TcpStream) {
let contents = get_page(resource);
let header = "HTTP/1.1 200 OK\r\n";
let content_type = format!("Content-Type: {}\r\n", mime);
let server = "Server: Herb/0.1.3\r\n";
let extra_fields = format!("{}\r\n", server);
let server = "Server: Herb/0.1.4\r\n";
let mut time = format!("Date: {}\r\n", grab_time());
time = time.replace("'","");
let extra_fields = format!("{}{}\r\n", server, time);
let response = format!("{}{}{}{}", header, content_type, extra_fields, contents);
stream.write(response.as_bytes()).unwrap();
@ -198,7 +211,7 @@ fn serve(mut stream: TcpStream) {
fn main() -> std::io::Result<()> {
let listen = TcpListener::bind("0.0.0.0:80")?;
let listen = TcpListener::bind("0.0.0.0:8080")?;
for stream in listen.incoming() {
println!("Serving incoming stream.");