Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
#4 Handles error objects send by fastcgi plugin in a special way to p…
Browse files Browse the repository at this point in the history
…revent premature end of operation followed by a blank page for the user. Added also a integration test to ensure this.
  • Loading branch information
blaubaer committed Jan 27, 2017
1 parent 258fe71 commit ce62106
Show file tree
Hide file tree
Showing 10 changed files with 778 additions and 25 deletions.
9 changes: 5 additions & 4 deletions .gitignore
@@ -1,5 +1,6 @@
*.iml
.idea
.gradle
build
vendor
/.idea
/.gradle
/build
/vendor
/learning
15 changes: 12 additions & 3 deletions filter.go
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"strconv"
"github.com/mholt/caddy/caddyhttp/fastcgi"
)

const defaultMaxBufferSize = 10 * 1024 * 1024
Expand All @@ -27,11 +28,19 @@ func (instance filterHandler) ServeHTTP(writer http.ResponseWriter, request *htt
})
wrapper.maximumBufferSize = instance.maximumBufferSize
result, err := instance.next.ServeHTTP(wrapper, request)
var logError error
if err != nil {
return result, err
var ok bool
// This handles https://github.com/echocat/caddy-filter/issues/4
// If the fastcgi module is used and the FastCGI server produces log output
// this is send (by the FastCGI module) as an error. We have to check this and
// handle this case of error in a special way.
if logError, ok = err.(fastcgi.LogError); !ok {
return result, err
}
}
if !wrapper.wasSomethingRecorded() || !wrapper.isBodyAllowed() {
return result, nil
return result, logError
}
body := wrapper.recorded()
atLeastOneRuleMatched := false
Expand All @@ -55,5 +64,5 @@ func (instance filterHandler) ServeHTTP(writer http.ResponseWriter, request *htt
if n < len(body) {
return result, io.ErrShortWrite
}
return result, nil
return result, logError
}
11 changes: 11 additions & 0 deletions filter_test.go
Expand Up @@ -6,6 +6,7 @@ import (
. "gopkg.in/check.v1"
"net/http"
"regexp"
"github.com/mholt/caddy/caddyhttp/fastcgi"
)

type filterTest struct {
Expand Down Expand Up @@ -68,6 +69,16 @@ func (s *filterTest) Test_withErrorInNext(c *C) {
c.Assert(s.writer.buffer.String(), Equals, "")
}

// This handles the bug https://github.com/echocat/caddy-filter/issues/4
// See filter.go for more details.
func (s *filterTest) Test_withLogErrorInNext(c *C) {
s.nextHandler.error = fastcgi.LogError("Oops")
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, DeepEquals, s.nextHandler.error)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "Hello 2nd is 'o'!")
}

func (s *filterTest) Test_withErrorInWriter(c *C) {
s.writer.error = errors.New("Oops")
status, err := s.handler.ServeHTTP(s.writer, s.request)
Expand Down
46 changes: 36 additions & 10 deletions integration_test.go
@@ -1,24 +1,28 @@
package filter

import (
"fmt"
"github.com/echocat/caddy-filter/utils/test"
. "github.com/echocat/gocheck-addons"
"github.com/mholt/caddy"
. "gopkg.in/check.v1"

"fmt"
"io/ioutil"
"net/http"
"github.com/echocat/caddy-filter/utils/test"
"github.com/mholt/caddy"

_ "github.com/mholt/caddy/caddyhttp/errors"
_ "github.com/mholt/caddy/caddyhttp/gzip"
_ "github.com/mholt/caddy/caddyhttp/markdown"
_ "github.com/mholt/caddy/caddyhttp/proxy"
_ "github.com/mholt/caddy/caddyhttp/root"
_ "github.com/mholt/caddy/caddyhttp/fastcgi"
_ "github.com/mholt/caddy/caddyhttp/log"
)

type integrationTest struct {
server *test.TestingServer
caddy *caddy.Instance
httpServer *test.TestingHttpServer
fcgiServer *test.TestingFcgiServer
caddy *caddy.Instance
}

func init() {
Expand All @@ -40,7 +44,7 @@ func (s *integrationTest) Test_staticWithGzip(c *C) {
}

func (s *integrationTest) Test_proxy(c *C) {
s.server = test.NewTestingServer(22770)
s.httpServer = test.NewTestingHttpServer(22770)

resp, err := http.Get("http://localhost:22780/default")
c.Assert(err, IsNil)
Expand All @@ -55,6 +59,22 @@ func (s *integrationTest) Test_proxyWithGzip(c *C) {
s.Test_proxy(c)
}

func (s *integrationTest) Test_fastcgi(c *C) {
s.fcgiServer = test.NewTestingFcgiServer(22790)

resp, err := http.Get("http://localhost:22780/index.cgi")
c.Assert(err, IsNil)

defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
c.Assert(string(content), Contains, "<title>Hello replaced world!</title>")
}

func (s *integrationTest) Test_fastcgiWithGzip(c *C) {
s.Test_fastcgi(c)
}

func (s *integrationTest) Test_markdown(c *C) {
resp, err := http.Get("http://localhost:22780/index.md")
c.Assert(err, IsNil)
Expand All @@ -70,17 +90,23 @@ func (s *integrationTest) Test_markdownWithGzip(c *C) {
}

func (s *integrationTest) SetUpTest(c *C) {
c.Check(s.server, IsNil)
c.Check(s.httpServer, IsNil)
c.Check(s.fcgiServer, IsNil)
c.Check(s.caddy, IsNil)
s.caddy = test.NewTestingCaddy(fmt.Sprintf("%s.conf", c.TestName()))
}

func (s *integrationTest) TearDownTest(c *C) {
if s.server != nil {
s.server.Close()
if s.httpServer != nil {
s.httpServer.Close()
}
if s.fcgiServer != nil {
s.fcgiServer.Close()
}
if s.caddy != nil {
s.caddy.Stop()
}
s.server = nil
s.httpServer = nil
s.fcgiServer = nil
s.caddy = nil
}
15 changes: 15 additions & 0 deletions resources/test/integrationTest.Test_fastcgi.conf
@@ -0,0 +1,15 @@
:22780 {
tls off
errors stdout
filter rule {
content_type "text/html.*"
search_pattern "Hello world!"
replacement "Hello replaced world!"
}
fastcgi / 127.0.0.1:22790 {
ext .cgi
split .cgi
index index.cgi
root resources/test/integrationTest.Test_fastcgi
}
}
16 changes: 16 additions & 0 deletions resources/test/integrationTest.Test_fastcgiWithGzip.conf
@@ -0,0 +1,16 @@
:22780 {
tls off
errors stdout
gzip
filter rule {
content_type "text/html.*"
search_pattern "Hello world!"
replacement "Hello replaced world!"
}
fastcgi / 127.0.0.1:22790 {
ext .cgi
split .cgi
index index.cgi
root resources/test/integrationTest.Test_fastcgi
}
}

0 comments on commit ce62106

Please sign in to comment.