Skip to content

Commit

Permalink
feat: add WithDisableDefaultDate, WithDisableDefaultContentType for s…
Browse files Browse the repository at this point in the history
…erver config options (#985)
  • Loading branch information
Skyenought committed Nov 13, 2023
1 parent 4e9bafb commit 837aa65
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
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 {
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)
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

0 comments on commit 837aa65

Please sign in to comment.