lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.76k stars 979 forks source link

Feature Request: Add an additional method on Response that allows you to see the resolved IP of the Response #1186

Open BobbyMcWho opened 4 years ago

BobbyMcWho commented 4 years ago

I've done some fairly comprehensive digging into the source code of this gem, typhoeus, and net::http, and I believe this should be something we can implement.

I would like to be able to access the resolved IP of the request on the response body, Net::HTTP I believe exposes this via #ipaddr, and Typhoeus via Response::Informations#primary_ip.

I believe this would involve adding the method to the Faraday::Response class similar to body, adding an ip attr to the Env class, and in each adapter changing the save_response call to also add the ip info to the env.

iMacTia commented 4 years ago

@BobbyMcWho sounds useful and I don't think this is currently available in Faraday. I checked the PRs history and I found a similar addition done for reason_phrase: https://github.com/lostisland/faraday/pull/547. I'm not a huge fan of the implementation, but I doubt there's an easier/better way to implement this, so it could be used as a reference for implementing this as well.

Just a list of "requirements":

  1. The change should be fully backwards-compatible (careful about Options definition, as the order of fields does matter)
  2. Appreciate this is a pain, but the implementation should be attempted for ALL adapters, not just Net::HTTP and Typhoeus. If an adapter is skipped it should be because it does not allow to fetch that info at all.
  3. I don't have strong feelings for the name of the new field, but I noticed Net::HTTP and Typhoeus already call it in a different way. It would be nice to find a technical reference for it and use that
BobbyMcWho commented 4 years ago

@iMacTia how do you feel about peer_ip ?

iMacTia commented 4 years ago

@BobbyMcWho that would work, I was thinking also of resolved_ip or some other reference to DNS, but as I mentioned I don't feel too strongly as far as it's a recognised technical term

BobbyMcWho commented 4 years ago

Yeah I started digging into this last night and there doesn't seem to be broad support for it in the http libraries, even Net::Http is a bit more of a challenge than I expected. I'll have to do some more digging when I have some time

raldred commented 1 year ago

I know this request is a bit stale, but knowing the host ip address would be super useful for us, to trace connections. NET::HTTP provides ipaddr which will return the underlying sockets peer address as already mentioned https://ruby-doc.org/stdlib-3.0.0/libdoc/net/http/rdoc/Net/HTTP.html#method-i-ipaddr

BobbyMcWho commented 1 year ago

I unfortunately don't have time to work on this, so if you want to pick up the mantle, I'm sure others will want it later as well 😅