http2.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. // Package http2 implements the HTTP/2 protocol.
  5. //
  6. // This package is low-level and intended to be used directly by very
  7. // few people. Most users will use it indirectly through the automatic
  8. // use by the net/http package (from Go 1.6 and later).
  9. // For use in earlier Go versions see ConfigureServer. (Transport support
  10. // requires Go 1.6 or later)
  11. //
  12. // See https://http2.github.io/ for more information on HTTP/2.
  13. package http2 // import "golang.org/x/net/http2"
  14. import (
  15. "bufio"
  16. "crypto/tls"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "os"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "time"
  27. "golang.org/x/net/http/httpguts"
  28. )
  29. var (
  30. VerboseLogs bool
  31. logFrameWrites bool
  32. logFrameReads bool
  33. // Enabling extended CONNECT by causes browsers to attempt to use
  34. // WebSockets-over-HTTP/2. This results in problems when the server's websocket
  35. // package doesn't support extended CONNECT.
  36. //
  37. // Disable extended CONNECT by default for now.
  38. //
  39. // Issue #71128.
  40. disableExtendedConnectProtocol = true
  41. )
  42. func init() {
  43. e := os.Getenv("GODEBUG")
  44. if strings.Contains(e, "http2debug=1") {
  45. VerboseLogs = true
  46. }
  47. if strings.Contains(e, "http2debug=2") {
  48. VerboseLogs = true
  49. logFrameWrites = true
  50. logFrameReads = true
  51. }
  52. if strings.Contains(e, "http2xconnect=1") {
  53. disableExtendedConnectProtocol = false
  54. }
  55. }
  56. const (
  57. // ClientPreface is the string that must be sent by new
  58. // connections from clients.
  59. ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  60. // SETTINGS_MAX_FRAME_SIZE default
  61. // https://httpwg.org/specs/rfc7540.html#rfc.section.6.5.2
  62. initialMaxFrameSize = 16384
  63. // NextProtoTLS is the NPN/ALPN protocol negotiated during
  64. // HTTP/2's TLS setup.
  65. NextProtoTLS = "h2"
  66. // https://httpwg.org/specs/rfc7540.html#SettingValues
  67. initialHeaderTableSize = 4096
  68. initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
  69. defaultMaxReadFrameSize = 1 << 20
  70. )
  71. var (
  72. clientPreface = []byte(ClientPreface)
  73. )
  74. type streamState int
  75. // HTTP/2 stream states.
  76. //
  77. // See http://tools.ietf.org/html/rfc7540#section-5.1.
  78. //
  79. // For simplicity, the server code merges "reserved (local)" into
  80. // "half-closed (remote)". This is one less state transition to track.
  81. // The only downside is that we send PUSH_PROMISEs slightly less
  82. // liberally than allowable. More discussion here:
  83. // https://lists.w3.org/Archives/Public/ietf-http-wg/2016JulSep/0599.html
  84. //
  85. // "reserved (remote)" is omitted since the client code does not
  86. // support server push.
  87. const (
  88. stateIdle streamState = iota
  89. stateOpen
  90. stateHalfClosedLocal
  91. stateHalfClosedRemote
  92. stateClosed
  93. )
  94. var stateName = [...]string{
  95. stateIdle: "Idle",
  96. stateOpen: "Open",
  97. stateHalfClosedLocal: "HalfClosedLocal",
  98. stateHalfClosedRemote: "HalfClosedRemote",
  99. stateClosed: "Closed",
  100. }
  101. func (st streamState) String() string {
  102. return stateName[st]
  103. }
  104. // Setting is a setting parameter: which setting it is, and its value.
  105. type Setting struct {
  106. // ID is which setting is being set.
  107. // See https://httpwg.org/specs/rfc7540.html#SettingFormat
  108. ID SettingID
  109. // Val is the value.
  110. Val uint32
  111. }
  112. func (s Setting) String() string {
  113. return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
  114. }
  115. // Valid reports whether the setting is valid.
  116. func (s Setting) Valid() error {
  117. // Limits and error codes from 6.5.2 Defined SETTINGS Parameters
  118. switch s.ID {
  119. case SettingEnablePush:
  120. if s.Val != 1 && s.Val != 0 {
  121. return ConnectionError(ErrCodeProtocol)
  122. }
  123. case SettingInitialWindowSize:
  124. if s.Val > 1<<31-1 {
  125. return ConnectionError(ErrCodeFlowControl)
  126. }
  127. case SettingMaxFrameSize:
  128. if s.Val < 16384 || s.Val > 1<<24-1 {
  129. return ConnectionError(ErrCodeProtocol)
  130. }
  131. case SettingEnableConnectProtocol:
  132. if s.Val != 1 && s.Val != 0 {
  133. return ConnectionError(ErrCodeProtocol)
  134. }
  135. }
  136. return nil
  137. }
  138. // A SettingID is an HTTP/2 setting as defined in
  139. // https://httpwg.org/specs/rfc7540.html#iana-settings
  140. type SettingID uint16
  141. const (
  142. SettingHeaderTableSize SettingID = 0x1
  143. SettingEnablePush SettingID = 0x2
  144. SettingMaxConcurrentStreams SettingID = 0x3
  145. SettingInitialWindowSize SettingID = 0x4
  146. SettingMaxFrameSize SettingID = 0x5
  147. SettingMaxHeaderListSize SettingID = 0x6
  148. SettingEnableConnectProtocol SettingID = 0x8
  149. )
  150. var settingName = map[SettingID]string{
  151. SettingHeaderTableSize: "HEADER_TABLE_SIZE",
  152. SettingEnablePush: "ENABLE_PUSH",
  153. SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
  154. SettingInitialWindowSize: "INITIAL_WINDOW_SIZE",
  155. SettingMaxFrameSize: "MAX_FRAME_SIZE",
  156. SettingMaxHeaderListSize: "MAX_HEADER_LIST_SIZE",
  157. SettingEnableConnectProtocol: "ENABLE_CONNECT_PROTOCOL",
  158. }
  159. func (s SettingID) String() string {
  160. if v, ok := settingName[s]; ok {
  161. return v
  162. }
  163. return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s))
  164. }
  165. // validWireHeaderFieldName reports whether v is a valid header field
  166. // name (key). See httpguts.ValidHeaderName for the base rules.
  167. //
  168. // Further, http2 says:
  169. //
  170. // "Just as in HTTP/1.x, header field names are strings of ASCII
  171. // characters that are compared in a case-insensitive
  172. // fashion. However, header field names MUST be converted to
  173. // lowercase prior to their encoding in HTTP/2. "
  174. func validWireHeaderFieldName(v string) bool {
  175. if len(v) == 0 {
  176. return false
  177. }
  178. for _, r := range v {
  179. if !httpguts.IsTokenRune(r) {
  180. return false
  181. }
  182. if 'A' <= r && r <= 'Z' {
  183. return false
  184. }
  185. }
  186. return true
  187. }
  188. func httpCodeString(code int) string {
  189. switch code {
  190. case 200:
  191. return "200"
  192. case 404:
  193. return "404"
  194. }
  195. return strconv.Itoa(code)
  196. }
  197. // from pkg io
  198. type stringWriter interface {
  199. WriteString(s string) (n int, err error)
  200. }
  201. // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
  202. type closeWaiter chan struct{}
  203. // Init makes a closeWaiter usable.
  204. // It exists because so a closeWaiter value can be placed inside a
  205. // larger struct and have the Mutex and Cond's memory in the same
  206. // allocation.
  207. func (cw *closeWaiter) Init() {
  208. *cw = make(chan struct{})
  209. }
  210. // Close marks the closeWaiter as closed and unblocks any waiters.
  211. func (cw closeWaiter) Close() {
  212. close(cw)
  213. }
  214. // Wait waits for the closeWaiter to become closed.
  215. func (cw closeWaiter) Wait() {
  216. <-cw
  217. }
  218. // bufferedWriter is a buffered writer that writes to w.
  219. // Its buffered writer is lazily allocated as needed, to minimize
  220. // idle memory usage with many connections.
  221. type bufferedWriter struct {
  222. _ incomparable
  223. conn net.Conn // immutable
  224. bw *bufio.Writer // non-nil when data is buffered
  225. byteTimeout time.Duration // immutable, WriteByteTimeout
  226. }
  227. func newBufferedWriter(conn net.Conn, timeout time.Duration) *bufferedWriter {
  228. return &bufferedWriter{
  229. conn: conn,
  230. byteTimeout: timeout,
  231. }
  232. }
  233. // bufWriterPoolBufferSize is the size of bufio.Writer's
  234. // buffers created using bufWriterPool.
  235. //
  236. // TODO: pick a less arbitrary value? this is a bit under
  237. // (3 x typical 1500 byte MTU) at least. Other than that,
  238. // not much thought went into it.
  239. const bufWriterPoolBufferSize = 4 << 10
  240. var bufWriterPool = sync.Pool{
  241. New: func() interface{} {
  242. return bufio.NewWriterSize(nil, bufWriterPoolBufferSize)
  243. },
  244. }
  245. func (w *bufferedWriter) Available() int {
  246. if w.bw == nil {
  247. return bufWriterPoolBufferSize
  248. }
  249. return w.bw.Available()
  250. }
  251. func (w *bufferedWriter) Write(p []byte) (n int, err error) {
  252. if w.bw == nil {
  253. bw := bufWriterPool.Get().(*bufio.Writer)
  254. bw.Reset((*bufferedWriterTimeoutWriter)(w))
  255. w.bw = bw
  256. }
  257. return w.bw.Write(p)
  258. }
  259. func (w *bufferedWriter) Flush() error {
  260. bw := w.bw
  261. if bw == nil {
  262. return nil
  263. }
  264. err := bw.Flush()
  265. bw.Reset(nil)
  266. bufWriterPool.Put(bw)
  267. w.bw = nil
  268. return err
  269. }
  270. type bufferedWriterTimeoutWriter bufferedWriter
  271. func (w *bufferedWriterTimeoutWriter) Write(p []byte) (n int, err error) {
  272. return writeWithByteTimeout(w.conn, w.byteTimeout, p)
  273. }
  274. // writeWithByteTimeout writes to conn.
  275. // If more than timeout passes without any bytes being written to the connection,
  276. // the write fails.
  277. func writeWithByteTimeout(conn net.Conn, timeout time.Duration, p []byte) (n int, err error) {
  278. if timeout <= 0 {
  279. return conn.Write(p)
  280. }
  281. for {
  282. conn.SetWriteDeadline(time.Now().Add(timeout))
  283. nn, err := conn.Write(p[n:])
  284. n += nn
  285. if n == len(p) || nn == 0 || !errors.Is(err, os.ErrDeadlineExceeded) {
  286. // Either we finished the write, made no progress, or hit the deadline.
  287. // Whichever it is, we're done now.
  288. conn.SetWriteDeadline(time.Time{})
  289. return n, err
  290. }
  291. }
  292. }
  293. func mustUint31(v int32) uint32 {
  294. if v < 0 || v > 2147483647 {
  295. panic("out of range")
  296. }
  297. return uint32(v)
  298. }
  299. // bodyAllowedForStatus reports whether a given response status code
  300. // permits a body. See RFC 7230, section 3.3.
  301. func bodyAllowedForStatus(status int) bool {
  302. switch {
  303. case status >= 100 && status <= 199:
  304. return false
  305. case status == 204:
  306. return false
  307. case status == 304:
  308. return false
  309. }
  310. return true
  311. }
  312. type httpError struct {
  313. _ incomparable
  314. msg string
  315. timeout bool
  316. }
  317. func (e *httpError) Error() string { return e.msg }
  318. func (e *httpError) Timeout() bool { return e.timeout }
  319. func (e *httpError) Temporary() bool { return true }
  320. var errTimeout error = &httpError{msg: "http2: timeout awaiting response headers", timeout: true}
  321. type connectionStater interface {
  322. ConnectionState() tls.ConnectionState
  323. }
  324. var sorterPool = sync.Pool{New: func() interface{} { return new(sorter) }}
  325. type sorter struct {
  326. v []string // owned by sorter
  327. }
  328. func (s *sorter) Len() int { return len(s.v) }
  329. func (s *sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] }
  330. func (s *sorter) Less(i, j int) bool { return s.v[i] < s.v[j] }
  331. // Keys returns the sorted keys of h.
  332. //
  333. // The returned slice is only valid until s used again or returned to
  334. // its pool.
  335. func (s *sorter) Keys(h http.Header) []string {
  336. keys := s.v[:0]
  337. for k := range h {
  338. keys = append(keys, k)
  339. }
  340. s.v = keys
  341. sort.Sort(s)
  342. return keys
  343. }
  344. func (s *sorter) SortStrings(ss []string) {
  345. // Our sorter works on s.v, which sorter owns, so
  346. // stash it away while we sort the user's buffer.
  347. save := s.v
  348. s.v = ss
  349. sort.Sort(s)
  350. s.v = save
  351. }
  352. // incomparable is a zero-width, non-comparable type. Adding it to a struct
  353. // makes that struct also non-comparable, and generally doesn't add
  354. // any size (as long as it's first).
  355. type incomparable [0]func()