forked from niemeyer/gopkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_proxy.go
128 lines (109 loc) · 3.09 KB
/
git_proxy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm).
// gopkg source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io"
"net/http"
"strings"
)
//
// GitHub Proxying for /git-upload-pack
// Note: this is similar to reverse proxy not exactly :)
//
// Hop-by-hop headers. These are removed when sent to the backend.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
var hopHeaders = []string{
"Connection",
"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te", // canonicalized version of "TE"
"Trailer", // not Trailers per URL above; http://www.rfc-editor.org/errata_search.php?eid=4522
"Transfer-Encoding",
"Upgrade",
}
// This method includes part of code from
// https://golang.org/src/net/http/httputil/reverseproxy.go
func proxyGitUploadPack(w http.ResponseWriter, r *http.Request, target string) {
outreq, _ := http.NewRequest("POST", target, r.Body)
outreq.Header = cloneHeader(r.Header)
outreq.Close = false
cleanHopHeaders(outreq.Header)
res, err := httpClient.Do(outreq)
if err != nil {
fmt.Printf("github proxy error: %v\n", err)
w.WriteHeader(http.StatusBadGateway)
return
}
cleanHopHeaders(res.Header)
copyHeader(w.Header(), res.Header)
// The "Trailer" header isn't included in the Transport's response,
// at least for *http.Transport. Build it up from Trailer.
announcedTrailers := len(res.Trailer)
if announcedTrailers > 0 {
trailerKeys := make([]string, 0, len(res.Trailer))
for k := range res.Trailer {
trailerKeys = append(trailerKeys, k)
}
w.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
}
w.WriteHeader(res.StatusCode)
if len(res.Trailer) > 0 {
// Force chunking if we saw a response trailer.
// This prevents net/http from calculating the length for short
// bodies and adding a Content-Length.
if fl, ok := w.(http.Flusher); ok {
fl.Flush()
}
}
_, _ = io.Copy(w, res.Body)
_ = res.Body.Close()
if len(res.Trailer) == announcedTrailers {
copyHeader(w.Header(), res.Trailer)
return
}
for k, vv := range res.Trailer {
k = http.TrailerPrefix + k
for _, v := range vv {
w.Header().Add(k, v)
}
}
}
func cloneHeader(h http.Header) http.Header {
h2 := make(http.Header, len(h))
for k, vv := range h {
vv2 := make([]string, len(vv))
copy(vv2, vv)
h2[k] = vv2
}
return h2
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func cleanHopHeaders(h http.Header) {
// Remove hop-by-hop headers listed in the "Connection" header.
// See RFC 2616, section 14.10.
if c := h.Get("Connection"); c != "" {
for _, f := range strings.Split(c, ",") {
if f = strings.TrimSpace(f); f != "" {
h.Del(f)
}
}
}
// Remove hop-by-hop headers to the backend. Especially
// important is "Connection" because we want a persistent
// connection, regardless of what the client sent to us.
for _, hh := range hopHeaders {
if h.Get(hh) != "" {
h.Del(hh)
}
}
}