From 6726a8d3ed101c9d96e9fc8f6fb4cbb1e1927859 Mon Sep 17 00:00:00 2001 From: Nick Poulden Date: Fri, 12 Jul 2019 16:23:33 -0600 Subject: [PATCH 1/6] Add CORS headers to gateway responses --- src/http/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/http/index.js b/src/http/index.js index 205c9edc16..87407c2a05 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -125,7 +125,13 @@ class HttpApi { } async _createGatewayServer (host, port, ipfs) { - const server = Hapi.server({ host, port }) + const server = Hapi.server({ + host, + port, + routes: { + cors: true + } + }) server.app.ipfs = ipfs await server.register({ From 0ef095755878bc29e6deb795bcb45ca8fbb75e47 Mon Sep 17 00:00:00 2001 From: Nick Poulden Date: Fri, 12 Jul 2019 16:42:50 -0600 Subject: [PATCH 2/6] Update index.js --- src/http/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http/index.js b/src/http/index.js index 87407c2a05..63ed93bd2e 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -125,8 +125,8 @@ class HttpApi { } async _createGatewayServer (host, port, ipfs) { - const server = Hapi.server({ - host, + const server = Hapi.server({ + host, port, routes: { cors: true From 1fa4d33407f03485f587ea5638e6b96c26b18cc8 Mon Sep 17 00:00:00 2001 From: Nick Poulden Date: Fri, 12 Jul 2019 21:23:04 -0600 Subject: [PATCH 3/6] Force tests From 4947bbfdee3b8dff30d77fb871d71089b857e44a Mon Sep 17 00:00:00 2001 From: Nick Poulden Date: Wed, 17 Jul 2019 11:00:04 -0700 Subject: [PATCH 4/6] Add test --- test/gateway/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/gateway/index.js b/test/gateway/index.js index 651cee1578..21c806dbfe 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -137,6 +137,22 @@ describe('HTTP Gateway', function () { expect(res.headers.suborigin).to.equal('ipfs000bafybeicg2rebjoofv4kbyovkw7af3rpiitvnl6i7ckcywaq6xjcxnc2mby') }) + it('returns CORS headers', async () => { + const res = await gateway.inject({ + method: 'OPTIONS', + url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', + headers: { + origin: "http://example.com", + "access-control-request-method": "GET", + "access-control-request-headers": "" + } + }) + + expect(res.statusCode).to.equal(200) + expect(res.headers['access-control-allow-origin']).to.equal('http://example.com') + expect(res.headers['access-control-allow-methods']).to.equal('GET') + }) + /* TODO when support for CIDv1 lands it('valid CIDv1', (done) => { gateway.inject({ From 7f2e975adfb100b87c9070fadd7b17b6dbcb93df Mon Sep 17 00:00:00 2001 From: Nick Poulden Date: Wed, 17 Jul 2019 11:10:01 -0700 Subject: [PATCH 5/6] Lint --- test/gateway/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/gateway/index.js b/test/gateway/index.js index 21c806dbfe..76a0e68bac 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -142,9 +142,9 @@ describe('HTTP Gateway', function () { method: 'OPTIONS', url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', headers: { - origin: "http://example.com", - "access-control-request-method": "GET", - "access-control-request-headers": "" + origin: 'http://example.com', + 'access-control-request-method': 'GET', + 'access-control-request-headers': '' } }) From 70c4064ad424d327d49a6124b6640970a4f2dd3c Mon Sep 17 00:00:00 2001 From: Nick Poulden Date: Wed, 17 Jul 2019 11:58:10 -0700 Subject: [PATCH 6/6] Force tests