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: improve IsMethod #1088

Merged
merged 5 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 9 additions & 9 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,47 +503,47 @@ func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) {

// IsGet returns true if request method is GET.
func (h *RequestHeader) IsGet() bool {
return bytes.Equal(h.Method(), strGet)
return string(h.Method()) == MethodGet
}

// IsPost returns true if request method is POST.
func (h *RequestHeader) IsPost() bool {
return bytes.Equal(h.Method(), strPost)
return string(h.Method()) == MethodPost
}

// IsPut returns true if request method is PUT.
func (h *RequestHeader) IsPut() bool {
return bytes.Equal(h.Method(), strPut)
return string(h.Method()) == MethodPut
}

// IsHead returns true if request method is HEAD.
func (h *RequestHeader) IsHead() bool {
return bytes.Equal(h.Method(), strHead)
return string(h.Method()) == MethodHead
}

// IsDelete returns true if request method is DELETE.
func (h *RequestHeader) IsDelete() bool {
return bytes.Equal(h.Method(), strDelete)
return string(h.Method()) == MethodDelete
}

// IsConnect returns true if request method is CONNECT.
func (h *RequestHeader) IsConnect() bool {
return bytes.Equal(h.Method(), strConnect)
return string(h.Method()) == MethodConnect
}

// IsOptions returns true if request method is OPTIONS.
func (h *RequestHeader) IsOptions() bool {
return bytes.Equal(h.Method(), strOptions)
return string(h.Method()) == MethodOptions
}

// IsTrace returns true if request method is TRACE.
func (h *RequestHeader) IsTrace() bool {
return bytes.Equal(h.Method(), strTrace)
return string(h.Method()) == MethodTrace
}

// IsPatch returns true if request method is PATCH.
func (h *RequestHeader) IsPatch() bool {
return bytes.Equal(h.Method(), strPatch)
return string(h.Method()) == MethodPatch
}

// IsHTTP11 returns true if the request is HTTP/1.1.
Expand Down
70 changes: 70 additions & 0 deletions header_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,73 @@ func BenchmarkRemoveNewLines(b *testing.B) {
})
}
}

func BenchmarkRequestHeaderIsPost(b *testing.B) {
req := &RequestHeader{method: strPost}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsPost()
}
})
}
func BenchmarkRequestHeaderIsHead(b *testing.B) {
req := &RequestHeader{method: strHead}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsHead()
}
})
}
func BenchmarkRequestHeaderIsPut(b *testing.B) {
req := &RequestHeader{method: strPut}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsPut()
}
})
}

func BenchmarkRequestHeaderIsDelete(b *testing.B) {
req := &RequestHeader{method: strDelete}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsDelete()
}
})
}

func BenchmarkRequestHeaderIsConnect(b *testing.B) {
req := &RequestHeader{method: strConnect}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsConnect()
}
})
}

func BenchmarkRequestHeaderIsOptions(b *testing.B) {
req := &RequestHeader{method: strOptions}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsOptions()
}
})
}

func BenchmarkRequestHeaderIsTrace(b *testing.B) {
req := &RequestHeader{method: strTrace}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsTrace()
}
})
}

func BenchmarkRequestHeaderIsPatch(b *testing.B) {
req := &RequestHeader{method: strPatch}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsPatch()
}
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the methods do the same so just a benchmark for IsGet is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx. i will change it.

18 changes: 9 additions & 9 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ var (

strResponseContinue = []byte("HTTP/1.1 100 Continue\r\n\r\n")

strGet = []byte(MethodGet)
strHead = []byte(MethodHead)
strPost = []byte(MethodPost)
strPut = []byte(MethodPut)
strDelete = []byte(MethodDelete)
strConnect = []byte(MethodConnect)
strOptions = []byte(MethodOptions)
strTrace = []byte(MethodTrace)
strPatch = []byte(MethodPatch)
strGet = []byte(MethodGet) //nolint:unused
strHead = []byte(MethodHead) //nolint:unused
strPost = []byte(MethodPost) //nolint:unused
strPut = []byte(MethodPut) //nolint:unused
strDelete = []byte(MethodDelete) //nolint:unused
strConnect = []byte(MethodConnect) //nolint:unused
strOptions = []byte(MethodOptions) //nolint:unused
strTrace = []byte(MethodTrace) //nolint:unused
strPatch = []byte(MethodPatch) //nolint:unused
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they are unused and unexported you can remove them.


strExpect = []byte(HeaderExpect)
strConnection = []byte(HeaderConnection)
Expand Down