Debian Codegraph @

tools/http2_interop/unknownframe.go

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