Carl Mastrangelo | 945836e | 2015-11-20 17:43:15 -0800 | [diff] [blame] | 1 | package http2interop |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // HTTP1Frame is not a real frame, but rather a way to represent an http1.x response. |
| 10 | type HTTP1Frame struct { |
| 11 | Header FrameHeader |
| 12 | Data []byte |
| 13 | } |
| 14 | |
| 15 | func (f *HTTP1Frame) GetHeader() *FrameHeader { |
| 16 | return &f.Header |
| 17 | } |
| 18 | |
| 19 | func (f *HTTP1Frame) ParsePayload(r io.Reader) error { |
| 20 | var buf bytes.Buffer |
| 21 | if _, err := io.Copy(&buf, r); err != nil { |
| 22 | return err |
| 23 | } |
| 24 | f.Data = buf.Bytes() |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | func (f *HTTP1Frame) MarshalPayload() ([]byte, error) { |
| 29 | return []byte(string(f.Data)), nil |
| 30 | } |
| 31 | |
| 32 | func (f *HTTP1Frame) MarshalBinary() ([]byte, error) { |
| 33 | buf, err := f.Header.MarshalBinary() |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | buf = append(buf, f.Data...) |
| 39 | |
| 40 | return buf, nil |
| 41 | } |
| 42 | |
| 43 | func (f *HTTP1Frame) String() string { |
| 44 | s := string(f.Data) |
| 45 | parts := strings.SplitN(s, "\n", 2) |
| 46 | headerleft, _ := f.Header.MarshalBinary() |
| 47 | |
| 48 | return strings.TrimSpace(string(headerleft) + parts[0]) |
| 49 | } |