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