nushell/crates/nu-command/src
Julian Aichholz 9c7e37f728
Fix: return all headers with the same name from http <method> (#9594)
# Description

Hi nushell!

Thanks so much for [adding http headers][headers]! I've been waiting for
this feature for a long time and it's great. However, I found that
`Record` as a return type and using `ureq`'s `response.header` api
results in missing header values when there are multiple with the same
name, as can occur with `set-cookie` and others.

This issue with http has been discussed at length [on
stackoverflow][stackoverflow] and in [`ureq` itself][ureq]. It seems
like concatenating header values with `,` is a common solution, but
tricky especially with `set-cookie` which may contain `,` in the
`Expires` field, as discussed in the former post.

I propose changing the return type to a `List` of `Record` so we can get
all of the header values without relying on ad-hoc mutation. This
solution does not return the headers in the same order as they appear in
the `Response` due to `ureq`'s `Response.all` API, but it's better than
dropping values imo.

This is a **breaking change**. I'm sure `ureq`'s
[`CookieStore`][cookiestore] is a better long-term solution for
returning cookies as a separate record on `http <method>`, but other
headers can be set multiple times as well.

# User-Facing Changes
- Changes the return type of an `http <method>` `header` field from
`Record` to `List` (Table with columns `name` and `value`)
- Returns all values of a header set multiple times instead of just the
first one duplicated

# Implementation

Quick note that running `header_names.dedup()` does not resolve the
necessity to iterate through the previously parsed headers since `dedup`
only removes identical values when they are next to each other in the
`Vec`. You could do a `sort` first, but header ordering can be important
in some cases, so I tried to avoid messing with that more than is
already the case with `Response.all`. Would love to see a better way of
doing this though!

# Tests + Formatting
No tests broke implementing this change. Not sure what endpoint to hit
or mock server to use to verify this in tests. I have some screenshots
to illustrate what I'm talking about.

Before:
![Screenshot 2023-07-03 at 12 50 17
AM](https://github.com/nushell/nushell/assets/39018167/41604bef-54c6-424b-91b2-6b89a020e4ff)

> `set-cookie` has the same value for every record field.
> Even if it did not I'm not sure how you would access the different
values since they all have the same key.

After:
![Screenshot 2023-07-03 at 12 49 45
AM](https://github.com/nushell/nushell/assets/39018167/4ee45e6e-3785-471f-aee7-5af185cd06c2)

> Actual values from the response returned for the same name

Make sure you've run and fixed any issues with these commands:

- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- [x] `cargo test --workspace` to check that all tests pass
- [x] `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library <-- Note: I did not see a `crates/nu-std/test/run.nu`
file so I ran the snippet below which returned without error
```nushell
for $i in (ls crates/nu-std/tests/*.nu) {
   cargo run -- $i.name
}
```

# Code of Conduct

Apologies for not opening an issue first. Just did this fix for myself
because it seemed simple enough before deciding to open this PR.

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
- [ ] update docs

[stackoverflow]:
https://stackoverflow.com/questions/3241326/set-more-than-one-http-header-with-the-same-name
[headers]: https://github.com/nushell/nushell/pull/8571
[ureq]: https://github.com/algesten/ureq/issues/95
[cookiestore]:
https://docs.rs/cookie_store/latest/cookie_store/struct.CookieStore.html
2023-07-03 10:20:39 -05:00
..
charting revert: move to ahash (#9464) 2023-06-18 15:27:57 +12:00
conversions convert a string to a raw binary string of 0s and 1s (#9534) 2023-06-28 13:04:07 -05:00
database Improve type hovers (#9515) 2023-06-29 05:19:48 +12:00
date Update some help examples (#8759) 2023-04-05 18:36:00 -05:00
debug remove let-env, focus on mutating $env (#9574) 2023-07-01 07:57:51 +12:00
deprecated fix typo in deprecated message: $nu should be $env (#9579) 2023-07-01 17:55:25 +02:00
env remove let-env, focus on mutating $env (#9574) 2023-07-01 07:57:51 +12:00
experimental fix is-admin example (#9350) 2023-06-03 15:11:14 -05:00
filesystem fix cd permissions when user belongs to folder group (#9531) 2023-06-30 11:03:26 +02:00
filters make the behaviours of last and first more consistent (#9582) 2023-07-03 10:19:50 -05:00
formats Apply nightly clippy fixes (#9482) 2023-06-20 10:17:33 +02:00
generators Document and critically review ShellError variants - Ep. 2 (#8326) 2023-03-06 11:31:07 +01:00
hash move common tools from nu-command to nu-cmd-base (#9455) 2023-06-22 14:45:54 -07:00
help Break up interdependencies of command crates (#9429) 2023-06-14 23:12:55 +02:00
math revert: move to ahash (#9464) 2023-06-18 15:27:57 +12:00
misc REFACTOR: move source out of deprecated commands (#9060) 2023-05-04 00:02:03 +02:00
network Fix: return all headers with the same name from http <method> (#9594) 2023-07-03 10:20:39 -05:00
path revert: move to ahash (#9464) 2023-06-18 15:27:57 +12:00
platform add input_output type to input list to return string (#9557) 2023-06-29 08:27:25 -05:00
random Document and critically review ShellError variants - Ep. 3 (#8340) 2023-03-06 18:33:09 +01:00
shells Fix usage for the exit command. (#9450) 2023-06-16 10:09:02 +02:00
strings A new subcommand to str, str-expand. (#9290) 2023-06-28 12:57:44 -05:00
system revert: move to ahash (#9464) 2023-06-18 15:27:57 +12:00
viewers nu-table/ Fix indexing issue for table --expand (#9484) 2023-06-20 20:27:00 +12:00
default_context.rs remove let-env, focus on mutating $env (#9574) 2023-07-01 07:57:51 +12:00
example_test.rs remove let-env, focus on mutating $env (#9574) 2023-07-01 07:57:51 +12:00
hook.rs cratification: move the bytes command to nu-cmd-extra (#9509) 2023-06-23 12:23:08 -07:00
lib.rs cratification: move the bytes command to nu-cmd-extra (#9509) 2023-06-23 12:23:08 -07:00
progress_bar.rs cp progress bar implementation (#8012) 2023-02-22 11:57:38 -08:00
sort_utils.rs Support passing an empty list to sort, uniq, sort-by, and uniq-by (issue #5957) (#8669) 2023-03-29 19:55:38 -07:00