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

Allow http commands' automatic redirect-following to be disabled #11329

Merged
merged 10 commits into from
Dec 28, 2023

Conversation

cyradotpink
Copy link
Contributor

Intends to close #8920

This PR suggests a new flag for the http commands, --redirect-mode, which enables users to choose between different redirect handling modes. The current behaviour of letting ureq silently follow redirects remains the default, but two new options are introduced here, following the lead of JavaScript's fetch API: "manual", where any 3xx response to a request is simply returned as the command's result, and "error", where any 3xx response causes a network error like those caused by 4xx and 5xx responses.

This PR is a draft. Tests have not been added or run, the flag is currently only implemented for the http get command, and design tweaks are likely to be appropriate.

Most notably, it's not obvious to me whether a single flag which can take one of three values is the nicest solution here.
We might instead consider two binary flags (like --no-following-redirects and --disallow-redirects, although I'm bad at naming things so I need help with that anyway), or completely drop the "error" option if it's not deemed useful enough. (I personally think it has some merit, especially since 4xx and 5xx responses are already treated as errors by default; So this would allow users to treat only immediate 2xx responses as success)

User-facing changes

New options for the http [method] commands. Behaviour remains unchanged when the command line flag introduced here is not used.

image

@cyradotpink cyradotpink changed the title Implement new redirect handling options for http get Allow http commands' automatic redirect-following to be disabled Dec 14, 2023
@sholderbach sholderbach added the networking All about our `http` and `url` commands and everything going accross the network. label Dec 14, 2023
@WindSoilder
Copy link
Collaborator

Thanks for your pr!

Actually I think it's good to have one --redirect-mode flag rather than having --no-following-redirects, --disallow-redirects flags. One reason is that it's hard to naming things, the other reason is we already have many flags on http relative commands.

@cyradotpink
Copy link
Contributor Author

cyradotpink commented Dec 15, 2023

That's a good point. Actually, on that note, as far as I can tell all the http commands' existing flags have shorthands, which makes it difficult to find an appropriate one for this new flag that is still free. Anyone have any thoughts on that? Like, whether we even want a shorthand for this flag?

And in general, any additional feedback on design and the names I chose would be appreciated before I get this PR ready for review :)

@WindSoilder
Copy link
Collaborator

Sadly to found that -r and -p are used commonly, and they are in good places.

I have 2 ideas:

  1. uppercase -R, represent redirect.
  2. uppercase -P, and changes from --redirect-mode to --redirect-policy.

Personally I'd prefer the 1st option.

@cyradotpink cyradotpink marked this pull request as ready for review December 23, 2023 15:44
Copy link
Collaborator

@WindSoilder WindSoilder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really thanks for your improvement on http commands, I think we can change something to make it better.

And, It's appreciated to have some tests for the feature. You can check here for more about testing code: https://github.com/nushell/nushell/tree/534287ed65630003d731bb8e0268a6e2f7f10bd2/crates/nu-command/tests/commands/network/http

crates/nu-command/src/network/http/client.rs Outdated Show resolved Hide resolved
crates/nu-command/src/network/http/client.rs Outdated Show resolved Hide resolved
crates/nu-command/src/network/http/client.rs Show resolved Hide resolved
@cyradotpink
Copy link
Contributor Author

cyradotpink commented Dec 26, 2023

And, It's appreciated to have some tests for the feature. You can check here for more about testing code:

Thanks for this note - I was hoping you'd say something about tests, because I couldn't figure how tests should be done here on my own. I'll add some :)

@WindSoilder
Copy link
Collaborator

Thanks! Would you please resolve conflict?

Copy link
Collaborator

@WindSoilder WindSoilder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working through all of this! Look good to me

@WindSoilder WindSoilder merged commit a86a7e6 into nushell:main Dec 28, 2023
19 checks passed
@cyradotpink
Copy link
Contributor Author

Thank you for the ideas and the reviews :))

dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
…ushell#11329)

Intends to close nushell#8920 

This PR suggests a new flag for the `http` commands, `--redirect-mode`,
which enables users to choose between different redirect handling modes.
The current behaviour of letting ureq silently follow redirects remains
the default, but two new options are introduced here, following the lead
of [JavaScript's `fetch`
API](https://developer.mozilla.org/en-US/docs/Web/API/fetch#redirect):
"manual", where any 3xx response to a request is simply returned as the
command's result, and "error", where any 3xx response causes a network
error like those caused by 4xx and 5xx responses.

This PR is a draft. Tests have not been added or run, the flag is
currently only implemented for the `http get` command, and design tweaks
are likely to be appropriate.

Most notably, it's not obvious to me whether a single flag which can
take one of three values is the nicest solution here.
We might instead consider two binary flags (like
`--no-following-redirects` and `--disallow-redirects`, although I'm bad
at naming things so I need help with that anyway), or completely drop
the "error" option if it's not deemed useful enough. (I personally think
it has some merit, especially since 4xx and 5xx responses are already
treated as errors by default; So this would allow users to treat only
immediate 2xx responses as success)

# User-facing changes
New options for the `http [method]` commands. Behaviour remains
unchanged when the command line flag introduced here is not used.


![image](https://github.com/nushell/nushell/assets/12228688/1eb89f14-7d48-4f41-8a3e-cc0f1bd0a4f8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
networking All about our `http` and `url` commands and everything going accross the network.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to not follow HTTP redirects
3 participants