From 458b25247dd9942f71b189d3fe829096f0ba4ec6 Mon Sep 17 00:00:00 2001 From: Jan Wolff Date: Wed, 20 May 2020 08:03:27 +0200 Subject: [PATCH] correctly use CR/LF line endings for textfiles --- src/main.rs | 18 +++++++++++------- src/server/handler.rs | 32 ++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a00173..296603d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,9 @@ fn help() { } fn parse_args() -> Option { + let mut config_fname = "/etc/sheldond.conf".to_string(); let mut args = env::args(); + loop { match args.next() { Some(arg) => { @@ -21,15 +23,14 @@ fn parse_args() -> Option { return None; } if arg == "-c" || arg == "--config" { - let config_fname = args.next().unwrap(); - return Some(config_fname); + config_fname = args.next().unwrap().to_string(); } } None => break, } } - Some("/etc/sheldond.conf".to_string()) + Some(config_fname) } fn parse_config(fname: String) -> server::ServerConfig { @@ -79,10 +80,13 @@ fn parse_config(fname: String) -> server::ServerConfig { } fn main() { - println!("sheldond {}", match option_env!("CARGO_PKG_VERSION") { - Some(v) => v, - None => "", - }); + println!( + "sheldond {}", + match option_env!("CARGO_PKG_VERSION") { + Some(v) => v, + None => "", + } + ); let config_fname = match parse_args() { Some(config_fname) => config_fname, diff --git a/src/server/handler.rs b/src/server/handler.rs index e122e96..c15d600 100644 --- a/src/server/handler.rs +++ b/src/server/handler.rs @@ -3,7 +3,7 @@ use crate::server::response; use crate::server::ServerConfig; use openssl::ssl::SslStream; use std::fs::File; -use std::io::{copy, BufReader, BufWriter}; +use std::io::{copy, BufRead, BufReader, BufWriter, Error, Write}; use std::net::TcpStream; use std::path::{Path, PathBuf}; use url::Url; @@ -57,6 +57,12 @@ fn gen_path_index(path: &Path) -> PathBuf { } } +fn write_line(line: &[u8], stream: &mut BufWriter) -> Result<(), Error> { + stream.write(line)?; + stream.write(b"\r\n")?; + Ok(()) +} + fn handle_response(config: &ServerConfig, url: Url, mut stream: &mut SslStream) { println!("responding for: {}", url); @@ -99,8 +105,26 @@ fn handle_response(config: &ServerConfig, url: Url, mut stream: &mut SslStream {} - Err(_e) => {} + + if mime_type.starts_with("text/") { + // We make sure to send all text/* files with correct CL/RF line endings + for line in buf_file.lines() { + match write_line(line.unwrap().as_bytes(), &mut buf_stream) { + Ok(_) => {} + Err(e) => { + println!("error while sending text file: {:?}\n {}", path, e); + return; + } + } + } + } else { + // Any other MIME type can just be copied as-is + match copy(&mut buf_file, &mut buf_stream) { + Ok(_) => {} + Err(e) => { + println!("error while sending file: {:?}\n {}", path, e); + return; + } + } } }