Commit 0592ff1c authored by Jacob Vosmaer's avatar Jacob Vosmaer

Remove relativeURIPath from gitReq

parent a7666718
......@@ -90,7 +90,7 @@ func compileRoutes(u *upstream) {
// Serve assets
httpRoute{"", regexp.MustCompile(`^/assets/`),
handleServeFile(documentRoot, CacheExpireMax,
u.handleServeFile(documentRoot, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(documentRoot,
handleRailsError(documentRoot,
......@@ -103,7 +103,7 @@ func compileRoutes(u *upstream) {
// Serve static files or forward the requests
httpRoute{"", nil,
handleServeFile(documentRoot, CacheDisabled,
u.handleServeFile(documentRoot, CacheDisabled,
handleDeployPage(documentRoot,
handleRailsError(documentRoot,
u.proxyRequest,
......
......@@ -16,9 +16,9 @@ const (
CacheExpireMax
)
func handleServeFile(documentRoot *string, cache CacheMode, notFoundHandler serviceHandleFunc) serviceHandleFunc {
func (u *upstream) handleServeFile(documentRoot *string, cache CacheMode, notFoundHandler serviceHandleFunc) serviceHandleFunc {
return func(w http.ResponseWriter, r *gitRequest) {
file := filepath.Join(*documentRoot, r.relativeURIPath)
file := filepath.Join(*documentRoot, u.relativeURIPath(cleanURIPath(r.URL.Path)))
// The filepath.Join does Clean traversing directories up
if !strings.HasPrefix(file, *documentRoot) {
......
......@@ -15,12 +15,11 @@ func TestServingNonExistingFile(t *testing.T) {
dir := "/path/to/non/existing/directory"
httpRequest, _ := http.NewRequest("GET", "/file", nil)
request := &gitRequest{
Request: httpRequest,
relativeURIPath: "/static/file",
Request: httpRequest,
}
w := httptest.NewRecorder()
handleServeFile(&dir, CacheDisabled, nil)(w, request)
newUpstream("http://localhost", nil).handleServeFile(&dir, CacheDisabled, nil)(w, request)
assertResponseCode(t, w, 404)
}
......@@ -33,26 +32,24 @@ func TestServingDirectory(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
request := &gitRequest{
Request: httpRequest,
relativeURIPath: "/",
Request: httpRequest,
}
w := httptest.NewRecorder()
handleServeFile(&dir, CacheDisabled, nil)(w, request)
newUpstream("http://localhost", nil).handleServeFile(&dir, CacheDisabled, nil)(w, request)
assertResponseCode(t, w, 404)
}
func TestServingMalformedUri(t *testing.T) {
dir := "/path/to/non/existing/directory"
httpRequest, _ := http.NewRequest("GET", "/file", nil)
httpRequest, _ := http.NewRequest("GET", "/../../../static/file", nil)
request := &gitRequest{
Request: httpRequest,
relativeURIPath: "/../../../static/file",
Request: httpRequest,
}
w := httptest.NewRecorder()
handleServeFile(&dir, CacheDisabled, nil)(w, request)
assertResponseCode(t, w, 500)
newUpstream("http://localhost", nil).handleServeFile(&dir, CacheDisabled, nil)(w, request)
assertResponseCode(t, w, 404)
}
func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
......@@ -60,11 +57,10 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
request := &gitRequest{
Request: httpRequest,
relativeURIPath: "/static/file",
}
executed := false
handleServeFile(&dir, CacheDisabled, func(w http.ResponseWriter, r *gitRequest) {
newUpstream("http://localhost", nil).handleServeFile(&dir, CacheDisabled, func(w http.ResponseWriter, r *gitRequest) {
executed = (r == request)
})(nil, request)
if !executed {
......@@ -81,15 +77,14 @@ func TestServingTheActualFile(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
request := &gitRequest{
Request: httpRequest,
relativeURIPath: "/file",
Request: httpRequest,
}
fileContent := "STATIC"
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder()
handleServeFile(&dir, CacheDisabled, nil)(w, request)
newUpstream("http://localhost", nil).handleServeFile(&dir, CacheDisabled, nil)(w, request)
assertResponseCode(t, w, 200)
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
......@@ -106,7 +101,6 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
request := &gitRequest{
Request: httpRequest,
relativeURIPath: "/file",
}
if enableGzip {
......@@ -124,7 +118,7 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
ioutil.WriteFile(filepath.Join(dir, "file"), []byte(fileContent), 0600)
w := httptest.NewRecorder()
handleServeFile(&dir, CacheDisabled, nil)(w, request)
newUpstream("http://localhost", nil).handleServeFile(&dir, CacheDisabled, nil)(w, request)
assertResponseCode(t, w, 200)
if enableGzip {
assertResponseHeader(t, w, "Content-Encoding", "gzip")
......
......@@ -57,9 +57,6 @@ type authorizationResponse struct {
type gitRequest struct {
*http.Request
authorizationResponse
// This field contains the URL.Path stripped from RelativeUrlRoot
relativeURIPath string
}
func newUpstream(authBackend string, authTransport http.RoundTripper) *upstream {
......@@ -87,6 +84,10 @@ func newUpstream(authBackend string, authTransport http.RoundTripper) *upstream
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 httpRoute
......@@ -112,11 +113,6 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
return
}
// Strip prefix and add "/"
// To match against non-relative URL
// Making it simpler for our matcher
relativeURIPath := cleanURIPath(strings.TrimPrefix(URIPath, u.relativeURLRoot))
// Look for a matching Git service
foundService := false
for _, g = range httpRoutes {
......@@ -124,7 +120,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue
}
if g.regex == nil || g.regex.MatchString(relativeURIPath) {
if g.regex == nil || g.regex.MatchString(u.relativeURIPath(URIPath)) {
foundService = true
break
}
......@@ -138,7 +134,6 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
request := gitRequest{
Request: r,
relativeURIPath: relativeURIPath,
}
g.handleFunc(&w, &request)
......
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