fix: Unix domain socket maddrs used with `NewApi`
Dominic Della Valle committed
Jul 14, 2023 at 14:00 UTC
23ca62ad2c231fec90ef4ebee67fb562928f6e38
1 file changed
+23
-4
client/rpc/api.go
+23
-4
@@ -5,6 +5,7 @@ import (
5
"encoding/json"
6
"errors"
7
"fmt"
8
+ "net"
9
"net/http"
10
"os"
11
"path/filepath"
@@ -98,11 +99,29 @@ func ApiAddr(ipfspath string) (ma.Multiaddr, error) {
99
100
// NewApi constructs HttpApi with specified endpoint.
101
func NewApi(a ma.Multiaddr) (*HttpApi, error) {
102
+ transport := &http.Transport{
103
+ Proxy: http.ProxyFromEnvironment,
104
+ DisableKeepAlives: true,
105
+ }
106
+
107
+ network, address, err := manet.DialArgs(a)
108
+ if err != nil {
109
+ return nil, err
110
+ }
111
+ if network == "unix" {
112
+ transport.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
113
+ return net.Dial("unix", address)
114
+ }
115
+ c := &http.Client{
116
+ Transport: transport,
117
+ }
118
+ // This will create an API client which
119
+ // makes requests to `http://unix`.
120
+ return NewURLApiWithClient(network, c)
121
+ }
122
+
123
c := &http.Client{
102
- Transport: &http.Transport{
103
- Proxy: http.ProxyFromEnvironment,
104
- DisableKeepAlives: true,
105
- },
124
+ Transport: transport,
125
}
126
127
return NewApiWithClient(a, c)