aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-02 20:17:50 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-02 20:17:50 +0200
commitfe212fe380a7ed880fa463beb7e6dbaea2e69d26 (patch)
tree0ea4357606ce16209fb90924177f204b499aaa1e
parentAdded client benchmarks into readme (diff)
downloadfasthttp-fe212fe380a7ed880fa463beb7e6dbaea2e69d26.tar.gz
fasthttp-fe212fe380a7ed880fa463beb7e6dbaea2e69d26.tar.bz2
fasthttp-fe212fe380a7ed880fa463beb7e6dbaea2e69d26.zip
Issue #6: Add support for PATCH method and other methods which may contain body
-rw-r--r--header.go12
-rw-r--r--header_regression_test.go35
-rw-r--r--http.go4
3 files changed, 39 insertions, 12 deletions
diff --git a/header.go b/header.go
index 04c5a3c..ce8b7dd 100644
--- a/header.go
+++ b/header.go
@@ -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]
}
diff --git a/header_regression_test.go b/header_regression_test.go
index e7530d9..f3c425f 100644
--- a/header_regression_test.go
+++ b/header_regression_test.go
@@ -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())
}
}
diff --git a/http.go b/http.go
index 43ef23a..2266c64 100644
--- a/http.go
+++ b/http.go
@@ -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)