-
Notifications
You must be signed in to change notification settings - Fork 9
/
git.go
83 lines (68 loc) · 2.26 KB
/
git.go
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
package txtdirect
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/caddyserver/caddy/caddyhttp/proxy"
"github.com/prometheus/common/log"
)
// Git keeps data for "git" type requests
type Git struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}
// NewGit returns a fresh instance of Git struct
func NewGit(w http.ResponseWriter, r *http.Request, c Config, rec record) *Git {
return &Git{
rw: w,
req: r,
c: c,
rec: rec,
}
}
// Proxy handles the requests for "git" type
func (g *Git) Proxy() error {
to, _, err := getBaseTarget(g.rec, g.req)
if err != nil {
return err
}
if !strings.HasPrefix(g.req.URL.Path, "/info") {
g.req.URL.Path = "/" + strings.Join(strings.Split(g.req.URL.Path, "/")[2:], "/")
}
u, err := url.Parse(to)
if err != nil {
return err
}
reverseProxy := proxy.NewSingleHostReverseProxy(u, "", proxyKeepalive, proxyTimeout, fallbackDelay)
tmpResponse := ProxyResponse{headers: make(http.Header)}
reverseProxy.ServeHTTP(&tmpResponse, g.req, nil)
// Decompress the body based on "Content-Encoding" header and write to a writer buffer
if err := tmpResponse.WriteBody(); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't write the response body: %s", err.Error())
}
// Replace the URL hosts with the request's host
if err := tmpResponse.ReplaceBody(u.Scheme, u.Host, g.req.Host); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't replace urls inside the response body: %s", err.Error())
}
copyHeader(g.rw.Header(), tmpResponse.Header())
// Write the status from the temporary ResponseWriter to the main ResponseWriter
g.rw.WriteHeader(tmpResponse.status)
// Write the final response from the temporary ResponseWriter to the main ResponseWriter
if _, err := g.rw.Write(tmpResponse.Body()); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't write the temporary response to main response body: %s", err.Error())
}
return nil
}
// ValidGitQuery checks the User-Agent header to make sure the requests are
// coming from a Git client.
func (g *Git) ValidGitQuery() bool {
if !strings.HasPrefix(g.req.Header.Get("User-Agent"), "git") {
fallback(g.rw, g.req, "website", g.rec.Code, g.c)
log.Errorf("[txtdirect]: The incoming request is not from a Git client.")
return false
}
return true
}