summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2020-05-20 08:03:27 +0200
committerJan Wolff <janw@mailbox.org>2020-05-20 08:03:27 +0200
commit458b25247dd9942f71b189d3fe829096f0ba4ec6 (patch)
tree553222effb22abad9a0d92a9be98222f928c4480
parentb6e428eba2397ab063f6580098fe100906c44fb1 (diff)
correctly use CR/LF line endings for textfiles
-rw-r--r--src/main.rs18
-rw-r--r--src/server/handler.rs32
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<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,
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<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;
+ }
+ }
}
}