You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.1 KiB

  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
  5. import (
  6. "errors"
  7. "fmt"
  8. )
  9. // An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
  10. type ErrCode uint32
  11. const (
  12. ErrCodeNo ErrCode = 0x0
  13. ErrCodeProtocol ErrCode = 0x1
  14. ErrCodeInternal ErrCode = 0x2
  15. ErrCodeFlowControl ErrCode = 0x3
  16. ErrCodeSettingsTimeout ErrCode = 0x4
  17. ErrCodeStreamClosed ErrCode = 0x5
  18. ErrCodeFrameSize ErrCode = 0x6
  19. ErrCodeRefusedStream ErrCode = 0x7
  20. ErrCodeCancel ErrCode = 0x8
  21. ErrCodeCompression ErrCode = 0x9
  22. ErrCodeConnect ErrCode = 0xa
  23. ErrCodeEnhanceYourCalm ErrCode = 0xb
  24. ErrCodeInadequateSecurity ErrCode = 0xc
  25. ErrCodeHTTP11Required ErrCode = 0xd
  26. )
  27. var errCodeName = map[ErrCode]string{
  28. ErrCodeNo: "NO_ERROR",
  29. ErrCodeProtocol: "PROTOCOL_ERROR",
  30. ErrCodeInternal: "INTERNAL_ERROR",
  31. ErrCodeFlowControl: "FLOW_CONTROL_ERROR",
  32. ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT",
  33. ErrCodeStreamClosed: "STREAM_CLOSED",
  34. ErrCodeFrameSize: "FRAME_SIZE_ERROR",
  35. ErrCodeRefusedStream: "REFUSED_STREAM",
  36. ErrCodeCancel: "CANCEL",
  37. ErrCodeCompression: "COMPRESSION_ERROR",
  38. ErrCodeConnect: "CONNECT_ERROR",
  39. ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM",
  40. ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
  41. ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED",
  42. }
  43. func (e ErrCode) String() string {
  44. if s, ok := errCodeName[e]; ok {
  45. return s
  46. }
  47. return fmt.Sprintf("unknown error code 0x%x", uint32(e))
  48. }
  49. // ConnectionError is an error that results in the termination of the
  50. // entire connection.
  51. type ConnectionError ErrCode
  52. func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
  53. // StreamError is an error that only affects one stream within an
  54. // HTTP/2 connection.
  55. type StreamError struct {
  56. StreamID uint32
  57. Code ErrCode
  58. Cause error // optional additional detail
  59. }
  60. func streamError(id uint32, code ErrCode) StreamError {
  61. return StreamError{StreamID: id, Code: code}
  62. }
  63. func (e StreamError) Error() string {
  64. if e.Cause != nil {
  65. return fmt.Sprintf("stream error: stream ID %d; %v; %v", e.StreamID, e.Code, e.Cause)
  66. }
  67. return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
  68. }
  69. // 6.9.1 The Flow Control Window
  70. // "If a sender receives a WINDOW_UPDATE that causes a flow control
  71. // window to exceed this maximum it MUST terminate either the stream
  72. // or the connection, as appropriate. For streams, [...]; for the
  73. // connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
  74. type goAwayFlowError struct{}
  75. func (goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
  76. // connError represents an HTTP/2 ConnectionError error code, along
  77. // with a string (for debugging) explaining why.
  78. //
  79. // Errors of this type are only returned by the frame parser functions
  80. // and converted into ConnectionError(Code), after stashing away
  81. // the Reason into the Framer's errDetail field, accessible via
  82. // the (*Framer).ErrorDetail method.
  83. type connError struct {
  84. Code ErrCode // the ConnectionError error code
  85. Reason string // additional reason
  86. }
  87. func (e connError) Error() string {
  88. return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
  89. }
  90. type pseudoHeaderError string
  91. func (e pseudoHeaderError) Error() string {
  92. return fmt.Sprintf("invalid pseudo-header %q", string(e))
  93. }
  94. type duplicatePseudoHeaderError string
  95. func (e duplicatePseudoHeaderError) Error() string {
  96. return fmt.Sprintf("duplicate pseudo-header %q", string(e))
  97. }
  98. type headerFieldNameError string
  99. func (e headerFieldNameError) Error() string {
  100. return fmt.Sprintf("invalid header field name %q", string(e))
  101. }
  102. type headerFieldValueError string
  103. func (e headerFieldValueError) Error() string {
  104. return fmt.Sprintf("invalid header field value %q", string(e))
  105. }
  106. var (
  107. errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
  108. errPseudoAfterRegular = errors.New("pseudo header field after regular")
  109. )