Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple set-cookie headers #51

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion linkup-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "linkup-cli"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[[bin]]
Expand Down
55 changes: 53 additions & 2 deletions linkup/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum HeaderName {
Referer,
Origin,
Host,
SetCookie,
}

impl From<HeaderName> for UniCase<String> {
Expand All @@ -25,6 +26,7 @@ impl From<HeaderName> for UniCase<String> {
HeaderName::Referer => "referer".into(),
HeaderName::Origin => "origin".into(),
HeaderName::Host => "host".into(),
HeaderName::SetCookie => "set-cookie".into(),
}
}
}
Expand Down Expand Up @@ -73,7 +75,14 @@ impl HeaderMap {
key: impl Into<UniCase<String>>,
value: impl ToString,
) -> Option<String> {
self.0.insert(key.into(), value.to_string())
let unicase_key = key.into();
if unicase_key == HeaderName::SetCookie.into() && self.0.contains_key(&unicase_key) {
let cookies = self.0.get(&unicase_key).unwrap();
let append_cookie = format!("{}, {}", cookies, value.to_string());
return self.0.insert(unicase_key, append_cookie);
}

self.0.insert(unicase_key, value.to_string())
}

pub fn extend(&mut self, iter: &HeaderMap) {
Expand Down Expand Up @@ -102,6 +111,16 @@ impl From<HeaderMap> for reqwest::header::HeaderMap {

for (key, value) in value.into_iter() {
if let Ok(header_name) = reqwest::header::HeaderName::from_bytes(key.as_bytes()) {
if UniCase::new(&header_name) == HeaderName::SetCookie.into() {
let cookies = value.split(", ").collect::<Vec<&str>>();
for cookie in cookies {
if let Ok(header_value) = reqwest::header::HeaderValue::from_str(cookie) {
header_map.append(header_name.clone(), header_value);
}
}
continue;
}

if let Ok(header_value) = reqwest::header::HeaderValue::from_str(&value) {
header_map.insert(header_name, header_value);
}
Expand Down Expand Up @@ -165,8 +184,10 @@ impl<'a>
}

#[cfg(test)]
#[cfg(feature = "reqwest")]
mod test {
use crate::HeaderMap;
use super::*;
use reqwest::header::{HeaderMap as ReqwestHeaderMap, HeaderValue, SET_COOKIE};

#[test]
fn get_case_insensitive() {
Expand All @@ -180,4 +201,34 @@ mod test {
assert_eq!(header_map.get("key"), Some("value_2"));
assert_eq!(header_map.get("KEY"), Some("value_2"));
}

#[test]
fn add_folded_cookie_headers() {
let mut header_map = HeaderMap::new();

// Cloudflare Workers-rs folds set-cookie headers into a single header
header_map.insert("set-cookie".to_string(), "cookie1=value1, cookie2=value2");

let reqwest_header_map: ReqwestHeaderMap = header_map.into();
let cookies: Vec<&HeaderValue> = reqwest_header_map.get_all(SET_COOKIE).iter().collect();

assert_eq!(reqwest_header_map.len(), 2);
assert!(cookies.contains(&&HeaderValue::from_static("cookie1=value1")));
assert!(cookies.contains(&&HeaderValue::from_static("cookie2=value2")));
}

#[test]
fn add_multi_cookie_headers() {
let mut header_map = HeaderMap::new();

header_map.insert("set-cookie".to_string(), "cookie1=value1");
header_map.insert("set-cookie".to_string(), "cookie2=value2");

let reqwest_header_map: ReqwestHeaderMap = header_map.into();

assert_eq!(reqwest_header_map.len(), 2);
let cookies: Vec<&HeaderValue> = reqwest_header_map.get_all(SET_COOKIE).iter().collect();
assert!(cookies.contains(&&HeaderValue::from_static("cookie1=value1")));
assert!(cookies.contains(&&HeaderValue::from_static("cookie2=value2")));
}
augustoccesar marked this conversation as resolved.
Show resolved Hide resolved
}