Skip to content

Commit

Permalink
Handle goaway and http1 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-mastrangelo committed Nov 24, 2015
1 parent 299bcd5 commit 945836e
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 77 deletions.
13 changes: 12 additions & 1 deletion tools/http2_interop/frameheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func (fh *FrameHeader) MarshalBinary() ([]byte, error) {
buf[0], buf[1], buf[2] = byte(fh.Length>>16), byte(fh.Length>>8), byte(fh.Length)
buf[3] = byte(fh.Type)
buf[4] = fh.Flags
binary.BigEndian.PutUint32(buf[5:], uint32(fh.StreamID))
var res uint32
if fh.Reserved {
res = 0x80000000
}
binary.BigEndian.PutUint32(buf[5:], uint32(fh.StreamID)|res)

return buf, nil
}
Expand Down Expand Up @@ -89,6 +93,8 @@ func (ft FrameType) String() string {
return "WINDOW_UPDATE"
case ContinuationFrameType:
return "CONTINUATION"
case HTTP1FrameType:
return "HTTP/1.? (Bad)"
default:
return fmt.Sprintf("UNKNOWN(%d)", byte(ft))
}
Expand All @@ -106,4 +112,9 @@ const (
GoAwayFrameType FrameType = 7
WindowUpdateFrameType FrameType = 8
ContinuationFrameType FrameType = 9

// HTTP1FrameType is not a real type, but rather a convenient way to check if the response
// is an http response. The type of a frame header is the 4th byte, which in an http1
// response will be "HTTP/1.1 200 OK" or something like that. The character for "P" is 80.
HTTP1FrameType FrameType = 80
)
49 changes: 49 additions & 0 deletions tools/http2_interop/http1frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package http2interop

import (
"bytes"
"io"
"strings"
)

// HTTP1Frame is not a real frame, but rather a way to represent an http1.x response.
type HTTP1Frame struct {
Header FrameHeader
Data []byte
}

func (f *HTTP1Frame) GetHeader() *FrameHeader {
return &f.Header
}

func (f *HTTP1Frame) ParsePayload(r io.Reader) error {
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return err
}
f.Data = buf.Bytes()
return nil
}

func (f *HTTP1Frame) MarshalPayload() ([]byte, error) {
return []byte(string(f.Data)), nil
}

func (f *HTTP1Frame) MarshalBinary() ([]byte, error) {
buf, err := f.Header.MarshalBinary()
if err != nil {
return nil, err
}

buf = append(buf, f.Data...)

return buf, nil
}

func (f *HTTP1Frame) String() string {
s := string(f.Data)
parts := strings.SplitN(s, "\n", 2)
headerleft, _ := f.Header.MarshalBinary()

return strings.TrimSpace(string(headerleft) + parts[0])
}
Loading

0 comments on commit 945836e

Please sign in to comment.