-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option for reading Netscape cookie file
- Loading branch information
Sunshine
committed
Nov 25, 2022
1 parent
db04d11
commit 35e3b3e
Showing
5 changed files
with
115 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
pub struct Cookie { | ||
pub domain: String, | ||
pub tailmatch: bool, | ||
pub path: String, | ||
pub secure: bool, | ||
pub expires: u64, | ||
pub name: String, | ||
pub value: String, | ||
} | ||
|
||
pub enum CookieFileContentsParseError { | ||
InvalidHeader, | ||
} | ||
|
||
impl Cookie { | ||
pub fn is_expired(&self) -> bool { | ||
let start = SystemTime::now(); | ||
let since_the_epoch = start | ||
.duration_since(UNIX_EPOCH) | ||
.expect("Time went backwards"); | ||
|
||
self.expires < since_the_epoch.as_secs() | ||
} | ||
} | ||
|
||
pub fn parse_cookie_file_contents( | ||
cookie_file_contents: &str, | ||
) -> Result<Vec<Cookie>, CookieFileContentsParseError> { | ||
let mut cookies: Vec<Cookie> = Vec::new(); | ||
|
||
for (i, line) in cookie_file_contents.lines().enumerate() { | ||
if i == 0 { | ||
if !line.eq_ignore_ascii_case("# HTTP Cookie File") | ||
&& !line.eq_ignore_ascii_case("# Netscape HTTP Cookie File") | ||
{ | ||
return Err(CookieFileContentsParseError::InvalidHeader); | ||
} | ||
} else { | ||
let mut fields = line.split("\t"); | ||
cookies.push(Cookie { | ||
domain: fields.next().unwrap().to_string(), | ||
tailmatch: fields.next().unwrap().to_string() == "TRUE", | ||
path: fields.next().unwrap().to_string(), | ||
secure: fields.next().unwrap().to_string() == "TRUE", | ||
expires: fields.next().unwrap().parse::<u64>().unwrap(), | ||
name: fields.next().unwrap().to_string(), | ||
value: fields.next().unwrap().to_string(), | ||
}); | ||
} | ||
} | ||
|
||
Ok(cookies) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod cookies; | ||
pub mod css; | ||
pub mod html; | ||
pub mod js; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters