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

response header rework #1869

Merged
merged 15 commits into from
Jan 15, 2021
Merged

response header rework #1869

merged 15 commits into from
Jan 15, 2021

Conversation

robjtede
Copy link
Member

@robjtede robjtede commented Jan 3, 2021

PR Type

Feature

PR Checklist

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt

Overview

This PR normalizes header methods used throughout the crates and enables header K/V pairs to use the same methods on Response as the typed headers (and give more attention to typed headers in the process).

In general, the signatures have changed from insert_header(key: K, value: V) to insert_header(header: H), where H implements a new trait IntoHeaderPair.

The easiest migration is simply to use insert_header((K, V)), noting the tuple which implements the new trait for all previously accepted types.

@robjtede robjtede added A-http project: actix-http B-semver-major breaking change requiring a major version bump labels Jan 3, 2021
@robjtede robjtede force-pushed the header-rework branch 2 times, most recently from 0188f07 to b476f39 Compare January 7, 2021 01:05
@robjtede robjtede marked this pull request as ready for review January 11, 2021 04:26
@robjtede robjtede requested review from a team January 11, 2021 04:26
@robjtede robjtede added A-awc project: awc A-web project: actix-web labels Jan 11, 2021
Copy link
Contributor

@fakeshadow fakeshadow left a comment

Choose a reason for hiding this comment

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

LGTM

@robjtede robjtede requested a review from a team January 11, 2021 13:07
Copy link
Contributor

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

I don't really like .insert_header((name, value)) but I guess it's better than having separate methods for typed and untyped headers and having to come up with distinct names for them. The only other solution I could think of is having the same method on two different traits and users importing the one they prefer (I doubt many people are using both typed and untyped headers in the same module).

Also, I see actix-http has its own typed headers; do these predate the headers crate or is there some other reason they are separate?

actix-http/CHANGES.md Outdated Show resolved Hide resolved
@robjtede
Copy link
Member Author

robjtede commented Jan 11, 2021

@jplatte

I don't really like .insert_header((name, value)) but

In my view, an increased focus on typed headers alleviates this enough to make it worth doing. In most cases you're adding common headers to responses, if we don't have the header someone's after we can add it. Plus typed headers are theoretically cheaper than (string, V) since the HeaderName conversion is free. Do you agree that this trade off becomes more worth it when typed headers are used more liberally?

You've pointed out the primary issue with the trait approach; we already have folks getting confused when trying to use extensions_mut for example.

There's some foreign trait rules things to workaround to use an external crate. It's come up and we're looking into the viability of it. Either way this PR would be largely similar.

@jplatte
Copy link
Contributor

jplatte commented Jan 11, 2021

In my view, an increased focus on typed headers alleviates this enough to make it worth doing. In most cases you're adding common headers to responses, if we don't have the header someone's after we can add it. Plus typed headers are theoretically cheaper than (string, V) since the HeaderName conversion is free. Do you agree that this trade off becomes more worth it when typed headers are used more liberally?

Yeah, I agree. Still it will be some annoying churn that has to be handled as part of transitioning to actix-web 4.0. Maybe you should consider having these new methods before the next major version and deprecate the old ones so upgrading can be done in independent steps. If it's too much work to backport these, maybe you could keep the old ones around as deprecated for the next major version.

@robjtede robjtede requested a review from a team January 11, 2021 14:34
@robjtede
Copy link
Member Author

I'll have a think about keeping old ones, deprecated, to make migration easier. Maybe just on ResponseBuilder where most churn would occur.

Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

So much more ergonomic. I like it.

@robjtede
Copy link
Member Author

I've added back the response builder methods set_header and header as deprecated.

@robjtede robjtede merged commit b1dd8d2 into master Jan 15, 2021
@robjtede robjtede deleted the header-rework branch January 15, 2021 02:11
@omid

This comment has been minimized.

@omid
Copy link
Contributor

omid commented Jan 19, 2021

Sorry for this false alarm, it's fixed by adding to Cargo.toml:

[patch.crates-io]
actix-http = { path = "../actix-web/actix-http" }
awc = { path = "../actix-web/awc" }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-awc project: awc A-http project: actix-http A-web project: actix-web B-semver-major breaking change requiring a major version bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants