http2.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // This is a work in progress. This package is low-level and intended
  10. // to be used directly by very few people. Most users will use it
  11. // indirectly through integration with the net/http package. See
  12. // ConfigureServer. That ConfigureServer call will likely be automatic
  13. // or available via an empty import in the future.
  14. //
  15. // This package currently targets draft-14. See http://http2.github.io/
  16. package http2
  17. import (
  18. "fmt"
  19. "net/http"
  20. "strconv"
  21. "sync"
  22. )
  23. var VerboseLogs = false
  24. const (
  25. // ClientPreface is the string that must be sent by new
  26. // connections from clients.
  27. ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  28. // SETTINGS_MAX_FRAME_SIZE default
  29. // http://http2.github.io/http2-spec/#rfc.section.6.5.2
  30. initialMaxFrameSize = 16384
  31. npnProto = "h2-14"
  32. // http://http2.github.io/http2-spec/#SettingValues
  33. initialHeaderTableSize = 4096
  34. initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
  35. defaultMaxReadFrameSize = 1 << 20
  36. )
  37. var (
  38. clientPreface = []byte(ClientPreface)
  39. )
  40. type streamState int
  41. const (
  42. stateIdle streamState = iota
  43. stateOpen
  44. stateHalfClosedLocal
  45. stateHalfClosedRemote
  46. stateResvLocal
  47. stateResvRemote
  48. stateClosed
  49. )
  50. var stateName = [...]string{
  51. stateIdle: "Idle",
  52. stateOpen: "Open",
  53. stateHalfClosedLocal: "HalfClosedLocal",
  54. stateHalfClosedRemote: "HalfClosedRemote",
  55. stateResvLocal: "ResvLocal",
  56. stateResvRemote: "ResvRemote",
  57. stateClosed: "Closed",
  58. }
  59. func (st streamState) String() string {
  60. return stateName[st]
  61. }
  62. // Setting is a setting parameter: which setting it is, and its value.
  63. type Setting struct {
  64. // ID is which setting is being set.
  65. // See http://http2.github.io/http2-spec/#SettingValues
  66. ID SettingID
  67. // Val is the value.
  68. Val uint32
  69. }
  70. func (s Setting) String() string {
  71. return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
  72. }
  73. // Valid reports whether the setting is valid.
  74. func (s Setting) Valid() error {
  75. // Limits and error codes from 6.5.2 Defined SETTINGS Parameters
  76. switch s.ID {
  77. case SettingEnablePush:
  78. if s.Val != 1 && s.Val != 0 {
  79. return ConnectionError(ErrCodeProtocol)
  80. }
  81. case SettingInitialWindowSize:
  82. if s.Val > 1<<31-1 {
  83. return ConnectionError(ErrCodeFlowControl)
  84. }
  85. case SettingMaxFrameSize:
  86. if s.Val < 16384 || s.Val > 1<<24-1 {
  87. return ConnectionError(ErrCodeProtocol)
  88. }
  89. }
  90. return nil
  91. }
  92. // A SettingID is an HTTP/2 setting as defined in
  93. // http://http2.github.io/http2-spec/#iana-settings
  94. type SettingID uint16
  95. const (
  96. SettingHeaderTableSize SettingID = 0x1
  97. SettingEnablePush SettingID = 0x2
  98. SettingMaxConcurrentStreams SettingID = 0x3
  99. SettingInitialWindowSize SettingID = 0x4
  100. SettingMaxFrameSize SettingID = 0x5
  101. SettingMaxHeaderListSize SettingID = 0x6
  102. )
  103. var settingName = map[SettingID]string{
  104. SettingHeaderTableSize: "HEADER_TABLE_SIZE",
  105. SettingEnablePush: "ENABLE_PUSH",
  106. SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
  107. SettingInitialWindowSize: "INITIAL_WINDOW_SIZE",
  108. SettingMaxFrameSize: "MAX_FRAME_SIZE",
  109. SettingMaxHeaderListSize: "MAX_HEADER_LIST_SIZE",
  110. }
  111. func (s SettingID) String() string {
  112. if v, ok := settingName[s]; ok {
  113. return v
  114. }
  115. return fmt.Sprintf("UNKNOWN_SETTING_%d", uint8(s))
  116. }
  117. func validHeader(v string) bool {
  118. if len(v) == 0 {
  119. return false
  120. }
  121. for _, r := range v {
  122. // "Just as in HTTP/1.x, header field names are
  123. // strings of ASCII characters that are compared in a
  124. // case-insensitive fashion. However, header field
  125. // names MUST be converted to lowercase prior to their
  126. // encoding in HTTP/2. "
  127. if r >= 127 || ('A' <= r && r <= 'Z') {
  128. return false
  129. }
  130. }
  131. return true
  132. }
  133. var httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
  134. func init() {
  135. for i := 100; i <= 999; i++ {
  136. if v := http.StatusText(i); v != "" {
  137. httpCodeStringCommon[i] = strconv.Itoa(i)
  138. }
  139. }
  140. }
  141. func httpCodeString(code int) string {
  142. if s, ok := httpCodeStringCommon[code]; ok {
  143. return s
  144. }
  145. return strconv.Itoa(code)
  146. }
  147. // from pkg io
  148. type stringWriter interface {
  149. WriteString(s string) (n int, err error)
  150. }
  151. // A gate lets two goroutines coordinate their activities.
  152. type gate chan struct{}
  153. func (g gate) Done() { g <- struct{}{} }
  154. func (g gate) Wait() { <-g }
  155. // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
  156. type closeWaiter struct {
  157. m sync.Mutex
  158. c sync.Cond
  159. closed bool
  160. }
  161. // Init makes a closeWaiter usable.
  162. // It exists because so a closeWaiter value can be placed inside a
  163. // larger struct and have the Mutex and Cond's memory in the same
  164. // allocation.
  165. func (cw *closeWaiter) Init() {
  166. cw.c.L = &cw.m
  167. }
  168. // Close marks the closeWwaiter as closed and unblocks any waiters.
  169. func (cw *closeWaiter) Close() {
  170. cw.m.Lock()
  171. cw.closed = true
  172. cw.m.Unlock()
  173. cw.c.Broadcast()
  174. }
  175. // Wait waits for the closeWaiter to become closed.
  176. func (cw *closeWaiter) Wait() {
  177. cw.m.Lock()
  178. defer cw.m.Unlock()
  179. for !cw.closed {
  180. cw.c.Wait()
  181. }
  182. }