From 810f6db8d2399fb515157024b54c3745df5385d3 Mon Sep 17 00:00:00 2001 From: ysqi Date: Fri, 12 Feb 2016 11:27:59 +0800 Subject: [PATCH 1/4] fix #1669 write empty body panic error --- context/context.go | 10 ++++++++++ context/output.go | 3 +-- router_test.go | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/context/context.go b/context/context.go index db790ff2..5f095f1a 100644 --- a/context/context.go +++ b/context/context.go @@ -24,11 +24,13 @@ package context import ( "bufio" + "bytes" "crypto/hmac" "crypto/sha1" "encoding/base64" "errors" "fmt" + "io" "net" "net/http" "strconv" @@ -184,6 +186,14 @@ func (w *Response) Write(p []byte) (int, error) { return w.ResponseWriter.Write(p) } +// Write writes the data to the connection as part of an HTTP reply, +// and sets `started` to true. +// started means the response has sent out. +func (w *Response) Copy(buf *bytes.Buffer) (int64, error) { + w.Started = true + return io.Copy(w.ResponseWriter, buf) +} + // WriteHeader sends an HTTP response header with status code, // and sets `started` to true. func (w *Response) WriteHeader(code int) { diff --git a/context/output.go b/context/output.go index 2d756e27..aa00244b 100644 --- a/context/output.go +++ b/context/output.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "html/template" - "io" "mime" "net/http" "path/filepath" @@ -75,7 +74,7 @@ func (output *BeegoOutput) Body(content []byte) { output.Status = 0 } - io.Copy(output.Context.ResponseWriter, buf) + output.Context.ResponseWriter.Copy(buf) } // Cookie sets cookie value via given key. diff --git a/router_test.go b/router_test.go index b0ae7a18..f26f0c86 100644 --- a/router_test.go +++ b/router_test.go @@ -65,6 +65,11 @@ func (tc *TestController) GetManyRouter() { tc.Ctx.WriteString(tc.Ctx.Input.Query(":id") + tc.Ctx.Input.Query(":page")) } +func (tc *TestController) GetEmptyBody() { + var res []byte + tc.Ctx.Output.Body(res) +} + type ResStatus struct { Code int Msg string @@ -239,6 +244,21 @@ func TestManyRoute(t *testing.T) { } } +// Test for issue #1669 +func TestEmptyResponse(t *testing.T) { + + r, _ := http.NewRequest("GET", "/beego-empty.html", nil) + w := httptest.NewRecorder() + + handler := NewControllerRegister() + handler.Add("/beego-empty.html", &TestController{}, "get:GetEmptyBody") + handler.ServeHTTP(w, r) + + if body := w.Body.String(); body != "" { + t.Error("want empty body") + } +} + func TestNotFound(t *testing.T) { r, _ := http.NewRequest("GET", "/", nil) w := httptest.NewRecorder() From d35c50a8e0bf0ace2c5567d8a21fbbe55643589c Mon Sep 17 00:00:00 2001 From: ysqi Date: Fri, 12 Feb 2016 11:36:25 +0800 Subject: [PATCH 2/4] return write body error --- context/output.go | 14 ++++++-------- controller.go | 3 +-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/context/output.go b/context/output.go index aa00244b..066859f2 100644 --- a/context/output.go +++ b/context/output.go @@ -56,7 +56,7 @@ func (output *BeegoOutput) Header(key, val string) { // Body sets response body content. // if EnableGzip, compress content string. // it sends out response body directly. -func (output *BeegoOutput) Body(content []byte) { +func (output *BeegoOutput) Body(content []byte) error { var encoding string var buf = &bytes.Buffer{} if output.EnableGzip { @@ -74,7 +74,8 @@ func (output *BeegoOutput) Body(content []byte) { output.Status = 0 } - output.Context.ResponseWriter.Copy(buf) + _, err := output.Context.ResponseWriter.Copy(buf) + return err } // Cookie sets cookie value via given key. @@ -185,8 +186,7 @@ func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) e if coding { content = []byte(stringsToJSON(string(content))) } - output.Body(content) - return nil + return output.Body(content) } // JSONP writes jsonp to response body. @@ -211,8 +211,7 @@ func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error { callbackContent.WriteString("(") callbackContent.Write(content) callbackContent.WriteString(");\r\n") - output.Body(callbackContent.Bytes()) - return nil + return output.Body(callbackContent.Bytes()) } // XML writes xml string to response body. @@ -229,8 +228,7 @@ func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error { http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError) return err } - output.Body(content) - return nil + return output.Body(content) } // Download forces response for download file. diff --git a/controller.go b/controller.go index a2943d42..b23f7617 100644 --- a/controller.go +++ b/controller.go @@ -185,8 +185,7 @@ func (c *Controller) Render() error { return err } c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8") - c.Ctx.Output.Body(rb) - return nil + return c.Ctx.Output.Body(rb) } // RenderString returns the rendered template string. Do not send out response. From 23860e680785757a28d3920492c421e8d857f63c Mon Sep 17 00:00:00 2001 From: ysqi Date: Fri, 12 Feb 2016 11:36:59 +0800 Subject: [PATCH 3/4] go fmt --- config.go | 2 +- staticfile_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index ffe92f06..e7f3ee98 100644 --- a/config.go +++ b/config.go @@ -15,11 +15,11 @@ package beego import ( + "fmt" "html/template" "os" "path/filepath" "strings" - "fmt" "github.com/astaxie/beego/config" "github.com/astaxie/beego/session" diff --git a/staticfile_test.go b/staticfile_test.go index e7003366..a043b4fd 100644 --- a/staticfile_test.go +++ b/staticfile_test.go @@ -7,8 +7,8 @@ import ( "io" "io/ioutil" "os" - "testing" "path/filepath" + "testing" ) var currentWorkDir, _ = os.Getwd() From 90344a7b8f47d8890ff3db56cab49fe10b35c986 Mon Sep 17 00:00:00 2001 From: ysqi Date: Sun, 6 Mar 2016 21:25:43 +0800 Subject: [PATCH 4/4] fix conflicts --- config.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/config.go b/config.go index 4f85ce02..2761e7cb 100644 --- a/config.go +++ b/config.go @@ -16,10 +16,6 @@ package beego import ( "fmt" -<<<<<<< HEAD - "html/template" -======= ->>>>>>> astaxie/develop "os" "path/filepath" "strings"