Commit 7415226f authored by Jacob Vosmaer's avatar Jacob Vosmaer

Get rid of gitRequest

parent b9409540
...@@ -17,7 +17,7 @@ import ( ...@@ -17,7 +17,7 @@ import (
"time" "time"
) )
func handleGetArchive(w http.ResponseWriter, r *gitRequest) { func handleGetArchive(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
var format string var format string
urlPath := r.URL.Path urlPath := r.URL.Path
switch filepath.Base(urlPath) { switch filepath.Base(urlPath) {
...@@ -34,16 +34,16 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) { ...@@ -34,16 +34,16 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
return return
} }
archiveFilename := path.Base(r.ArchivePath) archiveFilename := path.Base(a.ArchivePath)
if cachedArchive, err := os.Open(r.ArchivePath); err == nil { if cachedArchive, err := os.Open(a.ArchivePath); err == nil {
defer cachedArchive.Close() defer cachedArchive.Close()
log.Printf("Serving cached file %q", r.ArchivePath) log.Printf("Serving cached file %q", a.ArchivePath)
setArchiveHeaders(w, format, archiveFilename) setArchiveHeaders(w, format, archiveFilename)
// Even if somebody deleted the cachedArchive from disk since we opened // Even if somebody deleted the cachedArchive from disk since we opened
// the file, Unix file semantics guarantee we can still read from the // the file, Unix file semantics guarantee we can still read from the
// open file in this process. // open file in this process.
http.ServeContent(w, r.Request, "", time.Unix(0, 0), cachedArchive) http.ServeContent(w, r, "", time.Unix(0, 0), cachedArchive)
return return
} }
...@@ -51,7 +51,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) { ...@@ -51,7 +51,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
// safe. We create the tempfile in the same directory as the final cached // safe. We create the tempfile in the same directory as the final cached
// archive we want to create so that we can use an atomic link(2) operation // archive we want to create so that we can use an atomic link(2) operation
// to finalize the cached archive. // to finalize the cached archive.
tempFile, err := prepareArchiveTempfile(path.Dir(r.ArchivePath), archiveFilename) tempFile, err := prepareArchiveTempfile(path.Dir(a.ArchivePath), archiveFilename)
if err != nil { if err != nil {
fail500(w, fmt.Errorf("handleGetArchive: create tempfile: %v", err)) fail500(w, fmt.Errorf("handleGetArchive: create tempfile: %v", err))
return return
...@@ -61,7 +61,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) { ...@@ -61,7 +61,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
compressCmd, archiveFormat := parseArchiveFormat(format) compressCmd, archiveFormat := parseArchiveFormat(format)
archiveCmd := gitCommand("", "git", "--git-dir="+r.RepoPath, "archive", "--format="+archiveFormat, "--prefix="+r.ArchivePrefix+"/", r.CommitId) archiveCmd := gitCommand("", "git", "--git-dir="+a.RepoPath, "archive", "--format="+archiveFormat, "--prefix="+a.ArchivePrefix+"/", a.CommitId)
archiveStdout, err := archiveCmd.StdoutPipe() archiveStdout, err := archiveCmd.StdoutPipe()
if err != nil { if err != nil {
fail500(w, fmt.Errorf("handleGetArchive: archive stdout: %v", err)) fail500(w, fmt.Errorf("handleGetArchive: archive stdout: %v", err))
...@@ -117,7 +117,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) { ...@@ -117,7 +117,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
} }
} }
if err := finalizeCachedArchive(tempFile, r.ArchivePath); err != nil { if err := finalizeCachedArchive(tempFile, a.ArchivePath); err != nil {
logError(fmt.Errorf("handleGetArchive: finalize cached archive: %v", err)) logError(fmt.Errorf("handleGetArchive: finalize cached archive: %v", err))
return return
} }
......
...@@ -5,9 +5,8 @@ import ( ...@@ -5,9 +5,8 @@ import (
) )
func (u *upstream) artifactsAuthorizeHandler(h httpHandleFunc) httpHandleFunc { func (u *upstream) artifactsAuthorizeHandler(h httpHandleFunc) httpHandleFunc {
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) { return u.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
req := r.Request r.Header.Set(tempPathHeader, a.TempPath)
req.Header.Set(tempPathHeader, r.TempPath) h(w, r)
h(w, req)
}, "/authorize") }, "/authorize")
} }
...@@ -85,11 +85,11 @@ func (u *upstream) preAuthorizeHandler(h serviceHandleFunc, suffix string) httpH ...@@ -85,11 +85,11 @@ func (u *upstream) preAuthorizeHandler(h serviceHandleFunc, suffix string) httpH
return return
} }
g := &gitRequest{Request: r} a := &authorizationResponse{}
// The auth backend validated the client request and told us additional // The auth backend validated the client request and told us additional
// request metadata. We must extract this information from the auth // request metadata. We must extract this information from the auth
// response body. // response body.
if err := json.NewDecoder(authResponse.Body).Decode(&g.authorizationResponse); err != nil { if err := json.NewDecoder(authResponse.Body).Decode(a); err != nil {
fail500(w, fmt.Errorf("preAuthorizeHandler: decode authorization response: %v", err)) fail500(w, fmt.Errorf("preAuthorizeHandler: decode authorization response: %v", err))
return return
} }
...@@ -105,6 +105,6 @@ func (u *upstream) preAuthorizeHandler(h serviceHandleFunc, suffix string) httpH ...@@ -105,6 +105,6 @@ func (u *upstream) preAuthorizeHandler(h serviceHandleFunc, suffix string) httpH
} }
} }
h(w, g) h(w, r, a)
} }
} }
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"testing" "testing"
) )
func okHandler(w http.ResponseWriter, r *gitRequest) { func okHandler(w http.ResponseWriter, _ *http.Request, _ *authorizationResponse) {
w.WriteHeader(201) w.WriteHeader(201)
fmt.Fprint(w, "{\"status\":\"ok\"}") fmt.Fprint(w, "{\"status\":\"ok\"}")
} }
......
...@@ -27,22 +27,22 @@ func looksLikeRepo(p string) bool { ...@@ -27,22 +27,22 @@ func looksLikeRepo(p string) bool {
} }
func (u *upstream) repoPreAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc { func (u *upstream) repoPreAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc {
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) { return u.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
if r.RepoPath == "" { if a.RepoPath == "" {
fail500(w, errors.New("repoPreAuthorizeHandler: RepoPath empty")) fail500(w, errors.New("repoPreAuthorizeHandler: RepoPath empty"))
return return
} }
if !looksLikeRepo(r.RepoPath) { if !looksLikeRepo(a.RepoPath) {
http.Error(w, "Not Found", 404) http.Error(w, "Not Found", 404)
return return
} }
handleFunc(w, r) handleFunc(w, r, a)
}, "") }, "")
} }
func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) { func handleGetInfoRefs(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
rpc := r.URL.Query().Get("service") rpc := r.URL.Query().Get("service")
if !(rpc == "git-upload-pack" || rpc == "git-receive-pack") { if !(rpc == "git-upload-pack" || rpc == "git-receive-pack") {
// The 'dumb' Git HTTP protocol is not supported // The 'dumb' Git HTTP protocol is not supported
...@@ -51,7 +51,7 @@ func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) { ...@@ -51,7 +51,7 @@ func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) {
} }
// Prepare our Git subprocess // Prepare our Git subprocess
cmd := gitCommand(r.GL_ID, "git", subCommand(rpc), "--stateless-rpc", "--advertise-refs", r.RepoPath) cmd := gitCommand(a.GL_ID, "git", subCommand(rpc), "--stateless-rpc", "--advertise-refs", a.RepoPath)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
fail500(w, fmt.Errorf("handleGetInfoRefs: stdout: %v", err)) fail500(w, fmt.Errorf("handleGetInfoRefs: stdout: %v", err))
...@@ -86,7 +86,7 @@ func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) { ...@@ -86,7 +86,7 @@ func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) {
} }
} }
func handlePostRPC(w http.ResponseWriter, r *gitRequest) { func handlePostRPC(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
var err error var err error
// Get Git action from URL // Get Git action from URL
...@@ -98,7 +98,7 @@ func handlePostRPC(w http.ResponseWriter, r *gitRequest) { ...@@ -98,7 +98,7 @@ func handlePostRPC(w http.ResponseWriter, r *gitRequest) {
} }
// Prepare our Git subprocess // Prepare our Git subprocess
cmd := gitCommand(r.GL_ID, "git", subCommand(action), "--stateless-rpc", r.RepoPath) cmd := gitCommand(a.GL_ID, "git", subCommand(action), "--stateless-rpc", a.RepoPath)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
fail500(w, fmt.Errorf("handlePostRPC: stdout: %v", err)) fail500(w, fmt.Errorf("handlePostRPC: stdout: %v", err))
......
...@@ -18,29 +18,29 @@ import ( ...@@ -18,29 +18,29 @@ import (
) )
func (u *upstream) lfsAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc { func (u *upstream) lfsAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc {
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) { return u.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
if r.StoreLFSPath == "" { if a.StoreLFSPath == "" {
fail500(w, errors.New("lfsAuthorizeHandler: StoreLFSPath empty")) fail500(w, errors.New("lfsAuthorizeHandler: StoreLFSPath empty"))
return return
} }
if r.LfsOid == "" { if a.LfsOid == "" {
fail500(w, errors.New("lfsAuthorizeHandler: LfsOid empty")) fail500(w, errors.New("lfsAuthorizeHandler: LfsOid empty"))
return return
} }
if err := os.MkdirAll(r.StoreLFSPath, 0700); err != nil { if err := os.MkdirAll(a.StoreLFSPath, 0700); err != nil {
fail500(w, fmt.Errorf("lfsAuthorizeHandler: mkdir StoreLFSPath: %v", err)) fail500(w, fmt.Errorf("lfsAuthorizeHandler: mkdia StoreLFSPath: %v", err))
return return
} }
handleFunc(w, r) handleFunc(w, r, a)
}, "/authorize") }, "/authorize")
} }
func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) { func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
file, err := ioutil.TempFile(r.StoreLFSPath, r.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))
return return
...@@ -58,14 +58,14 @@ func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) { ...@@ -58,14 +58,14 @@ func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) {
} }
file.Close() file.Close()
if written != r.LfsSize { if written != a.LfsSize {
fail500(w, fmt.Errorf("handleStoreLfsObject: expected size %d, wrote %d", r.LfsSize, written)) fail500(w, fmt.Errorf("handleStoreLfsObject: expected size %d, wrote %d", a.LfsSize, written))
return return
} }
shaStr := hex.EncodeToString(hash.Sum(nil)) shaStr := hex.EncodeToString(hash.Sum(nil))
if shaStr != r.LfsOid { if shaStr != a.LfsOid {
fail500(w, fmt.Errorf("handleStoreLfsObject: expected sha256 %s, got %s", r.LfsOid, shaStr)) fail500(w, fmt.Errorf("handleStoreLfsObject: expected sha256 %s, got %s", a.LfsOid, shaStr))
return return
} }
...@@ -75,5 +75,5 @@ func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) { ...@@ -75,5 +75,5 @@ func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) {
r.ContentLength = 0 r.ContentLength = 0
// And proxy the request // And proxy the request
u.proxyRequest(w, r.Request) u.proxyRequest(w, r)
} }
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
"strings" "strings"
) )
type serviceHandleFunc func(w http.ResponseWriter, r *gitRequest) type serviceHandleFunc func(http.ResponseWriter, *http.Request, *authorizationResponse)
type upstream struct { type upstream struct {
httpClient *http.Client httpClient *http.Client
...@@ -52,13 +52,6 @@ type authorizationResponse struct { ...@@ -52,13 +52,6 @@ type authorizationResponse struct {
TempPath string TempPath string
} }
// A gitRequest is an *http.Request decorated with attributes returned by the
// GitLab Rails application.
type gitRequest struct {
*http.Request
authorizationResponse
}
func newUpstream(authBackend string, authTransport http.RoundTripper) *upstream { func newUpstream(authBackend string, authTransport http.RoundTripper) *upstream {
gitlabURL, err := url.Parse(authBackend) gitlabURL, err := url.Parse(authBackend)
if err != nil { if err != nil {
......
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