Skip to content

Commit bafcd7a

Browse files
committed
add a referer() option to disable setting Referer header on redirects
1 parent 57359a6 commit bafcd7a

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/client.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl Client {
6060
inner: Arc::new(ClientRef {
6161
hyper: RwLock::new(client),
6262
redirect_policy: Mutex::new(RedirectPolicy::default()),
63+
auto_referer: AtomicBool::new(true),
6364
auto_ungzip: AtomicBool::new(true),
6465
}),
6566
})
@@ -75,6 +76,13 @@ impl Client {
7576
*self.inner.redirect_policy.lock().unwrap() = policy;
7677
}
7778

79+
/// Enable or disable automatic setting of the `Referer` header.
80+
///
81+
/// Default is `true`.
82+
pub fn referer(&mut self, enable: bool) {
83+
self.inner.auto_referer.store(enable, Ordering::Relaxed);
84+
}
85+
7886
/// Set a timeout for both the read and write operations of a client.
7987
pub fn timeout(&mut self, timeout: Duration) {
8088
let mut client = self.inner.hyper.write().unwrap();
@@ -134,6 +142,7 @@ impl fmt::Debug for Client {
134142
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135143
f.debug_struct("Client")
136144
.field("redirect_policy", &self.inner.redirect_policy)
145+
.field("referer", &self.inner.auto_referer)
137146
.field("auto_ungzip", &self.inner.auto_ungzip)
138147
.finish()
139148
}
@@ -142,6 +151,7 @@ impl fmt::Debug for Client {
142151
struct ClientRef {
143152
hyper: RwLock<::hyper::Client>,
144153
redirect_policy: Mutex<RedirectPolicy>,
154+
auto_referer: AtomicBool,
145155
auto_ungzip: AtomicBool,
146156
}
147157

@@ -328,7 +338,9 @@ impl RequestBuilder {
328338

329339
url = match loc {
330340
Ok(loc) => {
331-
headers.set(Referer(url.to_string()));
341+
if client.auto_referer.load(Ordering::Relaxed) {
342+
headers.set(Referer(url.to_string()));
343+
}
332344
urls.push(url);
333345
let action = check_redirect(&client.redirect_policy.lock().unwrap(), &loc, &urls);
334346
match action {

tests/client.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,47 @@ fn test_redirect_policy_can_stop_redirects_without_an_error() {
242242
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dont".to_string())));
243243
}
244244

245+
#[test]
246+
fn test_referer_is_not_set_if_disabled() {
247+
let server = server! {
248+
request: b"\
249+
GET /no-refer HTTP/1.1\r\n\
250+
Host: $HOST\r\n\
251+
User-Agent: $USERAGENT\r\n\
252+
Accept: */*\r\n\
253+
Accept-Encoding: gzip\r\n\
254+
\r\n\
255+
",
256+
response: b"\
257+
HTTP/1.1 302 Found\r\n\
258+
Server: test-no-referer\r\n\
259+
Content-Length: 0\r\n\
260+
Location: /dst\r\n\
261+
Connection: close\r\n\
262+
\r\n\
263+
",
264+
265+
request: b"\
266+
GET /dst HTTP/1.1\r\n\
267+
Host: $HOST\r\n\
268+
User-Agent: $USERAGENT\r\n\
269+
Accept: */*\r\n\
270+
Accept-Encoding: gzip\r\n\
271+
\r\n\
272+
",
273+
response: b"\
274+
HTTP/1.1 200 OK\r\n\
275+
Server: test-dst\r\n\
276+
Content-Length: 0\r\n\
277+
\r\n\
278+
"
279+
};
280+
let mut client = reqwest::Client::new().unwrap();
281+
client.referer(false);
282+
client.get(&format!("http://{}/no-refer", server.addr()))
283+
.send().unwrap();
284+
}
285+
245286
#[test]
246287
fn test_accept_header_is_not_changed_if_set() {
247288
let server = server! {

0 commit comments

Comments
 (0)