blob: 6011eed4511c4a9f46ed77c8d55b3aa8be935b99 [file] [log] [blame]
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07001package http2interop
2
3import (
4 "fmt"
5 "io"
6)
7
8type PingFrame struct {
9 Header FrameHeader
10 Data []byte
11}
12
13const (
14 PING_ACK = 0x01
15)
16
17func (f *PingFrame) GetHeader() *FrameHeader {
18 return &f.Header
19}
20
21func (f *PingFrame) ParsePayload(r io.Reader) error {
22 raw := make([]byte, f.Header.Length)
23 if _, err := io.ReadFull(r, raw); err != nil {
24 return err
25 }
26 return f.UnmarshalPayload(raw)
27}
28
29func (f *PingFrame) UnmarshalPayload(raw []byte) error {
30 if f.Header.Length != len(raw) {
31 return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw))
32 }
33 if f.Header.Length != 8 {
34 return fmt.Errorf("Invalid Payload length %d", f.Header.Length)
35 }
36
37 f.Data = []byte(string(raw))
38
39 return nil
40}
41
42func (f *PingFrame) MarshalPayload() ([]byte, error) {
43 if len(f.Data) != 8 {
44 return nil, fmt.Errorf("Invalid Payload length %d", len(f.Data))
45 }
46 return []byte(string(f.Data)), nil
47}
48
49func (f *PingFrame) MarshalBinary() ([]byte, error) {
50 payload, err := f.MarshalPayload()
51 if err != nil {
52 return nil, err
53 }
54
55 f.Header.Length = len(payload)
56 f.Header.Type = PingFrameType
57 header, err := f.Header.MarshalBinary()
58 if err != nil {
59 return nil, err
60 }
61
62 header = append(header, payload...)
63
64 return header, nil
65}