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

More than one wildcard directive in accept-encoding header causes fall back to br not gzip #187

Closed
2 tasks done
Fdawgs opened this issue Oct 13, 2021 · 1 comment · Fixed by #189
Closed
2 tasks done

Comments

@Fdawgs
Copy link
Member

Fdawgs commented Oct 13, 2021

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.22.0

Plugin version

3.6.0

Node.js version

14.18.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

In the documentation it states that if the * wildcard directive is used in the Accept-Encoding request header then the plugin will use gzip. However, if multiple wildcard directives are included in the Accept-Encoding request header then it uses br.

I believe this is due to the following line only replacing the first occurrence of a *:

.replace('*', 'gzip') // consider the no-preference token as gzip for downstream compat

Suggested fix:
.replace(/\*/g, 'gzip')

This also occurs in fastify-static:
https://github.com/fastify/fastify-static/blob/master/index.js#L436

Happy to open a PR for this.

Steps to Reproduce

  1. Stand up server:
const fastify = require("fastify")();
const { createReadStream } = require("fs");

// Import plugins
const compress = require("fastify-compress");

async function server() {
	fastify.register(compress, { inflateIfDeflated: true }).route({
		method: "GET",
		url: "/foo",
		handler: (req, res) => {
			res.type("text/plain").compress(createReadStream("./package.json"));
		},
	});

	await fastify.listen({ port: 8000 });
}

server();
  1. Make a request with more than one wildcard directive present:

Request:

> GET /foo HTTP/1.1
> Host: localhost:8000
> User-Agent: insomnia/2021.5.3
> accept-encoding: *, *
> Accept: */*

Response:

< HTTP/1.1 200 OK
< content-type: text/plain
< vary: accept-encoding
< content-encoding: br
< Date: Wed, 13 Oct 2021 10:12:46 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
  1. Compare to a request with a single wildcard directive present:

Request:

> GET /foo HTTP/1.1
> Host: localhost:8000
> User-Agent: insomnia/2021.5.3
> accept-encoding: *
> Accept: */*

Response:

< HTTP/1.1 200 OK
< content-type: text/plain
< vary: accept-encoding
< content-encoding: gzip
< Date: Wed, 13 Oct 2021 10:20:21 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked

Expected Behavior

Content-Encoding value should be gzip.

@mcollina
Copy link
Member

Good spot and thanks... please send a PR!

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 a pull request may close this issue.

2 participants