mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 06:39:22 +02:00
add local_address bind for client builder (#2024)
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
### Added
|
||||
* `ClientResponse::timeout` for set the timeout of collecting response body. [#1931]
|
||||
* `ClientBuilder::local_address` for bind to a local ip address for this client. [#2024]
|
||||
|
||||
### Changed
|
||||
* Feature `cookies` is now optional and enabled by default. [#1981]
|
||||
@ -15,6 +16,7 @@
|
||||
[#1931]: https://github.com/actix/actix-web/pull/1931
|
||||
[#1981]: https://github.com/actix/actix-web/pull/1981
|
||||
[#2008]: https://github.com/actix/actix-web/pull/2008
|
||||
[#2024]: https://github.com/actix/actix-web/pull/2024
|
||||
|
||||
## 3.0.0-beta.2 - 2021-02-10
|
||||
### Added
|
||||
|
@ -47,7 +47,7 @@ trust-dns = ["actix-http/trust-dns"]
|
||||
actix-codec = "0.4.0-beta.1"
|
||||
actix-service = "2.0.0-beta.4"
|
||||
actix-http = "3.0.0-beta.3"
|
||||
actix-rt = "2"
|
||||
actix-rt = "2.1"
|
||||
|
||||
base64 = "0.13"
|
||||
bytes = "1"
|
||||
|
@ -1,5 +1,6 @@
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::net::IpAddr;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -25,6 +26,7 @@ pub struct ClientBuilder<T = (), U = ()> {
|
||||
conn_window_size: Option<u32>,
|
||||
headers: HeaderMap,
|
||||
timeout: Option<Duration>,
|
||||
local_address: Option<IpAddr>,
|
||||
connector: Connector<T, U>,
|
||||
}
|
||||
|
||||
@ -42,6 +44,7 @@ impl ClientBuilder {
|
||||
default_headers: true,
|
||||
headers: HeaderMap::new(),
|
||||
timeout: Some(Duration::from_secs(5)),
|
||||
local_address: None,
|
||||
connector: Connector::new(),
|
||||
max_http_version: None,
|
||||
stream_window_size: None,
|
||||
@ -72,6 +75,7 @@ where
|
||||
default_headers: self.default_headers,
|
||||
headers: self.headers,
|
||||
timeout: self.timeout,
|
||||
local_address: None,
|
||||
connector,
|
||||
max_http_version: self.max_http_version,
|
||||
stream_window_size: self.stream_window_size,
|
||||
@ -94,6 +98,12 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Set local IP Address the connector would use for establishing connection.
|
||||
pub fn local_address(mut self, addr: IpAddr) -> Self {
|
||||
self.local_address = Some(addr);
|
||||
self
|
||||
}
|
||||
|
||||
/// Maximum supported HTTP major version.
|
||||
///
|
||||
/// Supported versions are HTTP/1.1 and HTTP/2.
|
||||
@ -184,6 +194,9 @@ where
|
||||
if let Some(val) = self.stream_window_size {
|
||||
connector = connector.initial_window_size(val)
|
||||
};
|
||||
if let Some(val) = self.local_address {
|
||||
connector = connector.local_address(val);
|
||||
}
|
||||
|
||||
let config = ClientConfig {
|
||||
headers: self.headers,
|
||||
|
@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
@ -871,3 +872,34 @@ async fn client_bearer_auth() {
|
||||
let response = request.send().await.unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_local_address() {
|
||||
let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
|
||||
|
||||
let srv = test::start(move || {
|
||||
App::new().service(web::resource("/").route(web::to(
|
||||
move |req: HttpRequest| async move {
|
||||
assert_eq!(req.peer_addr().unwrap().ip(), ip);
|
||||
Ok::<_, Error>(HttpResponse::Ok())
|
||||
},
|
||||
)))
|
||||
});
|
||||
let client = awc::Client::builder().local_address(ip).finish();
|
||||
|
||||
let res = client.get(srv.url("/")).send().await.unwrap();
|
||||
|
||||
assert_eq!(res.status(), 200);
|
||||
|
||||
let client = awc::Client::builder()
|
||||
.connector(
|
||||
// connector local address setting should always be override by client builder.
|
||||
awc::Connector::new().local_address(IpAddr::V4(Ipv4Addr::new(128, 0, 0, 1))),
|
||||
)
|
||||
.local_address(ip)
|
||||
.finish();
|
||||
|
||||
let res = client.get(srv.url("/")).send().await.unwrap();
|
||||
|
||||
assert_eq!(res.status(), 200);
|
||||
}
|
||||
|
Reference in New Issue
Block a user