|
9 | 9 | "fmt"
|
10 | 10 | "io"
|
11 | 11 | "io/ioutil"
|
| 12 | + "net" |
12 | 13 | gohttp "net/http"
|
13 | 14 | "os"
|
14 | 15 | "path"
|
@@ -72,20 +73,51 @@ func NewShell(url string) *Shell {
|
72 | 73 | return NewShellWithClient(url, c)
|
73 | 74 | }
|
74 | 75 |
|
75 |
| -func NewShellWithClient(url string, c *gohttp.Client) *Shell { |
76 |
| - if a, err := ma.NewMultiaddr(url); err == nil { |
77 |
| - _, host, err := manet.DialArgs(a) |
78 |
| - if err == nil { |
79 |
| - url = host |
80 |
| - } |
81 |
| - } |
| 76 | +func NewShellWithClient(url string, client *gohttp.Client) *Shell { |
82 | 77 | var sh Shell
|
| 78 | + |
83 | 79 | sh.url = url
|
84 |
| - sh.httpcli = *c |
| 80 | + sh.httpcli = *client |
85 | 81 | // We don't support redirects.
|
86 | 82 | sh.httpcli.CheckRedirect = func(_ *gohttp.Request, _ []*gohttp.Request) error {
|
87 | 83 | return fmt.Errorf("unexpected redirect")
|
88 | 84 | }
|
| 85 | + |
| 86 | + maddr, err := ma.NewMultiaddr(url) |
| 87 | + if err != nil { |
| 88 | + return &sh |
| 89 | + } |
| 90 | + |
| 91 | + network, host, err := manet.DialArgs(maddr) |
| 92 | + if err != nil { |
| 93 | + return &sh |
| 94 | + } |
| 95 | + |
| 96 | + if network == "unix" { |
| 97 | + sh.url = network |
| 98 | + |
| 99 | + var tptCopy *gohttp.Transport |
| 100 | + if tpt, ok := sh.httpcli.Transport.(*gohttp.Transport); ok && tpt.DialContext == nil { |
| 101 | + tptCopy = tpt.Clone() |
| 102 | + } else if sh.httpcli.Transport == nil { |
| 103 | + tptCopy = &gohttp.Transport{ |
| 104 | + Proxy: gohttp.ProxyFromEnvironment, |
| 105 | + DisableKeepAlives: true, |
| 106 | + } |
| 107 | + } else { |
| 108 | + // custom Transport or custom Dialer, we are done here |
| 109 | + return &sh |
| 110 | + } |
| 111 | + |
| 112 | + tptCopy.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) { |
| 113 | + return net.Dial("unix", host) |
| 114 | + } |
| 115 | + |
| 116 | + sh.httpcli.Transport = tptCopy |
| 117 | + } else { |
| 118 | + sh.url = host |
| 119 | + } |
| 120 | + |
89 | 121 | return &sh
|
90 | 122 | }
|
91 | 123 |
|
|
0 commit comments