Skip to content
Snippets Groups Projects
Unverified Commit c1e83991 authored by Rohith Jayawardene's avatar Rohith Jayawardene Committed by GitHub
Browse files

Method check (#302)

* Method Check

Ensuring the http.Hijacker is implemented by the ResponseWriter

* HTTP Hijack Check

Ensure the http.Hijack method is implemented by the http.ResponseWriter before attempting to grab the client connection
parent c7d76dfd
No related branches found
No related tags found
No related merge requests found
......@@ -261,29 +261,34 @@ func transferBytes(src io.Reader, dest io.Writer, wg *sync.WaitGroup) (int64, er
// tryUpdateConnection attempt to upgrade the connection to a http pdy stream
func tryUpdateConnection(req *http.Request, writer http.ResponseWriter, endpoint *url.URL) error {
// step: dial the endpoint
tlsConn, err := tryDialEndpoint(endpoint)
server, err := tryDialEndpoint(endpoint)
if err != nil {
return err
}
defer tlsConn.Close()
defer server.Close()
// step: we need to hijack the underlining client connection
clientConn, _, err := writer.(http.Hijacker).Hijack()
// @check the the response writer implements the Hijack method
if _, ok := writer.(http.Hijacker); !ok {
return errors.New("writer does not implement http.Hijacker method")
}
// @step: get the client connection object
client, _, err := writer.(http.Hijacker).Hijack()
if err != nil {
return err
}
defer clientConn.Close()
defer client.Close()
// step: write the request to upstream
if err = req.Write(tlsConn); err != nil {
if err = req.Write(server); err != nil {
return err
}
// step: copy the date between client and upstream endpoint
// @step: copy the data between client and upstream endpoint
var wg sync.WaitGroup
wg.Add(2)
go transferBytes(tlsConn, clientConn, &wg)
go transferBytes(clientConn, tlsConn, &wg)
go transferBytes(server, client, &wg)
go transferBytes(client, server, &wg)
wg.Wait()
return nil
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment