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

Vary header in compression #399

Merged
merged 1 commit into from
Jan 21, 2024
Merged

Vary header in compression #399

merged 1 commit into from
Jan 21, 2024

Conversation

Dragonink
Copy link
Contributor

Motivation

From the "Compression in HTTP" MDN page:

As content negotiation has been used to choose a representation based on its encoding, the server must send a Vary header containing at least Accept-Encoding alongside this header in the response; that way, caches will be able to cache the different representations of the resource.

Solution

We just append Accept-Encoding to the Vary header when constructing the compression response.

@jplatte
Copy link
Collaborator

jplatte commented Aug 20, 2023

I wonder if we should also send the header in some of the conditions where we early return without a content-encoding, to indicate to caches that a different response body is available when accept-encoding is set. Wdyt?

@Dragonink
Copy link
Contributor Author

So we always send the Vary header whatever the body will be? You're right, we should do this.
I will fix that.

I have a question about branches: why has the v0.4.x branch (and the 0.4.3 version) diverged from master?
So what base branch should I target with this PR? master or v0.4.x?

@jplatte
Copy link
Collaborator

jplatte commented Aug 21, 2023

So we always send the Vary header whatever the body will be? You're right, we should do this. I will fix that.

No, not in every case. If should_compress is false, the body will never be compressed no matter which headers the client sends AFAIU. However, for Encoding::Identity, the body was not compressed specifically of what headers the client sent (again, AFAIU).

I have a question about branches: why has the v0.4.x branch (and the 0.4.3 version) diverged from master? So what base branch should I target with this PR? master or v0.4.x?

It's okay to make PRs against either. I guess if you want this PR to be released (soon-ish) as part of 0.4.x, it makes sense to make the PR against that. There's (so far) only a tiny difference between the branches, so it will be trivial to cherry-pick changes such that they affect both branches.

@Dragonink
Copy link
Contributor Author

So we should always send the Vary except when should_compress is false?

@jplatte
Copy link
Collaborator

jplatte commented Aug 21, 2023

No, I think there's another early-return branch (body already compressed, i.e. content-encoding already set) in which the middleware should essentially do nothing.

cc @davidpdrsn @Nehliin wdyt about when we should set vary?

@Dragonink
Copy link
Contributor Author

The Content-Encoding already set is tested in should_compress:

let should_compress = !res.headers().contains_key(header::CONTENT_ENCODING)
    && self.predicate.should_compress(&res);

Thus if I guard the Vary header with an if should_compress, the header will be added unless the response is already compressed or the response is never be compressed.

@jplatte
Copy link
Collaborator

jplatte commented Oct 1, 2023

Ping @davidpdrsn @Nehliin any opinions about this PR / the question of when vary should be sent?

Also @Dragonink have you looked into the test failure? (MSRV job failing was fixed in #418)

@Dragonink Dragonink changed the base branch from v0.4.x to master October 1, 2023 14:00
@Nehliin
Copy link
Collaborator

Nehliin commented Oct 23, 2023

sorry won't have time to review in the near future

@jplatte jplatte closed this Jan 21, 2024
@jplatte jplatte reopened this Jan 21, 2024
@jplatte jplatte enabled auto-merge (squash) January 21, 2024 12:09
@jplatte jplatte merged commit c205dea into tower-rs:main Jan 21, 2024
11 checks passed
@jplatte jplatte mentioned this pull request Jan 21, 2024
@Dragonink Dragonink deleted the compression-vary branch January 21, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants