Skip to content

Commit

Permalink
Prevents superfluous WriteHeader call in the error middleware
Browse files Browse the repository at this point in the history
Co-authored-by: LandryBe <lbenguigui@gmail.com>
  • Loading branch information
tomMoulard and lbenguigui committed Jan 2, 2023
1 parent b9a175f commit e1e8676
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
33 changes: 32 additions & 1 deletion integration/error_pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"net/http"
"net/http/httptest"
"os"
"time"

Expand Down Expand Up @@ -29,7 +30,7 @@ func (s *ErrorPagesSuite) TestSimpleConfiguration(c *check.C) {
file := s.adaptFile(c, "fixtures/error_pages/simple.toml", struct {
Server1 string
Server2 string
}{s.BackendIP, s.ErrorPageIP})
}{"http://" + s.BackendIP + ":80", s.ErrorPageIP})
defer os.Remove(file)

cmd, display := s.traefikCmd(withConfigFile(file))
Expand Down Expand Up @@ -67,3 +68,33 @@ func (s *ErrorPagesSuite) TestErrorPage(c *check.C) {
err = try.Request(frontendReq, 2*time.Second, try.BodyContains("An error occurred."))
c.Assert(err, checker.IsNil)
}

func (s *ErrorPagesSuite) TestErrorPageFlush(c *check.C) {
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Transfer-Encoding", "chunked")
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte("KO"))
}))

file := s.adaptFile(c, "fixtures/error_pages/simple.toml", struct {
Server1 string
Server2 string
}{srv.URL, s.ErrorPageIP})
defer os.Remove(file)

cmd, display := s.traefikCmd(withConfigFile(file))
defer display(c)
err := cmd.Start()
c.Assert(err, checker.IsNil)
defer s.killCmd(cmd)

frontendReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8080", nil)
c.Assert(err, checker.IsNil)
frontendReq.Host = "test.local"

err = try.Request(frontendReq, 2*time.Second,
try.BodyContains("An error occurred."),
try.HasHeaderValue("Content-Type", "text/html", true),
)
c.Assert(err, checker.IsNil)
}
2 changes: 1 addition & 1 deletion integration/fixtures/error_pages/simple.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
[http.services.service1.loadBalancer]
passHostHeader = true
[[http.services.service1.loadBalancer.servers]]
url = "http://{{.Server1}}:80"
url = "{{.Server1}}"

[http.services.error.loadBalancer]
[[http.services.error.loadBalancer.servers]]
Expand Down
9 changes: 9 additions & 0 deletions pkg/middlewares/customerrors/custom_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ func (cc *codeCatcher) Flush() {
// Otherwise, cc.code is actually a 200 here.
cc.WriteHeader(cc.code)

// We don't care about the contents of the response,
// since we want to serve the ones from the error page,
// so we just don't flush.
// (e.g., To prevent superfluous WriteHeader on request with a
// `Transfert-Encoding: chunked` header).
if cc.caughtFilteredCode {
return
}

if flusher, ok := cc.responseWriter.(http.Flusher); ok {
flusher.Flush()
}
Expand Down

0 comments on commit e1e8676

Please sign in to comment.