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

Remove relativeURIPath from gitReq

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