Skip to content

[bug] Panic on nil Hijack interface due to compress handler #169

@muirdm

Description

@muirdm

Describe the bug

The compress handler leaves an empty http.Hijacker interface in the wrapped response writer even when the incoming response writer does not implement http.Hijacker. This causes the wrapped response writer to still implement http.Hijacker, but if you try to call Hijack() it will panic.

Steps to Reproduce

type myWriter struct {
	http.ResponseWriter
}

func main() {
	http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
		if h, ok := w.(http.Hijacker); ok {
			h.Hijack()
		}
	})

	myMiddleware := func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(&myWriter{w}, r)
		})
	}

	handler := myMiddleware(handlers.CompressHandler(http.DefaultServeMux))

	go func() {
		log.Panic(http.ListenAndServe("127.0.0.1:1337", handler))
	}()

	time.Sleep(1 * time.Second)

	http.Get("http://127.0.0.1:1337/test")
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions