diff --git a/src/cookies.rs b/src/cookies.rs index f961aa04..c2cc0bf1 100644 --- a/src/cookies.rs +++ b/src/cookies.rs @@ -1,4 +1,5 @@ use std::time::{SystemTime, UNIX_EPOCH}; +use url::Url; pub struct Cookie { pub domain: String, @@ -23,6 +24,37 @@ impl Cookie { self.expires < since_the_epoch.as_secs() } + + pub fn matches_url(&self, url: &str) -> bool { + match Url::parse(&url) { + Ok(url) => { + match url.scheme() { + "http" => { + if self.secure { + return false; + } + }, + "https" => { }, + _ => { + return false; + } + } + + if let Some(domain) = url.domain() { + if !domain.eq_ignore_ascii_case(&self.domain) { + return false; + } + } + + // TODO: check path + }, + Err(_) => { + return false; + }, + } + + true + } } pub fn parse_cookie_file_contents( diff --git a/src/main.rs b/src/main.rs index aecbb741..365b7a22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,9 +140,9 @@ fn main() { }, }; - // Deal with cookie file + // Read and parse cookie file if let Some(opt_cookies) = options.cookies.clone() { - match std::fs::read_to_string(opt_cookies) { + match fs::read_to_string(opt_cookies) { Ok(str) => match parse_cookie_file_contents(&str) { Ok(cookies) => { for c in &cookies { diff --git a/src/opts.rs b/src/opts.rs index d54f9f1d..a45f18b4 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -1,6 +1,8 @@ use clap::{App, Arg, ArgAction}; use std::env; +use crate::cookies::Cookie; + #[derive(Default)] pub struct Options { pub no_audio: bool, @@ -8,6 +10,7 @@ pub struct Options { pub blacklist_domains: bool, pub no_css: bool, pub cookies: Option, + pub _cookies: Vec, pub domains: Option>, pub ignore_errors: bool, pub encoding: Option, diff --git a/src/utils.rs b/src/utils.rs index 621a0839..53497fa9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,11 @@ use reqwest::blocking::Client; -use reqwest::header::CONTENT_TYPE; +use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE, COOKIE}; use std::collections::HashMap; use std::fs; use std::path::{Path, PathBuf}; use url::Url; +// use crate::cookies::Cookie; use crate::opts::Options; use crate::url::{clean_url, parse_data_url}; @@ -304,7 +305,9 @@ pub fn retrieve_asset( } // URL not in cache, we retrieve the file - match client.get(url.as_str()).send() { + let mut headers = HeaderMap::new(); + headers.insert(COOKIE, HeaderValue::from_str("key=value").unwrap()); + match client.get(url.as_str()).headers(headers).send() { Ok(response) => { if !options.ignore_errors && response.status() != reqwest::StatusCode::OK { if !options.silent { diff --git a/tests/opts.rs b/tests/opts.rs index f307ec36..74b491aa 100644 --- a/tests/opts.rs +++ b/tests/opts.rs @@ -16,7 +16,7 @@ mod passing { assert_eq!(options.no_audio, false); assert_eq!(options.base_url, None); assert_eq!(options.no_css, false); - assert_eq!(options.charset, None); + assert_eq!(options.encoding, None); assert_eq!(options.no_frames, false); assert_eq!(options.no_fonts, false); assert_eq!(options.no_images, false);