Remove AgentBuilder::set headers
The headers are not scoped by host, which means using them for something like `Authorization` would effectively "leak" to all requests using the agent. Context: https://github.com/algesten/ureq/issues/203#issuecomment-716385310
This commit is contained in:
29
src/agent.rs
29
src/agent.rs
@@ -1,6 +1,5 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::header::{self, Header};
|
|
||||||
use crate::pool::ConnectionPool;
|
use crate::pool::ConnectionPool;
|
||||||
use crate::proxy::Proxy;
|
use crate::proxy::Proxy;
|
||||||
use crate::request::Request;
|
use crate::request::Request;
|
||||||
@@ -12,7 +11,6 @@ use {crate::cookies::CookieTin, cookie::Cookie, cookie_store::CookieStore, url::
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct AgentBuilder {
|
pub struct AgentBuilder {
|
||||||
headers: Vec<Header>,
|
|
||||||
config: AgentConfig,
|
config: AgentConfig,
|
||||||
max_idle_connections: usize,
|
max_idle_connections: usize,
|
||||||
max_idle_connections_per_host: usize,
|
max_idle_connections_per_host: usize,
|
||||||
@@ -69,8 +67,6 @@ pub(crate) struct AgentConfig {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
pub(crate) config: Arc<AgentConfig>,
|
pub(crate) config: Arc<AgentConfig>,
|
||||||
/// Copied into each request of this agent.
|
|
||||||
pub(crate) headers: Vec<Header>,
|
|
||||||
/// Reused agent state for repeated requests from this agent.
|
/// Reused agent state for repeated requests from this agent.
|
||||||
pub(crate) state: Arc<AgentState>,
|
pub(crate) state: Arc<AgentState>,
|
||||||
}
|
}
|
||||||
@@ -160,7 +156,6 @@ const DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST: usize = 1;
|
|||||||
impl AgentBuilder {
|
impl AgentBuilder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
AgentBuilder {
|
AgentBuilder {
|
||||||
headers: vec![],
|
|
||||||
config: AgentConfig {
|
config: AgentConfig {
|
||||||
proxy: None,
|
proxy: None,
|
||||||
timeout_connect: Some(Duration::from_secs(30)),
|
timeout_connect: Some(Duration::from_secs(30)),
|
||||||
@@ -186,7 +181,6 @@ impl AgentBuilder {
|
|||||||
// built Agent.
|
// built Agent.
|
||||||
pub fn build(self) -> Agent {
|
pub fn build(self) -> Agent {
|
||||||
Agent {
|
Agent {
|
||||||
headers: self.headers,
|
|
||||||
config: Arc::new(self.config),
|
config: Arc::new(self.config),
|
||||||
state: Arc::new(AgentState {
|
state: Arc::new(AgentState {
|
||||||
pool: ConnectionPool::new_with_limits(
|
pool: ConnectionPool::new_with_limits(
|
||||||
@@ -202,29 +196,6 @@ impl AgentBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a header field that will be present in all requests using the agent.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// let agent = ureq::builder()
|
|
||||||
/// .set("X-API-Key", "foobar")
|
|
||||||
/// .set("Accept", "text/plain")
|
|
||||||
/// .build();
|
|
||||||
///
|
|
||||||
/// let r = agent
|
|
||||||
/// .get("/my-page")
|
|
||||||
/// .call();
|
|
||||||
///
|
|
||||||
/// if let Ok(resp) = r {
|
|
||||||
/// println!("yay got {}", resp.into_string().unwrap());
|
|
||||||
/// } else {
|
|
||||||
/// println!("Oh no error!");
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub fn set(mut self, header: &str, value: &str) -> Self {
|
|
||||||
header::add_header(&mut self.headers, Header::new(header, value));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set the proxy server to use for all connections from this Agent.
|
/// Set the proxy server to use for all connections from this Agent.
|
||||||
///
|
///
|
||||||
/// Example:
|
/// Example:
|
||||||
|
|||||||
@@ -56,12 +56,11 @@ impl fmt::Debug for Request {
|
|||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
pub(crate) fn new(agent: Agent, method: String, url: String) -> Request {
|
pub(crate) fn new(agent: Agent, method: String, url: String) -> Request {
|
||||||
let headers = agent.headers.clone();
|
|
||||||
Request {
|
Request {
|
||||||
agent,
|
agent,
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
headers,
|
headers: vec![],
|
||||||
return_error_for_status: true,
|
return_error_for_status: true,
|
||||||
query: QString::default(),
|
query: QString::default(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use crate::test;
|
|
||||||
use crate::test::testserver::{read_headers, TestServer};
|
use crate::test::testserver::{read_headers, TestServer};
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
@@ -8,29 +7,6 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use super::super::*;
|
use super::super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn agent_reuse_headers() {
|
|
||||||
let agent = builder().set("Authorization", "Foo 12345").build();
|
|
||||||
|
|
||||||
test::set_handler("/agent_reuse_headers", |unit| {
|
|
||||||
assert!(unit.has("Authorization"));
|
|
||||||
assert_eq!(unit.header("Authorization").unwrap(), "Foo 12345");
|
|
||||||
test::make_response(200, "OK", vec!["X-Call: 1"], vec![])
|
|
||||||
});
|
|
||||||
|
|
||||||
let resp = agent.get("test://host/agent_reuse_headers").call().unwrap();
|
|
||||||
assert_eq!(resp.header("X-Call").unwrap(), "1");
|
|
||||||
|
|
||||||
test::set_handler("/agent_reuse_headers", |unit| {
|
|
||||||
assert!(unit.has("Authorization"));
|
|
||||||
assert_eq!(unit.header("Authorization").unwrap(), "Foo 12345");
|
|
||||||
test::make_response(200, "OK", vec!["X-Call: 2"], vec![])
|
|
||||||
});
|
|
||||||
|
|
||||||
let resp = agent.get("test://host/agent_reuse_headers").call().unwrap();
|
|
||||||
assert_eq!(resp.header("X-Call").unwrap(), "2");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handler that answers with a simple HTTP response, and times
|
// Handler that answers with a simple HTTP response, and times
|
||||||
// out idle connections after 2 seconds.
|
// out idle connections after 2 seconds.
|
||||||
fn idle_timeout_handler(mut stream: TcpStream) -> io::Result<()> {
|
fn idle_timeout_handler(mut stream: TcpStream) -> io::Result<()> {
|
||||||
|
|||||||
Reference in New Issue
Block a user