pass cookies to HTTP requests with the option "--cookies"

This commit is contained in:
Tobi823 2021-06-22 16:39:42 +02:00
parent 2539aac4c0
commit 8d29a2a370
2 changed files with 13 additions and 1 deletions

View File

@ -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 {

View File

@ -23,6 +23,7 @@ pub struct Options {
pub target: String,
pub no_color: bool,
pub unwrap_noscript: bool,
pub cookies: Option<String>,
}
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
}
}