Skip to content

Commit bb4f44f

Browse files
committed
echocat#6 Fixed bug that empty body responses are not handled correctly
1 parent 2a80c3f commit bb4f44f

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

filter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ func (instance filterHandler) ServeHTTP(writer http.ResponseWriter, request *htt
3939
return result, err
4040
}
4141
}
42-
if !wrapper.wasSomethingRecorded() || !wrapper.isBodyAllowed() {
42+
if !wrapper.isBodyAllowed() {
43+
return result, logError
44+
}
45+
if !wrapper.wasSomethingRecorded() {
46+
wrapper.writeHeadersToDelegate()
4347
return result, logError
4448
}
4549
body := wrapper.recorded()

integration_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,44 @@ func (s *integrationTest) Test_fastcgiWithGzip(c *C) {
9696
c.Assert(string(content), Contains, "<title>Hello replaced world!</title>")
9797
}
9898

99+
func (s *integrationTest) Test_fastcgiWithRedirect(c *C) {
100+
s.fcgiServer = test.NewTestingFcgiServer(22792)
101+
102+
client := http.Client{
103+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
104+
return http.ErrUseLastResponse
105+
},
106+
}
107+
req, err := http.NewRequest("GET", "http://localhost:22782/redirect.cgi", nil)
108+
109+
c.Assert(err, IsNil)
110+
resp, err := client.Do(req)
111+
c.Assert(err, IsNil)
112+
113+
defer resp.Body.Close()
114+
content, err := ioutil.ReadAll(resp.Body)
115+
c.Assert(err, IsNil)
116+
c.Assert(resp.StatusCode, Equals, 301)
117+
c.Assert(resp.Status, Equals, "301 Moved Permanently")
118+
c.Assert(string(content), IsEmpty)
119+
120+
resp2, err := http.Get("http://caddyserver.com")
121+
c.Assert(err, IsNil)
122+
defer resp2.Body.Close()
123+
content, err = ioutil.ReadAll(resp2.Body)
124+
c.Assert(err, IsNil)
125+
c.Assert(resp.StatusCode, Equals, 301)
126+
c.Assert(string(content), Contains, "<title>Caddy - ")
127+
128+
resp3, err := http.Get("http://localhost:22782/redirect.cgi")
129+
c.Assert(err, IsNil)
130+
defer resp3.Body.Close()
131+
content, err = ioutil.ReadAll(resp3.Body)
132+
c.Assert(err, IsNil)
133+
c.Assert(resp.StatusCode, Equals, 301)
134+
c.Assert(string(content), Contains, "<title>Replaced another!</title>")
135+
}
136+
99137
func (s *integrationTest) Test_markdown(c *C) {
100138
resp, err := http.Get("http://localhost:22783/index.md")
101139
c.Assert(err, IsNil)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:22782 {
2+
tls off
3+
errors stdout
4+
filter rule {
5+
content_type "text/html.*"
6+
search_pattern "I'am another!"
7+
replacement "Replaced another!"
8+
}
9+
fastcgi / 127.0.0.1:22792 {
10+
ext .cgi
11+
split .cgi
12+
index index.cgi
13+
root resources/test/integrationTest.Test_fastcgi
14+
}
15+
}

utils/test/testingFcgiServer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func NewTestingFcgiServer(port int) *TestingFcgiServer {
2222

2323
result.mux = http.NewServeMux()
2424
result.mux.HandleFunc("/index.cgi", result.handleIndexRequest)
25+
result.mux.HandleFunc("/redirect.cgi", result.handleRedirectRequest)
26+
result.mux.HandleFunc("/another.cgi", result.handleAnotherRequest)
2527

2628
result.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
2729
if err != nil {
@@ -51,6 +53,18 @@ func (instance *TestingFcgiServer) handleIndexRequest(resp http.ResponseWriter,
5153
"</html>"))
5254
}
5355

56+
func (instance *TestingFcgiServer) handleRedirectRequest(resp http.ResponseWriter, req *http.Request) {
57+
resp.Header().Set("Location", "/another.cgi")
58+
resp.WriteHeader(301)
59+
}
60+
61+
func (instance *TestingFcgiServer) handleAnotherRequest(resp http.ResponseWriter, req *http.Request) {
62+
resp.Write([]byte("<html>" +
63+
"<head><title>I'am another!</title></head>" +
64+
"<body><p>I'am another!</p></body>" +
65+
"</html>"))
66+
}
67+
5468
// Close closes the testing server graceful.
5569
func (instance *TestingFcgiServer) Close() {
5670
defer func() {

0 commit comments

Comments
 (0)