Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
5f692a76
Commit
5f692a76
authored
Dec 14, 2020
by
Marcin Rataj
Committed by
Adin Schmahmann
Jan 27, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: normalize remote service endpoint
Closes #7826
parent
c23ea346
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
6 deletions
+80
-6
core/commands/pin/remotepin.go
core/commands/pin/remotepin.go
+23
-6
core/commands/pin/remotepin_test.go
core/commands/pin/remotepin_test.go
+57
-0
No files found.
core/commands/pin/remotepin.go
View file @
5f692a76
...
...
@@ -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
:=
n
eturl
.
ParseRequestURI
(
url
)
if
err
!=
nil
||
!
strings
.
HasPrefix
(
u
.
Scheme
,
"http"
)
{
return
fmt
.
Errorf
(
"service endpoint must be a valid HTTP URL"
)
endpoint
,
err
:=
n
ormalizeEndpoint
(
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
}
core/commands/pin/remotepin_test.go
0 → 100644
View file @
5f692a76
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
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment