Return HTTP response error for malformed requests (#6517)

This commit is contained in:
Harshavardhana 2018-09-25 11:07:26 -07:00 committed by kannappanr
parent ec2295c3dc
commit 1e5ac39ff3
2 changed files with 13 additions and 7 deletions

View File

@ -35,6 +35,10 @@ import (
var sslRequiredErrMsg = []byte("HTTP/1.0 403 Forbidden\r\n\r\nSSL required")
var malformedErrMsgFn = func(data interface{}) string {
return fmt.Sprintf("HTTP/1.0 400 Bad Request\r\n\r\n%s", data)
}
// HTTP methods.
var methods = []string{
http.MethodGet,
@ -263,6 +267,7 @@ func (listener *httpListener) start() {
ctx := logger.SetReqInfo(context.Background(), reqInfo)
logger.LogIf(ctx, err)
}
bufconn.Write([]byte(malformedErrMsgFn(err)))
bufconn.Close()
return
}
@ -272,8 +277,9 @@ func (listener *httpListener) start() {
reqInfo := (&logger.ReqInfo{}).AppendTags("remoteAddr", bufconn.RemoteAddr().String())
reqInfo.AppendTags("localAddr", bufconn.LocalAddr().String())
ctx := logger.SetReqInfo(context.Background(), reqInfo)
logger.LogIf(ctx, fmt.Errorf("malformed HTTP invalid HTTP method %s", method))
err = fmt.Errorf("malformed HTTP invalid HTTP method %s", method)
logger.LogIf(ctx, err)
bufconn.Write([]byte(malformedErrMsgFn(err)))
bufconn.Close()
return
}

View File

@ -657,11 +657,11 @@ func TestHTTPListenerAcceptError(t *testing.T) {
continue
}
_, err = bufio.NewReader(conn).ReadString('\n')
if err == nil {
t.Errorf("Test %d: reply read: expected = EOF got = <nil>", i+1)
} else if err.Error() != "EOF" {
t.Errorf("Test %d: reply read: expected = EOF got = %v", i+1, err)
s, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
t.Errorf("Test %d: reply read: expected = <nil>, got = %s", i+1, err)
} else if !strings.Contains(s, "HTTP/1.0 400 Bad Request") {
t.Errorf("Test %d: reply read: expected = 'HTTP/1.0 400 Bad Request' got = %s", i+1, s)
}
conn.Close()