-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build on ubuntu-22.04 x86_64
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catch error here please