Commit eba05966 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Move 'relative URL' string stuff into urlPrefix

parent 0a5c156c
...@@ -17,9 +17,9 @@ const ( ...@@ -17,9 +17,9 @@ const (
CacheExpireMax CacheExpireMax
) )
func (u *upstream) handleServeFile(documentRoot string, cache CacheMode, notFoundHandler http.HandlerFunc) http.HandlerFunc { func handleServeFile(documentRoot string, prefix urlPrefix, cache CacheMode, notFoundHandler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
file := filepath.Join(documentRoot, u.relativeURIPath(cleanURIPath(r.URL.Path))) file := filepath.Join(documentRoot, prefix.strip(r.URL.Path))
// The filepath.Join does Clean traversing directories up // The filepath.Join does Clean traversing directories up
if !strings.HasPrefix(file, documentRoot) { if !strings.HasPrefix(file, documentRoot) {
......
...@@ -19,7 +19,7 @@ func TestServingNonExistingFile(t *testing.T) { ...@@ -19,7 +19,7 @@ func TestServingNonExistingFile(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil) httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest) handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 404) helper.AssertResponseCode(t, w, 404)
} }
...@@ -32,7 +32,7 @@ func TestServingDirectory(t *testing.T) { ...@@ -32,7 +32,7 @@ func TestServingDirectory(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil) httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest) handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 404) helper.AssertResponseCode(t, w, 404)
} }
...@@ -41,7 +41,7 @@ func TestServingMalformedUri(t *testing.T) { ...@@ -41,7 +41,7 @@ func TestServingMalformedUri(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/../../../static/file", nil) httpRequest, _ := http.NewRequest("GET", "/../../../static/file", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest) handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 404) helper.AssertResponseCode(t, w, 404)
} }
...@@ -50,7 +50,7 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil) httpRequest, _ := http.NewRequest("GET", "/file", nil)
executed := false executed := false
dummyUpstream.handleServeFile(dir, CacheDisabled, func(_ http.ResponseWriter, r *http.Request) { handleServeFile(dir, "/", CacheDisabled, func(_ http.ResponseWriter, r *http.Request) {
executed = (r == httpRequest) executed = (r == httpRequest)
})(nil, httpRequest) })(nil, httpRequest)
if !executed { if !executed {
...@@ -71,7 +71,7 @@ func TestServingTheActualFile(t *testing.T) { ...@@ -71,7 +71,7 @@ func TestServingTheActualFile(t *testing.T) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600) ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder() w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest) handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 200) helper.AssertResponseCode(t, w, 200)
if w.Body.String() != fileContent { if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String()) t.Error("We should serve the file: ", w.Body.String())
...@@ -102,7 +102,7 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) { ...@@ -102,7 +102,7 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600) ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder() w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest) handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 200) helper.AssertResponseCode(t, w, 200)
if enableGzip { if enableGzip {
helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip") helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
......
...@@ -22,11 +22,11 @@ import ( ...@@ -22,11 +22,11 @@ import (
) )
type upstream struct { type upstream struct {
API *api.API API *api.API
Proxy *proxy.Proxy Proxy *proxy.Proxy
DocumentRoot string DocumentRoot string
relativeURLRoot string urlPrefix urlPrefix
routes []route routes []route
} }
type route struct { type route struct {
...@@ -79,7 +79,7 @@ func (u *upstream) compileRoutes() { ...@@ -79,7 +79,7 @@ func (u *upstream) compileRoutes() {
// Serve assets // Serve assets
route{"", regexp.MustCompile(`^/assets/`), route{"", regexp.MustCompile(`^/assets/`),
u.handleServeFile(u.DocumentRoot, CacheExpireMax, handleServeFile(u.DocumentRoot, u.urlPrefix, CacheExpireMax,
handleDevelopmentMode(developmentMode, handleDevelopmentMode(developmentMode,
handleDeployPage(u.DocumentRoot, handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot, errorpage.Inject(u.DocumentRoot,
...@@ -92,7 +92,7 @@ func (u *upstream) compileRoutes() { ...@@ -92,7 +92,7 @@ func (u *upstream) compileRoutes() {
// Serve static files or forward the requests // Serve static files or forward the requests
route{"", nil, route{"", nil,
u.handleServeFile(u.DocumentRoot, CacheDisabled, handleServeFile(u.DocumentRoot, u.urlPrefix, CacheDisabled,
handleDeployPage(u.DocumentRoot, handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot, errorpage.Inject(u.DocumentRoot,
u.Proxy, u.Proxy,
...@@ -137,17 +137,13 @@ func newUpstream(authBackend string, authSocket string) *upstream { ...@@ -137,17 +137,13 @@ func newUpstream(authBackend string, authSocket string) *upstream {
URL: parsedURL, URL: parsedURL,
Version: Version, Version: Version,
}, },
Proxy: proxy.NewProxy(parsedURL, proxyTransport, Version), Proxy: proxy.NewProxy(parsedURL, proxyTransport, Version),
relativeURLRoot: relativeURLRoot, urlPrefix: urlPrefix(relativeURLRoot),
} }
up.compileRoutes() up.compileRoutes()
return up return up
} }
func (u *upstream) relativeURIPath(p string) string {
return cleanURIPath(strings.TrimPrefix(p, u.relativeURLRoot))
}
func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
var g route var g route
...@@ -168,7 +164,8 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -168,7 +164,8 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
// Check URL Root // Check URL Root
URIPath := cleanURIPath(r.URL.Path) URIPath := cleanURIPath(r.URL.Path)
if !strings.HasPrefix(URIPath, u.relativeURLRoot) && URIPath+"/" != u.relativeURLRoot { prefix := u.urlPrefix
if !prefix.match(URIPath) {
httpError(&w, r, fmt.Sprintf("Not found %q", URIPath), http.StatusNotFound) httpError(&w, r, fmt.Sprintf("Not found %q", URIPath), http.StatusNotFound)
return return
} }
...@@ -180,7 +177,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) { ...@@ -180,7 +177,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue continue
} }
if g.regex == nil || g.regex.MatchString(u.relativeURIPath(URIPath)) { if g.regex == nil || g.regex.MatchString(prefix.strip(URIPath)) {
foundService = true foundService = true
break break
} }
......
package main
import (
"strings"
)
type urlPrefix string
func (p urlPrefix) strip(path string) string {
return cleanURIPath(strings.TrimPrefix(path, string(p)))
}
func (p urlPrefix) match(path string) bool {
pre := string(p)
return strings.HasPrefix(path, pre) || path+"/" == pre
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment