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

Get rid of gitRequest

parent b9409540
......@@ -17,7 +17,7 @@ import (
"time"
)
func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
func handleGetArchive(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
var format string
urlPath := r.URL.Path
switch filepath.Base(urlPath) {
......@@ -34,16 +34,16 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
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()
log.Printf("Serving cached file %q", r.ArchivePath)
log.Printf("Serving cached file %q", a.ArchivePath)
setArchiveHeaders(w, format, archiveFilename)
// Even if somebody deleted the cachedArchive from disk since we opened
// the file, Unix file semantics guarantee we can still read from the
// 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
}
......@@ -51,7 +51,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
// 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
// to finalize the cached archive.
tempFile, err := prepareArchiveTempfile(path.Dir(r.ArchivePath), archiveFilename)
tempFile, err := prepareArchiveTempfile(path.Dir(a.ArchivePath), archiveFilename)
if err != nil {
fail500(w, fmt.Errorf("handleGetArchive: create tempfile: %v", err))
return
......@@ -61,7 +61,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
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()
if err != nil {
fail500(w, fmt.Errorf("handleGetArchive: archive stdout: %v", err))
......@@ -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))
return
}
......
......@@ -5,9 +5,8 @@ import (
)
func (u *upstream) artifactsAuthorizeHandler(h httpHandleFunc) httpHandleFunc {
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) {
req := r.Request
req.Header.Set(tempPathHeader, r.TempPath)
h(w, req)
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
r.Header.Set(tempPathHeader, a.TempPath)
h(w, r)
}, "/authorize")
}
......@@ -85,11 +85,11 @@ func (u *upstream) preAuthorizeHandler(h serviceHandleFunc, suffix string) httpH
return
}
g := &gitRequest{Request: r}
a := &authorizationResponse{}
// The auth backend validated the client request and told us additional
// request metadata. We must extract this information from the auth
// 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))
return
}
......@@ -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 (
"testing"
)
func okHandler(w http.ResponseWriter, r *gitRequest) {
func okHandler(w http.ResponseWriter, _ *http.Request, _ *authorizationResponse) {
w.WriteHeader(201)
fmt.Fprint(w, "{\"status\":\"ok\"}")
}
......
......@@ -27,22 +27,22 @@ func looksLikeRepo(p string) bool {
}
func (u *upstream) repoPreAuthorizeHandler(handleFunc serviceHandleFunc) httpHandleFunc {
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) {
if r.RepoPath == "" {
return u.preAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
if a.RepoPath == "" {
fail500(w, errors.New("repoPreAuthorizeHandler: RepoPath empty"))
return
}
if !looksLikeRepo(r.RepoPath) {
if !looksLikeRepo(a.RepoPath) {
http.Error(w, "Not Found", 404)
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")
if !(rpc == "git-upload-pack" || rpc == "git-receive-pack") {
// The 'dumb' Git HTTP protocol is not supported
......@@ -51,7 +51,7 @@ func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) {
}
// 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()
if err != nil {
fail500(w, fmt.Errorf("handleGetInfoRefs: stdout: %v", err))
......@@ -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
// Get Git action from URL
......@@ -98,7 +98,7 @@ func handlePostRPC(w http.ResponseWriter, r *gitRequest) {
}
// 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()
if err != nil {
fail500(w, fmt.Errorf("handlePostRPC: stdout: %v", err))
......
......@@ -18,29 +18,29 @@ import (
)
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"))
return
}
if r.LfsOid == "" {
if a.LfsOid == "" {
fail500(w, errors.New("lfsAuthorizeHandler: LfsOid empty"))
return
}
if err := os.MkdirAll(r.StoreLFSPath, 0700); err != nil {
fail500(w, fmt.Errorf("lfsAuthorizeHandler: mkdir StoreLFSPath: %v", err))
if err := os.MkdirAll(a.StoreLFSPath, 0700); err != nil {
fail500(w, fmt.Errorf("lfsAuthorizeHandler: mkdia StoreLFSPath: %v", err))
return
}
handleFunc(w, r)
handleFunc(w, r, a)
}, "/authorize")
}
func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) {
file, err := ioutil.TempFile(r.StoreLFSPath, r.LfsOid)
func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *http.Request, a *authorizationResponse) {
file, err := ioutil.TempFile(a.StoreLFSPath, a.LfsOid)
if err != nil {
fail500(w, fmt.Errorf("handleStoreLfsObject: create tempfile: %v", err))
return
......@@ -58,14 +58,14 @@ func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) {
}
file.Close()
if written != r.LfsSize {
fail500(w, fmt.Errorf("handleStoreLfsObject: expected size %d, wrote %d", r.LfsSize, written))
if written != a.LfsSize {
fail500(w, fmt.Errorf("handleStoreLfsObject: expected size %d, wrote %d", a.LfsSize, written))
return
}
shaStr := hex.EncodeToString(hash.Sum(nil))
if shaStr != r.LfsOid {
fail500(w, fmt.Errorf("handleStoreLfsObject: expected sha256 %s, got %s", r.LfsOid, shaStr))
if shaStr != a.LfsOid {
fail500(w, fmt.Errorf("handleStoreLfsObject: expected sha256 %s, got %s", a.LfsOid, shaStr))
return
}
......@@ -75,5 +75,5 @@ func (u *upstream) handleStoreLfsObject(w http.ResponseWriter, r *gitRequest) {
r.ContentLength = 0
// And proxy the request
u.proxyRequest(w, r.Request)
u.proxyRequest(w, r)
}
......@@ -15,7 +15,7 @@ import (
"strings"
)
type serviceHandleFunc func(w http.ResponseWriter, r *gitRequest)
type serviceHandleFunc func(http.ResponseWriter, *http.Request, *authorizationResponse)
type upstream struct {
httpClient *http.Client
......@@ -52,13 +52,6 @@ type authorizationResponse struct {
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 {
gitlabURL, err := url.Parse(authBackend)
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