use std::collections::HashMap;
use crate::context::CookieOptions;
pub fn parse_cookies(cookie_header: &str) -> HashMap<String, String> {
let mut cookies = HashMap::new();
for pair in cookie_header.split(';') {
let parts: Vec<&str> = pair.trim().splitn(2, '=').collect();
if parts.len() == 2 {
cookies.insert(parts[0].to_string(), parts[1].to_string());
}
}
cookies
}
pub fn parse_query_string(query: &str) -> HashMap<String, String> {
let mut params = HashMap::new();
for pair in query.split('&') {
let parts: Vec<&str> = pair.splitn(2, '=').collect();
if parts.len() == 2 {
params.insert(
urlencoding::decode(parts[0]).unwrap_or_default().to_string(),
urlencoding::decode(parts[1]).unwrap_or_default().to_string(),
);
}
}
params
}
pub fn build_cookie_string(name: &str, value: &str, options: CookieOptions) -> String {
let mut cookie = format!("{}={}", name, value);
if let Some(domain) = options.domain {
cookie.push_str(&format!("; Domain={}", domain));
}
if let Some(path) = options.path {
cookie.push_str(&format!("; Path={}", path));
}
if let Some(max_age) = options.max_age {
cookie.push_str(&format!("; Max-Age={}", max_age));
}
if options.http_only {
cookie.push_str("; HttpOnly");
}
if options.secure {
cookie.push_str("; Secure");
}
if let Some(same_site) = options.same_site {
cookie.push_str(&format!("; SameSite={}", same_site));
}
cookie
}
pub fn strip_bearer_prefix(auth_header: &str) -> Option<String> {
auth_header
.strip_prefix("Bearer ")
.map(|token| token.trim().to_string())
}
pub fn extract_bearer_or_value(s: &str) -> String {
strip_bearer_prefix(s).unwrap_or_else(|| s.trim().to_string())
}
#[inline]
pub fn strip_bearer_or_passthrough(s: &str) -> String {
extract_bearer_or_value(s)
}
#[deprecated(since = "0.2.0", note = "use strip_bearer_prefix or extract_bearer_or_value")]
pub fn extract_bearer_token(auth_header: &str) -> Option<String> {
strip_bearer_prefix(auth_header)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_cookies() {
let cookies = parse_cookies("session=abc123; user=john; theme=dark");
assert_eq!(cookies.get("session"), Some(&"abc123".to_string()));
assert_eq!(cookies.get("user"), Some(&"john".to_string()));
assert_eq!(cookies.get("theme"), Some(&"dark".to_string()));
}
#[test]
fn test_parse_query_string() {
let params = parse_query_string("name=John%20Doe&age=30&city=New%20York");
assert_eq!(params.get("name"), Some(&"John Doe".to_string()));
assert_eq!(params.get("age"), Some(&"30".to_string()));
assert_eq!(params.get("city"), Some(&"New York".to_string()));
}
#[test]
fn test_build_cookie_string() {
use crate::context::SameSite;
let cookie = build_cookie_string("session", "abc123", CookieOptions {
domain: Some("example.com".to_string()),
path: Some("/".to_string()),
max_age: Some(3600),
http_only: true,
secure: true,
same_site: Some(SameSite::Strict),
});
assert!(cookie.contains("session=abc123"));
assert!(cookie.contains("Domain=example.com"));
assert!(cookie.contains("Path=/"));
assert!(cookie.contains("Max-Age=3600"));
assert!(cookie.contains("HttpOnly"));
assert!(cookie.contains("Secure"));
assert!(cookie.contains("SameSite=Strict"));
}
#[test]
fn test_strip_bearer_prefix() {
assert_eq!(
strip_bearer_prefix("Bearer abc123xyz"),
Some("abc123xyz".to_string())
);
assert_eq!(
strip_bearer_prefix("Bearer token_with_spaces "),
Some("token_with_spaces".to_string())
);
assert_eq!(strip_bearer_prefix("Basic xyz"), None);
assert_eq!(strip_bearer_prefix("Bearer"), None);
}
#[test]
fn test_extract_bearer_or_value() {
assert_eq!(extract_bearer_or_value("Bearer abc"), "abc");
assert_eq!(extract_bearer_or_value("raw-token"), "raw-token");
assert_eq!(extract_bearer_or_value(" x "), "x");
}
}