http2.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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
  16. import (
  17. "bufio"
  18. "crypto/tls"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "net/http"
  23. "os"
  24. "sort"
  25. "strconv"
  26. "strings"
  27. "sync"
  28. )
  29. var (
  30. VerboseLogs bool
  31. logFrameWrites bool
  32. logFrameReads bool
  33. )
  34. func init() {
  35. e := os.Getenv("GODEBUG")
  36. if strings.Contains(e, "http2debug=1") {
  37. VerboseLogs = true
  38. }
  39. if strings.Contains(e, "http2debug=2") {
  40. VerboseLogs = true
  41. logFrameWrites = true
  42. logFrameReads = true
  43. }
  44. }
  45. const (
  46. // ClientPreface is the string that must be sent by new
  47. // connections from clients.
  48. ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  49. // SETTINGS_MAX_FRAME_SIZE default
  50. // http://http2.github.io/http2-spec/#rfc.section.6.5.2
  51. initialMaxFrameSize = 16384
  52. // NextProtoTLS is the NPN/ALPN protocol negotiated during
  53. // HTTP/2's TLS setup.
  54. NextProtoTLS = "h2"
  55. // http://http2.github.io/http2-spec/#SettingValues
  56. initialHeaderTableSize = 4096
  57. initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
  58. defaultMaxReadFrameSize = 1 << 20
  59. )
  60. var (
  61. clientPreface = []byte(ClientPreface)
  62. )
  63. type streamState int
  64. const (
  65. stateIdle streamState = iota
  66. stateOpen
  67. stateHalfClosedLocal
  68. stateHalfClosedRemote
  69. stateResvLocal
  70. stateResvRemote
  71. stateClosed
  72. )
  73. var stateName = [...]string{
  74. stateIdle: "Idle",
  75. stateOpen: "Open",
  76. stateHalfClosedLocal: "HalfClosedLocal",
  77. stateHalfClosedRemote: "HalfClosedRemote",
  78. stateResvLocal: "ResvLocal",
  79. stateResvRemote: "ResvRemote",
  80. stateClosed: "Closed",
  81. }
  82. func (st streamState) String() string {
  83. return stateName[st]
  84. }
  85. // Setting is a setting parameter: which setting it is, and its value.
  86. type Setting struct {
  87. // ID is which setting is being set.
  88. // See http://http2.github.io/http2-spec/#SettingValues
  89. ID SettingID
  90. // Val is the value.
  91. Val uint32
  92. }
  93. func (s Setting) String() string {
  94. return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
  95. }
  96. // Valid reports whether the setting is valid.
  97. func (s Setting) Valid() error {
  98. // Limits and error codes from 6.5.2 Defined SETTINGS Parameters
  99. switch s.ID {
  100. case SettingEnablePush:
  101. if s.Val != 1 && s.Val != 0 {
  102. return ConnectionError(ErrCodeProtocol)
  103. }
  104. case SettingInitialWindowSize:
  105. if s.Val > 1<<31-1 {
  106. return ConnectionError(ErrCodeFlowControl)
  107. }
  108. case SettingMaxFrameSize:
  109. if s.Val < 16384 || s.Val > 1<<24-1 {
  110. return ConnectionError(ErrCodeProtocol)
  111. }
  112. }
  113. return nil
  114. }
  115. // A SettingID is an HTTP/2 setting as defined in
  116. // http://http2.github.io/http2-spec/#iana-settings
  117. type SettingID uint16
  118. const (
  119. SettingHeaderTableSize SettingID = 0x1
  120. SettingEnablePush SettingID = 0x2
  121. SettingMaxConcurrentStreams SettingID = 0x3
  122. SettingInitialWindowSize SettingID = 0x4
  123. SettingMaxFrameSize SettingID = 0x5
  124. SettingMaxHeaderListSize SettingID = 0x6
  125. )
  126. var settingName = map[SettingID]string{
  127. SettingHeaderTableSize: "HEADER_TABLE_SIZE",
  128. SettingEnablePush: "ENABLE_PUSH",
  129. SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
  130. SettingInitialWindowSize: "INITIAL_WINDOW_SIZE",
  131. SettingMaxFrameSize: "MAX_FRAME_SIZE",
  132. SettingMaxHeaderListSize: "MAX_HEADER_LIST_SIZE",
  133. }
  134. func (s SettingID) String() string {
  135. if v, ok := settingName[s]; ok {
  136. return v
  137. }
  138. return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s))
  139. }
  140. var (
  141. errInvalidHeaderFieldName = errors.New("http2: invalid header field name")
  142. errInvalidHeaderFieldValue = errors.New("http2: invalid header field value")
  143. )
  144. // validHeaderFieldName reports whether v is a valid header field name (key).
  145. // RFC 7230 says:
  146. // header-field = field-name ":" OWS field-value OWS
  147. // field-name = token
  148. // tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
  149. // "^" / "_" / "
  150. // Further, http2 says:
  151. // "Just as in HTTP/1.x, header field names are strings of ASCII
  152. // characters that are compared in a case-insensitive
  153. // fashion. However, header field names MUST be converted to
  154. // lowercase prior to their encoding in HTTP/2. "
  155. func validHeaderFieldName(v string) bool {
  156. if len(v) == 0 {
  157. return false
  158. }
  159. for _, r := range v {
  160. if int(r) >= len(isTokenTable) || ('A' <= r && r <= 'Z') {
  161. return false
  162. }
  163. if !isTokenTable[byte(r)] {
  164. return false
  165. }
  166. }
  167. return true
  168. }
  169. // validHeaderFieldValue reports whether v is a valid header field value.
  170. //
  171. // RFC 7230 says:
  172. // field-value = *( field-content / obs-fold )
  173. // obj-fold = N/A to http2, and deprecated
  174. // field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  175. // field-vchar = VCHAR / obs-text
  176. // obs-text = %x80-FF
  177. // VCHAR = "any visible [USASCII] character"
  178. //
  179. // http2 further says: "Similarly, HTTP/2 allows header field values
  180. // that are not valid. While most of the values that can be encoded
  181. // will not alter header field parsing, carriage return (CR, ASCII
  182. // 0xd), line feed (LF, ASCII 0xa), and the zero character (NUL, ASCII
  183. // 0x0) might be exploited by an attacker if they are translated
  184. // verbatim. Any request or response that contains a character not
  185. // permitted in a header field value MUST be treated as malformed
  186. // (Section 8.1.2.6). Valid characters are defined by the
  187. // field-content ABNF rule in Section 3.2 of [RFC7230]."
  188. //
  189. // This function does not (yet?) properly handle the rejection of
  190. // strings that begin or end with SP or HTAB.
  191. func validHeaderFieldValue(v string) bool {
  192. for i := 0; i < len(v); i++ {
  193. if b := v[i]; b < ' ' && b != '\t' || b == 0x7f {
  194. return false
  195. }
  196. }
  197. return true
  198. }
  199. var httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
  200. func init() {
  201. for i := 100; i <= 999; i++ {
  202. if v := http.StatusText(i); v != "" {
  203. httpCodeStringCommon[i] = strconv.Itoa(i)
  204. }
  205. }
  206. }
  207. func httpCodeString(code int) string {
  208. if s, ok := httpCodeStringCommon[code]; ok {
  209. return s
  210. }
  211. return strconv.Itoa(code)
  212. }
  213. // from pkg io
  214. type stringWriter interface {
  215. WriteString(s string) (n int, err error)
  216. }
  217. // A gate lets two goroutines coordinate their activities.
  218. type gate chan struct{}
  219. func (g gate) Done() { g <- struct{}{} }
  220. func (g gate) Wait() { <-g }
  221. // A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
  222. type closeWaiter chan struct{}
  223. // Init makes a closeWaiter usable.
  224. // It exists because so a closeWaiter value can be placed inside a
  225. // larger struct and have the Mutex and Cond's memory in the same
  226. // allocation.
  227. func (cw *closeWaiter) Init() {
  228. *cw = make(chan struct{})
  229. }
  230. // Close marks the closeWaiter as closed and unblocks any waiters.
  231. func (cw closeWaiter) Close() {
  232. close(cw)
  233. }
  234. // Wait waits for the closeWaiter to become closed.
  235. func (cw closeWaiter) Wait() {
  236. <-cw
  237. }
  238. // bufferedWriter is a buffered writer that writes to w.
  239. // Its buffered writer is lazily allocated as needed, to minimize
  240. // idle memory usage with many connections.
  241. type bufferedWriter struct {
  242. w io.Writer // immutable
  243. bw *bufio.Writer // non-nil when data is buffered
  244. }
  245. func newBufferedWriter(w io.Writer) *bufferedWriter {
  246. return &bufferedWriter{w: w}
  247. }
  248. var bufWriterPool = sync.Pool{
  249. New: func() interface{} {
  250. // TODO: pick something better? this is a bit under
  251. // (3 x typical 1500 byte MTU) at least.
  252. return bufio.NewWriterSize(nil, 4<<10)
  253. },
  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(w.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. func mustUint31(v int32) uint32 {
  275. if v < 0 || v > 2147483647 {
  276. panic("out of range")
  277. }
  278. return uint32(v)
  279. }
  280. // bodyAllowedForStatus reports whether a given response status code
  281. // permits a body. See RFC2616, section 4.4.
  282. func bodyAllowedForStatus(status int) bool {
  283. switch {
  284. case status >= 100 && status <= 199:
  285. return false
  286. case status == 204:
  287. return false
  288. case status == 304:
  289. return false
  290. }
  291. return true
  292. }
  293. type httpError struct {
  294. msg string
  295. timeout bool
  296. }
  297. func (e *httpError) Error() string { return e.msg }
  298. func (e *httpError) Timeout() bool { return e.timeout }
  299. func (e *httpError) Temporary() bool { return true }
  300. var errTimeout error = &httpError{msg: "http2: timeout awaiting response headers", timeout: true}
  301. var isTokenTable = [127]bool{
  302. '!': true,
  303. '#': true,
  304. '$': true,
  305. '%': true,
  306. '&': true,
  307. '\'': true,
  308. '*': true,
  309. '+': true,
  310. '-': true,
  311. '.': true,
  312. '0': true,
  313. '1': true,
  314. '2': true,
  315. '3': true,
  316. '4': true,
  317. '5': true,
  318. '6': true,
  319. '7': true,
  320. '8': true,
  321. '9': true,
  322. 'A': true,
  323. 'B': true,
  324. 'C': true,
  325. 'D': true,
  326. 'E': true,
  327. 'F': true,
  328. 'G': true,
  329. 'H': true,
  330. 'I': true,
  331. 'J': true,
  332. 'K': true,
  333. 'L': true,
  334. 'M': true,
  335. 'N': true,
  336. 'O': true,
  337. 'P': true,
  338. 'Q': true,
  339. 'R': true,
  340. 'S': true,
  341. 'T': true,
  342. 'U': true,
  343. 'W': true,
  344. 'V': true,
  345. 'X': true,
  346. 'Y': true,
  347. 'Z': true,
  348. '^': true,
  349. '_': true,
  350. '`': true,
  351. 'a': true,
  352. 'b': true,
  353. 'c': true,
  354. 'd': true,
  355. 'e': true,
  356. 'f': true,
  357. 'g': true,
  358. 'h': true,
  359. 'i': true,
  360. 'j': true,
  361. 'k': true,
  362. 'l': true,
  363. 'm': true,
  364. 'n': true,
  365. 'o': true,
  366. 'p': true,
  367. 'q': true,
  368. 'r': true,
  369. 's': true,
  370. 't': true,
  371. 'u': true,
  372. 'v': true,
  373. 'w': true,
  374. 'x': true,
  375. 'y': true,
  376. 'z': true,
  377. '|': true,
  378. '~': true,
  379. }
  380. type connectionStater interface {
  381. ConnectionState() tls.ConnectionState
  382. }
  383. var sorterPool = sync.Pool{New: func() interface{} { return new(sorter) }}
  384. type sorter struct {
  385. v []string // owned by sorter
  386. }
  387. func (s *sorter) Len() int { return len(s.v) }
  388. func (s *sorter) Swap(i, j int) { s.v[i], s.v[j] = s.v[j], s.v[i] }
  389. func (s *sorter) Less(i, j int) bool { return s.v[i] < s.v[j] }
  390. // Keys returns the sorted keys of h.
  391. //
  392. // The returned slice is only valid until s used again or returned to
  393. // its pool.
  394. func (s *sorter) Keys(h http.Header) []string {
  395. keys := s.v[:0]
  396. for k := range h {
  397. keys = append(keys, k)
  398. }
  399. s.v = keys
  400. sort.Sort(s)
  401. return keys
  402. }
  403. func (s *sorter) SortStrings(ss []string) {
  404. // Our sorter works on s.v, which sorter owners, so
  405. // stash it away while we sort the user's buffer.
  406. save := s.v
  407. s.v = ss
  408. sort.Sort(s)
  409. s.v = save
  410. }