Skip to content
Open
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
5 changes: 2 additions & 3 deletions src/cortex-cli/src/agent_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#[cfg(test)]
mod tests {
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
use crate::agent_cmd::loader::{
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
};
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
use crate::agent_cmd::types::AgentMode;
use crate::utils::file::read_file_with_encoding;

#[test]
fn test_read_file_with_utf8() {
Expand Down
61 changes: 48 additions & 13 deletions src/cortex-cli/src/scrape_cmd/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::io::Write;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result, bail};
Expand Down Expand Up @@ -137,6 +138,7 @@ impl ScrapeCommand {
protocol
);
}
let initial_url = reqwest::Url::parse(&self.url).context("Invalid URL")?;

// Parse output format
let format: OutputFormat = self.format.parse()?;
Expand All @@ -149,9 +151,10 @@ impl ScrapeCommand {
} else {
reqwest::redirect::Policy::limited(10)
};
let cookie_jar = build_cookie_jar(&self.cookies, &initial_url);
let mut client_builder = create_client_builder()
.redirect(redirect_policy)
.cookie_store(true);
.cookie_provider(Arc::clone(&cookie_jar));

// Override timeout if specified (0 means no timeout)
// Apply timeout to both connection phase and overall request
Expand All @@ -175,13 +178,6 @@ impl ScrapeCommand {
// Add custom headers
let parsed_headers = parse_headers(&self.headers)?;

// Build cookie header from --cookie flags
let cookie_header = if !self.cookies.is_empty() {
Some(self.cookies.join("; "))
} else {
None
};

if self.verbose {
eprintln!("Fetching: {} (method: {})", self.url, method_upper);
}
Expand All @@ -202,11 +198,6 @@ impl ScrapeCommand {
request = request.header(name.as_str(), value.as_str());
}

// Add cookies if specified
if let Some(ref cookies) = cookie_header {
request = request.header("Cookie", cookies.as_str());
}

let response = request.send().await.context("Failed to fetch URL")?;

// For HEAD requests, just show headers and return
Expand Down Expand Up @@ -373,3 +364,47 @@ impl ScrapeCommand {
Ok(output)
}
}

fn build_cookie_jar(cookies: &[String], initial_url: &reqwest::Url) -> Arc<reqwest::cookie::Jar> {
let jar = Arc::new(reqwest::cookie::Jar::default());

for cookie in cookies {
for part in cookie
.split(';')
.map(str::trim)
.filter(|part| !part.is_empty())
{
jar.add_cookie_str(part, initial_url);
}
}

jar
}

#[cfg(test)]
mod tests {
use reqwest::cookie::CookieStore;
use reqwest::header::HeaderValue;

use super::*;

#[test]
fn test_cli_cookies_merge_with_redirect_set_cookies() {
let initial_url = reqwest::Url::parse("http://example.test/redirect").unwrap();
let final_url = reqwest::Url::parse("http://example.test/final").unwrap();
let jar = build_cookie_jar(&["my_token=abc".to_string()], &initial_url);

let set_cookie = [HeaderValue::from_static("server_session=xyz; Path=/")];
jar.set_cookies(&mut set_cookie.iter(), &initial_url);

let cookie_header = jar
.cookies(&final_url)
.expect("merged cookies should be sent to same-origin redirect target")
.to_str()
.unwrap()
.to_string();

assert!(cookie_header.contains("my_token=abc"));
assert!(cookie_header.contains("server_session=xyz"));
}
}