From 0ca0d552ebe0740eba62772a289e13c6f8496684 Mon Sep 17 00:00:00 2001 From: Tobias Breitwieser Date: Fri, 25 Sep 2015 14:10:03 +0200 Subject: [PATCH 1/2] change to official http2 repo The golang.org/x/net/http2 is now the official http2 repo. It is advised to change the imports to it. --- server/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 4f29c6460..03bca6078 100644 --- a/server/server.go +++ b/server/server.go @@ -14,7 +14,7 @@ import ( "os" "os/signal" - "github.com/bradfitz/http2" + "golang.org/x/net/http2" ) // Server represents an instance of a server, which serves From be6fc35326051094b3dcbd5fa7c106ae57705dc1 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 27 Sep 2015 18:27:45 -0600 Subject: [PATCH 2/2] fastcgi: Fix REQUEST_URI if rewrite directive changes URL --- middleware/fastcgi/fastcgi.go | 13 ++++++++++++- middleware/rewrite/rewrite.go | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 85e5b5228..cdb8e91fd 100644 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -166,6 +166,17 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] scriptFilename = absPath } + // Get the request URI. The request URI might be as it came in over the wire, + // or it might have been rewritten internally by the rewrite middleware (see issue #256). + // If it was rewritten, there will be a header indicating the original URL, + // which is needed to get the correct RequestURI value for PHP apps. + const internalRewriteFieldName = "Caddy-Rewrite-Original-URI" + reqURI := r.URL.RequestURI() + if origURI := r.Header.Get(internalRewriteFieldName); origURI != "" { + reqURI = origURI + r.Header.Del(internalRewriteFieldName) + } + // Some variables are unused but cleared explicitly to prevent // the parent environment from interfering. env = map[string]string{ @@ -192,7 +203,7 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] "DOCUMENT_ROOT": h.AbsRoot, "DOCUMENT_URI": docURI, "HTTP_HOST": r.Host, // added here, since not always part of headers - "REQUEST_URI": r.URL.RequestURI(), + "REQUEST_URI": reqURI, "SCRIPT_FILENAME": scriptFilename, "SCRIPT_NAME": scriptName, } diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index ed2e9bd9f..301e81cd5 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -49,6 +49,9 @@ func NewSimpleRule(from, to string) SimpleRule { // Rewrite rewrites the internal location of the current request. func (s SimpleRule) Rewrite(r *http.Request) bool { if s.From == r.URL.Path { + // take note of this rewrite for internal use by fastcgi + // all we need is the URI, not full URL + r.Header.Set("Caddy-Rewrite-Original-URI", r.URL.RequestURI()) r.URL.Path = s.To return true } @@ -129,6 +132,10 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool { return false } + // take note of this rewrite for internal use by fastcgi + // all we need is the URI, not full URL + req.Header.Set("Caddy-Rewrite-Original-URI", req.URL.RequestURI()) + // perform rewrite req.URL.Path = url.Path if url.RawQuery != "" {