|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "sync" |
| 10 | +) |
| 11 | + |
| 12 | +// RegisterManagedHTTPTransport registers a Go-native implementation of an |
| 13 | +// HTTP/S transport that doesn't rely on any system libraries (e.g. |
| 14 | +// libopenssl/libmbedtls). |
| 15 | +// |
| 16 | +// If Shutdown or ReInit are called, make sure that the smart transports are |
| 17 | +// freed before it. |
| 18 | +func RegisterManagedHTTPTransport(protocol string) (*RegisteredSmartTransport, error) { |
| 19 | + return NewRegisteredSmartTransport(protocol, true, httpSmartSubtransportFactory) |
| 20 | +} |
| 21 | + |
| 22 | +func registerManagedHTTP() error { |
| 23 | + globalRegisteredSmartTransports.Lock() |
| 24 | + defer globalRegisteredSmartTransports.Unlock() |
| 25 | + |
| 26 | + for _, protocol := range []string{"http", "https"} { |
| 27 | + if _, ok := globalRegisteredSmartTransports.transports[protocol]; ok { |
| 28 | + continue |
| 29 | + } |
| 30 | + managed, err := newRegisteredSmartTransport(protocol, true, httpSmartSubtransportFactory, true) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("failed to register transport for %q: %v", protocol, err) |
| 33 | + } |
| 34 | + globalRegisteredSmartTransports.transports[protocol] = managed |
| 35 | + } |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func httpSmartSubtransportFactory(remote *Remote, transport *Transport) (SmartSubtransport, error) { |
| 40 | + var proxyFn func(*http.Request) (*url.URL, error) |
| 41 | + proxyOpts, err := transport.SmartProxyOptions() |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + switch proxyOpts.Type { |
| 46 | + case ProxyTypeNone: |
| 47 | + proxyFn = nil |
| 48 | + case ProxyTypeAuto: |
| 49 | + proxyFn = http.ProxyFromEnvironment |
| 50 | + case ProxyTypeSpecified: |
| 51 | + parsedUrl, err := url.Parse(proxyOpts.Url) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + proxyFn = http.ProxyURL(parsedUrl) |
| 57 | + } |
| 58 | + |
| 59 | + return &httpSmartSubtransport{ |
| 60 | + transport: transport, |
| 61 | + client: &http.Client{ |
| 62 | + Transport: &http.Transport{ |
| 63 | + Proxy: proxyFn, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, nil |
| 67 | +} |
| 68 | + |
| 69 | +type httpSmartSubtransport struct { |
| 70 | + transport *Transport |
| 71 | + client *http.Client |
| 72 | +} |
| 73 | + |
| 74 | +func (t *httpSmartSubtransport) Action(url string, action SmartServiceAction) (SmartSubtransportStream, error) { |
| 75 | + var req *http.Request |
| 76 | + var err error |
| 77 | + switch action { |
| 78 | + case SmartServiceActionUploadpackLs: |
| 79 | + req, err = http.NewRequest("GET", url+"/info/refs?service=git-upload-pack", nil) |
| 80 | + |
| 81 | + case SmartServiceActionUploadpack: |
| 82 | + req, err = http.NewRequest("POST", url+"/git-upload-pack", nil) |
| 83 | + if err != nil { |
| 84 | + break |
| 85 | + } |
| 86 | + req.Header.Set("Content-Type", "application/x-git-upload-pack-request") |
| 87 | + |
| 88 | + case SmartServiceActionReceivepackLs: |
| 89 | + req, err = http.NewRequest("GET", url+"/info/refs?service=git-receive-pack", nil) |
| 90 | + |
| 91 | + case SmartServiceActionReceivepack: |
| 92 | + req, err = http.NewRequest("POST", url+"/info/refs?service=git-upload-pack", nil) |
| 93 | + if err != nil { |
| 94 | + break |
| 95 | + } |
| 96 | + req.Header.Set("Content-Type", "application/x-git-receive-pack-request") |
| 97 | + |
| 98 | + default: |
| 99 | + err = errors.New("unknown action") |
| 100 | + } |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + req.Header.Set("User-Agent", "git/2.0 (git2go)") |
| 107 | + |
| 108 | + stream := newManagedHttpStream(t, req) |
| 109 | + if req.Method == "POST" { |
| 110 | + stream.recvReply.Add(1) |
| 111 | + stream.sendRequestBackground() |
| 112 | + } |
| 113 | + |
| 114 | + return stream, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (t *httpSmartSubtransport) Close() error { |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (t *httpSmartSubtransport) Free() { |
| 122 | + t.client = nil |
| 123 | +} |
| 124 | + |
| 125 | +type httpSmartSubtransportStream struct { |
| 126 | + owner *httpSmartSubtransport |
| 127 | + req *http.Request |
| 128 | + resp *http.Response |
| 129 | + reader *io.PipeReader |
| 130 | + writer *io.PipeWriter |
| 131 | + sentRequest bool |
| 132 | + recvReply sync.WaitGroup |
| 133 | + httpError error |
| 134 | +} |
| 135 | + |
| 136 | +func newManagedHttpStream(owner *httpSmartSubtransport, req *http.Request) *httpSmartSubtransportStream { |
| 137 | + r, w := io.Pipe() |
| 138 | + return &httpSmartSubtransportStream{ |
| 139 | + owner: owner, |
| 140 | + req: req, |
| 141 | + reader: r, |
| 142 | + writer: w, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) { |
| 147 | + if !self.sentRequest { |
| 148 | + self.recvReply.Add(1) |
| 149 | + if err := self.sendRequest(); err != nil { |
| 150 | + return 0, err |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if err := self.writer.Close(); err != nil { |
| 155 | + return 0, err |
| 156 | + } |
| 157 | + |
| 158 | + self.recvReply.Wait() |
| 159 | + |
| 160 | + if self.httpError != nil { |
| 161 | + return 0, self.httpError |
| 162 | + } |
| 163 | + |
| 164 | + return self.resp.Body.Read(buf) |
| 165 | +} |
| 166 | + |
| 167 | +func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) { |
| 168 | + if self.httpError != nil { |
| 169 | + return 0, self.httpError |
| 170 | + } |
| 171 | + return self.writer.Write(buf) |
| 172 | +} |
| 173 | + |
| 174 | +func (self *httpSmartSubtransportStream) Free() { |
| 175 | + if self.resp != nil { |
| 176 | + self.resp.Body.Close() |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func (self *httpSmartSubtransportStream) sendRequestBackground() { |
| 181 | + go func() { |
| 182 | + self.httpError = self.sendRequest() |
| 183 | + }() |
| 184 | + self.sentRequest = true |
| 185 | +} |
| 186 | + |
| 187 | +func (self *httpSmartSubtransportStream) sendRequest() error { |
| 188 | + defer self.recvReply.Done() |
| 189 | + self.resp = nil |
| 190 | + |
| 191 | + var resp *http.Response |
| 192 | + var err error |
| 193 | + var userName string |
| 194 | + var password string |
| 195 | + for { |
| 196 | + req := &http.Request{ |
| 197 | + Method: self.req.Method, |
| 198 | + URL: self.req.URL, |
| 199 | + Header: self.req.Header, |
| 200 | + } |
| 201 | + if req.Method == "POST" { |
| 202 | + req.Body = self.reader |
| 203 | + req.ContentLength = -1 |
| 204 | + } |
| 205 | + |
| 206 | + req.SetBasicAuth(userName, password) |
| 207 | + resp, err = http.DefaultClient.Do(req) |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + |
| 212 | + if resp.StatusCode == http.StatusOK { |
| 213 | + break |
| 214 | + } |
| 215 | + |
| 216 | + if resp.StatusCode == http.StatusUnauthorized { |
| 217 | + resp.Body.Close() |
| 218 | + |
| 219 | + cred, err := self.owner.transport.SmartCredentials("", CredTypeUserpassPlaintext) |
| 220 | + if err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + |
| 224 | + userName, password, err = cred.GetUserpassPlaintext() |
| 225 | + if err != nil { |
| 226 | + return err |
| 227 | + } |
| 228 | + |
| 229 | + continue |
| 230 | + } |
| 231 | + |
| 232 | + // Any other error we treat as a hard error and punt back to the caller |
| 233 | + resp.Body.Close() |
| 234 | + return fmt.Errorf("Unhandled HTTP error %s", resp.Status) |
| 235 | + } |
| 236 | + |
| 237 | + self.sentRequest = true |
| 238 | + self.resp = resp |
| 239 | + return nil |
| 240 | +} |
0 commit comments