http1frame.go 1.5 KB

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