Use proper content-type when it is not present (#1023)

Co-authored-by: liuchenxing <liuchenxing@bytedance.com>
This commit is contained in:
MoreFreeze
2021-05-17 15:20:18 +08:00
committed by GitHub
parent 04cde74c41
commit ffa0cabed8
3 changed files with 36 additions and 9 deletions
+8 -8
View File
@@ -32,10 +32,10 @@ type ResponseHeader struct {
noDefaultContentType bool
noDefaultDate bool
statusCode int
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
statusCode int
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
contentType []byte
server []byte
@@ -64,9 +64,9 @@ type RequestHeader struct {
// for reducing RequestHeader object size.
cookiesCollected bool
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
contentLength int
contentLengthBytes []byte
secureErrorLogMessage bool
method []byte
requestURI []byte
@@ -1649,7 +1649,7 @@ func (h *RequestHeader) AppendBytes(dst []byte) []byte {
contentType := h.ContentType()
if len(contentType) == 0 && !h.ignoreBody() {
contentType = strPostArgsContentType
contentType = strDefaultContentType
}
if len(contentType) > 0 {
dst = appendHeaderLine(dst, strContentType, contentType)
+27 -1
View File
@@ -1250,6 +1250,33 @@ func TestResponseContentTypeNoDefaultNotEmpty(t *testing.T) {
}
}
func TestRequestContentTypeDefaultNotEmpty(t *testing.T) {
t.Parallel()
var h RequestHeader
h.SetMethod(MethodPost)
h.SetContentLength(5)
w := &bytes.Buffer{}
bw := bufio.NewWriter(w)
if err := h.Write(bw); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if err := bw.Flush(); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
var h1 RequestHeader
br := bufio.NewReader(w)
if err := h1.Read(br); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if string(h1.contentType) != "application/octet-stream" {
t.Fatalf("unexpected Content-Type %q. Expecting %q", h1.contentType, "application/octet-stream")
}
}
func TestResponseDateNoDefaultNotEmpty(t *testing.T) {
t.Parallel()
@@ -2416,7 +2443,6 @@ func TestResponseHeaderReadErrorSecureLog(t *testing.T) {
testResponseHeaderReadSecuredError(t, h, "HTTP/1.1 123foobar OK\r\nContent-Length: 123\r\nContent-Type: text/html\r\n\r\n")
testResponseHeaderReadSecuredError(t, h, "HTTP/1.1 foobar344 OK\r\nContent-Length: 123\r\nContent-Type: text/html\r\n\r\n")
// no headers
testResponseHeaderReadSecuredError(t, h, "HTTP/1.1 200 OK\r\n")
+1
View File
@@ -77,6 +77,7 @@ var (
strIdentity = []byte("identity")
str100Continue = []byte("100-continue")
strPostArgsContentType = []byte("application/x-www-form-urlencoded")
strDefaultContentType = []byte("application/octet-stream")
strMultipartFormData = []byte("multipart/form-data")
strBoundary = []byte("boundary")
strBytes = []byte("bytes")