add resolve_name, normalize_url_to_path

This commit is contained in:
2020-06-21 14:43:52 +08:00
parent 1b554cb1b1
commit 6b6c957802

View File

@@ -1,7 +1,7 @@
use std::fs; use std::fs;
use std::collections::HashSet; use std::collections::HashSet;
use clap::{ Arg, ArgMatches, SubCommand, App, }; use clap::{ Arg, ArgMatches, SubCommand, App, };
use url::Url; use url::{ Url, Host, };
use crate::cmd::{ Command, CommandError, }; use crate::cmd::{ Command, CommandError, };
use crate::har::*; use crate::har::*;
@@ -81,18 +81,38 @@ impl Command for CommandDownload {
} }
fn _resolve_name(_base: &str, url: &str) -> String { fn _resolve_name(_base: &str, url: &str) -> String {
let ret = String::with_capacity(url.len()); let _url_to_path = _normalize_url_to_path(url);
let url = match Url::parse(url) { "".into()
}
fn _normalize_url_to_path(url: &str) -> Vec<String> {
let mut ret = vec![];
let parsed_url = match Url::parse(url) {
Ok(u) => u, Err(e) => { Ok(u) => u, Err(e) => {
warn!("Url: {}, parse failed: {}", url, e); warn!("Url: {}, parse failed: {}", url, e);
return url.chars() return vec![url.chars()
.filter(|c| (*c >= 'a' && *c <= 'z') .filter(|c| (*c >= 'a' && *c <= 'z')
|| (*c >= 'A' && *c <= 'Z') || (*c >= 'A' && *c <= 'Z')
|| (*c >= '0' && *c <= '9') || (*c >= '0' && *c <= '9')
|| *c == '_' || *c == '-' || *c == '_' || *c == '-'
) )
.collect::<String>(); .collect::<String>()];
}, },
}; };
let host = match parsed_url.host() {
None => "".to_owned(),
Some(h) => match h {
Host::Domain(d) => d.to_owned(),
Host::Ipv4(ipv4) => ipv4.to_string(),
Host::Ipv6(ipv6) => ipv6.to_string(),
},
};
ret.push(host);
if let Some(segments) = parsed_url.path_segments() {
for seg in segments {
ret.push(seg.into());
}
}
ret ret
} }