add tests, lib
This commit is contained in:
40
src/lib.rs
Normal file
40
src/lib.rs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
use http::Uri;
|
||||||
|
use rust_util::*;
|
||||||
|
|
||||||
|
pub fn make_url(base_url: &str, ts_url: &str) -> XResult<String> {
|
||||||
|
let lower_ts_url = ts_url.to_lowercase();
|
||||||
|
if lower_ts_url.starts_with("http://") || lower_ts_url.starts_with("https://") {
|
||||||
|
return Ok(ts_url.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsed_base_url = base_url.parse::<Uri>()?;
|
||||||
|
let lower_scheme_str = match parsed_base_url.scheme_str() {
|
||||||
|
None => return Err(new_box_error(&format!("Base url has no scheme: {}", base_url))),
|
||||||
|
Some(s) => s.to_lowercase(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if ts_url.starts_with("//") {
|
||||||
|
return Ok(iff!(lower_scheme_str == "http", "http:", "https:").to_owned() + ts_url);
|
||||||
|
}
|
||||||
|
|
||||||
|
let authority = match parsed_base_url.authority() {
|
||||||
|
None => return Err(new_box_error(&format!("Base url has no authority: {}", base_url))),
|
||||||
|
Some(a) => a,
|
||||||
|
};
|
||||||
|
|
||||||
|
if ts_url.starts_with('/') {
|
||||||
|
return Ok(format!("{}://{}{}", lower_scheme_str, authority, ts_url));
|
||||||
|
}
|
||||||
|
|
||||||
|
let relative_base_url = format!("{}://{}{}", lower_scheme_str, authority, parsed_base_url.path());
|
||||||
|
if relative_base_url.ends_with('/') {
|
||||||
|
// ends with '/'
|
||||||
|
Ok(relative_base_url + ts_url)
|
||||||
|
} else {
|
||||||
|
// not ends with '/'
|
||||||
|
let mut splited_by_slash = relative_base_url.split('/').collect::<Vec<_>>();
|
||||||
|
let splited_by_slash_len = splited_by_slash.len();
|
||||||
|
splited_by_slash[splited_by_slash_len - 1] = ts_url;
|
||||||
|
Ok(splited_by_slash.join("/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/main.rs
39
src/main.rs
@@ -1,5 +1,4 @@
|
|||||||
use http::Uri;
|
use http::Uri;
|
||||||
use rust_util::*;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
@@ -22,42 +21,6 @@ fn main() {
|
|||||||
v[l]="xxxxx";
|
v[l]="xxxxx";
|
||||||
}
|
}
|
||||||
println!("{:?}", v.join("/"));
|
println!("{:?}", v.join("/"));
|
||||||
}
|
|
||||||
|
|
||||||
fn _make_url(base_url: &str, ts_url: &str) -> XResult<String> {
|
// make_url("", "").ok();
|
||||||
let lower_ts_url = ts_url.to_lowercase();
|
|
||||||
if lower_ts_url.starts_with("http://") || lower_ts_url.starts_with("https://") {
|
|
||||||
return Ok(ts_url.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
let parsed_base_url = base_url.parse::<Uri>()?;
|
|
||||||
let lower_scheme_str = match parsed_base_url.scheme_str() {
|
|
||||||
None => return Err(new_box_error(&format!("Base url has no scheme: {}", base_url))),
|
|
||||||
Some(s) => s.to_lowercase(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if ts_url.starts_with("//") {
|
|
||||||
return Ok(iff!(lower_scheme_str == "http", "http:", "https:").to_owned() + ts_url);
|
|
||||||
}
|
|
||||||
|
|
||||||
let authority = match parsed_base_url.authority() {
|
|
||||||
None => return Err(new_box_error(&format!("Base url has no authority: {}", base_url))),
|
|
||||||
Some(a) => a,
|
|
||||||
};
|
|
||||||
|
|
||||||
if ts_url.starts_with('/') {
|
|
||||||
return Ok(format!("{}://{}{}", lower_scheme_str, authority, ts_url));
|
|
||||||
}
|
|
||||||
|
|
||||||
let relative_base_url = format!("{}://{}{}", lower_scheme_str, authority, parsed_base_url.path());
|
|
||||||
if relative_base_url.ends_with('/') {
|
|
||||||
// ends with '/'
|
|
||||||
Ok(relative_base_url + ts_url)
|
|
||||||
} else {
|
|
||||||
// not ends with '/'
|
|
||||||
let mut splited_by_slash = relative_base_url.split('/').collect::<Vec<_>>();
|
|
||||||
let splited_by_slash_len = splited_by_slash.len();
|
|
||||||
splited_by_slash[splited_by_slash_len - 1] = ts_url;
|
|
||||||
Ok(splited_by_slash.join("/"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
tests/test_make_url.rs
Normal file
16
tests/test_make_url.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use m3u8download::make_url;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_make_url() {
|
||||||
|
assert_eq!("http://example.com/a/b.ts", &make_url("", "http://example.com/a/b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/a/b.ts", &make_url("", "https://example.com/a/b.ts").unwrap());
|
||||||
|
assert_eq!("http://example.com/a/b.ts", &make_url("HTTP://example.com/x/y.m3u8", "//example.com/a/b.ts").unwrap());
|
||||||
|
assert_eq!("http://example.com/a/b.ts", &make_url("http://example.com/x/y.m3u8", "//example.com/a/b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/a/b.ts", &make_url("HTTPS://example.com/x/y.m3u8", "//example.com/a/b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/a/b.ts", &make_url("https://example.com/x/y.m3u8", "//example.com/a/b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/a/b.ts", &make_url("https://example.com/x/y.m3u8", "/a/b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/x/b.ts", &make_url("https://example.com/x/y.m3u8", "b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/x/b.ts", &make_url("https://example.com/x/y", "b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/x/y/b.ts", &make_url("https://example.com/x/y/", "b.ts").unwrap());
|
||||||
|
assert_eq!("https://example.com/x/y/a/b.ts", &make_url("https://example.com/x/y/", "a/b.ts").unwrap());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user