Skip to content

Commit

Permalink
feat: improve IsMethod (#1088)
Browse files Browse the repository at this point in the history
* feat: improve bytesEqual

* benchmark

* nolint:unused

* remove unused  code

* Update client.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
  • Loading branch information
tylitianrui and erikdubbelboer authored Sep 6, 2021
1 parent 5d73da3 commit f0a2189
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
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)
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()
}
})
}
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

0 comments on commit f0a2189

Please sign in to comment.