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

No longer use global *documentRoot

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