good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
f4841ff4
Commit
f4841ff4
authored
Jul 25, 2017
by
Lewis Marshall
Committed by
Felix Lange
Jul 25, 2017
Browse files
Options
Downloads
Patches
Plain Diff
swarm/api/http: redirect root manifest requests to include trailing slash (#14806)
parent
3a678a15
Loading
Loading
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
swarm/api/http/server.go
+6
-0
6 additions, 0 deletions
swarm/api/http/server.go
swarm/api/http/server_test.go
+62
-0
62 additions, 0 deletions
swarm/api/http/server_test.go
with
68 additions
and
0 deletions
swarm/api/http/server.go
+
6
−
0
View file @
f4841ff4
...
...
@@ -522,6 +522,12 @@ func (s *Server) HandleGetList(w http.ResponseWriter, r *Request) {
// HandleGetFile handles a GET request to bzz://<manifest>/<path> and responds
// with the content of the file at <path> from the given <manifest>
func
(
s
*
Server
)
HandleGetFile
(
w
http
.
ResponseWriter
,
r
*
Request
)
{
// ensure the root path has a trailing slash so that relative URLs work
if
r
.
uri
.
Path
==
""
&&
!
strings
.
HasSuffix
(
r
.
URL
.
Path
,
"/"
)
{
http
.
Redirect
(
w
,
&
r
.
Request
,
r
.
URL
.
Path
+
"/"
,
http
.
StatusMovedPermanently
)
return
}
key
,
err
:=
s
.
api
.
Resolve
(
r
.
uri
)
if
err
!=
nil
{
s
.
Error
(
w
,
r
,
fmt
.
Errorf
(
"error resolving %s: %s"
,
r
.
uri
.
Addr
,
err
))
...
...
This diff is collapsed.
Click to expand it.
swarm/api/http/server_test.go
+
62
−
0
View file @
f4841ff4
...
...
@@ -18,12 +18,16 @@ package http_test
import
(
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"sync"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/swarm/api"
swarm
"github.com/ethereum/go-ethereum/swarm/api/client"
"github.com/ethereum/go-ethereum/swarm/storage"
"github.com/ethereum/go-ethereum/swarm/testutil"
)
...
...
@@ -128,3 +132,61 @@ func TestBzzrGetPath(t *testing.T) {
}
}
// TestBzzRootRedirect tests that getting the root path of a manifest without
// a trailing slash gets redirected to include the trailing slash so that
// relative URLs work as expected.
func
TestBzzRootRedirect
(
t
*
testing
.
T
)
{
srv
:=
testutil
.
NewTestSwarmServer
(
t
)
defer
srv
.
Close
()
// create a manifest with some data at the root path
client
:=
swarm
.
NewClient
(
srv
.
URL
)
data
:=
[]
byte
(
"data"
)
file
:=
&
swarm
.
File
{
ReadCloser
:
ioutil
.
NopCloser
(
bytes
.
NewReader
(
data
)),
ManifestEntry
:
api
.
ManifestEntry
{
Path
:
""
,
ContentType
:
"text/plain"
,
Size
:
int64
(
len
(
data
)),
},
}
hash
,
err
:=
client
.
Upload
(
file
,
""
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// define a CheckRedirect hook which ensures there is only a single
// redirect to the correct URL
redirected
:=
false
httpClient
:=
http
.
Client
{
CheckRedirect
:
func
(
req
*
http
.
Request
,
via
[]
*
http
.
Request
)
error
{
if
redirected
{
return
errors
.
New
(
"too many redirects"
)
}
redirected
=
true
expectedPath
:=
"/bzz:/"
+
hash
+
"/"
if
req
.
URL
.
Path
!=
expectedPath
{
return
fmt
.
Errorf
(
"expected redirect to %q, got %q"
,
expectedPath
,
req
.
URL
.
Path
)
}
return
nil
},
}
// perform the GET request and assert the response
res
,
err
:=
httpClient
.
Get
(
srv
.
URL
+
"/bzz:/"
+
hash
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
res
.
Body
.
Close
()
if
!
redirected
{
t
.
Fatal
(
"expected GET /bzz:/<hash> to redirect to /bzz:/<hash>/ but it didn't"
)
}
gotData
,
err
:=
ioutil
.
ReadAll
(
res
.
Body
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
bytes
.
Equal
(
gotData
,
data
)
{
t
.
Fatalf
(
"expected response to equal %q, got %q"
,
data
,
gotData
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment