httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
33.75k stars 3.68k forks source link

Conditional host resolving #422

Closed RafPe closed 3 years ago

RafPe commented 8 years ago

Hey ,

First of all really great tool. Cannot imagine work without now :) What I would be looking for is what we have in cURL which allows resolve host name on the fly when making request

curl --resolve example.com:443:1.2.3.4 https://example.com

Is it already available and I'm missing something or could that be a feature request ?

kfix commented 8 years ago

I do this with requests using this evil adapter I made:

from requests.adapters import HTTPAdapter
from types import MethodType
# http://docs.python-requests.org/en/master/_modules/requests/adapters/
class TransparentProxyAdapter(HTTPAdapter):
    # opens sockets to an arbitrary fixhost instead of deriving it from the URL's Host
    def __init__(self, tproxy, **kwargs):
       self.tproxy = tproxy
       self.timeout = 5
       super(TransparentProxyAdapter,self).__init__(**kwargs)

    def get_connection(self, url, proxies=None):
       # monkeypatch the connection pool's _new_conn()
       conn = super(TransparentProxyAdapter,self).get_connection(url, proxies)
       conn.fixhost = self.tproxy
       conn._new_conn = MethodType(self.monkeypatched_new_conn.__func__, conn, conn.__class__)
       return conn

    # https://github.com/shazow/urllib3/blob/1.16/urllib3/connectionpool.py#L208
    def monkeypatched_new_conn(self):
       # https://hg.python.org/cpython/file/2.7/Lib/httplib.py
       def monkeypatched_connect(self):
           orighost = self.host
           self.host = self.fixhost #switcheroo
           self.orig_connect()
           self.host = orighost #put it back

       self.num_connections += 1
       # monkeypatch the connection's connect()
       if self.fixhost != None and self.proxy == None:
          conn = self.ConnectionCls(self.host, self.port, timeout=self.timeout)
          conn.fixhost = self.fixhost
          conn.orig_connect = conn.connect
          conn.connect = MethodType(monkeypatched_connect, conn, conn.__class__)
       return conn

unfortunately urllib3 has no hooking to perform resolution statically and ignoring socket.getaddrinfo, which would be more comparable to how --resolve works with libcurl.

gabetax commented 7 years ago

Duplicate of #99