http2.go 3.1 KB

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