diff --git a/packet/http.go b/packet/http.go index 2e75260..5ca57f3 100644 --- a/packet/http.go +++ b/packet/http.go @@ -2,6 +2,7 @@ package packet import ( "bufio" + "io" "net" "net/http" "strings" @@ -56,12 +57,16 @@ func ParseUrl(raw []byte) { } -func NewHttpPacket(raw []byte) (*HttpPacket, error) { - pkt := &HttpPacket{raw: raw} - - pkt.parse() - - return pkt, nil +func NewHttpPacketFromReader(rdr io.Reader) (*HttpPacket, error) { + sb := strings.Builder{} + tee := io.TeeReader(rdr, &sb) + p := &HttpPacket{} + err := parse(p, bufio.NewReader(tee)) + if err != nil { + return nil, err + } + p.raw = []byte(sb.String()) + return p, nil } func (p *HttpPacket) Raw() []byte { @@ -123,8 +128,7 @@ func (p *HttpPacket) Tidy() { p.raw = []byte(result) } -func (p *HttpPacket) parse() error { - reader := bufio.NewReader(strings.NewReader(string(p.raw))) +func parse(p *HttpPacket, reader *bufio.Reader) error { request, err := http.ReadRequest(reader) if err != nil { return err @@ -152,6 +156,5 @@ func (p *HttpPacket) parse() error { } request.Body.Close() - return nil } diff --git a/proxy/proxy.go b/proxy/proxy.go index d4301ae..01c05fa 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -59,17 +59,15 @@ func (pxy *Proxy) Start() { } go func() { - tmpBuf := make([]byte, pxy.bufferSize) - b, err := ReadBytes(conn.(*net.TCPConn), tmpBuf) + pkt, err := packet.NewHttpPacketFromReader(conn) if err != nil { return } - log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(b)) + log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(pkt.Raw())) - pkt, err := packet.NewHttpPacket(b) if err != nil { - log.Debug("[PROXY] Error while parsing request: ", string(b)) + log.Debug("[PROXY] Error while parsing request: ", string(pkt.Raw())) conn.Close() return }