diff --git a/.gitignore b/.gitignore index a919aef..81f9781 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.har # ---> Rust # Generated by Cargo # will have compiled files and executables diff --git a/src/cmd_download.rs b/src/cmd_download.rs new file mode 100644 index 0000000..aaea82d --- /dev/null +++ b/src/cmd_download.rs @@ -0,0 +1,32 @@ +use std::fs; +use clap::{ Arg, ArgMatches, SubCommand, App, }; +use crate::cmd::{ Command, CommandError, }; +use crate::har::*; + +pub struct CommandDownload; + +impl Command for CommandDownload { + + fn subcommand<'a>(&self) -> App<'a, 'a> { + SubCommand::with_name(self.name()).about("Download subcommand") + .arg(Arg::with_name("FILE_NAME").required(true).help("Har file name")) + } + + fn name(&self) -> &str { + "download" + } + + fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { + let file_name = sub_arg_matches.value_of("FILE_NAME").unwrap(); + let har_str = fs::read_to_string(file_name)?; + let har: Har = serde_json::from_str(&har_str)?; + + // println!("{:?}", har); + + har.log.entries.iter().for_each(|e| { + println!("{:<100} {}", e.request.url, e.server_ip_address); + }); + + Ok(()) + } +} diff --git a/src/cmd_sample.rs b/src/cmd_sample.rs deleted file mode 100644 index b1bd812..0000000 --- a/src/cmd_sample.rs +++ /dev/null @@ -1,20 +0,0 @@ -use clap::{ ArgMatches, SubCommand, App, }; -use crate::cmd::{ Command, CommandError, }; - -pub struct CommandSample; - -impl Command for CommandSample { - - fn subcommand<'a>(&self) -> App<'a, 'a> { - SubCommand::with_name(self.name()).about("Sample subcommand") - } - - fn name(&self) -> &str { - "sample" - } - - fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { - println!("This is test command!"); - Ok(()) - } -} diff --git a/src/har.rs b/src/har.rs index c9e0531..50fb9d9 100644 --- a/src/har.rs +++ b/src/har.rs @@ -2,48 +2,95 @@ use serde::{Serialize, Deserialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct HarCreator { - name: String, - version: String, + pub name: String, + pub version: String, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct HarPageTimings { #[serde(rename = "onContentLoad")] - on_content_load: f64, + pub on_content_load: f64, #[serde(rename = "onLoad")] - on_load: f64, + pub on_load: f64, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct HarPage { #[serde(rename = "startedDateTime")] - started_date_time: String, - id: String, - title: String, + pub started_date_time: String, + pub id: String, + pub title: String, #[serde(rename = "pageTimings")] - page_timings: HarPageTimings, + pub page_timings: HarPageTimings, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HarHeader { + pub name: String, + pub value: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HarQueryParam { + pub name: String, + pub value: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HarCookie { + pub name: String, + pub value: String, + pub expires: Option, + #[serde(rename = "httpOnly")] + pub http_only: bool, + pub secure: bool, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct HarRequest { + pub method: String, + pub url: String, + #[serde(rename = "httpVersion")] + pub http_version: String, + pub headers: Vec, + #[serde(rename = "queryString")] + pub query_string: Vec, + pub cookies: Vec, + #[serde(rename = "headersSize")] + pub headers_size: i64, + #[serde(rename = "bodySize")] + pub body_size: i64, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct HarEntry { - // startedDateTime - time: f64, - // request + #[serde(rename = "startedDateTime")] + pub started_date_time: String, + pub time: f64, + pub request: HarRequest, // response // cache // timings - // serverIPAddress + #[serde(rename = "serverIPAddress")] + pub server_ip_address: String, // _initiator - // _priority - // _resourceType - // connection - pageref: String, // @see Harpage::id + #[serde(rename = "_priority")] + pub priority: String, + #[serde(rename = "_resourceType")] + pub resource_ype: String, + pub connection: Option, + pub pageref: Option, // @see Harpage::id } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct HarLog { - version: String, - creator: HarCreator, - pages: Vec, - entries: Vec, + pub version: String, + pub creator: HarCreator, + pub pages: Vec, + pub entries: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Har { + pub log: HarLog, } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 234931c..195936a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,17 @@ use clap::App; mod cmd; -mod cmd_sample; +mod cmd_download; mod cmd_default; mod har; use cmd::{ Command, CommandError, }; -use cmd_sample::CommandSample; +use cmd_download::CommandDownload; use cmd_default::CommandDefault; fn main() -> CommandError { - let commands = vec![ - CommandSample{}, + let commands: Vec> = vec![ + Box::new(CommandDownload{}), ]; let mut app = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION"))