Skip to content

Commit

Permalink
use kern 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ltheinrich committed Mar 26, 2020
1 parent 1070618 commit b9daacf
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 179 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stratos"
version = "0.1.7"
version = "0.1.8"
authors = ["Lennart Heinrich <[email protected]>"]
edition = "2018"
license = "ISC"
Expand All @@ -9,7 +9,7 @@ description = "Stratosphere balloon log analyzer"
readme = "README.md"

[dependencies]
kern = "1.0.3"
kern = "1.1.0"
plotlib = "0.5.0"

[profile.release]
Expand Down
18 changes: 2 additions & 16 deletions src/common/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Constant variables

use kern::meta;
use kern::version;

pub const HEAD: &str = include_str!("../../web/head.html");
pub const BACK: &str =
Expand All @@ -18,23 +18,9 @@ pub const HELP: &str = "Benutzung: stratos [OPTIONEN]\nString S, Integer I, Bool
--size I Maximale Log-Größe in MB (10)
--threads I Anzahl der anfangs startenden Threads (2)
--log Logging der Anfragen aktivieren";
const CARGO_TOML: &str = include_str!("../../Cargo.toml");
static mut VERSION: &str = "";
pub const CARGO_TOML: &str = include_str!("../../Cargo.toml");

/// Get HTML footer with version
pub fn footer() -> String {
format!("</div></div><div class=\"cr\"><small class=\"form-text text-muted\">Stratos v{} &copy; 2019 Lennart Heinrich</small><a href=\"https://github.com/ltheinrich/stratos/issues\">Fehler melden</a></div></body></html>", version())
}

/// Get version
pub fn version() -> &'static str {
unsafe { VERSION }
}

/// Set version (unsafe!)
pub fn init_version() {
unsafe {
VERSION = meta::version(CARGO_TOML);
println!("Stratos {} (c) 2019 Lennart Heinrich\n", VERSION);
}
}
15 changes: 9 additions & 6 deletions src/common/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::analyze::{highest, highest_x, highest_y, lowest_x, lowest_y, set_rang
use crate::parse::to_xy;
use crate::XY;
use crate::{Log, Parameters};
use kern::Fail;
use plotlib::page::Page;
use plotlib::repr::Plot;
use plotlib::style::PointStyle;
use plotlib::view::ContinuousView;
use std::error;

/// None instead of ""
pub fn none_empty<'a, 'b>(opt: Option<&'a &'b str>) -> Option<&'a &'b str> {
Expand All @@ -21,12 +21,12 @@ pub fn none_empty<'a, 'b>(opt: Option<&'a &'b str>) -> Option<&'a &'b str> {
}

/// Analyse log and return svg image
pub fn draw<'a>(log: &'a str, params: Parameters) -> Result<String, Box<dyn error::Error>> {
pub fn draw(log: &str, params: Parameters) -> Result<String, Fail> {
// parse log
let log = Log::from(&log)?;
let x_values = log.at_key(params.x_axis)?;
let y_values = log.at_key(params.y_axis)?;
let values = to_xy(&x_values, &y_values)?;
let x_values = log.at_key(params.x_axis).or_else(Fail::from)?;
let y_values = log.at_key(params.y_axis).or_else(Fail::from)?;
let values = to_xy(&x_values, &y_values).or_else(Fail::from)?;

// get highest and lowest
let highest_x = highest_x(&values).1;
Expand Down Expand Up @@ -112,7 +112,10 @@ pub fn draw<'a>(log: &'a str, params: Parameters) -> Result<String, Box<dyn erro
}

// return output
Ok(Page::single(&view).to_svg()?.to_string())
Ok(Page::single(&view)
.to_svg()
.or_else(Fail::from)?
.to_string())
}

/// Create plot
Expand Down
8 changes: 4 additions & 4 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::common::*;
use crate::http::{read_header, redirect, respond, HttpMethod, HttpRequest};
use crate::parse::Log;
use kern::Error;
use kern::Fail;
use std::collections::BTreeMap;
use std::net::{TcpListener, TcpStream};
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -191,7 +191,7 @@ fn handle_options(stream: &mut TcpStream, post_params: &BTreeMap<&str, &str>, fi
fn get_xy_names<'a>(
stream: &mut TcpStream,
post_params: &BTreeMap<&'a str, &'a str>,
) -> Result<(&'a str, &'a str), Error> {
) -> Result<(&'a str, &'a str), Fail> {
// x-axis
let x_axis = match post_params.get("x") {
Some(x_axis) => x_axis,
Expand All @@ -208,7 +208,7 @@ fn get_xy_names<'a>(
None,
)
.unwrap();
return Error::from("x-Achse wurde nicht angegeben");
return Fail::from("x-Achse wurde nicht angegeben");
}
};

Expand All @@ -228,7 +228,7 @@ fn get_xy_names<'a>(
None,
)
.unwrap();
return Error::from("y-Achse wurde nicht angegeben");
return Fail::from("y-Achse wurde nicht angegeben");
}
};

Expand Down
35 changes: 17 additions & 18 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! HTTP parsing

use crate::{common::*, version};
use kern::net::Stream;
use kern::Error;
use crate::common::*;
use kern::{version, Fail};
use std::collections::BTreeMap;
use std::io;
use std::io::{self, Read, Write};
use std::net::TcpStream;

/// HTTP request method (GET or POST)
Expand Down Expand Up @@ -96,7 +95,7 @@ impl<'a> HttpRequest<'a> {
// read body
while raw_body.len() < con_len {
let mut rest_body = vec![0u8; 65536];
let length = stream.r(&mut rest_body).ok()?;
let length = stream.read(&mut rest_body).ok()?;
rest_body.truncate(length);
raw_body.append(&mut rest_body);
}
Expand Down Expand Up @@ -230,8 +229,8 @@ pub fn respond(
) -> io::Result<()> {
// write headers to stream
stream
.wa(format!(
"HTTP/1.1 200 OK\r\nServer: ltheinrich.de stratos/{}\r\nContent-Type: {}\r\nContent-Length: {}{}\r\n\r\n",
.write_all(format!(
"HTTP/1.1 200 OK\r\nServer: ltheinrich.de/stratos v{}\r\nContent-Type: {}\r\nContent-Length: {}{}\r\n\r\n",
version(),
content_type,
content.len() + 2, // bugfix (proxying)
Expand All @@ -245,40 +244,40 @@ pub fn respond(
.as_bytes())?;

// write body and end
stream.wa(content)?;
stream.wa(b"\r\n")?;
stream.f()
stream.write_all(content)?;
stream.write_all(b"\r\n")?;
stream.flush()
}

/// HTTP redirecter
pub fn redirect(stream: &mut TcpStream, url: &str) -> io::Result<()> {
// write redirect headers and simple body
stream.wa(format!(
"HTTP/1.1 303 See Other\r\nServer: ltheinrich.de stratos/{}\r\nLocation: {1}\r\n\r\n<html><head><title>Moved</title></head><body><h1>Moved</h1><p><a href=\"{1}\">{1}</a></p></body></html>\r\n",
stream.write_all(format!(
"HTTP/1.1 303 See Other\r\nServer: ltheinrich.de/stratos v{}\r\nLocation: {1}\r\n\r\n<html><head><title>Moved</title></head><body><h1>Moved</h1><p><a href=\"{1}\">{1}</a></p></body></html>\r\n",
version(),
url
)
.as_bytes())
}

/// Read until \r\n\r\n (just working, uncommented)
pub fn read_header(stream: &mut TcpStream) -> Result<(String, Vec<u8>), Error> {
pub fn read_header(stream: &mut TcpStream) -> Result<(String, Vec<u8>), Fail> {
let mut header = Vec::new();
let mut rest = Vec::new();
let mut buf = vec![0u8; 8192];

'l: loop {
let length = match stream.r(&mut buf) {
let length = match stream.read(&mut buf) {
Ok(length) => length,
Err(err) => return Error::from(err),
Err(err) => return Fail::from(err),
};
for (i, &c) in buf.iter().enumerate() {
if c == b'\r' {
if buf.len() < i + 4 {
let mut buf_temp = vec![0u8; buf.len() - (i + 4)];
match stream.r(&mut buf_temp) {
match stream.read(&mut buf_temp) {
Ok(_) => {}
Err(err) => return Error::from(err),
Err(err) => return Fail::from(err),
};
let buf2 = [&buf[..], &buf_temp[..]].concat();
if buf2[i + 1] == b'\n' && buf2[i + 2] == b'\r' && buf2[i + 3] == b'\n' {
Expand Down Expand Up @@ -308,7 +307,7 @@ pub fn read_header(stream: &mut TcpStream) -> Result<(String, Vec<u8>), Error> {
Ok((
match String::from_utf8(header) {
Ok(header) => header,
Err(err) => return Error::from(err),
Err(err) => return Fail::from(err),
},
rest,
))
Expand Down
Loading

0 comments on commit b9daacf

Please sign in to comment.