Skip to content
New issue

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

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

Already on GitHub? # 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 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
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ var clientURLResponseChPool sync.Pool

func clientPostURL(dst []byte, url string, postArgs *Args, c clientDoer) (statusCode int, body []byte, err error) {
req := AcquireRequest()
req.Header.SetMethodBytes(strPost)
req.Header.SetMethod(MethodPost)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IT IS BETTER THAN MINE. THX

req.Header.SetContentTypeBytes(strPostArgsContentType)
if postArgs != nil {
if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (h *RequestHeader) SetRefererBytes(referer []byte) {
// Method returns HTTP request method.
func (h *RequestHeader) Method() []byte {
if len(h.method) == 0 {
return strGet
return []byte(MethodGet)
}
return h.method
}
Expand Down 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
9 changes: 9 additions & 0 deletions header_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,12 @@ func BenchmarkRemoveNewLines(b *testing.B) {
})
}
}

func BenchmarkRequestHeaderIsGet(b *testing.B) {
req := &RequestHeader{method: []byte(MethodGet)}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
req.IsGet()
}
})
}
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.

10 changes: 0 additions & 10 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ 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)

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