http2.go 11 KB

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