Skip to content

Commit

Permalink
enhance: performance
Browse files Browse the repository at this point in the history
  • Loading branch information
huhu415 committed Nov 11, 2024
1 parent e007a88 commit eabc1be
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
48 changes: 38 additions & 10 deletions handle/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,50 @@ import (
"github.com/v2fly/v2ray-core/v5/transport/internet/tcp"
)

var anyMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
http.MethodHead, http.MethodOptions, http.MethodDelete,
http.MethodTrace, "PROPFIND", "PROPPATCH", "MKCOL", "COPY", "MOVE", "LOCK", "UNLOCK",
http.MethodConnect,
var anyMethodSet = map[string]struct{}{
http.MethodGet: {},
http.MethodPost: {},
http.MethodPut: {},
http.MethodPatch: {},
http.MethodHead: {},
http.MethodOptions: {},
http.MethodDelete: {},
http.MethodTrace: {},
http.MethodConnect: {},
"PROPFIND": {},
"PROPPATCH": {},
"MKCOL": {},
"COPY": {},
"MOVE": {},
"LOCK": {},
"UNLOCK": {},
"LINK": {},
"UNLINK": {},
"PURGE": {},
"VIEW": {},
"REPORT": {},
"SEARCH": {},
"CHECKOUT": {},
"CHECKIN": {},
"MERGE": {},
"SUBSCRIBE": {},
"UNSUBSCRIBE": {},
"NOTIFY": {},
}

func isHTTP(peek []byte) bool {
tempPeekString := strings.ToUpper(string(peek))
logrus.Debug(tempPeekString)
for _, m := range anyMethods {
if strings.HasPrefix(tempPeekString, m) {
return true
}

first, _, ok := strings.Cut(tempPeekString, " ")
if !ok {
return false
}

if _, ok := anyMethodSet[first]; !ok {
return false
}
return false
return true
}

func isEnglishLetter(b byte) bool {
Expand Down
55 changes: 55 additions & 0 deletions handle/tools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package handle

import "testing"

func Test_isHTTP(t *testing.T) {
tests := []struct {
peek []byte
want bool
}{
{
peek: []byte("GET /path "), // 10 bytes
want: true,
},
{
peek: []byte("POST /path "), // 10 bytes
want: true,
},
{
peek: []byte("put /path "), // 10 bytes
want: true,
},
{
peek: []byte(" "), // 10 spaces
want: false,
},
{
peek: []byte("INVALID /p "), // 10 bytes
want: false,
},
{
peek: []byte("GET "), // 10 bytes
want: true,
},
{
peek: []byte("GET /p?k=v"), // 10 bytes
want: true,
},
{
peek: []byte("DELETE /pa"), // 10 bytes
want: true,
},
{
peek: []byte("PROPPATCH 123123"), // 10 bytes
want: true,
},
}

for _, tt := range tests {
t.Run("isHttp", func(t *testing.T) {
if got := isHTTP(tt.peek); got != tt.want {
t.Errorf("isHTTP() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit eabc1be

Please sign in to comment.