Skip to content

Allow to specify server IP address #1067

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

Merged
merged 7 commits into from
Oct 16, 2021
Merged

Allow to specify server IP address #1067

merged 7 commits into from
Oct 16, 2021

Conversation

zhenyolka
Copy link
Contributor

cpp-httplib uses getaddrinfo to get server IP address to connect to. However sometimes this function might not work. For example when staticially linked with musl binary running on system. that don't have /etc/resolv.conf, android for example. Also plain DNS requests may be spoofed by ISP to block access to sites. Some Russian ISPs do that and with implementing by government National System of Domain Names this problem will grow. Also it affects privacy.
One of solution is to allow projects that use this library specify IP address of server when do HTTP/HTTPS request. So we can resolve IP over DoH/DoT or any other secure way and then pass it to cpp-httplib Client class constructor.
This pull request implements this feature.

@zhenyolka
Copy link
Contributor Author

@yhirose take a look. This PR for my issue

@yhirose
Copy link
Owner

yhirose commented Oct 8, 2021

@zhenyolka, thank you for the pull request. Could you add unit tests in test/test.cc as I always ask contributors to do so. When it comes to adding more constructors in client, I am not comfortable with them because this feature is rarely used and others users will easily get confused with the parameter ip. I would prefer adding set_ip_address(const std::string &ip) instead. Thanks!

@PixlRainbow, do you have any thought about this PR?

@PixlRainbow
Copy link
Contributor

PixlRainbow commented Oct 8, 2021

This is similar to CURL's --resolve example.com:443:198.18.110.10 parameter.
Perhaps instead of set_ip_address(const std::string &ip) to set a single IP, or instead of having a single parameter in the constructor to set ip, you could use set_hostname_addr_map(const std::map<std::string, std::string> addr_map) to provide multiple name resolution so that it can handle the case where one domain returns a HTTP 3xx redirect to a different domain with different IP.

@yhirose
Copy link
Owner

yhirose commented Oct 8, 2021

@PixlRainbow, thanks for the excellent suggestion!
@zhenyolka, set_hostname_addr_map(const std::map<std::string, std::string> addr_map) looks very easy for me to understand what it tries to do instead of adding new constructors with ip. Could you adjust your code accordingly? Thanks a lot!

@yhirose
Copy link
Owner

yhirose commented Oct 12, 2021

@zhenyolka, thanks for the update. Could you add a unit test in test/test.cc to verify your implementation?

@yhirose yhirose force-pushed the master branch 2 times, most recently from a9aceab to c384be0 Compare October 14, 2021 14:52
@zhenyolka
Copy link
Contributor Author

@yhirose done. Could you check?

@yhirose yhirose merged commit 4f8fcdb into yhirose:master Oct 16, 2021
@yhirose
Copy link
Owner

yhirose commented Oct 16, 2021

@zhenyolka, thanks for the excellent contribution!

@yhirose yhirose mentioned this pull request Oct 17, 2021
ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this pull request May 2, 2023
* Allow to specify server IP address

* Reimplement in set_hostname_addr_map

* Add tests for set_hostname_addr_map

* Fix tests after implement set_hostname_addr_map

* SpecifyServerIPAddressTest.RealHostname typo
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.

3 participants