Skip to content

Commit f0a2189

Browse files
feat: improve IsMethod (#1088)
* 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>
1 parent 5d73da3 commit f0a2189

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ var clientURLResponseChPool sync.Pool
950950

951951
func clientPostURL(dst []byte, url string, postArgs *Args, c clientDoer) (statusCode int, body []byte, err error) {
952952
req := AcquireRequest()
953-
req.Header.SetMethodBytes(strPost)
953+
req.Header.SetMethod(MethodPost)
954954
req.Header.SetContentTypeBytes(strPostArgsContentType)
955955
if postArgs != nil {
956956
if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {

header.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func (h *RequestHeader) SetRefererBytes(referer []byte) {
443443
// Method returns HTTP request method.
444444
func (h *RequestHeader) Method() []byte {
445445
if len(h.method) == 0 {
446-
return strGet
446+
return []byte(MethodGet)
447447
}
448448
return h.method
449449
}
@@ -503,47 +503,47 @@ func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) {
503503

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

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

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

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

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

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

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

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

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

549549
// IsHTTP11 returns true if the request is HTTP/1.1.

header_timing_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,12 @@ func BenchmarkRemoveNewLines(b *testing.B) {
178178
})
179179
}
180180
}
181+
182+
func BenchmarkRequestHeaderIsGet(b *testing.B) {
183+
req := &RequestHeader{method: []byte(MethodGet)}
184+
b.RunParallel(func(pb *testing.PB) {
185+
for pb.Next() {
186+
req.IsGet()
187+
}
188+
})
189+
}

strings.go

-10
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ var (
2424

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

27-
strGet = []byte(MethodGet)
28-
strHead = []byte(MethodHead)
29-
strPost = []byte(MethodPost)
30-
strPut = []byte(MethodPut)
31-
strDelete = []byte(MethodDelete)
32-
strConnect = []byte(MethodConnect)
33-
strOptions = []byte(MethodOptions)
34-
strTrace = []byte(MethodTrace)
35-
strPatch = []byte(MethodPatch)
36-
3727
strExpect = []byte(HeaderExpect)
3828
strConnection = []byte(HeaderConnection)
3929
strContentLength = []byte(HeaderContentLength)

0 commit comments

Comments
 (0)