Skip to content

Commit

Permalink
Directly read HTTP response from connection instead of copying to byt…
Browse files Browse the repository at this point in the history
…es first.
  • Loading branch information
anuraaga committed Sep 22, 2022
1 parent ed852ae commit 0cc0c0e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
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)

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,
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
}

0 comments on commit 0cc0c0e

Please sign in to comment.