2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-20 02:55:46 +08:00

optimize(matchers): ReadLine block

This commit is contained in:
tongqin 2022-04-29 12:00:05 +08:00
parent 5ec6847320
commit 01a6a465fc

View File

@ -86,17 +86,28 @@ func TLS(versions ...int) Matcher {
const maxHTTPRead = 4096 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 // HTTP1 parses the first line or upto 4096 bytes of the request to see if
// the conection contains an HTTP request. // the conection contains an HTTP request.
func HTTP1() Matcher { func HTTP1() Matcher {
return func(r io.Reader) bool { return func(r io.Reader) bool {
br := bufio.NewReader(&io.LimitedReader{R: r, N: maxHTTPRead}) 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() l, part, err := br.ReadLine()
if err != nil || part { if err != nil || part {
return false return false
} }
_, _, proto, ok := parseRequestLine(string(l)) _, _, proto, ok := parseRequestLine(string(firstByte) + string(l))
if !ok { if !ok {
return false return false
} }