Skip to content

Commit fa38250

Browse files
author
luominggang
committed
Support body with GET method
1 parent a5fce2a commit fa38250

File tree

3 files changed

+9
-19
lines changed

3 files changed

+9
-19
lines changed

header.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,19 +1594,14 @@ func (h *RequestHeader) AppendBytes(dst []byte) []byte {
15941594
dst = appendHeaderLine(dst, strHost, host)
15951595
}
15961596

1597+
// TODO: Here just for a proxy server. For a normal client, we may need to set a default Content-Type
15971598
contentType := h.ContentType()
1598-
if !h.ignoreBody() {
1599-
if len(contentType) == 0 {
1600-
contentType = strPostArgsContentType
1601-
}
1602-
dst = appendHeaderLine(dst, strContentType, contentType)
1603-
1604-
if len(h.contentLengthBytes) > 0 {
1605-
dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
1606-
}
1607-
} else if len(contentType) > 0 {
1599+
if len(contentType) > 0 {
16081600
dst = appendHeaderLine(dst, strContentType, contentType)
16091601
}
1602+
if len(h.contentLengthBytes) > 0 {
1603+
dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
1604+
}
16101605

16111606
for i, n := 0, len(h.h); i < n; i++ {
16121607
kv := &h.h[i]

http.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,20 +1136,18 @@ func (req *Request) Write(w *bufio.Writer) error {
11361136
req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
11371137
}
11381138

1139-
hasBody := !req.Header.ignoreBody()
1139+
if len(body) == 0 && !req.Header.ignoreBody() {
1140+
body = req.postArgs.QueryString()
1141+
}
1142+
hasBody := len(body) > 0
11401143
if hasBody {
1141-
if len(body) == 0 {
1142-
body = req.postArgs.QueryString()
1143-
}
11441144
req.Header.SetContentLength(len(body))
11451145
}
11461146
if err = req.Header.Write(w); err != nil {
11471147
return err
11481148
}
11491149
if hasBody {
11501150
_, err = w.Write(body)
1151-
} else if len(body) > 0 {
1152-
return fmt.Errorf("non-zero body for non-POST request. body=%q", body)
11531151
}
11541152
return err
11551153
}

http_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,9 +1499,6 @@ func testResponseSuccess(t *testing.T, statusCode int, contentType, serverName,
14991499
func TestRequestWriteError(t *testing.T) {
15001500
// no host
15011501
testRequestWriteError(t, "", "/foo/bar", "", "", "")
1502-
1503-
// get with body
1504-
testRequestWriteError(t, "GET", "/foo/bar", "aaa.com", "", "foobar")
15051502
}
15061503

15071504
func testRequestWriteError(t *testing.T, method, requestURI, host, userAgent, body string) {

0 commit comments

Comments
 (0)