routes/git.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package routes
import (
"bytes"
"compress/gzip"
"io"
"log"
"net/http"
"alin.ovh/elgit/git/service"
"github.com/savsgio/atreugo/v11"
"github.com/valyala/fasthttp"
)
func (d *deps) InfoRefs(rc *atreugo.RequestCtx) error {
repoName, _ := rc.UserValue("repoName").(string)
repo := d.repos.BySlug[repoName]
svc := rc.QueryArgs().Peek("service")
if string(svc) == "git-receive-pack" {
return rc.TextResponse("no pushing allowed!", fasthttp.StatusBadRequest)
}
rc.SetStatusCode(http.StatusOK)
rc.Response.Header.Set("content-type", "application/x-git-upload-pack-advertisement")
rc.Response.Header.Set("cache-control", "no-cache")
cmd := service.Command{
Dir: repo.Path,
Stdout: rc,
}
return cmd.InfoRefs(rc)
}
func (d *deps) UploadPack(rc *atreugo.RequestCtx) error {
repoName, _ := rc.UserValue("repoName").(string)
repo := d.repos.BySlug[repoName]
rc.SetStatusCode(http.StatusOK)
rc.SetContentType("application/x-git-upload-pack-result")
rc.Response.Header.Set("Connection", "Keep-Alive")
rc.Response.Header.Set("Transfer-Encoding", "chunked")
rc.Response.Header.Set("Cache-Control", "no-cache")
var reader io.Reader
if rc.Request.IsBodyStream() {
reader = rc.Request.BodyStream()
defer func() {
err := rc.Request.CloseBodyStream()
if err != nil {
log.Printf("git: failed to close gzip reader: %s", err)
}
}()
} else {
reader = bytes.NewReader(rc.Request.Body())
}
if bytes.Contains(rc.Request.Header.ContentEncoding(), []byte("gzip")) {
var err error
reader, err = gzip.NewReader(reader)
if err != nil {
return rc.ErrorResponse(err, 500)
}
}
cmd := service.Command{
Dir: repo.Path,
Stdout: rc,
Stdin: reader,
}
return cmd.UploadPack(rc)
}
|