Skip to content

Commit

Permalink
Merge pull request #3213 from getlantern/2.0.7-fixes
Browse files Browse the repository at this point in the history
Changes to return correct response code from the local HTTP proxy and…
  • Loading branch information
myleshorton committed Oct 1, 2015
2 parents 5a50d31 + 907a878 commit cfa200d
Show file tree
Hide file tree
Showing 8 changed files with 10,316 additions and 46,399 deletions.
38 changes: 24 additions & 14 deletions src/github.com/getlantern/flashlight/client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (client *Client) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
// Direct proxying can only be used for plain HTTP connections.
log.Debugf("Reverse proxying %s %v", req.Method, req.URL)
rp.ServeHTTP(resp, req)
} else {
log.Debugf("Could not get a reverse proxy connection -- responding bad gateway")
respondBadGateway(resp, fmt.Sprintf("Unable to get a connection: %s", err))
}
}

Expand Down Expand Up @@ -75,18 +78,6 @@ func (client *Client) intercept(resp http.ResponseWriter, req *http.Request) {
return
}

// Respond OK as soon as possible, even if we don't have the outbound connection
// established yet, to avoid timeouts on the client application
success := make(chan bool, 1)
go func() {
if e := respondOK(clientConn, req); e != nil {
log.Errorf("Unable to respond OK: %s", e)
success <- false
return
}
success <- true
}()

// Establish outbound connection.
addr := hostIncludingPort(req, 443)
d := func(network, addr string) (net.Conn, error) {
Expand All @@ -107,9 +98,20 @@ func (client *Client) intercept(resp http.ResponseWriter, req *http.Request) {
}
if err != nil {
log.Debugf("Could not dial %v", err)
respondBadGatewayHijacked(clientConn, req)
return
}

success := make(chan bool, 1)
go func() {
if e := respondOK(clientConn, req); e != nil {
log.Errorf("Unable to respond OK: %s", e)
success <- false
return
}
success <- true
}()

if <-success {
// Pipe data between the client and the proxy.
pipeData(clientConn, connOut, func() { closeOnce.Do(closeConns) })
Expand All @@ -135,18 +137,26 @@ func pipeData(clientConn net.Conn, connOut net.Conn, closeFunc func()) {

func respondOK(writer io.Writer, req *http.Request) error {
log.Debugf("Responding OK to %v", req.URL)
return respondHijacked(writer, req, http.StatusOK)
}

func respondBadGatewayHijacked(writer io.Writer, req *http.Request) error {
return respondHijacked(writer, req, http.StatusBadGateway)
}

func respondHijacked(writer io.Writer, req *http.Request, statusCode int) error {
log.Debugf("Responding %v to %v", statusCode, req.URL)
defer func() {
if err := req.Body.Close(); err != nil {
log.Debugf("Error closing body of OK response: %s", err)
}
}()

resp := &http.Response{
StatusCode: http.StatusOK,
StatusCode: statusCode,
ProtoMajor: 1,
ProtoMinor: 1,
}

return resp.Write(writer)
}

Expand Down
Loading

0 comments on commit cfa200d

Please sign in to comment.