http2.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. // Package http2 implements the HTTP/2 protocol.
  6. //
  7. // It currently targets draft-13. See http://http2.github.io/
  8. package http2
  9. import (
  10. "bytes"
  11. "crypto/tls"
  12. "io"
  13. "log"
  14. "net/http"
  15. "sync"
  16. )
  17. const (
  18. // ClientPreface is the string that must be sent by new
  19. // connections from clients.
  20. ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  21. )
  22. var (
  23. clientPreface = []byte(ClientPreface)
  24. )
  25. const npnProto = "h2-13"
  26. // Server is an HTTP2 server.
  27. type Server struct {
  28. // MaxStreams optionally ...
  29. MaxStreams int
  30. mu sync.Mutex
  31. }
  32. func (srv *Server) handleClientConn(hs *http.Server, c *tls.Conn, h http.Handler) {
  33. cc := &clientConn{hs, c, h}
  34. cc.serve()
  35. }
  36. type clientConn struct {
  37. hs *http.Server
  38. c *tls.Conn
  39. h http.Handler
  40. }
  41. func (cc *clientConn) logf(format string, args ...interface{}) {
  42. if lg := cc.hs.ErrorLog; lg != nil {
  43. lg.Printf(format, args...)
  44. } else {
  45. log.Printf(format, args...)
  46. }
  47. }
  48. func (cc *clientConn) serve() {
  49. defer cc.c.Close()
  50. log.Printf("HTTP/2 connection from %v on %p", cc.c.RemoteAddr(), cc.hs)
  51. buf := make([]byte, len(ClientPreface))
  52. // TODO: timeout reading from the client
  53. if _, err := io.ReadFull(cc.c, buf); err != nil {
  54. cc.logf("error reading client preface: %v", err)
  55. return
  56. }
  57. if !bytes.Equal(buf, clientPreface) {
  58. cc.logf("bogus greeting from client: %q", buf)
  59. return
  60. }
  61. log.Printf("client %v said hello", cc.c.RemoteAddr())
  62. for {
  63. fh, err := ReadFrameHeader(cc.c)
  64. if err != nil {
  65. if err != io.EOF {
  66. cc.logf("error reading frame: %v", err)
  67. }
  68. return
  69. }
  70. f, err := typeFrameParser(fh.Type)(fh, cc.c)
  71. if h2e, ok := err.(Error); ok {
  72. if h2e.IsConnectionError() {
  73. log.Printf("Disconnection; connection error: %v", err)
  74. return
  75. }
  76. // TODO: stream errors, etc
  77. }
  78. if err != nil {
  79. log.Printf("Disconnection to other error: %v", err)
  80. return
  81. }
  82. log.Printf("read frame: %#v", f)
  83. }
  84. }
  85. // ConfigureServer adds HTTP2 support to s as configured by the HTTP/2
  86. // server configuration in conf. The configuration may be nil.
  87. //
  88. // ConfigureServer must be called before s beings serving.
  89. func ConfigureServer(s *http.Server, conf *Server) {
  90. if conf == nil {
  91. conf = new(Server)
  92. }
  93. if s.TLSConfig == nil {
  94. s.TLSConfig = new(tls.Config)
  95. }
  96. haveNPN := false
  97. for _, p := range s.TLSConfig.NextProtos {
  98. if p == npnProto {
  99. haveNPN = true
  100. break
  101. }
  102. }
  103. if !haveNPN {
  104. s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, npnProto)
  105. }
  106. if s.TLSNextProto == nil {
  107. s.TLSNextProto = map[string]func(*http.Server, *tls.Conn, http.Handler){}
  108. }
  109. s.TLSNextProto[npnProto] = func(hs *http.Server, c *tls.Conn, h http.Handler) {
  110. conf.handleClientConn(hs, c, h)
  111. }
  112. }