mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-03 15:27:57 +08:00
parent
c23ea34652
commit
5f692a76de
@ -443,12 +443,12 @@ TIP:
|
||||
}
|
||||
|
||||
name := req.Arguments[0]
|
||||
url := strings.TrimSuffix(req.Arguments[1], "/pins") // fix /pins/pins :-)
|
||||
url := req.Arguments[1]
|
||||
key := req.Arguments[2]
|
||||
|
||||
u, err := neturl.ParseRequestURI(url)
|
||||
if err != nil || !strings.HasPrefix(u.Scheme, "http") {
|
||||
return fmt.Errorf("service endpoint must be a valid HTTP URL")
|
||||
endpoint, err := normalizeEndpoint(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg, err := repo.Config()
|
||||
@ -465,7 +465,7 @@ TIP:
|
||||
|
||||
cfg.Pinning.RemoteServices[name] = config.RemotePinningService{
|
||||
Api: config.RemotePinningServiceApi{
|
||||
Endpoint: url,
|
||||
Endpoint: endpoint,
|
||||
Key: key,
|
||||
},
|
||||
}
|
||||
@ -708,7 +708,11 @@ func getRemotePinService(env cmds.Environment, name string) (*pinclient.Client,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pinclient.NewClient(url, key), nil
|
||||
endpoint, err := normalizeEndpoint(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pinclient.NewClient(endpoint, key), nil
|
||||
}
|
||||
|
||||
func getRemotePinServiceInfo(env cmds.Environment, name string) (url, key string, err error) {
|
||||
@ -734,3 +738,16 @@ func getRemotePinServiceInfo(env cmds.Environment, name string) (url, key string
|
||||
}
|
||||
return service.Api.Endpoint, service.Api.Key, nil
|
||||
}
|
||||
|
||||
func normalizeEndpoint(endpoint string) (string, error) {
|
||||
uri, err := neturl.ParseRequestURI(endpoint)
|
||||
if err != nil || !strings.HasPrefix(uri.Scheme, "http") {
|
||||
return "", fmt.Errorf("service endpoint must be a valid HTTP URL")
|
||||
}
|
||||
// avoid //pins (https://github.com/ipfs/go-ipfs/issues/7826)
|
||||
uri.Path = strings.TrimSuffix(uri.Path, "/")
|
||||
// avoid /pins/pins
|
||||
uri.Path = strings.TrimSuffix(uri.Path, "/pins")
|
||||
|
||||
return uri.String(), nil
|
||||
}
|
||||
|
||||
57
core/commands/pin/remotepin_test.go
Normal file
57
core/commands/pin/remotepin_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package pin
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeEndpoint(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
err string
|
||||
out string
|
||||
}{
|
||||
{
|
||||
in: "https://1.example.com",
|
||||
err: "",
|
||||
out: "https://1.example.com",
|
||||
},
|
||||
{
|
||||
in: "https://2.example.com/",
|
||||
err: "",
|
||||
out: "https://2.example.com",
|
||||
},
|
||||
{
|
||||
in: "https://3.example.com/pins/",
|
||||
err: "",
|
||||
out: "https://3.example.com",
|
||||
},
|
||||
{
|
||||
in: "https://4.example.com/pins",
|
||||
err: "",
|
||||
out: "https://4.example.com",
|
||||
},
|
||||
{
|
||||
in: "http://192.168.0.5:45000/pins",
|
||||
err: "",
|
||||
out: "http://192.168.0.5:45000",
|
||||
},
|
||||
{
|
||||
in: "foo://4.example.com/pins",
|
||||
err: "service endpoint must be a valid HTTP URL",
|
||||
out: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
out, err := normalizeEndpoint(tc.in)
|
||||
if out != tc.out {
|
||||
t.Errorf("unexpected endpoint for %q: expected %q; got %q", tc.in, tc.out, out)
|
||||
continue
|
||||
}
|
||||
if err != nil && tc.err != err.Error() {
|
||||
t.Errorf("unexpected error for %q: expected %q; got %q", tc.in, tc.err, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user