Commit ad3c668c authored by Kirill Smelkov's avatar Kirill Smelkov

X blob serving very draftly works

parent fa260e77
Pipeline #109 failed with stage
......@@ -9,11 +9,13 @@ import (
"net/http"
"strings"
"log"
"time"
)
func preAuthorizeHandler(handleFunc serviceHandleFunc, suffix string) serviceHandleFunc {
return func(w http.ResponseWriter, r *gitRequest) {
log.Printf("AUTH1")
Tstart := time.Now()
//log.Printf("AUTH1")
authReq, err := r.u.newUpstreamRequest(r.Request, nil, suffix)
if err != nil {
fail500(w, "newUpstreamRequest", err)
......@@ -67,7 +69,13 @@ func preAuthorizeHandler(handleFunc serviceHandleFunc, suffix string) serviceHan
}
}
Tendauth := time.Now()
handleFunc(w, r)
Tend := time.Now()
log.Printf("Tauth:\t%s", Tendauth.Sub(Tstart))
log.Printf("Tauth+handle:\t%s", Tend.Sub(Tstart))
}
}
......
......@@ -6,7 +6,9 @@ package main
import (
"io"
// "os"
"log"
"time"
"strings"
"regexp"
"net/http"
......@@ -30,8 +32,10 @@ func blobPreAuthorizeHandler(handleFunc serviceHandleFunc) serviceHandleFunc {
}
func handleGetBlobRaw(w http.ResponseWriter, r *gitRequest) {
log.Printf("BLOB1")
// extract project name
Tstart := time.Now()
// extract project & refpath
// /namespace/project/raw/branch/file -> /namespace/project, branch/file
projectRe := regexp.MustCompile(`^/[\w\.-]+/[\w\.-]+/`)
project := projectRe.FindString(r.Request.URL.Path)
refpath := r.Request.URL.Path[len(project):]
......@@ -39,21 +43,19 @@ func handleGetBlobRaw(w http.ResponseWriter, r *gitRequest) {
fail500(w, "extract project name", nil)
return
}
//assert project[-1] == "/"
project = project[:len(project)-1]
log.Printf("project: %v", project)
if refpath[:4] != "raw/" {
fail500(w, "refpath != raw/...", nil)
return
}
refpath = refpath[4:]
log.Printf("refpath: %v", refpath)
//log.Printf("BLOB1 %v %v", project, refpath)
// request to verify whether download is possible via asking as git fetch would do
// XXX privateToken not propagated ...
reqCheckDownload, err := http.NewRequest("GET", project + ".git/info/refs?service=git-upload-pack", nil)
// XXX privateToken not propagated, etc ...
reqDownloadAccess, err := http.NewRequest("GET", project + ".git/info/refs?service=git-upload-pack", nil)
if err != nil {
fail500(w, "GET git-upload-pack", err)
return
......@@ -61,27 +63,32 @@ func handleGetBlobRaw(w http.ResponseWriter, r *gitRequest) {
// swap original request to 'verify-download' one
//requestBlob := r.Request
r.Request = reqCheckDownload
r.Request = reqDownloadAccess
preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) {
handleGetBlobRaw2(w, r, refpath)
}, "") (w, r)
Tend := time.Now()
log.Printf("Tall: %s", Tend.Sub(Tstart))
}
func handleGetBlobRaw2(w http.ResponseWriter, r *gitRequest, refpath string) {
Tstart := time.Now()
// XXX we assume <ref>/<path> format and ref not containing "/"
// XXX but gitlab allows ref with / and tries to do longest-match to existing refs
// TODO use reqDownloadAccess respose body - it contain all refs
s := strings.SplitN(refpath, "/", 2)
log.Printf("RAW2 s: %v", s)
if len(s) != 2 {
fail500(w, "refpath split", nil)
return
}
ref, path := s[0], s[1]
//log.Printf("BLOB2 %v %v", ref, path)
blobCmd := gitCommand(""/*XXX GL_ID*/, "git", "--git-dir="+r.RepoPath, "cat-file", "blob", ref + ":" + path)
blobCmd := gitCommand(""/*XXX GL_ID*/, "git", "--git-dir="+r.RepoPath, "cat-file", "blob", "--", ref + ":" + path)
blobStdout, err := blobCmd.StdoutPipe()
if err != nil {
fail500(w, "handleGetBlobRaw", err)
......@@ -97,6 +104,10 @@ func handleGetBlobRaw2(w http.ResponseWriter, r *gitRequest, refpath string) {
//setRawHeaders(...)
w.WriteHeader(200) // XXX too early
//_, err = io.Copy(os.Stdout, blobStdout)
if err != nil {
panic(err)
}
if _, err := io.Copy(w, blobStdout); err != nil {
logContext("io.Copy", err)
return
......@@ -105,4 +116,7 @@ func handleGetBlobRaw2(w http.ResponseWriter, r *gitRequest, refpath string) {
logContext("wait", err)
return
}
Tend := time.Now()
log.Printf("Tblob2: %s", Tend.Sub(Tstart))
}
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