jnunemaker / jnunemaker/httparty
local_port and local_host
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 5.9k
- Forks
- 973
- Avg merge
- 1h 59m
- Merged PRs (30d)
- 1
Description
After finding this question, I found that whenever a local_port is specified, it will automatically assume that we try to bind to localhost, as per getaddrinfo(3):
If the AI_PASSIVE flag is not set in hints.ai_flags, then the returned socket
addresses will be suitable for use with connect(2), sendto(2), or sendmsg(2).
If node is NULL, then the network address will be set to the loopback inter‐
face address (INADDR_LOOPBACK for IPv4 addresses, IN6ADDR_LOOPBACK_INIT for
IPv6 address); this is used by applications that intend to communicate with
peers running on the same host.
However, binding on a loopback address makes the network unreachable.
For the low level ruby API this is desirable as it may be needed, for Net::HTTP it may still be used this way, but for a higher level library such as HTTParty, this behavior seems inconsistent and illogical, as it basically breaks the expected behavior:
HTTParty.get 'http://google.com', local_port: 60000
# Errno::ENETUNREACH: Failed to open TCP connection to google.com:80 (Network is unreachable - connect(2) for "google.com" port 80)
Specifying the local_host to localhost or 127.0.0.1 produces two different kind of errors if you try to access and external service:
HTTParty.get 'http://google.com', local_host: 'localhost'
# Errno::ENETUNREACH: Failed to open TCP connection to google.com:80 (Network is unreachable - connect(2) for "google.com" port 80)
HTTParty.get 'http://google.com', local_host: '127.0.0.1'
# Errno::EINVAL: Failed to open TCP connection to google.com:80 (Invalid argument - connect(2) for "google.com" port 80)
# but both works if you indeed try to access a local service
HTTParty.get 'http://localhost:3000', local_host: '127.0.0.1'
As such, would it be possible to document that in the comments where we can find local_port, and maybe prevent setting local_port with local_host nil?
This way we can see somewhere that setting local_port requires a local_host too, and also that specifying 127.0.0.1 for the local_host prevents any connection to an external service.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the comments and handling for the local_port and local_host options, then read how these values are passed to the connection setup. Document that local_port requires an appropriate local_host and that loopback addresses prevent connections to external services; any validation should match the existing option behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100