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,41 +85,43 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te ...@@ -85,41 +85,43 @@ 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 {
tempPath := r.Header.Get(tempPathHeader) return func(w http.ResponseWriter, r *http.Request) {
if tempPath == "" { tempPath := r.Header.Get(tempPathHeader)
fail500(w, errors.New("handleFileUploads: TempPath empty")) if tempPath == "" {
return fail500(w, errors.New("handleFileUploads: TempPath empty"))
} return
r.Header.Del(tempPathHeader) }
r.Header.Del(tempPathHeader)
var body bytes.Buffer var body bytes.Buffer
writer := multipart.NewWriter(&body) writer := multipart.NewWriter(&body)
defer writer.Close() defer writer.Close()
// Rewrite multipart form data // Rewrite multipart form data
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))
}
return
} }
return
}
if cleanup != nil { if cleanup != nil {
defer cleanup() defer cleanup()
} }
// Close writer // Close writer
writer.Close() writer.Close()
// Hijack the request // Hijack the request
r.Body = ioutil.NopCloser(&body) r.Body = ioutil.NopCloser(&body)
r.ContentLength = int64(body.Len()) r.ContentLength = int64(body.Len())
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