mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 04:51:30 +00:00
fix #1669 write empty body panic error
This commit is contained in:
parent
2b23764ee0
commit
810f6db8d2
@ -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) {
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -75,7 +74,7 @@ func (output *BeegoOutput) Body(content []byte) {
|
|||||||
output.Status = 0
|
output.Status = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
io.Copy(output.Context.ResponseWriter, buf)
|
output.Context.ResponseWriter.Copy(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cookie sets cookie value via given key.
|
// Cookie sets cookie value via given key.
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user