Skip to content

Instantly share code, notes, and snippets.

@simonjohansson
Created August 19, 2015 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonjohansson/847e972f1459ea4cd65e to your computer and use it in GitHub Desktop.
Save simonjohansson/847e972f1459ea4cd65e to your computer and use it in GitHub Desktop.
diff --git a/proxy/proxy.go b/proxy/proxy.go
index 1bd8f20..8dec23a 100644
--- a/proxy/proxy.go
+++ b/proxy/proxy.go
@@ -2,18 +2,18 @@ package proxy
import (
"errors"
+ "fmt"
+ "github.com/cloudfoundry/dropsonde"
+ "github.com/cloudfoundry/gorouter/access_log"
+ router_http "github.com/cloudfoundry/gorouter/common/http"
+ "github.com/cloudfoundry/gorouter/route"
+ steno "github.com/cloudfoundry/gosteno"
"io"
"net"
"net/http"
"net/http/httputil"
"strings"
"time"
-
- "github.com/cloudfoundry/dropsonde"
- "github.com/cloudfoundry/gorouter/access_log"
- router_http "github.com/cloudfoundry/gorouter/common/http"
- "github.com/cloudfoundry/gorouter/route"
- steno "github.com/cloudfoundry/gosteno"
)
const (
@@ -117,6 +117,8 @@ func (p *proxy) lookup(request *http.Request) *route.Pool {
}
func (p *proxy) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
+ fmt.Println("In proxy.ServeHTTP")
+ fmt.Println("Request: ", request)
startedAt := time.Now()
accessLog := access_log.AccessLogRecord{
@@ -207,7 +209,9 @@ func (p *proxy) ServeHTTP(responseWriter http.ResponseWriter, request *http.Requ
},
}
+ fmt.Println("X-Forwarded-For before newReverseProxy.ServeHTTP: ", request.Header.Get("X-Forwarded-For"))
p.newReverseProxy(roundTripper, request).ServeHTTP(proxyWriter, request)
+ fmt.Println("X-Forwarded-For after newReverseProxy.ServeHTTP: ", request.Header.Get("X-Forwarded-For"))
accessLog.FinishedAt = time.Now()
accessLog.BodyBytesSent = proxyWriter.Size()
diff --git a/src/net/http/httputil/reverseproxy.go b/src/net/http/httputil/reverseproxy.go
index ab46370..d5f5d94 100644
--- a/src/net/http/httputil/reverseproxy.go
+++ b/src/net/http/httputil/reverseproxy.go
@@ -7,6 +7,7 @@
package httputil
import (
+ "fmt"
"io"
"log"
"net"
@@ -101,6 +102,7 @@ var hopHeaders = []string{
}
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
+ fmt.Println("In net/http/httputil/reverseproxy.go: ServeHTTP")
transport := p.Transport
if transport == nil {
transport = http.DefaultTransport
@@ -132,6 +134,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
}
+ fmt.Println("X-Forwarded-For in req before 'If we aren't the first proxy retain prior': ", req.Header.Get("X-Forwarded-For"))
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
@@ -140,7 +143,9 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
outreq.Header.Set("X-Forwarded-For", clientIP)
+ fmt.Println("X-Forwarded-For in outreq: ", outreq.Header.Get("X-Forwarded-For"))
}
+ fmt.Println("X-Forwarded-For in req after 'If we aren't the first proxy retain prior': ", req.Header.Get("X-Forwarded-For"))
res, err := transport.RoundTrip(outreq)
if err != nil {
@@ -158,6 +163,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(res.StatusCode)
p.copyResponse(rw, res.Body)
+ fmt.Println("Done in net/http/httputil/reverseproxy.go: ServeHTTP")
}
func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment