Skip to content

[BUG] CompressHandler if combined with http.Fileserver misses "Content-Encoding: deflate" on 404 status codes #259

Open
@Lercher

Description

@Lercher

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

curl --output - -i --compressed http://localhost:9000/notfound.html outputs

HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Date: Mon, 18 Nov 2024 13:26:14 GMT
Content-Length: 25

210Q(HLOU��/QH�/�K�☻♦��

for the program listed in "steps to reproduce".

Expected Behavior

It should output either "deflate and len 25 bytes"

HTTP/1.1 404 Not Found
Content-Encoding: deflate
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Date: Mon, 18 Nov 2024 12:52:02 GMT
Content-Length: 25

404 page not found

or "not compressed and len 19 bytes"

HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 18 Nov 2024 12:54:26 GMT
Content-Length: 19

404 page not found

Steps To Reproduce

  • go version go1.23.3 windows/amd64
  • github.com/gorilla/handlers v1.5.2
  • github.com/gorilla/mux v1.8.1
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
)

func main() {
	log.Println("Listening on http://localhost:9000/index.html")

	mux := mux.NewRouter()
	mux.Use(handlers.CompressHandler)

	mux.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprintln(w, `<a href="/error404.html">Click me for a 404 error.</a> or use <pre>curl --output - -i --compressed http://localhost:9000</pre>`)
	})
	mux.PathPrefix("/").Methods("GET").Handler(http.FileServer(http.Dir(".")))

	log.Fatal(http.ListenAndServe(":9000", mux))
}

The "expected behavior" is present if the line mux.PathPrefix("/").Methods("GET").Handler(http.FileServer(http.Dir("."))) is commented out. It degrades to the "current behavior" if the FileServer is activated.

It works in both cases if a normal OK status code is produced (/index.html).

Chrome Dev tools behave similarly but different in details b/c of gzip / deflate request headers, I guess.

Anything else?

Sorry for not investigating more detail for the sample program. My more complex code worked with Go 1.22.x and handlers v1.5.1 and I don't know if it's an issue of the newer Go std lib, of gorilla/mux or gorilla/handlers.

Thanks
Martin

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions