Skip to content

Commit 7b20141

Browse files
authored
fix(gateway): return HTTP 500 on namesys.ErrResolveFailed (#150)
1 parent 5aceeb9 commit 7b20141

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

gateway/gateway_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,17 @@ func TestGatewayGet(t *testing.T) {
349349
{"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"},
350350
{"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
351351
{"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"},
352-
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusBadRequest, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
353-
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusBadRequest, "ipfs resolve -r /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
352+
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
353+
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusInternalServerError, "ipfs resolve -r /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
354354
{"127.0.0.1:8080", "/ipns/example.com", http.StatusOK, "fnord"},
355355
{"example.com", "/", http.StatusOK, "fnord"},
356356

357357
{"working.example.com", "/", http.StatusOK, "fnord"},
358358
{"double.example.com", "/", http.StatusOK, "fnord"},
359359
{"triple.example.com", "/", http.StatusOK, "fnord"},
360360
{"working.example.com", k.String(), http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com" + k.String() + ": no link named \"ipfs\" under " + k.Cid().String() + "\n"},
361-
{"broken.example.com", "/", http.StatusBadRequest, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
362-
{"broken.example.com", k.String(), http.StatusBadRequest, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
361+
{"broken.example.com", "/", http.StatusInternalServerError, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
362+
{"broken.example.com", k.String(), http.StatusInternalServerError, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
363363
// This test case ensures we don't treat the TLD as a file extension.
364364
{"example.man", "/", http.StatusOK, "fnord"},
365365
} {

gateway/handler.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
cid "github.com/ipfs/go-cid"
1919
ipld "github.com/ipfs/go-ipld-format"
2020
logging "github.com/ipfs/go-log"
21+
"github.com/ipfs/go-namesys"
2122
"github.com/ipfs/go-path/resolver"
2223
coreiface "github.com/ipfs/interface-go-ipfs-core"
2324
ipath "github.com/ipfs/interface-go-ipfs-core/path"
@@ -743,6 +744,10 @@ func (i *handler) handlePathResolution(w http.ResponseWriter, r *http.Request, r
743744
case coreiface.ErrOffline:
744745
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusServiceUnavailable)
745746
return nil, nil, false
747+
case namesys.ErrResolveFailed:
748+
// Note: webError will replace http.StatusBadRequest with StatusNotFound or StatusRequestTimeout if necessary
749+
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusInternalServerError)
750+
return nil, nil, false
746751
default:
747752
// The path can't be resolved.
748753
if isUnixfsResponseFormat(responseFormat) {
@@ -767,7 +772,7 @@ func (i *handler) handlePathResolution(w http.ResponseWriter, r *http.Request, r
767772
}
768773
}
769774

770-
// Note: webError will replace http.StatusBadRequest with StatusNotFound if necessary
775+
// Note: webError will replace http.StatusBadRequest with StatusNotFound or StatusRequestTimeout if necessary
771776
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusBadRequest)
772777
return nil, nil, false
773778
}

0 commit comments

Comments
 (0)