all repos

mugit @ e1d1a9eda5d5e5aacfe4a2f899f9f65ba5b772d5

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
git: archive, 3 months ago
1
package handlers
2
3
import (
4
	"compress/gzip"
5
	"fmt"
6
	"io"
7
	"log/slog"
8
	"net/http"
9
10
	securejoin "github.com/cyphar/filepath-securejoin"
11
	"olexsmir.xyz/mugit/internal/git/gitx"
12
)
13
14
// multiplex, check if the request smells like gitprotocol-http(5), if so, it
15
// passes it to git smart http, otherwise renders templates
16
func (h *handlers) multiplex(w http.ResponseWriter, r *http.Request) {
17
	if r.URL.RawQuery == "service=git-receive-pack" {
18
		w.WriteHeader(http.StatusBadRequest)
19
		w.Write([]byte("http pushing is not supported"))
20
		return
21
	}
22
23
	path := r.PathValue("rest")
24
	if path == "info/refs" && r.Method == "GET" && r.URL.RawQuery == "service=git-upload-pack" {
25
		h.infoRefs(w, r)
26
	} else if path == "git-upload-pack" && r.Method == "POST" {
27
		h.uploadPack(w, r)
28
	} else if r.Method == "GET" {
29
		h.repoIndex(w, r)
30
	}
31
}
32
33
func (h *handlers) infoRefs(w http.ResponseWriter, r *http.Request) {
34
	name := getNormalizedName(r.PathValue("name"))
35
	_, err := h.openPublicRepo(name, "")
36
	if err != nil {
37
		h.write404(w, err)
38
		return
39
	}
40
41
	repoPath := repoNameToPath(name)
42
	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)
43
	if err != nil {
44
		w.WriteHeader(http.StatusBadRequest)
45
		slog.Error("git: info/refs", "err", err)
46
		return
47
	}
48
49
	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
50
	w.WriteHeader(http.StatusOK)
51
	if err := gitx.InfoRefs(r.Context(), 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
	reader := io.Reader(r.Body)
67
	if r.Header.Get("Content-Encoding") == "gzip" {
68
		gr, gerr := gzip.NewReader(r.Body)
69
		if gerr != nil {
70
			http.Error(w, "invalid gzip encoding", http.StatusBadRequest)
71
			slog.Error("git: gzip reader", "err", gerr)
72
			return
73
		}
74
		defer gr.Close()
75
		reader = gr
76
	}
77
78
	repoPath := repoNameToPath(name)
79
	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)
80
	if err != nil {
81
		http.Error(w, "invalid path", http.StatusBadRequest)
82
		slog.Error("git: upload-pack path", "err", err)
83
		return
84
	}
85
86
	w.Header().Set("content-type", "application/x-git-upload-pack-result")
87
	w.Header().Set("Connection", "Keep-Alive")
88
	w.Header().Set("Transfer-Encoding", "chunked")
89
	w.WriteHeader(http.StatusOK)
90
91
	if err := gitx.UploadPack(r.Context(), path, true, reader, newFlushWriter(w)); err != nil {
92
		// Don't call w.WriteHeader here - connection already started!
93
		slog.Error("git: upload-pack", "err", err)
94
		return
95
	}
96
}
97
98
func (h *handlers) archiveHandler(w http.ResponseWriter, r *http.Request) {
99
	name := getNormalizedName(r.PathValue("name"))
100
	ref := r.PathValue("ref")
101
102
	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoNameToPath(name))
103
	if err != nil {
104
		http.Error(w, "invalid path", http.StatusBadRequest)
105
		slog.Error("git: upload-pack path", "err", err)
106
		return
107
	}
108
109
	_, err = h.openPublicRepo(name, ref)
110
	if err != nil {
111
		h.write404(w, err)
112
		return
113
	}
114
115
	filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
116
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
117
	w.Header().Set("Content-Type", "application/gzip")
118
	w.WriteHeader(http.StatusOK)
119
120
	if err := gitx.ArchiveTar(r.Context(), path, ref, w); err != nil {
121
		slog.Error("git: archive", "ref", ref, "err", err)
122
		return
123
	}
124
}
125
126
type flushWriter struct {
127
	w io.Writer
128
	f http.Flusher
129
}
130
131
func newFlushWriter(w http.ResponseWriter) io.Writer {
132
	f, _ := w.(http.Flusher)
133
	return &flushWriter{w: w, f: f}
134
}
135
136
func (fw *flushWriter) Write(p []byte) (int, error) {
137
	n, err := fw.w.Write(p)
138
	if fw.f != nil {
139
		fw.f.Flush()
140
	}
141
	return n, err
142
}