settings.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 The gRPC Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package http2interop
  15. import (
  16. "encoding/binary"
  17. "fmt"
  18. "io"
  19. )
  20. const (
  21. SETTINGS_ACK = 1
  22. )
  23. type SettingsFrame struct {
  24. Header FrameHeader
  25. Params []SettingsParameter
  26. }
  27. type SettingsIdentifier uint16
  28. const (
  29. SettingsHeaderTableSize SettingsIdentifier = 1
  30. SettingsEnablePush SettingsIdentifier = 2
  31. SettingsMaxConcurrentStreams SettingsIdentifier = 3
  32. SettingsInitialWindowSize SettingsIdentifier = 4
  33. SettingsMaxFrameSize SettingsIdentifier = 5
  34. SettingsMaxHeaderListSize SettingsIdentifier = 6
  35. )
  36. const (
  37. SETTINGS_FLAG_ACK byte = 0x01
  38. )
  39. func (si SettingsIdentifier) String() string {
  40. switch si {
  41. case SettingsHeaderTableSize:
  42. return "SETTINGS_HEADER_TABLE_SIZE"
  43. case SettingsEnablePush:
  44. return "SETTINGS_ENABLE_PUSH"
  45. case SettingsMaxConcurrentStreams:
  46. return "SETTINGS_MAX_CONCURRENT_STREAMS"
  47. case SettingsInitialWindowSize:
  48. return "SETTINGS_INITIAL_WINDOW_SIZE"
  49. case SettingsMaxFrameSize:
  50. return "SETTINGS_MAX_FRAME_SIZE"
  51. case SettingsMaxHeaderListSize:
  52. return "SETTINGS_MAX_HEADER_LIST_SIZE"
  53. default:
  54. return fmt.Sprintf("SETTINGS_UNKNOWN(%d)", uint16(si))
  55. }
  56. }
  57. type SettingsParameter struct {
  58. Identifier SettingsIdentifier
  59. Value uint32
  60. }
  61. func (f *SettingsFrame) GetHeader() *FrameHeader {
  62. return &f.Header
  63. }
  64. func (f *SettingsFrame) ParsePayload(r io.Reader) error {
  65. raw := make([]byte, f.Header.Length)
  66. if _, err := io.ReadFull(r, raw); err != nil {
  67. return err
  68. }
  69. return f.UnmarshalPayload(raw)
  70. }
  71. func (f *SettingsFrame) UnmarshalPayload(raw []byte) error {
  72. if f.Header.Length != len(raw) {
  73. return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw))
  74. }
  75. if f.Header.Length%6 != 0 {
  76. return fmt.Errorf("Invalid Payload length %d", f.Header.Length)
  77. }
  78. f.Params = make([]SettingsParameter, 0, f.Header.Length/6)
  79. for i := 0; i < len(raw); i += 6 {
  80. f.Params = append(f.Params, SettingsParameter{
  81. Identifier: SettingsIdentifier(binary.BigEndian.Uint16(raw[i : i+2])),
  82. Value: binary.BigEndian.Uint32(raw[i+2 : i+6]),
  83. })
  84. }
  85. return nil
  86. }
  87. func (f *SettingsFrame) MarshalPayload() ([]byte, error) {
  88. raw := make([]byte, len(f.Params)*6)
  89. for i, p := range f.Params {
  90. binary.BigEndian.PutUint16(raw[i*6:i*6+2], uint16(p.Identifier))
  91. binary.BigEndian.PutUint32(raw[i*6+2:i*6+6], p.Value)
  92. }
  93. return raw, nil
  94. }
  95. func (f *SettingsFrame) MarshalBinary() ([]byte, error) {
  96. payload, err := f.MarshalPayload()
  97. if err != nil {
  98. return nil, err
  99. }
  100. f.Header.Length = len(payload)
  101. f.Header.Type = SettingsFrameType
  102. header, err := f.Header.MarshalBinary()
  103. if err != nil {
  104. return nil, err
  105. }
  106. header = append(header, payload...)
  107. return header, nil
  108. }