http2.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. func validHeader(v string) bool {
  48. if len(v) == 0 {
  49. return false
  50. }
  51. for _, r := range v {
  52. // "Just as in HTTP/1.x, header field names are
  53. // strings of ASCII characters that are compared in a
  54. // case-insensitive fashion. However, header field
  55. // names MUST be converted to lowercase prior to their
  56. // encoding in HTTP/2. "
  57. if r >= 127 || ('A' <= r && r <= 'Z') {
  58. return false
  59. }
  60. }
  61. return true
  62. }
  63. var httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
  64. func init() {
  65. for i := 100; i <= 999; i++ {
  66. if v := http.StatusText(i); v != "" {
  67. httpCodeStringCommon[i] = strconv.Itoa(i)
  68. }
  69. }
  70. }
  71. func httpCodeString(code int) string {
  72. if s, ok := httpCodeStringCommon[code]; ok {
  73. return s
  74. }
  75. return strconv.Itoa(code)
  76. }