mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Issue #6: Add support for PATCH method and other methods which may contain body
This commit is contained in:
@@ -146,7 +146,7 @@ func (h *ResponseHeader) SetContentLength(contentLength int) {
|
||||
// It may be negative:
|
||||
// -1 means Transfer-Encoding: chunked.
|
||||
func (h *RequestHeader) ContentLength() int {
|
||||
if !h.IsPost() && !h.IsPut() {
|
||||
if h.noBody() {
|
||||
return 0
|
||||
}
|
||||
h.parseRawHeaders()
|
||||
@@ -1038,7 +1038,7 @@ func (h *RequestHeader) AppendBytes(dst []byte) []byte {
|
||||
}
|
||||
|
||||
contentType := h.ContentType()
|
||||
if h.IsPost() || h.IsPut() {
|
||||
if !h.noBody() {
|
||||
if len(contentType) == 0 {
|
||||
contentType = strPostArgsContentType
|
||||
}
|
||||
@@ -1092,6 +1092,10 @@ func (h *ResponseHeader) parse(buf []byte) (int, error) {
|
||||
return m + n, nil
|
||||
}
|
||||
|
||||
func (h *RequestHeader) noBody() bool {
|
||||
return h.IsGet() || h.IsHead()
|
||||
}
|
||||
|
||||
func (h *RequestHeader) parse(buf []byte) (int, error) {
|
||||
m, err := h.parseFirstLine(buf)
|
||||
if err != nil {
|
||||
@@ -1099,7 +1103,7 @@ func (h *RequestHeader) parse(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
var n int
|
||||
if h.IsPost() || h.IsPut() {
|
||||
if !h.noBody() {
|
||||
n, err = h.parseHeaders(buf[m:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -1345,7 +1349,7 @@ func (h *RequestHeader) parseHeaders(buf []byte) (int, error) {
|
||||
if h.contentLength < 0 {
|
||||
h.contentLengthBytes = h.contentLengthBytes[:0]
|
||||
}
|
||||
if !h.IsPost() && !h.IsPut() {
|
||||
if h.noBody() {
|
||||
h.contentLength = 0
|
||||
h.contentLengthBytes = h.contentLengthBytes[:0]
|
||||
}
|
||||
|
||||
@@ -6,16 +6,25 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRequestHeaderSetContentType_Issue6(t *testing.T) {
|
||||
func TestIssue6RequestHeaderSetContentType(t *testing.T) {
|
||||
testIssue6RequestHeaderSetContentType(t, "GET")
|
||||
testIssue6RequestHeaderSetContentType(t, "POST")
|
||||
testIssue6RequestHeaderSetContentType(t, "PUT")
|
||||
testIssue6RequestHeaderSetContentType(t, "PATCH")
|
||||
}
|
||||
|
||||
func testIssue6RequestHeaderSetContentType(t *testing.T, method string) {
|
||||
contentType := "application/json"
|
||||
contentLength := 123
|
||||
|
||||
var h RequestHeader
|
||||
h.SetMethod(method)
|
||||
h.SetRequestURI("http://localhost/test")
|
||||
h.SetContentType(contentType)
|
||||
h.SetContentLength(contentLength)
|
||||
|
||||
issue6VerifyRequestHeader(t, &h, contentType, contentLength, method)
|
||||
|
||||
if string(h.ContentType()) != contentType {
|
||||
t.Fatalf("unexpected content-type: %q. Expecting %q", h.ContentType(), contentType)
|
||||
}
|
||||
s := h.String()
|
||||
|
||||
var h1 RequestHeader
|
||||
@@ -24,7 +33,21 @@ func TestRequestHeaderSetContentType_Issue6(t *testing.T) {
|
||||
if err := h1.Read(br); err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
if string(h1.ContentType()) != contentType {
|
||||
t.Fatalf("unexpected content-type: %q. Expecting %q", h1.ContentType(), contentType)
|
||||
issue6VerifyRequestHeader(t, &h1, contentType, contentLength, method)
|
||||
}
|
||||
|
||||
func issue6VerifyRequestHeader(t *testing.T, h *RequestHeader, contentType string, contentLength int, method string) {
|
||||
if string(h.ContentType()) != contentType {
|
||||
t.Fatalf("unexpected content-type: %q. Expecting %q. method=%q", h.ContentType(), contentType, method)
|
||||
}
|
||||
if string(h.Method()) != method {
|
||||
t.Fatalf("unexpected method: %q. Expecting %q", h.Method(), method)
|
||||
}
|
||||
if method != "GET" {
|
||||
if h.ContentLength() != contentLength {
|
||||
t.Fatalf("unexpected content-length: %d. Expecting %d. method=%q", h.ContentLength(), contentLength, method)
|
||||
}
|
||||
} else if h.ContentLength() != 0 {
|
||||
t.Fatalf("unexpected content-length for GET method: %d. Expecting 0", h.ContentLength())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ func (req *Request) readLimitBody(r *bufio.Reader, maxBodySize int, getOnly bool
|
||||
return errGetOnly
|
||||
}
|
||||
|
||||
if req.Header.IsPost() || req.Header.IsPut() {
|
||||
if !req.Header.noBody() {
|
||||
contentLength := req.Header.ContentLength()
|
||||
if contentLength > 0 {
|
||||
// Pre-read multipart form data of known length.
|
||||
@@ -426,7 +426,7 @@ func (req *Request) Write(w *bufio.Writer) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if req.Header.IsPost() || req.Header.IsPut() {
|
||||
if !req.Header.noBody() {
|
||||
_, err = w.Write(req.body)
|
||||
} else if len(req.body) > 0 {
|
||||
return fmt.Errorf("Non-zero body for non-POST request. body=%q", req.body)
|
||||
|
||||
Reference in New Issue
Block a user