http2.go 11 KB

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