Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Changes required for #968 feat/Gateway #989

Merged
merged 9 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/http/gateway/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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()
}

Expand Down
6 changes: 3 additions & 3 deletions src/http/gateway/resources/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ 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
}).code(400).takeover()
}

return reply({
ref: `/ipfs/${request.params.hash}`
ref: `/ipfs/${request.params.cid}`
})
},
handler: (request, reply) => {
Expand Down
4 changes: 2 additions & 2 deletions src/http/gateway/routes/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 6 additions & 17 deletions test/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Expand Down Expand Up @@ -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()
})
Expand Down