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

Directly read HTTP response from connection instead of copying to byt… #79

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 5 additions & 17 deletions ftwhttp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ftwhttp

import (
"bufio"
"bytes"
"errors"
"io"
"net"
Expand Down Expand Up @@ -65,25 +64,16 @@ func (c *Connection) send(data []byte) (int, error) {

}

func (c *Connection) receive() ([]byte, error) {
func (c *Connection) receive() (io.Reader, error) {
log.Trace().Msg("ftw/http: receiving data")
var err error
var buf []byte

// We assume the response body can be handled in memory without problems
// That's why we use io.ReadAll
if err = c.connection.SetReadDeadline(time.Now().Add(c.readTimeout)); err == nil {
buf, err = io.ReadAll(c.connection)
}

if neterr, ok := err.(net.Error); ok && !neterr.Timeout() {
log.Error().Msgf("ftw/http: %s\n", err.Error())
} else {
err = nil
if err := c.connection.SetReadDeadline(time.Now().Add(c.readTimeout)); err != nil {
return nil, err
}
log.Trace().Msgf("ftw/http: received data - %q", buf)
fzipi marked this conversation as resolved.
Show resolved Hide resolved

return buf, err
return c.connection, nil
}

// Request will use all the inputs and send a raw http request to the destination
Expand All @@ -108,21 +98,19 @@ func (c *Connection) Request(request *Request) error {
// Response reads the response sent by the WAF and return the corresponding struct
// It leverages the go stdlib for reading and parsing the response
func (c *Connection) Response() (*Response, error) {
data, err := c.receive()
r, err := c.receive()

if err != nil {
return nil, err
}

r := bytes.NewReader(data)
reader := *bufio.NewReader(r)

httpResponse, err := http.ReadResponse(&reader, nil)
if err != nil {
return nil, err
}
response := Response{
RAW: data,
anuraaga marked this conversation as resolved.
Show resolved Hide resolved
Parsed: *httpResponse,
}
return &response, err
Expand Down
1 change: 0 additions & 1 deletion ftwhttp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,5 @@ type Request struct {

// Response represents the http response received from the server/waf
type Response struct {
RAW []byte
Parsed http.Response
}