From 8d29a2a37043842abe4225ba85bc0382e4b68e07 Mon Sep 17 00:00:00 2001 From: Tobi823 Date: Tue, 22 Jun 2021 16:39:42 +0200 Subject: [PATCH] pass cookies to HTTP requests with the option "--cookies" --- src/main.rs | 8 +++++++- src/opts.rs | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 7798759..e923efb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use encoding_rs::Encoding; use html5ever::rcdom::RcDom; use reqwest::blocking::Client; -use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; +use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT, COOKIE}; use std::collections::HashMap; use std::fs; use std::io::{self, prelude::*, Error, Write}; @@ -155,6 +155,12 @@ fn main() { HeaderValue::from_str(&user_agent).expect("Invalid User-Agent header specified"), ); } + if let Some(cookies) = &options.cookies { + header_map.insert( + COOKIE, + HeaderValue::from_str(&cookies).expect("Invalid cookies specified"), + ); + } let client = if options.timeout > 0 { Client::builder().timeout(Duration::from_secs(options.timeout)) } else { diff --git a/src/opts.rs b/src/opts.rs index 2ca692b..554c94e 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -23,6 +23,7 @@ pub struct Options { pub target: String, pub no_color: bool, pub unwrap_noscript: bool, + pub cookies: Option, } const ASCII: &'static str = " \ @@ -68,6 +69,7 @@ impl Options { .args_from_usage("-t, --timeout=[60] 'Adjusts network request timeout'") .args_from_usage("-u, --user-agent=[Firefox] 'Sets custom User-Agent string'") .args_from_usage("-v, --no-video 'Removes video sources'") + .args_from_usage("--cookies, --cookies=[UTF-8] 'Set cookies for HTTP requests. This format is being used: name1=value1;name2=value2'") .arg( Arg::with_name("target") .required(true) @@ -122,6 +124,10 @@ impl Options { } } + if let Some(cookies) = app.value_of("cookies") { + options.cookies = Some(str!(cookies)); + } + options } }