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 |
package handlers
import (
"compress/gzip"
"fmt"
"io"
"log/slog"
"net/http"
securejoin "github.com/cyphar/filepath-securejoin"
"olexsmir.xyz/mugit/internal/git/gitx"
)
// multiplex, check if the request smells like gitprotocol-http(5), if so, it
// passes it to git smart http, otherwise renders templates
func (h *handlers) multiplex(w http.ResponseWriter, r *http.Request) {
if r.URL.RawQuery == "service=git-receive-pack" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("http pushing is not supported"))
return
}
path := r.PathValue("rest")
if path == "info/refs" && r.Method == "GET" && r.URL.RawQuery == "service=git-upload-pack" {
h.infoRefs(w, r)
} else if path == "git-upload-pack" && r.Method == "POST" {
h.uploadPack(w, r)
} else if r.Method == "GET" {
h.repoIndex(w, r)
}
}
func (h *handlers) infoRefs(w http.ResponseWriter, r *http.Request) {
name := getNormalizedName(r.PathValue("name"))
_, err := h.openPublicRepo(name, "")
if err != nil {
h.write404(w, err)
return
}
repoPath := repoNameToPath(name)
path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
slog.Error("git: info/refs", "err", err)
return
}
w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
w.WriteHeader(http.StatusOK)
if err := gitx.InfoRefs(r.Context(), path, w); err != nil {
w.WriteHeader(http.StatusInternalServerError)
slog.Error("git: info/refs", "err", err)
return
}
}
func (h *handlers) uploadPack(w http.ResponseWriter, r *http.Request) {
name := getNormalizedName(r.PathValue("name"))
_, err := h.openPublicRepo(name, "")
if err != nil {
h.write404(w, err)
return
}
reader := io.Reader(r.Body)
if r.Header.Get("Content-Encoding") == "gzip" {
gr, gerr := gzip.NewReader(r.Body)
if gerr != nil {
http.Error(w, "invalid gzip encoding", http.StatusBadRequest)
slog.Error("git: gzip reader", "err", gerr)
return
}
defer gr.Close()
reader = gr
}
repoPath := repoNameToPath(name)
path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)
if err != nil {
http.Error(w, "invalid path", http.StatusBadRequest)
slog.Error("git: upload-pack path", "err", err)
return
}
w.Header().Set("content-type", "application/x-git-upload-pack-result")
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
if err := gitx.UploadPack(r.Context(), path, true, reader, newFlushWriter(w)); err != nil {
// Don't call w.WriteHeader here - connection already started!
slog.Error("git: upload-pack", "err", err)
return
}
}
func (h *handlers) archiveHandler(w http.ResponseWriter, r *http.Request) {
name := getNormalizedName(r.PathValue("name"))
ref := r.PathValue("ref")
path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoNameToPath(name))
if err != nil {
http.Error(w, "invalid path", http.StatusBadRequest)
slog.Error("git: upload-pack path", "err", err)
return
}
_, err = h.openPublicRepo(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
}
}
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
}
|