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