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

76 lines
1.5 KiB
Go
Raw Normal View History

package cmux_test
import (
"crypto/rand"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
errcheck 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())
2016-01-07 23:15:03 +08:00
"strings"
"github.com/soheilhy/cmux"
)
type anotherHTTPHandler struct{}
func (h *anotherHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "example http response")
}
func serveHTTP1(l net.Listener) {
s := &http.Server{
Handler: &anotherHTTPHandler{},
}
errcheck 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())
2016-01-07 23:15:03 +08:00
if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
}
func serveHTTPS(l net.Listener) {
// Load certificates.
certificate, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
if err != nil {
2016-02-21 23:46:12 +08:00
log.Panic(err)
}
config := &tls.Config{
Certificates: []tls.Certificate{certificate},
Rand: rand.Reader,
}
// Create TLS listener.
tlsl := tls.NewListener(l, config)
// Serve HTTP over TLS.
serveHTTP1(tlsl)
}
2015-08-02 00:07:30 +08:00
// This is an example for serving HTTP and HTTPS on the same port.
func Example_bothHTTPAndHTTPS() {
// Create the TCP listener.
l, err := net.Listen("tcp", "127.0.0.1:50051")
if err != nil {
2016-02-21 23:46:12 +08:00
log.Panic(err)
}
// Create a mux.
m := cmux.New(l)
// We first match on HTTP 1.1 methods.
httpl := m.Match(cmux.HTTP1Fast())
// If not matched, we assume that its TLS.
//
// Note that you can take this listener, do TLS handshake and
// create another mux to multiplex the connections over TLS.
tlsl := m.Match(cmux.Any())
go serveHTTP1(httpl)
go serveHTTPS(tlsl)
errcheck 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())
2016-01-07 23:15:03 +08:00
if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
}