refactor(maestro): share cookie name + extraction across CLI and registry (NAP-273 F)
ci / rust (push) Successful in 6m45s

Promote `COOKIE_NAME` and a robust `extract_cookie` (iterates every `;`-pair,
so it works on both `Cookie:` and `Set-Cookie:` headers regardless of position)
into `maestro-protocol`. The registry re-exports them; the CLI drops its weaker
`extract_cookie_value` (which only inspected the first pair) and its duplicate
`COOKIE_NAME`. Both ends of the wire now agree on one definition.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-18 14:35:30 +00:00
co-authored by Claude Opus 4.8
parent 71a45db0a4
commit 75d162dea3
3 changed files with 42 additions and 50 deletions
+2 -25
View File
@@ -5,6 +5,7 @@ use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use maestro_protocol::{extract_cookie, COOKIE_NAME};
use reqwest::header::{HeaderMap, HeaderValue, COOKIE, SET_COOKIE};
use reqwest::{Method, Response, StatusCode};
use serde::de::DeserializeOwned;
@@ -12,8 +13,6 @@ use serde::Serialize;
use crate::config::{CliConfig, ConfigError};
const COOKIE_NAME: &str = "maestro_session";
/// Time budget for establishing a TCP+TLS connection to the registry/daemon.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
/// Total time budget for a single request (connect + send + receive). Bounds
@@ -124,7 +123,7 @@ impl RegistryClient {
let Some(raw) = headers.get(SET_COOKIE).and_then(|h| h.to_str().ok()) else {
return Ok(());
};
let Some(val) = extract_cookie_value(raw, COOKIE_NAME) else {
let Some(val) = extract_cookie(raw, COOKIE_NAME) else {
return Ok(());
};
let mut guard = self.cookie.lock().unwrap();
@@ -178,32 +177,10 @@ async fn ok_or_body(resp: Response) -> Result<Response, HttpError> {
Err(HttpError::ServerError { status, body })
}
/// Extract `name=value` from a `Set-Cookie:` value. Ignores attributes.
pub fn extract_cookie_value<'a>(raw: &'a str, name: &str) -> Option<&'a str> {
let first = raw.split(';').next()?.trim();
let (k, v) = first.split_once('=')?;
(k.trim() == name).then(|| v.trim())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_named_cookie_value() {
let raw = "maestro_session=abc.def.ghi; HttpOnly; SameSite=Strict; Path=/; Max-Age=2592000";
assert_eq!(
extract_cookie_value(raw, "maestro_session"),
Some("abc.def.ghi")
);
}
#[test]
fn returns_none_for_other_cookie_name() {
let raw = "other=1; HttpOnly";
assert_eq!(extract_cookie_value(raw, "maestro_session"), None);
}
/// A peer that accepts the TCP connection but never sends a response must
/// not hang the client forever: the request timeout has to fire (NAP-262).
#[tokio::test]