diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go index b393bc77865..8dbe4d6b90e 100644 --- a/core/corehttp/gateway.go +++ b/core/corehttp/gateway.go @@ -15,6 +15,7 @@ import ( type GatewayConfig struct { Headers map[string][]string Writable bool + FetchBlocks bool PathPrefixes []string } @@ -26,13 +27,14 @@ func ConfigFromNode(n *core.IpfsNode) (*GatewayConfig, error) { gc := &GatewayConfig{ Headers: cfg.Gateway.HTTPHeaders, PathPrefixes: cfg.Gateway.PathPrefixes, + FetchBlocks: cfg.Gateway.FetchBlocks, } return gc, nil } func GatewayOption(gc GatewayConfig, paths ...string) ServeOption { return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { - gateway := newGatewayHandler(n, gc, coreapi.NewCoreAPI(n)) + gateway := newGatewayHandler(n, gc, coreapi.NewCoreAPI(n, !gc.FetchBlocks)) for _, p := range paths { mux.Handle(p+"/", gateway) diff --git a/docs/config.md b/docs/config.md index 33dc9d3aae4..f4b9b4d5af8 100644 --- a/docs/config.md +++ b/docs/config.md @@ -229,6 +229,11 @@ A boolean to configure whether the gateway is writeable or not. Default: `false` +- `FetchBlocks` +A boolean to configure whether the gateway should fetch blocks from network to satisfy requests. + +Default: `true` + - `PathPrefixes` TODO diff --git a/repo/config/gateway.go b/repo/config/gateway.go index a8ba7059071..ca13fcc8837 100644 --- a/repo/config/gateway.go +++ b/repo/config/gateway.go @@ -6,4 +6,5 @@ type Gateway struct { RootRedirect string Writable bool PathPrefixes []string + FetchBlocks bool // Fetch blocks for requests } diff --git a/repo/config/init.go b/repo/config/init.go index 2b1def8b522..8a7fbf77f35 100644 --- a/repo/config/init.go +++ b/repo/config/init.go @@ -51,6 +51,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { Gateway: Gateway{ RootRedirect: "", Writable: false, + FetchBlocks: true, PathPrefixes: []string{}, HTTPHeaders: map[string][]string{ "Access-Control-Allow-Origin": []string{"*"}, diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index c5a4aa39c75..ae92a4da739 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -195,4 +195,30 @@ test_expect_success "GET compact blocks succeeds" ' test_kill_ipfs_daemon +test_expect_success "set FetchBlocks to false in config" ' + ipfs config --bool=false Gateway.FetchBlocks +' + +test_launch_ipfs_daemon + +port=$GWAY_PORT +apiport=$API_PORT + +test_expect_success "try fetching not present key from offline gateway" ' + echo "hi" | ipfs add --hash-only -Q > hi.hash + test_expect_code 22 curl -f "http://127.0.0.1:$port/ipfs/$(cat hi.hash)" +' + +test_expect_success "try fetching present key from offline gateway" ' + echo "hi" | ipfs add -Q > hi.hash && + echo "http://127.0.0.1:$port/ipfs/$(cat hi.hash)" && + curl -f "http://127.0.0.1:$port/ipfs/$(cat hi.hash)" +' + +test_kill_ipfs_daemon + +test_expect_success "set FetchBlocks to true in config" ' + ipfs config --bool=true Gateway.FetchBlocks +' + test_done