fill download

This commit is contained in:
2020-06-19 01:13:18 +08:00
parent 6d8c4acf34
commit 2294a2f5d3
5 changed files with 160 additions and 20 deletions

View File

@@ -21,10 +21,40 @@ impl Command for CommandDownload {
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,*/ e.pageref);
har.log.pages.iter().for_each(|page| {
info!("Start download page_id: {}, title: {}", page.id, page.title);
har.downlaod_page(page, Box::new(|_har: &Har, _page_id: &str, _har_page: &HarPage, har_entry: &HarEntry| {
info!("Processing url: {}", har_entry.request.url);
let client = match reqwest::blocking::ClientBuilder::new().build() {
Ok(c) => c, Err(e) => {
error!("Create client from ClientBuilder failed: {}", e);
return;
},
};
let reqeust = &har_entry.request;
let mut client = match reqeust.method.as_str() {
"GET" => client.get(&reqeust.url),
// "POST" => client.post(&reqeust.url), // TODO ???
_ => {
error!("Method not supported: {}, of url: {}", reqeust.method, reqeust.url);
return;
},
};
for header in &reqeust.headers {
client = client.header(&header.name, &header.value);
}
let response = match client.send() {
Ok(r) => r, Err(e) => {
error!("Request url: {}, error: {}", reqeust.url, e);
return;
},
};
let code = response.status().as_u16();
if code != 200 {
// TODO ...
return;
}
}))
});
Ok(())

View File

@@ -95,19 +95,18 @@ pub struct Har {
pub log: HarLog,
}
// TODO
pub fn downlaod_page(har: &Har, har_page: &HarPage) {
let page_id = &har_page.id;
har.log.entries.iter().filter(|e| if let Some(pageref) = &e.pageref {
pageref == page_id
} else {
false
}).for_each(|e| {
make_request(&e.request);
});
}
impl Har {
// GET, POST
pub fn make_request(har_request: &HarRequest) -> Option<()> { // ???
None
}
pub fn downlaod_page(&self, har_page: &HarPage, callback: Box<dyn Fn(&Har, &str, &HarPage, &HarEntry) -> ()>) {
let page_id = &har_page.id;
self.log.entries.iter().filter(|e| if let Some(pageref) = &e.pageref {
pageref == page_id
} else {
warn!("Not matched url: {}", e.request.url);
false
}).for_each(|e| {
info!("Processing url: {}", e.request.url);
callback(&self, page_id, har_page, e);
});
}
}

View File

@@ -1,3 +1,4 @@
#[macro_use] extern crate log;
use clap::App;
mod cmd;
@@ -10,6 +11,9 @@ use cmd_download::CommandDownload;
use cmd_default::CommandDefault;
fn main() -> CommandError {
env_logger::init();
info!("hardownload started");
let commands: Vec<Box<dyn Command>> = vec![
Box::new(CommandDownload{}),
];
@@ -24,9 +28,11 @@ fn main() -> CommandError {
let matches = app.get_matches();
for command in &commands {
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
info!("matched subcommand: {}", command.name());
return command.run(&matches, sub_cmd_matches);
}
}
info!("matched default subcommand");
CommandDefault::run(&matches)
}