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

Alternative for cURL --resolve flag #317

Closed
JoyceBabu opened this issue Jun 26, 2023 · 14 comments · Fixed by #327
Closed

Alternative for cURL --resolve flag #317

JoyceBabu opened this issue Jun 26, 2023 · 14 comments · Fixed by #327
Assignees
Labels
good first issue Good for newcomers

Comments

@JoyceBabu
Copy link

Is there an alternative for the --resolve flag of cURL? Setting the Host header alone does not work when the target server is using SNI. In httpie there is a feature request for this, but is pending on an upstream PR.

httpie/cli#99

@ducaale
Copy link
Owner

ducaale commented Jun 26, 2023

This is something we can easily support by making use of reqwest's ClientBuilder::resolve() method.

Would you be interested in opening a PR for adding this feature?

@ducaale ducaale added the good first issue Good for newcomers label Jun 26, 2023
@JoyceBabu
Copy link
Author

JoyceBabu commented Jun 26, 2023

Sorry, I do not have the enough experience with Rust to undertake something like this.

@iammatthi
Copy link

ClientBuilder::resolve() seems to not consider ports.

From reqwest docs:

Since the DNS protocol has no notion of ports, if you wish to send traffic to a particular port you must include this port in the URL itself, any port in the overridden addr will be ignored and traffic sent to the conventional port for the given scheme (e.g. 80 for http).

So would it be okay to use this flag with the --resolve <HOST:ADDRESS> syntax (e.g.: xh example.com --resolve example.com:127.0.0.1)?

@ducaale
Copy link
Owner

ducaale commented Jul 8, 2023

Hmm, that's unfortunate.

I prefer us to stay compatible with cURL's HOST:PORT:ADDRESS format if possible. So if a user passes --resolve example.com:443:127.0.0.1, we rewrite it to resolve("example.com", "127.0.0.1:443".parse()?).

@JoyceBabu
Copy link
Author

In the --resolve option, the ip address given in the substitution (resolved) ip address, not the DNS resolver ip address. So, --resolve example.com:443:127.0.0.1 requests curl to use the ip address 127.0.0.1 for any connection to https://example.com. Since the IP address is already mentioned, there is no need to do any further DNS resolution.

For example, if we have a web page at http://example.org that redirects to https://example.com/?sid=12345, and you want to test it locally you can use

curl -L --resolve example.org:80:127.0.0.1  --resolve example.com:443:127.0.0.1 http://example.org

@iammatthi
Copy link

I've tried to use the resolve() method, to resolve http://example.com:3000 to 127.0.0.1. By harcoding it I've found that it resolves it correctly with:
client.resolve("example.com", "127.0.0.1:80".parse().unwrap())
but not with:
client.resolve("example.com:3000", "127.0.0.1:80".parse().unwrap()).
In fact I think that the default resolver from reqwest works only with a simple domain name (without port).

An option seems to be to develop a custom resolver by using the dns_resolver method.

@iammatthi
Copy link

iammatthi commented Jul 10, 2023

I've tried to use the resolve() method, to resolve http://example.com:3000 to 127.0.0.1. By harcoding it I've found that it resolves it correctly with:
client.resolve("example.com", "127.0.0.1:80".parse().unwrap())
but not with:
client.resolve("example.com:3000", "127.0.0.1:80".parse().unwrap()).
In fact I think that the default resolver from reqwest works only with a simple domain name (without port).

An option seems to be to develop a custom resolver by using the dns_resolver method.

I meant that I wanted to resolve http://example.com:3000 as 127.0.0.1:3000.

@ducaale
Copy link
Owner

ducaale commented Jul 15, 2023

@iammatthi After rereading your message and the docs, I'm realising that the port number in SocketAddr is always ignored by reqwest.

An option seems to be to develop a custom resolver by using the dns_resolver method.

The resolver passed to ClientBuilder::dns_resolver() doesn't receive a port number, so want can't make it reliably work for redirected requests. Although interestingly enough, changing the port would lead to resolve being called again.

@JoyceBabu How useful do you find a --resolve flag that doesn't take a port number?

xh --resolve=example.org:127.0.0.1 --resolve=example.com:127.0.0.1 http://example.org

@JoyceBabu
Copy link
Author

I have never had a situation where I had to resolve the hostname to different ip address based on the port number. But I think it would be better to follow the syntax of curl, even if the port number is silently ignored. This allows compatibility with curl, and at the same time makes it possible to implement this properly, if there ever is a need for it.

@ducaale
Copy link
Owner

ducaale commented Aug 5, 2023

Sounds good.

In that case, let's support both HOST:ADDRESS and HOST:PORT:ADDRESS. And to avoid any potential confusion, we can warn users that we're ignoring the port number in the latter syntax.

@blyxxyz
Copy link
Collaborator

blyxxyz commented Aug 7, 2023

You can't usually plug curl flags into xh/HTTPie anyway, so I don't think that kind of compatibility has a lot of upside.

Maybe if we detect the curl syntax we can suggest the port-less syntax in the error message?

If we support it now with broken behavior then implementing it properly later would technically be a compatibility break.

@lapo-luchini
Copy link

Please implement in any form, so that feature can be used. 😇

@rivasdiaz
Copy link

Can I humbly request a release now that this issue is merged?
I need to use this functionality very often, so I'm eager to try it with xh.

Thanks!

@ducaale
Copy link
Owner

ducaale commented Nov 15, 2023

@rivasdiaz the plan is to cut a new release this weekend. In the meantime, you can build xh from source and test the feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants