mirror of
https://github.com/soheilhy/cmux.git
synced 2024-11-10 11:41:52 +08:00
4146b137dd
go/src/github.com/soheilhy/cmux/cmux.go:127:13 c.Close() go/src/github.com/soheilhy/cmux/cmux.go:134:9 c.Close() go/src/github.com/soheilhy/cmux/cmux.go:137:15 m.root.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:43:9 s.Serve(l) go/src/github.com/soheilhy/cmux/cmux_test.go:52:20 defer r.Body.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:72:12 s.Register(TestRPCRcvr{}) go/src/github.com/soheilhy/cmux/cmux_test.go:103:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:109:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/cmux_test.go:116:20 defer r.Body.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:125:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:133:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/cmux_test.go:141:15 defer l.Close() go/src/github.com/soheilhy/cmux/cmux_test.go:147:15 go muxl.Serve() go/src/github.com/soheilhy/cmux/example_recursive_test.go:27:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_recursive_test.go:56:12 s.Register(&RecursiveRPCRcvr{}) go/src/github.com/soheilhy/cmux/example_recursive_test.go:88:15 go tlsm.Serve() go/src/github.com/soheilhy/cmux/example_recursive_test.go:89:12 tcpm.Serve() go/src/github.com/soheilhy/cmux/example_test.go:30:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:34:9 io.Copy(ws, ws) go/src/github.com/soheilhy/cmux/example_test.go:41:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:53:12 s.Register(&ExampleRPCRcvr{}) go/src/github.com/soheilhy/cmux/example_test.go:68:13 grpcs.Serve(l) go/src/github.com/soheilhy/cmux/example_test.go:97:9 m.Serve() go/src/github.com/soheilhy/cmux/example_tls_test.go:24:9 s.Serve(l) go/src/github.com/soheilhy/cmux/example_tls_test.go:69:9 m.Serve() go/src/github.com/soheilhy/cmux/matchers.go:151:14 hdec.Write(f.HeaderBlockFragment())
164 lines
3.5 KiB
Go
164 lines
3.5 KiB
Go
package cmux
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"golang.org/x/net/http2"
|
|
"golang.org/x/net/http2/hpack"
|
|
)
|
|
|
|
// Any is a Matcher that matches any connection.
|
|
func Any() Matcher {
|
|
return func(r io.Reader) bool { return true }
|
|
}
|
|
|
|
// PrefixMatcher returns a matcher that matches a connection if it
|
|
// starts with any of the strings in strs.
|
|
func PrefixMatcher(strs ...string) Matcher {
|
|
pt := newPatriciaTreeString(strs...)
|
|
return func(r io.Reader) bool {
|
|
return pt.matchPrefix(r)
|
|
}
|
|
}
|
|
|
|
var defaultHTTPMethods = []string{
|
|
"OPTIONS",
|
|
"GET",
|
|
"HEAD",
|
|
"POST",
|
|
"PUT",
|
|
"DELETE",
|
|
"TRACE",
|
|
"CONNECT",
|
|
}
|
|
|
|
// HTTP1Fast only matches the methods in the HTTP request.
|
|
//
|
|
// This matcher is very optimistic: if it returns true, it does not mean that
|
|
// the request is a valid HTTP response. If you want a correct but slower HTTP1
|
|
// matcher, use HTTP1 instead.
|
|
func HTTP1Fast(extMethods ...string) Matcher {
|
|
return PrefixMatcher(append(defaultHTTPMethods, extMethods...)...)
|
|
}
|
|
|
|
const (
|
|
maxHTTPRead = 4096
|
|
)
|
|
|
|
// 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})
|
|
l, part, err := br.ReadLine()
|
|
if err != nil || part {
|
|
return false
|
|
}
|
|
|
|
_, _, proto, ok := parseRequestLine(string(l))
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
v, _, ok := http.ParseHTTPVersion(proto)
|
|
return ok && v == 1
|
|
}
|
|
}
|
|
|
|
// grabbed from net/http.
|
|
func parseRequestLine(line string) (method, uri, proto string, ok bool) {
|
|
s1 := strings.Index(line, " ")
|
|
s2 := strings.Index(line[s1+1:], " ")
|
|
if s1 < 0 || s2 < 0 {
|
|
return
|
|
}
|
|
s2 += s1 + 1
|
|
return line[:s1], line[s1+1 : s2], line[s2+1:], true
|
|
}
|
|
|
|
var (
|
|
http2Preface = []byte(http2.ClientPreface)
|
|
)
|
|
|
|
// HTTP2 parses the frame header of the first frame to detect whether the
|
|
// connection is an HTTP2 connection.
|
|
func HTTP2() Matcher {
|
|
return func(r io.Reader) bool {
|
|
return hasHTTP2Preface(r)
|
|
}
|
|
}
|
|
|
|
// HTTP1HeaderField returns a matcher matching the header fields of the first
|
|
// request of an HTTP 1 connection.
|
|
func HTTP1HeaderField(name, value string) Matcher {
|
|
return func(r io.Reader) bool {
|
|
return matchHTTP1Field(r, name, value)
|
|
}
|
|
}
|
|
|
|
// HTTP2HeaderField resturns a matcher matching the header fields of the first
|
|
// headers frame.
|
|
func HTTP2HeaderField(name, value string) Matcher {
|
|
return func(r io.Reader) bool {
|
|
return matchHTTP2Field(r, name, value)
|
|
}
|
|
}
|
|
|
|
func hasHTTP2Preface(r io.Reader) (ok bool) {
|
|
b := make([]byte, len(http2Preface))
|
|
n, err := r.Read(b)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
b = b[:n]
|
|
return bytes.Equal(b, http2Preface)
|
|
}
|
|
|
|
func matchHTTP1Field(r io.Reader, name, value string) (matched bool) {
|
|
req, err := http.ReadRequest(bufio.NewReader(r))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return req.Header.Get(name) == value
|
|
}
|
|
|
|
func matchHTTP2Field(r io.Reader, name, value string) (matched bool) {
|
|
if !hasHTTP2Preface(r) {
|
|
return false
|
|
}
|
|
|
|
framer := http2.NewFramer(ioutil.Discard, r)
|
|
hdec := hpack.NewDecoder(uint32(4<<10), func(hf hpack.HeaderField) {
|
|
if hf.Name == name && hf.Value == value {
|
|
matched = true
|
|
}
|
|
})
|
|
for {
|
|
f, err := framer.ReadFrame()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
switch f := f.(type) {
|
|
case *http2.HeadersFrame:
|
|
if _, err := hdec.Write(f.HeaderBlockFragment()); err != nil {
|
|
return false
|
|
}
|
|
if matched {
|
|
return true
|
|
}
|
|
|
|
if f.FrameHeader.Flags&http2.FlagHeadersEndHeaders != 0 {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|