Skip to content

Commit 4527475

Browse files
authored
fix: set user-agent header in runtime transport (#9434)
1 parent af0fee2 commit 4527475

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

crates/cli/src/utils/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ pub const STATIC_FUZZ_SEED: [u8; 32] = [
3939
0x5d, 0x64, 0x0b, 0x19, 0xad, 0xf0, 0xe3, 0x57, 0xb8, 0xd4, 0xbe, 0x7d, 0x49, 0xee, 0x70, 0xe6,
4040
];
4141

42-
const DEFAULT_USER_AGENT: &str = concat!("foundry/", env!("CARGO_PKG_VERSION"));
43-
4442
/// Useful extensions to [`std::path::Path`].
4543
pub trait FoundryPathExt {
4644
/// Returns true if the [`Path`] ends with `.t.sol`
@@ -112,11 +110,7 @@ pub fn get_provider_builder(config: &Config) -> Result<ProviderBuilder> {
112110
builder = builder.timeout(Duration::from_secs(rpc_timeout));
113111
}
114112

115-
if let Some(mut rpc_headers) = config.eth_rpc_headers.clone() {
116-
if !rpc_headers.iter().any(|h| h.starts_with("User-Agent:")) {
117-
rpc_headers.push(format!("User-Agent:{DEFAULT_USER_AGENT}"));
118-
}
119-
113+
if let Some(rpc_headers) = config.eth_rpc_headers.clone() {
120114
builder = builder.headers(rpc_headers);
121115
}
122116

crates/common/src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub const OPTIMISM_SYSTEM_ADDRESS: Address = address!("deaddeaddeaddeaddeaddeadd
4040
/// Transaction identifier of System transaction types
4141
pub const SYSTEM_TRANSACTION_TYPE: u8 = 126;
4242

43+
/// Default user agent set as the header for requests that don't specify one.
44+
pub const DEFAULT_USER_AGENT: &str = concat!("foundry/", env!("CARGO_PKG_VERSION"));
45+
4346
/// Returns whether the sender is a known L2 system sender that is the first tx in every block.
4447
///
4548
/// Transactions from these senders usually don't have a any fee information.

crates/common/src/provider/runtime_transport.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Runtime transport that connects on first request, which can take either of an HTTP,
22
//! WebSocket, or IPC transport and supports retries based on CUPS logic.
33
4-
use crate::REQUEST_TIMEOUT;
4+
use crate::{DEFAULT_USER_AGENT, REQUEST_TIMEOUT};
55
use alloy_json_rpc::{RequestPacket, ResponsePacket};
66
use alloy_pubsub::{PubSubConnect, PubSubFrontend};
77
use alloy_rpc_types::engine::{Claims, JwtSecret};
@@ -176,6 +176,14 @@ impl RuntimeTransport {
176176
);
177177
}
178178

179+
if !headers.iter().any(|(k, _v)| k.as_str().starts_with("User-Agent:")) {
180+
headers.insert(
181+
reqwest::header::USER_AGENT,
182+
HeaderValue::from_str(DEFAULT_USER_AGENT)
183+
.expect("User-Agent should be valid string"),
184+
);
185+
}
186+
179187
client_builder = client_builder.default_headers(headers);
180188

181189
let client =

0 commit comments

Comments
 (0)