Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 30 additions & 31 deletions pkg/event_processor/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,84 +31,83 @@ type HTTPRequest struct {
bufReader *bufio.Reader
}

func (this *HTTPRequest) Init() {
this.reader = bytes.NewBuffer(nil)
this.bufReader = bufio.NewReader(this.reader)
func (hr *HTTPRequest) Init() {
hr.reader = bytes.NewBuffer(nil)
hr.bufReader = bufio.NewReader(hr.reader)
}

func (this *HTTPRequest) Name() string {
func (hr *HTTPRequest) Name() string {
return "HTTPRequest"
}

func (this *HTTPRequest) PacketType() PacketType {
return this.packerType
func (hr *HTTPRequest) PacketType() PacketType {
return hr.packerType
}

func (this *HTTPRequest) ParserType() ParserType {
func (hr *HTTPRequest) ParserType() ParserType {
return ParserTypeHttpRequest
}

func (this *HTTPRequest) Write(b []byte) (int, error) {
func (hr *HTTPRequest) Write(b []byte) (int, error) {
// 如果未初始化
if !this.isInit {
n, e := this.reader.Write(b)
if !hr.isInit {
n, e := hr.reader.Write(b)
if e != nil {
return n, e
}
req, err := http.ReadRequest(this.bufReader)
req, err := http.ReadRequest(hr.bufReader)
if err != nil {
return 0, err
}
this.request = req
this.isInit = true
hr.request = req
hr.isInit = true
return n, nil
}

// 如果已初始化
l, e := this.reader.Write(b)
l, e := hr.reader.Write(b)
if e != nil {
return 0, e
}

// TODO 检测是否接收完整个包
if false {
this.isDone = true
hr.isDone = true
}

return l, nil
}

func (this *HTTPRequest) detect(payload []byte) error {
//this.Init()
func (hr *HTTPRequest) detect(payload []byte) error {
//hr.Init()
rd := bytes.NewReader(payload)
buf := bufio.NewReader(rd)
req, err := http.ReadRequest(buf)
if err != nil {
return err
}
this.request = req
hr.request = req
return nil
}

func (this *HTTPRequest) IsDone() bool {
return this.isDone
func (hr *HTTPRequest) IsDone() bool {
return hr.isDone
}

func (this *HTTPRequest) Reset() {
this.isDone = false
this.isInit = false
this.reader.Reset()
this.bufReader.Reset(this.reader)
func (hr *HTTPRequest) Reset() {
hr.isDone = false
hr.isInit = false
hr.reader.Reset()
hr.bufReader.Reset(hr.reader)
}

func (this *HTTPRequest) Display() []byte {
if this.request.Proto == "HTTP/2.0" {
return this.reader.Bytes()
func (hr *HTTPRequest) Display() []byte {
if hr.request.Proto == "HTTP/2.0" {
return hr.reader.Bytes()
}
b, e := httputil.DumpRequest(this.request, true)
b, e := httputil.DumpRequest(hr.request, true)
if e != nil {
log.Println("DumpRequest error:", e)
return nil
return hr.reader.Bytes()
}
return b
}
Expand Down
85 changes: 42 additions & 43 deletions pkg/event_processor/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,122 +39,121 @@ type HTTPResponse struct {
bufReader *bufio.Reader
}

func (this *HTTPResponse) Init() {
this.reader = bytes.NewBuffer(nil)
this.bufReader = bufio.NewReader(this.reader)
this.receivedLen = 0
this.headerLength = 0
func (hr *HTTPResponse) Init() {
hr.reader = bytes.NewBuffer(nil)
hr.bufReader = bufio.NewReader(hr.reader)
hr.receivedLen = 0
hr.headerLength = 0
}

func (this *HTTPResponse) Name() string {
func (hr *HTTPResponse) Name() string {
return "HTTPResponse"
}

func (this *HTTPResponse) PacketType() PacketType {
return this.packerType
func (hr *HTTPResponse) PacketType() PacketType {
return hr.packerType
}

func (this *HTTPResponse) ParserType() ParserType {
func (hr *HTTPResponse) ParserType() ParserType {
return ParserTypeHttpResponse
}

func (this *HTTPResponse) Write(b []byte) (int, error) {
func (hr *HTTPResponse) Write(b []byte) (int, error) {
var l int
var e error
var req *http.Response
// 如果未初始化
if !this.isInit {
l, e = this.reader.Write(b)
if !hr.isInit {
l, e = hr.reader.Write(b)
if e != nil {
return l, e
}
req, e = http.ReadResponse(this.bufReader, nil)
req, e = http.ReadResponse(hr.bufReader, nil)

if e != nil {
return 0, e
}

this.response = req
this.isInit = true
hr.response = req
hr.isInit = true
} else {
// 如果已初始化
l, e = this.reader.Write(b)
l, e = hr.reader.Write(b)
if e != nil {
return 0, e
}
}
this.receivedLen += int64(l)
hr.receivedLen += int64(l)

// 检测是否接收完整个包
//if this.response.ContentLength >= this.receivedLen {
//if hr.response.ContentLength >= hr.receivedLen {
if false {
this.isDone = true
hr.isDone = true
}

return l, nil
}

func (this *HTTPResponse) detect(payload []byte) error {
func (hr *HTTPResponse) detect(payload []byte) error {
rd := bytes.NewReader(payload)
buf := bufio.NewReader(rd)
res, err := http.ReadResponse(buf, nil)
if err != nil {
return err
}
this.response = res
hr.response = res
return nil
}

func (this *HTTPResponse) IsDone() bool {
return this.isDone
func (hr *HTTPResponse) IsDone() bool {
return hr.isDone
}

func (this *HTTPResponse) Reset() {
this.isDone = false
this.isInit = false
this.reader.Reset()
this.bufReader.Reset(this.reader)
func (hr *HTTPResponse) Reset() {
hr.isDone = false
hr.isInit = false
hr.reader.Reset()
hr.bufReader.Reset(hr.reader)
}

func (this *HTTPResponse) Display() []byte {
func (hr *HTTPResponse) Display() []byte {
var reader io.ReadCloser
var err error
switch this.response.Header.Get("Content-Encoding") {
switch hr.response.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(this.response.Body)
reader, err = gzip.NewReader(hr.response.Body)
if err != nil {
log.Println(err)
break
}

// gzip uncompressed success
this.response.Body = reader
this.packerType = PacketTypeGzip
hr.response.Body = reader
hr.packerType = PacketTypeGzip
defer reader.Close()
default:
//reader = this.response.Body
this.packerType = PacketTypeNull
//log.Println("not gzip content")
//reader = hr.response.Body
hr.packerType = PacketTypeNull
//TODO for debug
//return []byte("")
}
headerMap := bytes.NewBufferString("")
for k, v := range this.response.Header {
for k, v := range hr.response.Header {
headerMap.WriteString(fmt.Sprintf("\t%s\t=>\t%s\n", k, v))
}
log.Printf("HTTPS Headers \n\t%s", headerMap.String())
//log.Printf("HTTPS Headers \n\t%s", headerMap.String())

var b []byte
var e error

if this.response.ContentLength == 0 {
b, e = httputil.DumpResponse(this.response, false)
if hr.response.ContentLength == 0 {
b, e = httputil.DumpResponse(hr.response, false)
} else {
b, e = httputil.DumpResponse(this.response, true)
b, e = httputil.DumpResponse(hr.response, true)
}
if e != nil {
log.Println("DumpResponse error:", e)
return []byte("")
log.Println("[http response] DumpResponse error:", e)
return hr.reader.Bytes()
}
return b
}
Expand Down
Loading