mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
CookieJar support comment line
This commit is contained in:
@@ -274,10 +274,42 @@ impl Cookie {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, derive_more::From)]
|
||||
/// Line of a netscape cookie
|
||||
pub enum CookieJarLine {
|
||||
/// Comment
|
||||
Comment(String),
|
||||
/// Cookie
|
||||
Cookie(Cookie),
|
||||
}
|
||||
|
||||
impl CookieJarLine {
|
||||
pub fn is_expired(&self) -> bool {
|
||||
match self {
|
||||
Self::Cookie(c) => c.is_expired(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_same_key(&self, c: &Cookie) -> bool {
|
||||
match self {
|
||||
Self::Cookie(a) => a.is_same_key(c),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_netscape_str(&self) -> String {
|
||||
match self {
|
||||
Self::Cookie(c) => c.to_netscape_str(),
|
||||
Self::Comment(c) => format!("{}\n", c.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
/// Cookies Jar
|
||||
pub struct CookieJar {
|
||||
cookies: Vec<Cookie>,
|
||||
cookies: Vec<CookieJarLine>,
|
||||
}
|
||||
|
||||
impl CookieJar {
|
||||
@@ -292,12 +324,12 @@ impl CookieJar {
|
||||
while i < self.cookies.len() {
|
||||
let a = &self.cookies[i];
|
||||
if a.is_same_key(&c) {
|
||||
self.cookies[i] = c;
|
||||
self.cookies[i] = CookieJarLine::from(c);
|
||||
return;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
self.cookies.push(c);
|
||||
self.cookies.push(CookieJarLine::from(c));
|
||||
}
|
||||
|
||||
/// Check and remove all expired cookies
|
||||
@@ -321,8 +353,13 @@ impl CookieJar {
|
||||
pub fn get<S: AsRef<str> + ?Sized>(&self, name: &S) -> Option<&Cookie> {
|
||||
let name = name.as_ref();
|
||||
for i in self.cookies.iter() {
|
||||
if i._name == name {
|
||||
return Some(i);
|
||||
match i {
|
||||
CookieJarLine::Cookie(i) => {
|
||||
if i._name == name {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
@@ -346,6 +383,7 @@ impl CookieJar {
|
||||
let mut l = line.unwrap();
|
||||
l = l.trim().to_string();
|
||||
if l.starts_with("#") {
|
||||
self.cookies.push(CookieJarLine::Comment(l));
|
||||
continue;
|
||||
}
|
||||
let mut s = l.split('\t');
|
||||
@@ -404,7 +442,7 @@ impl CookieJar {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> core::slice::Iter<Cookie> {
|
||||
pub fn iter(&self) -> core::slice::Iter<CookieJarLine> {
|
||||
self.cookies.iter()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::cookies::Cookie;
|
||||
use crate::cookies::CookieJarLine;
|
||||
use crate::cookies::ManagedCookieJar;
|
||||
use crate::ext::atomic::AtomicQuick;
|
||||
use crate::ext::json::ToJson;
|
||||
@@ -63,11 +64,16 @@ pub fn gen_cookie_header<U: IntoUrl>(c: &WebClient, url: U) -> String {
|
||||
let mut s = String::from("");
|
||||
let u = url.as_str();
|
||||
for a in c.get_cookies().jar.get_ref().iter() {
|
||||
if a.matched(u) {
|
||||
if s.len() > 0 {
|
||||
s += " ";
|
||||
match a {
|
||||
CookieJarLine::Cookie(a) => {
|
||||
if a.matched(u) {
|
||||
if s.len() > 0 {
|
||||
s += " ";
|
||||
}
|
||||
s += a.get_name_value().as_str();
|
||||
}
|
||||
}
|
||||
s += a.get_name_value().as_str();
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
s
|
||||
|
||||
Reference in New Issue
Block a user