diff --git a/src/http/gateway/resolver.js b/src/http/gateway/resolver.js index bc9ec1d5a0..f2dc4a07be 100644 --- a/src/http/gateway/resolver.js +++ b/src/http/gateway/resolver.js @@ -3,10 +3,11 @@ const mh = require('multihashes') const promisify = require('promisify-es6') const eachOfSeries = require('async/eachOfSeries') +const CID = require('cids') +const Unixfs = require('ipfs-unixfs') const debug = require('debug') const log = debug('jsipfs:http-gateway:resolver') log.error = debug('jsipfs:http-gateway:resolver:error') - const html = require('./utils/html') const PathUtil = require('./utils/path') @@ -41,32 +42,31 @@ const resolveMultihash = promisify((ipfs, path, callback) => { const partsLength = parts.length let currentMultihash = parts[0] - + let currentCid eachOfSeries(parts, (multihash, currentIndex, next) => { - // throws error when invalid multihash is passed - mh.validate(mh.fromB58String(currentMultihash)) + // throws error when invalid CID is passed + try { + currentCid = new CID(mh.fromB58String(currentMultihash)) + } catch (e) { + if (e) throw e + } + log('currentMultihash: ', currentMultihash) log('currentIndex: ', currentIndex, '/', partsLength) - ipfs.object.get(currentMultihash, { enc: 'base58' }, (err, dagNode) => { + ipfs.dag.get(currentCid, (err, result) => { if (err) { return next(err) } + let dagNode = result.value if (currentIndex === partsLength - 1) { - // leaf node - log('leaf node: ', currentMultihash) - - // TODO: Check if it is a directory by using Unixfs Type, right now - // it won't detect empty dirs - if (dagNode.links && - dagNode.links.length > 0 && - dagNode.links[0].name.length > 0) { - // this is a directory. - + let dagDataObj = Unixfs.unmarshal(dagNode.data) + if (dagDataObj.type === 'directory') { let isDirErr = new Error('This dag node is a directory') // add currentMultihash as a fileName so it can be used by resolveDirectory isDirErr.fileName = currentMultihash return next(isDirErr) } + return next() } diff --git a/src/http/gateway/resources/gateway.js b/src/http/gateway/resources/gateway.js index 37ccba22b4..5c472d1492 100644 --- a/src/http/gateway/resources/gateway.js +++ b/src/http/gateway/resources/gateway.js @@ -12,8 +12,8 @@ const PathUtils = require('../utils/path') const Stream = require('stream') module.exports = { - checkHash: (request, reply) => { - if (!request.params.hash) { + checkCID: (request, reply) => { + if (!request.params.cid) { return reply({ Message: 'Path Resolve error: path must contain at least one component', Code: 0 @@ -21,7 +21,7 @@ module.exports = { } return reply({ - ref: `/ipfs/${request.params.hash}` + ref: `/ipfs/${request.params.cid}` }) }, handler: (request, reply) => { diff --git a/src/http/gateway/routes/gateway.js b/src/http/gateway/routes/gateway.js index e1c0f3222f..5eddeb7c21 100644 --- a/src/http/gateway/routes/gateway.js +++ b/src/http/gateway/routes/gateway.js @@ -7,10 +7,10 @@ module.exports = (server) => { gateway.route({ method: '*', - path: '/ipfs/{hash*}', + path: '/ipfs/{cid*}', config: { pre: [ - { method: resources.gateway.checkHash, assign: 'args' } + { method: resources.gateway.checkCID, assign: 'args' } ], handler: resources.gateway.handler } diff --git a/test/gateway/index.js b/test/gateway/index.js index afa9cb70d9..9a77f9903c 100644 --- a/test/gateway/index.js +++ b/test/gateway/index.js @@ -6,34 +6,23 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const API = require('../../src/http') -const ncp = require('ncp').ncp -const path = require('path') -const clean = require('../utils/clean') - -describe('HTTP GATEWAY', () => { - const repoExample = path.join(__dirname, '../go-ipfs-repo') - const repoTests = path.join(__dirname, '../repo-tests-run') +describe('HTTP Gateway', () => { let http = {} let gateway before((done) => { - http.api = new API(repoTests) - - ncp(repoExample, repoTests, (err) => { - expect(err).to.not.exist() + http.api = new API() - http.api.start(false, () => { - gateway = http.api.server.select('Gateway') - done() - }) + http.api.start(true, () => { + gateway = http.api.server.select('Gateway') + done() }) }) after((done) => { http.api.stop((err) => { expect(err).to.not.exist() - clean(repoTests) done() }) }) @@ -67,7 +56,7 @@ describe('HTTP GATEWAY', () => { url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' }, (res) => { expect(res.statusCode).to.equal(200) - expect(res.rawPayload).to.deep.equal(new Buffer('hello world' + '\n')) + expect(res.rawPayload).to.deep.equal(Buffer.from('hello world' + '\n')) expect(res.payload).to.equal('hello world' + '\n') done() })