Commit 1559a5f7 authored by Jacob Vosmaer's avatar Jacob Vosmaer

No longer use global *documentRoot

parent de0cffc2
......@@ -7,9 +7,9 @@ import (
"path/filepath"
)
func handleDeployPage(documentRoot *string, handler http.Handler) http.HandlerFunc {
func handleDeployPage(documentRoot string, handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
deployPage := filepath.Join(*documentRoot, "index.html")
deployPage := filepath.Join(documentRoot, "index.html")
data, err := ioutil.ReadFile(deployPage)
if err != nil {
handler.ServeHTTP(w, r)
......
......@@ -20,7 +20,7 @@ func TestIfNoDeployPageExist(t *testing.T) {
w := httptest.NewRecorder()
executed := false
handleDeployPage(&dir, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
handleDeployPage(dir, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
executed = true
}))(w, nil)
if !executed {
......@@ -41,7 +41,7 @@ func TestIfDeployPageExist(t *testing.T) {
w := httptest.NewRecorder()
executed := false
handleDeployPage(&dir, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
handleDeployPage(dir, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
executed = true
}))(w, nil)
if executed {
......
......@@ -80,5 +80,7 @@ func main() {
}()
}
log.Fatal(http.Serve(listener, newUpstream(*authBackend, *authSocket)))
upstream := newUpstream(*authBackend, *authSocket)
upstream.DocumentRoot = *documentRoot
log.Fatal(http.Serve(listener, upstream))
}
......@@ -17,12 +17,12 @@ const (
CacheExpireMax
)
func (u *upstream) handleServeFile(documentRoot *string, cache CacheMode, notFoundHandler http.HandlerFunc) http.HandlerFunc {
func (u *upstream) handleServeFile(documentRoot string, 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, u.relativeURIPath(cleanURIPath(r.URL.Path)))
// The filepath.Join does Clean traversing directories up
if !strings.HasPrefix(file, *documentRoot) {
if !strings.HasPrefix(file, documentRoot) {
helper.Fail500(w, &os.PathError{
Op: "open",
Path: file,
......
......@@ -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)
dummyUpstream.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)
dummyUpstream.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)
dummyUpstream.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) {
dummyUpstream.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)
dummyUpstream.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)
dummyUpstream.handleServeFile(dir, CacheDisabled, nil)(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
if enableGzip {
helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
......
......@@ -24,6 +24,7 @@ import (
type upstream struct {
API *api.API
Proxy *proxy.Proxy
DocumentRoot string
authBackend string
relativeURLRoot string
routes []route
......@@ -79,10 +80,10 @@ func (u *upstream) compileRoutes() {
// Serve assets
route{"", regexp.MustCompile(`^/assets/`),
u.handleServeFile(documentRoot, CacheExpireMax,
u.handleServeFile(u.DocumentRoot, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(documentRoot,
errorpage.Inject(*documentRoot,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
),
),
......@@ -92,9 +93,9 @@ func (u *upstream) compileRoutes() {
// Serve static files or forward the requests
route{"", nil,
u.handleServeFile(documentRoot, CacheDisabled,
handleDeployPage(documentRoot,
errorpage.Inject(*documentRoot,
u.handleServeFile(u.DocumentRoot, CacheDisabled,
handleDeployPage(u.DocumentRoot,
errorpage.Inject(u.DocumentRoot,
u.Proxy,
),
),
......
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