Skip to content

Commit

Permalink
apply relevant cookies to network requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunshine committed Dec 4, 2022
1 parent 35e3b3e commit b44c390
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
32 changes: 32 additions & 0 deletions src/cookies.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::time::{SystemTime, UNIX_EPOCH};
use url::Url;

pub struct Cookie {
pub domain: String,
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use clap::{App, Arg, ArgAction};
use std::env;

use crate::cookies::Cookie;

#[derive(Default)]
pub struct Options {
pub no_audio: bool,
pub base_url: Option<String>,
pub blacklist_domains: bool,
pub no_css: bool,
pub cookies: Option<String>,
pub _cookies: Vec<Cookie>,
pub domains: Option<Vec<String>>,
pub ignore_errors: bool,
pub encoding: Option<String>,
Expand Down
7 changes: 5 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b44c390

Please sign in to comment.