use case_insensitive_string::CaseInsensitiveString;
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
mod platform {
pub use crate::utils::http_std::{
get_sub_info_from_header, get_sub_info_from_response, parse_proxy, web_get, web_get_async,
web_patch_async, web_post_async, HttpError, HttpResponse, ProxyConfig,
};
}
#[cfg(target_arch = "wasm32")]
mod platform {
pub use crate::utils::http_wasm::{
get_sub_info_from_header, get_sub_info_from_response, parse_proxy, web_get, web_get_async,
web_patch_async, web_post_async, HttpError, HttpResponse, ProxyConfig,
};
}
pub use platform::*;
pub async fn web_get_content_async(
url: &str,
proxy_config: &ProxyConfig,
headers: Option<&HashMap<CaseInsensitiveString, String>>,
) -> Result<String, String> {
match web_get_async(url, proxy_config, headers).await {
Ok(response) => {
if (200..300).contains(&response.status) {
Ok(response.body)
} else {
Err(format!("HTTP error {}: {}", response.status, response.body))
}
}
Err(e) => Err(e.message),
}
}
pub fn get_sub_info_from_header(headers: &HashMap<String, String>) -> String {
let mut sub_info = String::new();
let mut upload: u64 = 0;
let mut download: u64 = 0;
let mut total: u64 = 0;
let mut expire: String = String::new();
if let Some(userinfo) = headers.get("subscription-userinfo") {
for info_item in userinfo.split(';') {
let info_item = info_item.trim();
if info_item.starts_with("upload=") {
if let Ok(value) = info_item[7..].parse::<u64>() {
upload = value;
}
} else if info_item.starts_with("download=") {
if let Ok(value) = info_item[9..].parse::<u64>() {
download = value;
}
} else if info_item.starts_with("total=") {
if let Ok(value) = info_item[6..].parse::<u64>() {
total = value;
}
} else if info_item.starts_with("expire=") {
expire = info_item[7..].to_string();
}
}
}
if upload > 0 || download > 0 {
sub_info.push_str(&format!("upload={}, download={}", upload, download));
}
if total > 0 {
if !sub_info.is_empty() {
sub_info.push_str(", ");
}
sub_info.push_str(&format!("total={}", total));
}
if !expire.is_empty() {
if !sub_info.is_empty() {
sub_info.push_str(", ");
}
sub_info.push_str(&format!("expire={}", expire));
}
sub_info
}
pub fn get_sub_info_from_response(
headers: &HashMap<String, String>,
sub_info: &mut String,
) -> bool {
let header_info = get_sub_info_from_header(headers);
if !header_info.is_empty() {
if !sub_info.is_empty() {
sub_info.push_str(", ");
}
sub_info.push_str(&header_info);
true
} else {
false
}
}