mugit/internal/handlers/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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
package handlers
import (
"compress/gzip"
"fmt"
"io"
"log/slog"
"net/http"
"olexsmir.xyz/mugit/internal/git"
"olexsmir.xyz/mugit/internal/git/gitx"
)
func (h *handlers) infoRefsHandler(w http.ResponseWriter, r *http.Request) {
path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"), "")
if err != nil {
h.gitError(w, http.StatusNotFound, "repository not found")
return
}
service := r.URL.Query().Get("service")
gitProtocol := r.Header.Get("Git-Protocol")
switch service {
case "git-upload-pack":
w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
w.WriteHeader(http.StatusOK)
if err := gitx.InfoRefs(r.Context(), path, gitProtocol, w); err != nil {
h.gitError(w, http.StatusInternalServerError, err.Error())
slog.Error("git: info/refs", "err", err)
return
}
case "git-receive-pack":
h.receivePackHandler(w, r)
default:
h.gitError(w, http.StatusBadRequest, "service unsupported")
}
}
const uploadPackExpectedContentType = "application/x-git-upload-pack-request"
func (h *handlers) uploadPackHandler(w http.ResponseWriter, r *http.Request) {
path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"), "")
if err != nil {
h.gitError(w, http.StatusNotFound, "repository not found")
return
}
contentType := r.Header.Get("Content-Type")
if contentType != uploadPackExpectedContentType {
h.gitError(w, http.StatusUnsupportedMediaType, "provided content type is not supported")
return
}
bodyReader := r.Body
if r.Header.Get("Content-Encoding") == "gzip" {
gzipReader, err := gzip.NewReader(r.Body)
if err != nil {
h.gitError(w, http.StatusInternalServerError, err.Error())
slog.Error("git: failed to create gzip reader", "err", err)
return
}
defer gzipReader.Close()
bodyReader = gzipReader
}
w.Header().Set("Content-Type", "application/x-git-upload-pack-result")
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
w.WriteHeader(http.StatusOK)
if err := gitx.UploadPack(r.Context(), path, true, bodyReader, newFlushWriter(w)); err != nil {
h.gitError(w, http.StatusInternalServerError, err.Error())
slog.Error("git: upload-pack", "err", err)
return
}
}
func (h *handlers) receivePackHandler(w http.ResponseWriter, _ *http.Request) {
h.gitError(w, http.StatusForbidden, "pushes are only supported over ssh")
}
func (h *handlers) archiveHandler(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
ref := h.parseRef(r.PathValue("ref"))
path, err := h.checkRepoPublicityAndGetPath(name, ref)
if err != nil {
h.write404(w, err)
return
}
filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
w.Header().Set("Content-Type", "application/gzip")
w.WriteHeader(http.StatusOK)
if err := gitx.ArchiveTar(r.Context(), path, ref, w); err != nil {
slog.Error("git: archive", "ref", ref, "err", err)
return
}
}
func (h *handlers) gitError(w http.ResponseWriter, code int, msg string) {
w.Header().Set("content-type", "text/plain; charset=UTF-8")
w.WriteHeader(code)
fmt.Fprintf(w, "%s\n", msg)
}
func (h *handlers) checkRepoPublicityAndGetPath(name string, ref string) (string, error) {
name = git.ResolveName(name)
path, err := git.ResolvePath(h.c.Repo.Dir, name)
if err != nil {
return "", err
}
if _, oerr := git.OpenPublic(path, ref); oerr != nil {
return "", oerr
}
return path, err
}
func (h *handlers) openPublicRepo(name, ref string) (*git.Repo, error) {
name = git.ResolveName(name)
path, err := git.ResolvePath(h.c.Repo.Dir, name)
if err != nil {
return nil, err
}
return git.OpenPublic(path, ref)
}
type flushWriter struct {
w io.Writer
f http.Flusher
}
func newFlushWriter(w http.ResponseWriter) io.Writer {
f, _ := w.(http.Flusher)
return &flushWriter{w: w, f: f}
}
func (fw *flushWriter) Write(p []byte) (int, error) {
n, err := fw.w.Write(p)
if fw.f != nil {
fw.f.Flush()
}
return n, err
}
|