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>
Lars Gierth committed
Jun 8, 2017 at 03:20 UTC
4fe5b3eab706eb75e5597ba063375902f483a3e7
2 files changed
+24
-1
core/corehttp/gateway_handler.go
+3
-1
@@ -280,7 +280,9 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
280
case err == nil:
281
log.Debugf("found index.html link for %s", urlPath)
282
283
- if urlPath[len(urlPath)-1] != '/' {
283
+ dirwithoutslash := urlPath[len(urlPath)-1] != '/'
284
+ goget := r.URL.Query().Get("go-get") == "1"
285
+ if dirwithoutslash && !goget {
286
// See comment above where originalUrlPath is declared.
287
http.Redirect(w, r, originalUrlPath+"/", 302)
288
log.Debugf("redirect to %s", originalUrlPath+"/")
core/corehttp/gateway_test.go
+21
@@ -490,6 +490,27 @@ func TestCacheControlImmutable(t *testing.T) {
490
}
491
}
492
493
+func TestGoGetSupport(t *testing.T) {
494
+ ts, _ := newTestServerAndNode(t, nil)
495
+ t.Logf("test server url: %s", ts.URL)
496
+ defer ts.Close()
497
+
498
+ // mimic go-get
499
+ req, err := http.NewRequest("GET", ts.URL+emptyDir+"?go-get=1", nil)
500
+ if err != nil {
501
+ t.Fatal(err)
502
+ }
503
+
504
+ res, err := doWithoutRedirect(req)
505
+ if err != nil {
506
+ t.Fatal(err)
507
+ }
508
+
509
+ if res.StatusCode != 200 {
510
+ t.Errorf("status is %d, expected 200", res.StatusCode)
511
+ }
512
+}
513
+
514
func TestVersion(t *testing.T) {
515
config.CurrentCommit = "theshortcommithash"
516