1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 01:20:54 +00:00

Merge pull request #1679 from ysqi/emptybodyfix

fix #1669 and return IO error
This commit is contained in:
astaxie 2016-03-08 09:48:17 +08:00
commit 2f18b9103b
5 changed files with 38 additions and 12 deletions

View File

@ -24,11 +24,13 @@ package context
import ( import (
"bufio" "bufio"
"bytes"
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
"io"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
@ -184,6 +186,14 @@ func (w *Response) Write(p []byte) (int, error) {
return w.ResponseWriter.Write(p) 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, // WriteHeader sends an HTTP response header with status code,
// and sets `started` to true. // and sets `started` to true.
func (w *Response) WriteHeader(code int) { func (w *Response) WriteHeader(code int) {

View File

@ -21,7 +21,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"io"
"mime" "mime"
"net/http" "net/http"
"path/filepath" "path/filepath"
@ -57,7 +56,7 @@ func (output *BeegoOutput) Header(key, val string) {
// Body sets response body content. // Body sets response body content.
// if EnableGzip, compress content string. // if EnableGzip, compress content string.
// it sends out response body directly. // it sends out response body directly.
func (output *BeegoOutput) Body(content []byte) { func (output *BeegoOutput) Body(content []byte) error {
var encoding string var encoding string
var buf = &bytes.Buffer{} var buf = &bytes.Buffer{}
if output.EnableGzip { if output.EnableGzip {
@ -75,7 +74,8 @@ func (output *BeegoOutput) Body(content []byte) {
output.Status = 0 output.Status = 0
} }
io.Copy(output.Context.ResponseWriter, buf) _, err := output.Context.ResponseWriter.Copy(buf)
return err
} }
// Cookie sets cookie value via given key. // Cookie sets cookie value via given key.
@ -186,8 +186,7 @@ func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) e
if coding { if coding {
content = []byte(stringsToJSON(string(content))) content = []byte(stringsToJSON(string(content)))
} }
output.Body(content) return output.Body(content)
return nil
} }
// JSONP writes jsonp to response body. // JSONP writes jsonp to response body.
@ -212,8 +211,7 @@ func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error {
callbackContent.WriteString("(") callbackContent.WriteString("(")
callbackContent.Write(content) callbackContent.Write(content)
callbackContent.WriteString(");\r\n") callbackContent.WriteString(");\r\n")
output.Body(callbackContent.Bytes()) return output.Body(callbackContent.Bytes())
return nil
} }
// XML writes xml string to response body. // XML writes xml string to response body.
@ -230,8 +228,7 @@ func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError) http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
return err return err
} }
output.Body(content) return output.Body(content)
return nil
} }
// Download forces response for download file. // Download forces response for download file.

View File

@ -185,8 +185,7 @@ func (c *Controller) Render() error {
return err return err
} }
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8") c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
c.Ctx.Output.Body(rb) return c.Ctx.Output.Body(rb)
return nil
} }
// RenderString returns the rendered template string. Do not send out response. // RenderString returns the rendered template string. Do not send out response.

View File

@ -65,6 +65,11 @@ func (tc *TestController) GetManyRouter() {
tc.Ctx.WriteString(tc.Ctx.Input.Query(":id") + tc.Ctx.Input.Query(":page")) 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 { type ResStatus struct {
Code int Code int
Msg string 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) { func TestNotFound(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil) r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -7,8 +7,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"testing"
"path/filepath" "path/filepath"
"testing"
) )
var currentWorkDir, _ = os.Getwd() var currentWorkDir, _ = os.Getwd()