unknownframe.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "fmt"
  17. "io"
  18. )
  19. type UnknownFrame struct {
  20. Header FrameHeader
  21. Data []byte
  22. }
  23. func (f *UnknownFrame) GetHeader() *FrameHeader {
  24. return &f.Header
  25. }
  26. func (f *UnknownFrame) ParsePayload(r io.Reader) error {
  27. raw := make([]byte, f.Header.Length)
  28. if _, err := io.ReadFull(r, raw); err != nil {
  29. return err
  30. }
  31. return f.UnmarshalPayload(raw)
  32. }
  33. func (f *UnknownFrame) UnmarshalPayload(raw []byte) error {
  34. if f.Header.Length != len(raw) {
  35. return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw))
  36. }
  37. f.Data = []byte(string(raw))
  38. return nil
  39. }
  40. func (f *UnknownFrame) MarshalPayload() ([]byte, error) {
  41. return []byte(string(f.Data)), nil
  42. }
  43. func (f *UnknownFrame) MarshalBinary() ([]byte, error) {
  44. f.Header.Length = len(f.Data)
  45. buf, err := f.Header.MarshalBinary()
  46. if err != nil {
  47. return nil, err
  48. }
  49. payload, err := f.MarshalPayload()
  50. if err != nil {
  51. return nil, err
  52. }
  53. buf = append(buf, payload...)
  54. return buf, nil
  55. }