Commit 570911ae authored by Jacob Vosmaer's avatar Jacob Vosmaer

Start using Proxy as a http.Handler

parent c7e3abe4
......@@ -48,6 +48,10 @@ type httpRoute struct {
type httpHandleFunc func(http.ResponseWriter, *http.Request)
func (h httpHandleFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h(w, r)
}
const projectPattern = `^/[^/]+/[^/]+/`
const gitProjectPattern = `^/[^/]+/[^/]+\.git/`
......@@ -86,7 +90,7 @@ func compileRoutes(u *upstream) {
httpRoute{"GET", regexp.MustCompile(projectsAPIPattern + `repository/archive.tar.bz2\z`), api.repoPreAuthorizeHandler(handleGetArchive)},
// CI Artifacts API
httpRoute{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(api.artifactsAuthorizeHandler(proxy.handleFileUploads))},
httpRoute{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(api.artifactsAuthorizeHandler(handleFileUploads(proxy)))},
// Explicitly proxy API requests
httpRoute{"", regexp.MustCompile(apiPattern), proxy.ServeHTTP},
......
......@@ -85,7 +85,8 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
return cleanup, nil
}
func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
func handleFileUploads(h http.Handler) httpHandleFunc {
return func(w http.ResponseWriter, r *http.Request) {
tempPath := r.Header.Get(tempPathHeader)
if tempPath == "" {
fail500(w, errors.New("handleFileUploads: TempPath empty"))
......@@ -101,7 +102,7 @@ func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
cleanup, err := rewriteFormFilesFromMultipart(r, writer, tempPath)
if err != nil {
if err == http.ErrNotMultipart {
p.ServeHTTP(w, r)
h.ServeHTTP(w, r)
} else {
fail500(w, fmt.Errorf("handleFileUploads: extract files from multipart: %v", err))
}
......@@ -121,5 +122,6 @@ func (p *Proxy) handleFileUploads(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Content-Type", writer.FormDataContentType())
// Proxy the request
p.ServeHTTP(w, r)
h.ServeHTTP(w, r)
}
}
......@@ -17,7 +17,7 @@ import (
func TestUploadTempPathRequirement(t *testing.T) {
response := httptest.NewRecorder()
request := &http.Request{}
dummyUpstream.Proxy.handleFileUploads(response, request)
handleFileUploads(dummyUpstream.Proxy).ServeHTTP(response, request)
assertResponseCode(t, response, 500)
}
......@@ -53,7 +53,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
httpRequest.Header.Set(tempPathHeader, tempPath)
u := newUpstream(ts.URL, nil)
u.Proxy.handleFileUploads(response, httpRequest)
handleFileUploads(u.Proxy).ServeHTTP(response, httpRequest)
assertResponseCode(t, response, 202)
if response.Body.String() != "RESPONSE" {
t.Fatal("Expected RESPONSE in response body")
......@@ -128,7 +128,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
response := httptest.NewRecorder()
u := newUpstream(ts.URL, nil)
u.Proxy.handleFileUploads(response, httpRequest)
handleFileUploads(u.Proxy).ServeHTTP(response, httpRequest)
assertResponseCode(t, response, 202)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
......
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