Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClientBuilder::local_address option to bind to a local IP address #451

Merged
merged 4 commits into from
Feb 11, 2019

Conversation

itsHabib
Copy link
Contributor

@itsHabib itsHabib commented Feb 1, 2019

Resolves #414

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome start! Left comments inline.

CHANGELOG.md Outdated
@@ -5,6 +5,7 @@
- Add `ClientBuilder::h2_prior_knowledge()` option to force HTTP2.
- Add `Response::content_length()` to get the content-length of a response.
- Enable ALPN h2 with the rustls-tls backend.
- Add `ClientBuilder::local_address` option to bind to a local IP address
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about changelog entries, I fill this in when tagging a release :)

@@ -325,6 +330,12 @@ impl ClientBuilder {
pub fn dns_threads(self, _threads: usize) -> ClientBuilder {
self
}

/// Bind to a local IP Address
pub fn local_address(mut self, addr: Option<IpAddr>) -> ClientBuilder {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider an argument of addr: impl Into<Option<IpAddr>>. This way, in the common case, it reads more fluently, like builder.local_address(ip) instead of builder.local_address(Some(ip)).

src/client.rs Outdated
/// .local_address(Some(local_addr.into()))
/// .build().unwrap();
/// ```
pub fn local_address(self, addr: Option<IpAddr>) -> ClientBuilder {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here about impl Into<Option<IpAddr>>.

src/client.rs Outdated
///
/// ```
/// use std::net::Ipv4Addr;
/// let local_addr = Ipv4Addr::new(12, 4, 1, 8);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this works too: IpAddr::from([12, 4, 1, 8])

src/connect.rs Outdated

pub(crate) fn local_address(&self, _addr: Option<IpAddr>) {
match self.inner {
#[cfg(not(feature = "tls"))]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to support even when TLS is enabled, we'll probably need to pass the option during construction instead.

let tls = try_!(tls.build());

let mut http = http_connector()?;
http.set_local_address(local_addr.into());
Copy link
Contributor Author

@itsHabib itsHabib Feb 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanmonstar Why is this the only place I had to use into()? The error was something along the lines of expecting std::option::Option, got type parameter when not using into()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually looks like some of the builds failed with that same error. I will try adding into() to the rest.

@itsHabib itsHabib force-pushed the itshabib-client-local-addr branch from cbdf107 to e5fc9db Compare February 4, 2019 16:25
@itsHabib
Copy link
Contributor Author

itsHabib commented Feb 5, 2019

Ready for another look whenever you are available!

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woot! Thanks for putting this together!

(And excuse the wait, I was offline all last week.)

@seanmonstar seanmonstar merged commit 4dc679d into seanmonstar:master Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants