Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WithDisableDefaultDate, WithDisableDefaultContentType for server config options #985

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/app/server/hertz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,29 @@ func TestValidateConfigAndBindConfig(t *testing.T) {
assert.Nil(t, err)
time.Sleep(100 * time.Millisecond)
}

func TestWithDisableDefaultDate(t *testing.T) {
h := New(
WithHostPorts("localhost:8321"),
WithDisableDefaultDate(true),
)
h.GET("/", func(_ context.Context, c *app.RequestContext) {})
go h.Spin()
time.Sleep(100 * time.Millisecond)
hc := http.Client{Timeout: time.Second}
r, _ := hc.Get("http://127.0.0.1:8321") //nolint:errcheck
assert.DeepEqual(t, "", r.Header.Get("Date"))
}

func TestWithDisableDefaultContentType(t *testing.T) {
h := New(
WithHostPorts("localhost:8324"),
WithDisableDefaultContentType(true),
)
h.GET("/", func(_ context.Context, c *app.RequestContext) {})
go h.Spin()
time.Sleep(100 * time.Millisecond)
hc := http.Client{Timeout: time.Second}
r, _ := hc.Get("http://127.0.0.1:8324") //nolint:errcheck
assert.DeepEqual(t, "", r.Header.Get("Content-Type"))
}
12 changes: 12 additions & 0 deletions pkg/app/server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,15 @@ func WithDisableHeaderNamesNormalizing(disable bool) config.Option {
o.DisableHeaderNamesNormalizing = disable
}}
}

func WithDisableDefaultDate(disable bool) config.Option {
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
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
}}
}
8 changes: 8 additions & 0 deletions pkg/common/config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type Options struct {
RemoveExtraSlash bool
UnescapePathValues bool
DisablePreParseMultipartForm bool
NoDefaultDate bool
NoDefaultContentType bool
StreamRequestBody bool
NoDefaultServerHeader bool
DisablePrintRoute bool
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions pkg/protocol/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
12 changes: 12 additions & 0 deletions pkg/protocol/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,15 @@ func TestResponseHeaderCopyTo(t *testing.T) {
assert.DeepEqual(t, hCopy.noDefaultContentType, true)
assert.DeepEqual(t, hCopy.GetHeaderLength(), 100)
}

func TestResponseHeaderDateEmpty(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
}
}
5 changes: 5 additions & 0 deletions pkg/protocol/http1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var (
type Option struct {
StreamRequestBody bool
GetOnly bool
NoDefaultDate bool
NoDefaultContentType bool
DisablePreParseMultipartForm bool
DisableKeepalive bool
NoDefaultServerHeader bool
Expand Down Expand Up @@ -181,6 +183,9 @@ func (s Server) Serve(c context.Context, conn network.Conn) (err error) {
})
}

ctx.Response.Header.SetNoDefaultDate(s.NoDefaultDate)
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
ctx.Response.Header.SetNoDefaultContentType(s.NoDefaultContentType)

if s.DisableHeaderNamesNormalizing {
ctx.Request.Header.DisableNormalizing()
ctx.Response.Header.DisableNormalizing()
Expand Down
2 changes: 2 additions & 0 deletions pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,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.
Expand Down