1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 01:12:24 +08:00

gateway: don't redirect to trailing slash if it's go get

This enables `go get` to parse go-import meta tags from index.html
files stored in IPFS. One tiny step toward whyrusleeping/gx-go#2.

For an import like `ipfs.io/ipfs/QmFoo/mypkg`, the gateway would
previously redirect to `/ipfs/QmFoo/mypkg/` (note the trailing slash),
which the `go get` tool can't deal with.

Thankfully, `go get` sets a URL query parameter (`?go-get=1`) which
we can use to switch off the redirect in this case.

License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
This commit is contained in:
Lars Gierth
2017-06-08 03:20:04 +02:00
parent 20dae521ef
commit 4fe5b3eab7
2 changed files with 24 additions and 1 deletions

View File

@ -280,7 +280,9 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
case err == nil:
log.Debugf("found index.html link for %s", urlPath)
if urlPath[len(urlPath)-1] != '/' {
dirwithoutslash := urlPath[len(urlPath)-1] != '/'
goget := r.URL.Query().Get("go-get") == "1"
if dirwithoutslash && !goget {
// See comment above where originalUrlPath is declared.
http.Redirect(w, r, originalUrlPath+"/", 302)
log.Debugf("redirect to %s", originalUrlPath+"/")

View File

@ -490,6 +490,27 @@ func TestCacheControlImmutable(t *testing.T) {
}
}
func TestGoGetSupport(t *testing.T) {
ts, _ := newTestServerAndNode(t, nil)
t.Logf("test server url: %s", ts.URL)
defer ts.Close()
// mimic go-get
req, err := http.NewRequest("GET", ts.URL+emptyDir+"?go-get=1", nil)
if err != nil {
t.Fatal(err)
}
res, err := doWithoutRedirect(req)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Errorf("status is %d, expected 200", res.StatusCode)
}
}
func TestVersion(t *testing.T) {
config.CurrentCommit = "theshortcommithash"