http2.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "net/http"
  19. "strconv"
  20. )
  21. var VerboseLogs = false
  22. const (
  23. // ClientPreface is the string that must be sent by new
  24. // connections from clients.
  25. ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  26. // SETTINGS_MAX_FRAME_SIZE default
  27. // http://http2.github.io/http2-spec/#rfc.section.6.5.2
  28. initialMaxFrameSize = 16384
  29. npnProto = "h2-14"
  30. // http://http2.github.io/http2-spec/#SettingValues
  31. initialHeaderTableSize = 4096
  32. initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
  33. )
  34. var (
  35. clientPreface = []byte(ClientPreface)
  36. )
  37. type streamState int
  38. const (
  39. stateIdle streamState = iota
  40. stateOpen
  41. stateHalfClosedLocal
  42. stateHalfClosedRemote
  43. stateResvLocal
  44. stateResvRemote
  45. stateClosed
  46. )
  47. var stateName = [...]string{
  48. stateIdle: "Idle",
  49. stateOpen: "Open",
  50. stateHalfClosedLocal: "HalfClosedLocal",
  51. stateHalfClosedRemote: "HalfClosedRemote",
  52. stateResvLocal: "ResvLocal",
  53. stateResvRemote: "ResvRemote",
  54. stateClosed: "Closed",
  55. }
  56. func (st streamState) String() string {
  57. return stateName[st]
  58. }
  59. func validHeader(v string) bool {
  60. if len(v) == 0 {
  61. return false
  62. }
  63. for _, r := range v {
  64. // "Just as in HTTP/1.x, header field names are
  65. // strings of ASCII characters that are compared in a
  66. // case-insensitive fashion. However, header field
  67. // names MUST be converted to lowercase prior to their
  68. // encoding in HTTP/2. "
  69. if r >= 127 || ('A' <= r && r <= 'Z') {
  70. return false
  71. }
  72. }
  73. return true
  74. }
  75. var httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
  76. func init() {
  77. for i := 100; i <= 999; i++ {
  78. if v := http.StatusText(i); v != "" {
  79. httpCodeStringCommon[i] = strconv.Itoa(i)
  80. }
  81. }
  82. }
  83. func httpCodeString(code int) string {
  84. if s, ok := httpCodeStringCommon[code]; ok {
  85. return s
  86. }
  87. return strconv.Itoa(code)
  88. }
  89. // from pkg io
  90. type stringWriter interface {
  91. WriteString(s string) (n int, err error)
  92. }
  93. // A gate lets two goroutines coordinate their activities.
  94. type gate chan struct{}
  95. func (g gate) Done() { g <- struct{}{} }
  96. func (g gate) Wait() { <-g }