aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCeleste <colean@colean.cc>2021-12-22 20:30:53 +0000
committerCeleste <colean@colean.cc>2021-12-22 20:30:53 +0000
commit3d0f673e199fc05e88e083e8a67dc578de52293e (patch)
tree245ac9736fd8c1aab36c77806c5a71fcce9e3d7e
parent14ee095313415ce0f591659261e4f18eca83aa96 (diff)
Add date field but only on Unix hosts. Sorry friens who use Windows!
-rw-r--r--src/main.rs19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index dad2472..5b36d12 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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.");