diff --git a/pkg/app/server/option.go b/pkg/app/server/option.go index fcf380485..e7970348c 100644 --- a/pkg/app/server/option.go +++ b/pkg/app/server/option.go @@ -382,3 +382,15 @@ func WithDisableHeaderNamesNormalizing(disable bool) config.Option { o.DisableHeaderNamesNormalizing = disable }} } + +func WithDisableDefaultDate(disable bool) config.Option { + return config.Option{F: func(o *config.Options) { + o.NoDefaultDate = disable + }} +} + +func WithDisableDefaultContentType(disable bool) config.Option { + return config.Option{F: func(o *config.Options) { + o.NoDefaultContentType = disable + }} +} diff --git a/pkg/common/config/option.go b/pkg/common/config/option.go index 048fb366f..417955fc9 100644 --- a/pkg/common/config/option.go +++ b/pkg/common/config/option.go @@ -58,6 +58,8 @@ type Options struct { RemoveExtraSlash bool UnescapePathValues bool DisablePreParseMultipartForm bool + NoDefaultDate bool + NoDefaultContentType bool StreamRequestBody bool NoDefaultServerHeader bool DisablePrintRoute bool @@ -191,6 +193,12 @@ func NewOptions(opts []Option) *Options { // like they are normal requests DisablePreParseMultipartForm: false, + // When set to true, causes the default Content-Type header to be excluded from the response. + NoDefaultContentType: false, + + // When set to true, causes the default date header to be excluded from the response. + NoDefaultDate: false, + // Routes info printing is not disabled by default // Disabled when set to True DisablePrintRoute: false, diff --git a/pkg/protocol/header.go b/pkg/protocol/header.go index 14df744ef..7edf19b18 100644 --- a/pkg/protocol/header.go +++ b/pkg/protocol/header.go @@ -950,6 +950,11 @@ func (h *ResponseHeader) SetNoDefaultContentType(b bool) { h.noDefaultContentType = b } +// SetNoDefaultDate set noDefaultDate value of ResponseHeader. +func (h *ResponseHeader) SetNoDefaultDate(b bool) { + h.noDefaultDate = b +} + // SetServerBytes sets Server header value. func (h *ResponseHeader) SetServerBytes(server []byte) { h.server = append(h.server[:0], server...) diff --git a/pkg/protocol/header_test.go b/pkg/protocol/header_test.go index 8d768fd78..a2123df74 100644 --- a/pkg/protocol/header_test.go +++ b/pkg/protocol/header_test.go @@ -720,3 +720,18 @@ func TestResponseHeaderCopyTo(t *testing.T) { assert.DeepEqual(t, hCopy.noDefaultContentType, true) assert.DeepEqual(t, hCopy.GetHeaderLength(), 100) } + +func TestResponseDateNoDefaultNotEmpty(t *testing.T) { + t.Parallel() + + var h ResponseHeader + h.noDefaultDate = true + headers := string(h.Header()) + + if strings.Contains(headers, "\r\nDate: ") { + t.Fatalf("ResponseDateNoDefaultNotEmpty fail, response: \n%+v\noutcome: \n%q\n", h, headers) //nolint:govet + } +} + +func TestResponseDateNoDefaultEmpty(t *testing.T) { +} diff --git a/pkg/protocol/http1/server.go b/pkg/protocol/http1/server.go index 66cb01e85..aa2db7422 100644 --- a/pkg/protocol/http1/server.go +++ b/pkg/protocol/http1/server.go @@ -56,6 +56,8 @@ var ( type Option struct { StreamRequestBody bool GetOnly bool + NoDefaultDate bool + NoDefaultContentType bool DisablePreParseMultipartForm bool DisableKeepalive bool NoDefaultServerHeader bool @@ -181,6 +183,9 @@ func (s Server) Serve(c context.Context, conn network.Conn) (err error) { }) } + ctx.Response.Header.SetNoDefaultDate(s.NoDefaultDate) + ctx.Response.Header.SetNoDefaultContentType(s.NoDefaultContentType) + if s.DisableHeaderNamesNormalizing { ctx.Request.Header.DisableNormalizing() ctx.Response.Header.DisableNormalizing() diff --git a/pkg/route/engine.go b/pkg/route/engine.go index 168c79b48..f2b2071b4 100644 --- a/pkg/route/engine.go +++ b/pkg/route/engine.go @@ -1056,6 +1056,8 @@ func newHttp1OptionFromEngine(engine *Engine) *http1.Option { EnableTrace: engine.IsTraceEnable(), HijackConnHandle: engine.HijackConnHandle, DisableHeaderNamesNormalizing: engine.options.DisableHeaderNamesNormalizing, + NoDefaultDate: engine.options.NoDefaultDate, + NoDefaultContentType: engine.options.NoDefaultContentType, } // Idle timeout of standard network must not be zero. Set it to -1 seconds if it is zero. // Due to the different triggering ways of the network library, see the actual use of this value for the detailed reasons.