xref: /aosp_15_r20/external/grpc-grpc/tools/http2_interop/http1frame.go (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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
15package http2interop
16
17import (
18	"bytes"
19	"io"
20	"strings"
21)
22
23// HTTP1Frame is not a real frame, but rather a way to represent an http1.x response.
24type HTTP1Frame struct {
25	Header FrameHeader
26	Data   []byte
27}
28
29func (f *HTTP1Frame) GetHeader() *FrameHeader {
30	return &f.Header
31}
32
33func (f *HTTP1Frame) ParsePayload(r io.Reader) error {
34	var buf bytes.Buffer
35	if _, err := io.Copy(&buf, r); err != nil {
36		return err
37	}
38	f.Data = buf.Bytes()
39	return nil
40}
41
42func (f *HTTP1Frame) MarshalPayload() ([]byte, error) {
43	return []byte(string(f.Data)), nil
44}
45
46func (f *HTTP1Frame) MarshalBinary() ([]byte, error) {
47	buf, err := f.Header.MarshalBinary()
48	if err != nil {
49		return nil, err
50	}
51
52	buf = append(buf, f.Data...)
53
54	return buf, nil
55}
56
57func (f *HTTP1Frame) String() string {
58	s := string(f.Data)
59	parts := strings.SplitN(s, "\n", 2)
60	headerleft, _ := f.Header.MarshalBinary()
61
62	return strings.TrimSpace(string(headerleft) + parts[0])
63}
64