Commit eba05966 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Move 'relative URL' string stuff into urlPrefix

parent 0a5c156c
......@@ -17,9 +17,9 @@ const (
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) {
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
if !strings.HasPrefix(file, documentRoot) {
......
......@@ -19,7 +19,7 @@ func TestServingNonExistingFile(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
}
......@@ -32,7 +32,7 @@ func TestServingDirectory(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
}
......@@ -41,7 +41,7 @@ func TestServingMalformedUri(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/../../../static/file", nil)
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
}
......@@ -50,7 +50,7 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
executed := false
dummyUpstream.handleServeFile(dir, CacheDisabled, func(_ http.ResponseWriter, r *http.Request) {
handleServeFile(dir, "/", CacheDisabled, func(_ http.ResponseWriter, r *http.Request) {
executed = (r == httpRequest)
})(nil, httpRequest)
if !executed {
......@@ -71,7 +71,7 @@ func TestServingTheActualFile(t *testing.T) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
......@@ -102,7 +102,7 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest)
handleServeFile(dir, "/", CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
if enableGzip {
helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
......
......@@ -25,7 +25,7 @@ type upstream struct {
API *api.API
Proxy *proxy.Proxy
DocumentRoot string
relativeURLRoot string
urlPrefix urlPrefix
routes []route
}
......@@ -79,7 +79,7 @@ func (u *upstream) compileRoutes() {
// Serve assets
route{"", regexp.MustCompile(`^/assets/`),
u.handleServeFile(u.DocumentRoot, CacheExpireMax,
handleServeFile(u.DocumentRoot, u.urlPrefix, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
......@@ -92,7 +92,7 @@ func (u *upstream) compileRoutes() {
// Serve static files or forward the requests
route{"", nil,
u.handleServeFile(u.DocumentRoot, CacheDisabled,
handleServeFile(u.DocumentRoot, u.urlPrefix, CacheDisabled,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
......@@ -138,16 +138,12 @@ func newUpstream(authBackend string, authSocket string) *upstream {
Version: Version,
},
Proxy: proxy.NewProxy(parsedURL, proxyTransport, Version),
relativeURLRoot: relativeURLRoot,
urlPrefix: urlPrefix(relativeURLRoot),
}
up.compileRoutes()
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) {
var g route
......@@ -168,7 +164,8 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
// Check URL Root
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)
return
}
......@@ -180,7 +177,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue
}
if g.regex == nil || g.regex.MatchString(u.relativeURIPath(URIPath)) {
if g.regex == nil || g.regex.MatchString(prefix.strip(URIPath)) {
foundService = true
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