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> {
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<String> {
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,

View file

@ -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<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>) {
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_stream = BufWriter::new(stream);
match copy(&mut buf_file, &mut buf_stream) {
Ok(_s) => {}
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;
}
}
}
}