all repos

mugit @ 99ee247

🐮 git server that your cow will love

mugit/internal/handlers/git.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
support both repo and repo.git as names, 4 months ago
1
package handlers
2
3
import (
4
	"compress/gzip"
5
	"io"
6
	"log/slog"
7
	"net/http"
8
9
	securejoin "github.com/cyphar/filepath-securejoin"
10
	"olexsmir.xyz/mugit/internal/git/gitservice"
11
)
12
13
// multiplex, check if the request smells like gitprotocol-http(5), if so, it
14
// passes it to git smart http, otherwise renders templates
15
func (h *handlers) multiplex(w http.ResponseWriter, r *http.Request) {
16
	if r.URL.RawQuery == "service=git-receive-pack" {
17
		w.WriteHeader(http.StatusBadRequest)
18
		w.Write([]byte("http pushing is not supported"))
19
		return
20
	}
21
22
	path := r.PathValue("rest")
23
	if path == "info/refs" && r.Method == "GET" && r.URL.RawQuery == "service=git-upload-pack" {
24
		h.infoRefs(w, r)
25
	} else if path == "git-upload-pack" && r.Method == "POST" {
26
		h.uploadPack(w, r)
27
	} else if r.Method == "GET" {
28
		h.repoIndex(w, r)
29
	}
30
}
31
32
func (h *handlers) infoRefs(w http.ResponseWriter, r *http.Request) {
33
	name := getNormalizedName(r.PathValue("name"))
34
	_, err := h.openPublicRepo(name, "")
35
	if err != nil {
36
		h.write404(w, err)
37
		return
38
	}
39
40
	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
41
	w.WriteHeader(http.StatusOK)
42
43
	repoPath := repoNameToPath(name)
44
	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)
45
	if err != nil {
46
		w.WriteHeader(http.StatusBadRequest)
47
		slog.Error("git: info/refs", "err", err)
48
		return
49
	}
50
51
	if err := gitservice.InfoRefs(path, w); err != nil {
52
		w.WriteHeader(http.StatusInternalServerError)
53
		slog.Error("git: info/refs", "err", err)
54
		return
55
	}
56
}
57
58
func (h *handlers) uploadPack(w http.ResponseWriter, r *http.Request) {
59
	name := getNormalizedName(r.PathValue("name"))
60
	_, err := h.openPublicRepo(name, "")
61
	if err != nil {
62
		h.write404(w, err)
63
		return
64
	}
65
66
	w.Header().Set("content-type", "application/x-git-upload-pack-result")
67
	w.Header().Set("Connection", "Keep-Alive")
68
	w.Header().Set("Transfer-Encoding", "chunked")
69
	w.WriteHeader(http.StatusOK)
70
71
	reader := io.Reader(r.Body)
72
	if r.Header.Get("Content-Encoding") == "gzip" {
73
		gr, gerr := gzip.NewReader(r.Body)
74
		if gerr != nil {
75
			w.WriteHeader(http.StatusInternalServerError)
76
			slog.Error("git: gzip reader", "err", gerr)
77
			return
78
		}
79
		defer gr.Close()
80
		reader = gr
81
	}
82
83
	repoPath := repoNameToPath(name)
84
	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)
85
	if err != nil {
86
		w.WriteHeader(http.StatusBadRequest)
87
		slog.Error("git: info/refs", "err", err)
88
		return
89
	}
90
91
	if err := gitservice.UploadPack(path, true, reader, newFlushWriter(w)); err != nil {
92
		w.WriteHeader(http.StatusInternalServerError)
93
		slog.Error("git: upload-pack", "err", err)
94
		return
95
	}
96
}
97
98
type flushWriter struct {
99
	w io.Writer
100
	f http.Flusher
101
}
102
103
func newFlushWriter(w http.ResponseWriter) io.Writer {
104
	f, _ := w.(http.Flusher)
105
	return &flushWriter{w: w, f: f}
106
}
107
108
func (fw *flushWriter) Write(p []byte) (int, error) {
109
	n, err := fw.w.Write(p)
110
	if fw.f != nil {
111
		fw.f.Flush()
112
	}
113
	return n, err
114
}