http: set the proxy from the user-given options

This commit is contained in:
Carlos Martín Nieto 2017-05-21 21:47:33 +02:00 committed by Carlos Martín Nieto
parent 50f588fcc4
commit 22da146353
2 changed files with 33 additions and 1 deletions

27
http.go
View File

@ -28,6 +28,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"runtime"
"unsafe"
@ -142,8 +143,32 @@ func (self *ManagedTransport) ensureClient() error {
return nil
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var cpopts C.git_proxy_options
if ret := C.git_transport_smart_proxy_options(&cpopts, self.owner); ret < 0 {
return MakeGitError(ret)
}
var proxyFn func(*http.Request) (*url.URL, error)
proxyOpts := proxyOptionsFromC(&cpopts)
switch proxyOpts.Type {
case ProxyTypeNone:
proxyFn = nil
case ProxyTypeAuto:
proxyFn = http.ProxyFromEnvironment
case ProxyTypeSpecified:
parsedUrl, err := url.Parse(proxyOpts.Url)
if err != nil {
return err
}
proxyFn = http.ProxyURL(parsedUrl)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Proxy: proxyFn,
}
self.client = &http.Client{Transport: transport}

View File

@ -141,6 +141,13 @@ type ProxyOptions struct {
Url string
}
func proxyOptionsFromC(copts *C.git_proxy_options) ProxyOptions {
return ProxyOptions{
Type: ProxyType(copts._type),
Url: C.GoString(copts.url),
}
}
type Remote struct {
ptr *C.git_remote
callbacks RemoteCallbacks