Add date field but only on Unix hosts. Sorry friens who use Windows!
This commit is contained in:
parent
14ee095313
commit
3d0f673e19
1 changed files with 16 additions and 3 deletions
19
src/main.rs
19
src/main.rs
|
@ -2,6 +2,7 @@ use std::io::{Write, BufReader, BufRead};
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::string::{String};
|
use std::string::{String};
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
fn detect_media_type(filename: String) -> String {
|
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 {
|
fn process_request(request: Vec<u8>) -> String {
|
||||||
let mut input = String::from_utf8_lossy(&request).to_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 contents = get_page(resource);
|
||||||
let header = "HTTP/1.1 200 OK\r\n";
|
let header = "HTTP/1.1 200 OK\r\n";
|
||||||
let content_type = format!("Content-Type: {}\r\n", mime);
|
let content_type = format!("Content-Type: {}\r\n", mime);
|
||||||
let server = "Server: Herb/0.1.3\r\n";
|
let server = "Server: Herb/0.1.4\r\n";
|
||||||
let extra_fields = format!("{}\r\n", server);
|
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);
|
let response = format!("{}{}{}{}", header, content_type, extra_fields, contents);
|
||||||
|
|
||||||
stream.write(response.as_bytes()).unwrap();
|
stream.write(response.as_bytes()).unwrap();
|
||||||
|
@ -198,7 +211,7 @@ fn serve(mut stream: TcpStream) {
|
||||||
|
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
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() {
|
for stream in listen.incoming() {
|
||||||
println!("Serving incoming stream.");
|
println!("Serving incoming stream.");
|
||||||
|
|
Loading…
Reference in a new issue