Commit 3dd96bd4 authored by Jacob Vosmaer's avatar Jacob Vosmaer

More ServeHTTP and detach methods from API

parent 34fa7497
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"net/http" "net/http"
) )
func (api *API) artifactsAuthorizeHandler(h httpHandleFunc) httpHandleFunc { func artifactsAuthorizeHandler(api *API, h httpHandleFunc) httpHandleFunc {
return api.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *apiResponse) { return api.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *apiResponse) {
r.Header.Set(tempPathHeader, a.TempPath) r.Header.Set(tempPathHeader, a.TempPath)
h(w, r) h(w, r)
......
...@@ -59,13 +59,13 @@ func (s *errorPageResponseWriter) Flush() { ...@@ -59,13 +59,13 @@ func (s *errorPageResponseWriter) Flush() {
s.WriteHeader(http.StatusOK) s.WriteHeader(http.StatusOK)
} }
func handleRailsError(documentRoot *string, handler httpHandleFunc) httpHandleFunc { func handleRailsError(documentRoot *string, handler http.Handler) httpHandleFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
rw := errorPageResponseWriter{ rw := errorPageResponseWriter{
rw: w, rw: w,
path: documentRoot, path: documentRoot,
} }
defer rw.Flush() defer rw.Flush()
handler(&rw, r) handler.ServeHTTP(&rw, r)
} }
} }
...@@ -21,11 +21,11 @@ func TestIfErrorPageIsPresented(t *testing.T) { ...@@ -21,11 +21,11 @@ func TestIfErrorPageIsPresented(t *testing.T) {
ioutil.WriteFile(filepath.Join(dir, "404.html"), []byte(errorPage), 0600) ioutil.WriteFile(filepath.Join(dir, "404.html"), []byte(errorPage), 0600)
w := httptest.NewRecorder() w := httptest.NewRecorder()
h := httpHandleFunc(func(w http.ResponseWriter, _ *http.Request) {
handleRailsError(&dir, func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
fmt.Fprint(w, "Not Found") fmt.Fprint(w, "Not Found")
})(w, nil) })
handleRailsError(&dir, h)(w, nil)
w.Flush() w.Flush()
assertResponseCode(t, w, 404) assertResponseCode(t, w, 404)
...@@ -41,11 +41,11 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) { ...@@ -41,11 +41,11 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
errorResponse := "ERROR" errorResponse := "ERROR"
h := httpHandleFunc(func(w http.ResponseWriter, _ *http.Request) {
handleRailsError(&dir, func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
fmt.Fprint(w, errorResponse) fmt.Fprint(w, errorResponse)
})(w, nil) })
handleRailsError(&dir, h)(w, nil)
w.Flush() w.Flush()
assertResponseCode(t, w, 404) assertResponseCode(t, w, 404)
......
...@@ -26,7 +26,7 @@ func looksLikeRepo(p string) bool { ...@@ -26,7 +26,7 @@ func looksLikeRepo(p string) bool {
return true return true
} }
func (api *API) repoPreAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc { func repoPreAuthorizeHandler(api *API, handleFunc serviceHandleFunc) httpHandleFunc {
return api.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *apiResponse) { return api.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *apiResponse) {
if a.RepoPath == "" { if a.RepoPath == "" {
fail500(w, errors.New("repoPreAuthorizeHandler: RepoPath empty")) fail500(w, errors.New("repoPreAuthorizeHandler: RepoPath empty"))
......
...@@ -17,7 +17,7 @@ import ( ...@@ -17,7 +17,7 @@ import (
"path/filepath" "path/filepath"
) )
func (api *API) lfsAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc { func lfsAuthorizeHandler(api *API, handleFunc serviceHandleFunc) httpHandleFunc {
return api.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *apiResponse) { return api.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *apiResponse) {
if a.StoreLFSPath == "" { if a.StoreLFSPath == "" {
...@@ -39,7 +39,8 @@ func (api *API) lfsAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc ...@@ -39,7 +39,8 @@ func (api *API) lfsAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc
}, "/authorize") }, "/authorize")
} }
func (p *Proxy) handleStoreLfsObject(w http.ResponseWriter, r *http.Request, a *apiResponse) { func handleStoreLfsObject(h http.Handler) serviceHandleFunc {
return func(w http.ResponseWriter, r *http.Request, a *apiResponse) {
file, err := ioutil.TempFile(a.StoreLFSPath, a.LfsOid) file, err := ioutil.TempFile(a.StoreLFSPath, a.LfsOid)
if err != nil { if err != nil {
fail500(w, fmt.Errorf("handleStoreLfsObject: create tempfile: %v", err)) fail500(w, fmt.Errorf("handleStoreLfsObject: create tempfile: %v", err))
...@@ -75,5 +76,6 @@ func (p *Proxy) handleStoreLfsObject(w http.ResponseWriter, r *http.Request, a * ...@@ -75,5 +76,6 @@ func (p *Proxy) handleStoreLfsObject(w http.ResponseWriter, r *http.Request, a *
r.ContentLength = 0 r.ContentLength = 0
// And proxy the request // And proxy the request
p.ServeHTTP(w, r) h.ServeHTTP(w, r)
}
} }
...@@ -70,27 +70,27 @@ func compileRoutes(u *upstream) { ...@@ -70,27 +70,27 @@ func compileRoutes(u *upstream) {
proxy := u.Proxy proxy := u.Proxy
httpRoutes = []httpRoute{ httpRoutes = []httpRoute{
// Git Clone // Git Clone
httpRoute{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), api.repoPreAuthorizeHandler(handleGetInfoRefs)}, httpRoute{"GET", regexp.MustCompile(gitProjectPattern + `info/refs\z`), repoPreAuthorizeHandler(api, handleGetInfoRefs)},
httpRoute{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(api.repoPreAuthorizeHandler(handlePostRPC))}, httpRoute{"POST", regexp.MustCompile(gitProjectPattern + `git-upload-pack\z`), contentEncodingHandler(repoPreAuthorizeHandler(api, handlePostRPC))},
httpRoute{"POST", regexp.MustCompile(gitProjectPattern + `git-receive-pack\z`), contentEncodingHandler(api.repoPreAuthorizeHandler(handlePostRPC))}, httpRoute{"POST", regexp.MustCompile(gitProjectPattern + `git-receive-pack\z`), contentEncodingHandler(repoPreAuthorizeHandler(api, handlePostRPC))},
httpRoute{"PUT", regexp.MustCompile(gitProjectPattern + `gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`), api.lfsAuthorizeHandler(proxy.handleStoreLfsObject)}, httpRoute{"PUT", regexp.MustCompile(gitProjectPattern + `gitlab-lfs/objects/([0-9a-f]{64})/([0-9]+)\z`), lfsAuthorizeHandler(api, handleStoreLfsObject(proxy))},
// Repository Archive // Repository Archive
httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.zip\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.zip\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.gz\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.gz\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.bz2\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectPattern + `repository/archive.tar.bz2\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
// Repository Archive API // Repository Archive API
httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.zip\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.zip\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.gz\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.gz\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), api.repoPreAuthorizeHandler(handleGetArchive)}, httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), repoPreAuthorizeHandler(api, handleGetArchive)},
// CI Artifacts API // CI Artifacts API
httpRoute{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(api.artifactsAuthorizeHandler(handleFileUploads(proxy)))}, httpRoute{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(artifactsAuthorizeHandler(api, handleFileUploads(proxy)))},
// Explicitly proxy API requests // Explicitly proxy API requests
httpRoute{"", regexp.MustCompile(apiPattern), proxy}, httpRoute{"", regexp.MustCompile(apiPattern), proxy},
...@@ -102,7 +102,7 @@ func compileRoutes(u *upstream) { ...@@ -102,7 +102,7 @@ func compileRoutes(u *upstream) {
handleDevelopmentMode(developmentMode, handleDevelopmentMode(developmentMode,
handleDeployPage(documentRoot, handleDeployPage(documentRoot,
handleRailsError(documentRoot, handleRailsError(documentRoot,
proxy.ServeHTTP, proxy,
), ),
), ),
), ),
...@@ -114,7 +114,7 @@ func compileRoutes(u *upstream) { ...@@ -114,7 +114,7 @@ func compileRoutes(u *upstream) {
u.handleServeFile(documentRoot, CacheDisabled, u.handleServeFile(documentRoot, CacheDisabled,
handleDeployPage(documentRoot, handleDeployPage(documentRoot,
handleRailsError(documentRoot, handleRailsError(documentRoot,
proxy.ServeHTTP, 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