Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse http2 data in text mode #580

Merged
merged 6 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions pkg/event_processor/http2_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright 2024 yuweizzz <yuwei764969238@gmail.com>. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package event_processor

import (
"bufio"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"log"

"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
)

const H2Magic = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
const H2MagicLen = len(H2Magic)

type HTTP2Request struct {
framer *http2.Framer
packerType PacketType
isDone bool
isInit bool
reader *bytes.Buffer
bufReader *bufio.Reader
}

func (h2r *HTTP2Request) detect(payload []byte) error {
data := string(payload[0:H2MagicLen])
if data != H2Magic {
return errors.New("Not match http2 magic")
}
return nil
}

func (h2r *HTTP2Request) Init() {
h2r.reader = bytes.NewBuffer(nil)
h2r.bufReader = bufio.NewReader(h2r.reader)
h2r.framer = http2.NewFramer(nil, h2r.bufReader)
h2r.framer.ReadMetaHeaders = hpack.NewDecoder(0, nil)
}

func (h2r *HTTP2Request) Write(b []byte) (int, error) {
if !h2r.isInit {
h2r.Init()
h2r.isInit = true
}
length, err := h2r.reader.Write(b)
if err != nil {
return 0, err
}
return length, nil
}

func (h2r *HTTP2Request) ParserType() ParserType {
return ParserTypeHttp2Request
}

func (h2r *HTTP2Request) PacketType() PacketType {
return h2r.packerType
}

func (h2r *HTTP2Request) Name() string {
return "HTTP2Request"
}

func (h2r *HTTP2Request) IsDone() bool {
return h2r.isDone
}

func (h2r *HTTP2Request) Display() []byte {
h2r.bufReader.Discard(H2MagicLen)

Check failure on line 86 in pkg/event_processor/http2_request.go

View workflow job for this annotation

GitHub Actions / build on ubuntu-22.04 x86_64

Error return value of `h2r.bufReader.Discard` is not checked (errcheck)

Check failure on line 86 in pkg/event_processor/http2_request.go

View workflow job for this annotation

GitHub Actions / build on ubuntu-20.04 x86_64

Error return value of `h2r.bufReader.Discard` is not checked (errcheck)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch error here please

var encoding string
dataBuf := bytes.NewBuffer(nil)
frameBuf := bytes.NewBufferString("")
for {
f, err := h2r.framer.ReadFrame()
if err != nil {
if err != io.EOF {
log.Println("[http2 request] Dump HTTP2 Frame error:", err)
}
break
}
switch f := f.(type) {
case *http2.MetaHeadersFrame:
frameBuf.WriteString(fmt.Sprintf("\nFrame Type\t=>\tHEADERS\n"))
for _, header := range f.Fields {
frameBuf.WriteString(fmt.Sprintf("%s\n", header.String()))
if header.Name == "content-encoding" {
encoding = header.Value
}
}
case *http2.DataFrame:
_, err := dataBuf.Write(f.Data())
if err != nil {
log.Println("[http2 request] Write HTTP2 Data Frame buffuer error:", err)
}
default:
fh := f.Header()
frameBuf.WriteString(fmt.Sprintf("\nFrame Type\t=>\t%s\n", fh.Type.String()))
}
}
// merge data frame
if dataBuf.Len() > 0 {
frameBuf.WriteString(fmt.Sprintf("\nFrame Type\t=>\tDATA\n"))
payload := dataBuf.Bytes()
switch encoding {
case "gzip":
reader, err := gzip.NewReader(bytes.NewReader(payload))
if err != nil {
log.Println("[http2 request] Create gzip reader error:", err)
break
}
payload, err = io.ReadAll(reader)
if err != nil {
log.Println("[http2 request] Uncompress gzip data error:", err)
break
}
h2r.packerType = PacketTypeGzip
defer reader.Close()
default:
h2r.packerType = PacketTypeNull
}
frameBuf.Write(payload)
}
return frameBuf.Bytes()
}

func init() {
h2r := &HTTP2Request{}
h2r.Init()
Register(h2r)
}

func (h2r *HTTP2Request) Reset() {
h2r.isDone = false
h2r.isInit = false
h2r.reader.Reset()
h2r.bufReader.Reset(h2r.reader)
}
153 changes: 153 additions & 0 deletions pkg/event_processor/http2_responese.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright 2024 yuweizzz <yuwei764969238@gmail.com>. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package event_processor

import (
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"log"

"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
)

type HTTP2Response struct {
framer *http2.Framer
packerType PacketType
isDone bool
isInit bool
reader *bytes.Buffer
bufReader *bufio.Reader
}

func (h2r *HTTP2Response) detect(payload []byte) error {
rd := bytes.NewReader(payload)
buf := bufio.NewReader(rd)
framer := http2.NewFramer(nil, buf)
framer.ReadMetaHeaders = hpack.NewDecoder(0, nil)
_, err := framer.ReadFrame()
if err != nil {
return err
}
return nil
}

func (h2r *HTTP2Response) Init() {
h2r.reader = bytes.NewBuffer(nil)
h2r.bufReader = bufio.NewReader(h2r.reader)
h2r.framer = http2.NewFramer(nil, h2r.bufReader)
h2r.framer.ReadMetaHeaders = hpack.NewDecoder(0, nil)
}

func (h2r *HTTP2Response) Write(b []byte) (int, error) {
if !h2r.isInit {
h2r.Init()
h2r.isInit = true
}
length, err := h2r.reader.Write(b)
if err != nil {
return 0, err
}
return length, nil
}

func (h2r *HTTP2Response) ParserType() ParserType {
return ParserTypeHttp2Response
}

func (h2r *HTTP2Response) PacketType() PacketType {
return h2r.packerType
}

func (h2r *HTTP2Response) Name() string {
return "HTTP2Response"
}

func (h2r *HTTP2Response) IsDone() bool {
return h2r.isDone
}

func (h2r *HTTP2Response) Display() []byte {
var encoding string
dataBuf := bytes.NewBuffer(nil)
frameBuf := bytes.NewBufferString("")
for {
f, err := h2r.framer.ReadFrame()
if err != nil {
if err != io.EOF {
log.Println("[http2 response] Dump HTTP2 Frame error:", err)
}
break
}
switch f := f.(type) {
case *http2.MetaHeadersFrame:
frameBuf.WriteString(fmt.Sprintf("\nFrame Type\t=>\tHEADERS\n"))
for _, header := range f.Fields {
frameBuf.WriteString(fmt.Sprintf("%s\n", header.String()))
if header.Name == "content-encoding" {
encoding = header.Value
}
}
case *http2.DataFrame:
_, err := dataBuf.Write(f.Data())
if err != nil {
log.Println("[http2 response] Write HTTP2 Data Frame buffuer error:", err)
}
default:
fh := f.Header()
frameBuf.WriteString(fmt.Sprintf("\nFrame Type\t=>\t%s\n", fh.Type.String()))
}
}
// merge data frame
if dataBuf.Len() > 0 {
frameBuf.WriteString(fmt.Sprintf("\nFrame Type\t=>\tDATA\n"))
payload := dataBuf.Bytes()
switch encoding {
case "gzip":
reader, err := gzip.NewReader(bytes.NewReader(payload))
if err != nil {
log.Println("[http2 response] Create gzip reader error:", err)
break
}
payload, err = io.ReadAll(reader)
if err != nil {
log.Println("[http2 response] Uncompress gzip data error:", err)
break
}
h2r.packerType = PacketTypeGzip
defer reader.Close()
default:
h2r.packerType = PacketTypeNull
}
frameBuf.Write(payload)
}
return frameBuf.Bytes()
}

func init() {
h2r := &HTTP2Response{}
h2r.Init()
Register(h2r)
}

func (h2r *HTTP2Response) Reset() {
h2r.isDone = false
h2r.isInit = false
h2r.reader.Reset()
h2r.bufReader.Reset(h2r.reader)
}
6 changes: 2 additions & 4 deletions pkg/event_processor/iparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ func NewParser(payload []byte) IParser {
case ParserTypeHttpResponse:
newParser = new(HTTPResponse)
case ParserTypeHttp2Request:
// TODO support HTTP2 request
// via golang.org/x/net/http2
//hpack.NewEncoder(buf)
newParser = new(HTTP2Request)
case ParserTypeHttp2Response:
// TODO support HTTP2 response
newParser = new(HTTP2Response)
default:
newParser = new(DefaultParser)
}
Expand Down
Loading