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
6851ede8
Commit
6851ede8
authored
Dec 15, 2020
by
Marcin Rataj
Committed by
Adin Schmahmann
Jan 27, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: normalization with path.Clean
parent
5f692a76
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
11 deletions
+32
-11
core/commands/pin/remotepin.go
core/commands/pin/remotepin.go
+14
-3
core/commands/pin/remotepin_test.go
core/commands/pin/remotepin_test.go
+18
-8
No files found.
core/commands/pin/remotepin.go
View file @
6851ede8
...
...
@@ -10,6 +10,7 @@ import (
"time"
neturl
"net/url"
gopath
"path"
"golang.org/x/sync/errgroup"
...
...
@@ -744,10 +745,20 @@ func normalizeEndpoint(endpoint string) (string, error) {
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)
// cleanup trailing and duplicate slashes (https://github.com/ipfs/go-ipfs/issues/7826)
uri
.
Path
=
gopath
.
Clean
(
uri
.
Path
)
uri
.
Path
=
strings
.
TrimSuffix
(
uri
.
Path
,
"."
)
uri
.
Path
=
strings
.
TrimSuffix
(
uri
.
Path
,
"/"
)
// avoid /pins/pins
uri
.
Path
=
strings
.
TrimSuffix
(
uri
.
Path
,
"/pins"
)
// remove any query params
if
uri
.
RawQuery
!=
""
||
uri
.
RawFragment
!=
""
{
return
""
,
fmt
.
Errorf
(
"service endpoint should be provided without any query parameters"
)
}
if
strings
.
HasSuffix
(
uri
.
Path
,
"/pins"
)
{
return
""
,
fmt
.
Errorf
(
"service endpoint should be provided without the /pins suffix"
)
}
return
uri
.
String
(),
nil
}
core/commands/pin/remotepin_test.go
View file @
6851ede8
...
...
@@ -22,16 +22,26 @@ func TestNormalizeEndpoint(t *testing.T) {
},
{
in
:
"https://3.example.com/pins/"
,
err
:
""
,
out
:
"
https://3.example.com
"
,
err
:
"
service endpoint should be provided without the /pins suffix
"
,
out
:
""
,
},
{
in
:
"https://4.example.com/pins"
,
err
:
"service endpoint should be provided without the /pins suffix"
,
out
:
""
,
},
{
in
:
"https://5.example.com/./some//nonsense/../path/../path/"
,
err
:
""
,
out
:
"https://4.example.com"
,
out
:
"https://5.example.com/some/path"
,
},
{
in
:
"https://6.example.com/endpoint/?query=val"
,
err
:
"service endpoint should be provided without any query parameters"
,
out
:
""
,
},
{
in
:
"http://192.168.0.5:45000/
pins
"
,
in
:
"http://192.168.0.5:45000/"
,
err
:
""
,
out
:
"http://192.168.0.5:45000"
,
},
...
...
@@ -44,14 +54,14 @@ func TestNormalizeEndpoint(t *testing.T) {
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
}
if
out
!=
tc
.
out
{
t
.
Errorf
(
"unexpected endpoint for %q: expected %q; got %q"
,
tc
.
in
,
tc
.
out
,
out
)
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