all repos

mugit @ 96b3828

🐮 git server that your cow will love

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

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