all repos

mugit @ 88da23a7359ef5095d24302ed438c95c78ddba73

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
fix: url encode ref names, so '/' and other symbols are supported, 3 months ago
1
package handlers
2
3
import (
4
	"fmt"
5
	"io"
6
	"log/slog"
7
	"net/http"
8
9
	"olexsmir.xyz/mugit/internal/git"
10
	"olexsmir.xyz/mugit/internal/git/gitx"
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" && path == "" {
28
		h.repoIndex(w, r)
29
	} else {
30
		h.write404(w, nil)
31
	}
32
}
33
34
func (h *handlers) infoRefs(w http.ResponseWriter, r *http.Request) {
35
	path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"))
36
	if err != nil {
37
		h.write404(w, err)
38
		return
39
	}
40
41
	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
42
	w.WriteHeader(http.StatusOK)
43
	if err := gitx.InfoRefs(r.Context(), path, w); err != nil {
44
		w.WriteHeader(http.StatusInternalServerError)
45
		slog.Error("git: info/refs", "err", err)
46
		return
47
	}
48
}
49
50
func (h *handlers) uploadPack(w http.ResponseWriter, r *http.Request) {
51
	path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"))
52
	if err != nil {
53
		h.write404(w, err)
54
		return
55
	}
56
57
	w.Header().Set("Content-Type", "application/x-git-upload-pack-result")
58
	w.Header().Set("Cache-Control", "no-cache")
59
	w.WriteHeader(http.StatusOK)
60
61
	if err := gitx.UploadPack(r.Context(), path, true, r.Body, newFlushWriter(w)); err != nil {
62
		slog.Error("git: upload-pack", "err", err)
63
		return
64
	}
65
}
66
67
func (h *handlers) archiveHandler(w http.ResponseWriter, r *http.Request) {
68
	ref := h.parseRef(r.PathValue("ref"))
69
	name := r.PathValue("name")
70
	path, err := h.checkRepoPublicityAndGetPath(name)
71
	if err != nil {
72
		h.write404(w, err)
73
		return
74
	}
75
76
	filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
77
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
78
	w.Header().Set("Content-Type", "application/gzip")
79
	w.WriteHeader(http.StatusOK)
80
81
	if err := gitx.ArchiveTar(r.Context(), path, ref, w); err != nil {
82
		slog.Error("git: archive", "ref", ref, "err", err)
83
		return
84
	}
85
}
86
87
func (h *handlers) checkRepoPublicityAndGetPath(name string) (string, error) {
88
	name = git.ResolveName(name)
89
	path, err := git.ResolvePath(h.c.Repo.Dir, name)
90
	if err != nil {
91
		return "", err
92
	}
93
94
	if _, err := git.OpenPublic(path, ""); err != nil {
95
		return "", err
96
	}
97
98
	return path, err
99
}
100
101
func (h *handlers) openPublicRepo(name, ref string) (*git.Repo, error) {
102
	name = git.ResolveName(name)
103
	path, err := git.ResolvePath(h.c.Repo.Dir, name)
104
	if err != nil {
105
		return nil, err
106
	}
107
	return git.OpenPublic(path, ref)
108
}
109
110
type flushWriter struct {
111
	w io.Writer
112
	f http.Flusher
113
}
114
115
func newFlushWriter(w http.ResponseWriter) io.Writer {
116
	f, _ := w.(http.Flusher)
117
	return &flushWriter{w: w, f: f}
118
}
119
120
func (fw *flushWriter) Write(p []byte) (int, error) {
121
	n, err := fw.w.Write(p)
122
	if fw.f != nil {
123
		fw.f.Flush()
124
	}
125
	return n, err
126
}