correctly use CR/LF line endings for textfiles

This commit is contained in:
Jan Wolff 2020-05-20 08:03:27 +02:00
parent b6e428eba2
commit 458b25247d
2 changed files with 39 additions and 11 deletions

View file

@ -12,7 +12,9 @@ fn help() {
} }
fn parse_args() -> Option<String> { fn parse_args() -> Option<String> {
let mut config_fname = "/etc/sheldond.conf".to_string();
let mut args = env::args(); let mut args = env::args();
loop { loop {
match args.next() { match args.next() {
Some(arg) => { Some(arg) => {
@ -21,15 +23,14 @@ fn parse_args() -> Option<String> {
return None; return None;
} }
if arg == "-c" || arg == "--config" { if arg == "-c" || arg == "--config" {
let config_fname = args.next().unwrap(); config_fname = args.next().unwrap().to_string();
return Some(config_fname);
} }
} }
None => break, None => break,
} }
} }
Some("/etc/sheldond.conf".to_string()) Some(config_fname)
} }
fn parse_config(fname: String) -> server::ServerConfig { fn parse_config(fname: String) -> server::ServerConfig {
@ -79,10 +80,13 @@ fn parse_config(fname: String) -> server::ServerConfig {
} }
fn main() { fn main() {
println!("sheldond {}", match option_env!("CARGO_PKG_VERSION") { println!(
Some(v) => v, "sheldond {}",
None => "", match option_env!("CARGO_PKG_VERSION") {
}); Some(v) => v,
None => "",
}
);
let config_fname = match parse_args() { let config_fname = match parse_args() {
Some(config_fname) => config_fname, Some(config_fname) => config_fname,

View file

@ -3,7 +3,7 @@ use crate::server::response;
use crate::server::ServerConfig; use crate::server::ServerConfig;
use openssl::ssl::SslStream; use openssl::ssl::SslStream;
use std::fs::File; 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::net::TcpStream;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use url::Url; use url::Url;
@ -57,6 +57,12 @@ fn gen_path_index(path: &Path) -> PathBuf {
} }
} }
fn write_line<T: Write>(line: &[u8], stream: &mut BufWriter<T>) -> Result<(), Error> {
stream.write(line)?;
stream.write(b"\r\n")?;
Ok(())
}
fn handle_response(config: &ServerConfig, url: Url, mut stream: &mut SslStream<TcpStream>) { fn handle_response(config: &ServerConfig, url: Url, mut stream: &mut SslStream<TcpStream>) {
println!("responding for: {}", url); println!("responding for: {}", url);
@ -99,8 +105,26 @@ fn handle_response(config: &ServerConfig, url: Url, mut stream: &mut SslStream<T
let mut buf_file = BufReader::new(file); let mut buf_file = BufReader::new(file);
let mut buf_stream = BufWriter::new(stream); let mut buf_stream = BufWriter::new(stream);
match copy(&mut buf_file, &mut buf_stream) {
Ok(_s) => {} if mime_type.starts_with("text/") {
Err(_e) => {} // 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;
}
}
} }
} }