Skip to content

Commit

Permalink
Close request body, use CopyBuffer for request stream and err chan 1
Browse files Browse the repository at this point in the history
  • Loading branch information
film42 committed Oct 30, 2016
1 parent b45e342 commit 6f9656b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func (c *connection) Handle() {
logger.Warn.Println(c.id, "Incoming connection disconnected.")
return
}

if err != nil {
logger.Warn.Println(c.id, "Could not parse or read request from incoming connection:", err)
return
}

defer request.Body.Close()

logger.Info.Println(c.id, "Processing connection to:", request.Method, request.Host)

if request.Method == "CONNECT" {
Expand All @@ -46,11 +47,11 @@ func (c *connection) Handle() {
}

// Spawn incoming->outgoing and outgoing->incoming streams.
signal := make(chan error)
signal := make(chan error, 1)
go streamBytes(c.incoming, c.outgoing, signal)
go streamBytes(c.outgoing, c.incoming, signal)

// Wait for either stream to complete and finish.
// Wait for either stream to complete and finish. The second will always be an error.
err = <-signal
if err != nil {
logger.Warn.Println(c.id, "Error reading or writing data", request.Host, err)
Expand Down
3 changes: 2 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type proxy interface {
}

func streamBytes(src net.Conn, dest net.Conn, signal chan error) {
_, err := io.Copy(dest, src)
buffer := make([]byte, 1024)
_, err := io.CopyBuffer(dest, src, buffer)
signal <- err
}

0 comments on commit 6f9656b

Please sign in to comment.