From 01a6a465fc70201239808b5407928c2fb453cf7e Mon Sep 17 00:00:00 2001 From: tongqin <2756145382@qq.com> Date: Fri, 29 Apr 2022 12:00:05 +0800 Subject: [PATCH] optimize(matchers): ReadLine block --- matchers.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/matchers.go b/matchers.go index 878ae98..15547d2 100644 --- a/matchers.go +++ b/matchers.go @@ -86,17 +86,28 @@ func TLS(versions ...int) Matcher { const maxHTTPRead = 4096 +var httpMethodByte = map[byte]struct{}{'G': {}, 'P': {}, 'H': {}, 'D': {}, 'C': {}, 'O': {}, 'T': {}} + // HTTP1 parses the first line or upto 4096 bytes of the request to see if // the conection contains an HTTP request. func HTTP1() Matcher { return func(r io.Reader) bool { br := bufio.NewReader(&io.LimitedReader{R: r, N: maxHTTPRead}) + firstByte, err := br.ReadByte() + if err != nil { + return false + } + _, ok := httpMethodByte[firstByte] + if !ok { + return false + } + l, part, err := br.ReadLine() if err != nil || part { return false } - _, _, proto, ok := parseRequestLine(string(l)) + _, _, proto, ok := parseRequestLine(string(firstByte) + string(l)) if !ok { return false }