Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Make url-add follow HTTP redirections #514

Merged
merged 4 commits into from
Aug 6, 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
33 changes: 23 additions & 10 deletions src/util/url-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,41 @@ module.exports = (arg) => {
opts = {}
}

if (typeof url !== 'string' ||
!url.startsWith('http')) {
if (!validUrl(url)) {
return callback(new Error('"url" param must be an http(s) url'))
}

callback = once(callback)

request(parseUrl(url).protocol)(url, (res) => {
res.once('error', callback)
if (res.statusCode >= 400) {
return callback(new Error(`Failed to download with ${res.statusCode}`))
}
requestWithRedirect(url, opts, send, callback)
})
}

const validUrl = (url) => typeof url === 'string' && url.startsWith('http')

const requestWithRedirect = (url, opts, send, callback) => {
request(parseUrl(url).protocol)(url, (res) => {
res.once('error', callback)
if (res.statusCode >= 400) {
return callback(new Error(`Failed to download with ${res.statusCode}`))
}

const redirection = res.headers.location

if (res.statusCode >= 300 && res.statusCode < 400 && redirection) {
if (!validUrl(redirection)) {
return callback(new Error('redirection url must be an http(s) url'))
}
requestWithRedirect(redirection, opts, send, callback)
} else {
const params = {
path: 'add',
qs: opts,
files: res
}

// Transform the response stream to DAGNode values
const transform = (res, callback) => DAGNodeStream.streamToValue(send, res, callback)
send.andTransform(params, transform, callback)
}).end()
})
}
}).end()
}
8 changes: 8 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ describe('.util', () => {
done()
})
})

it('.urlAdd http with redirection', (done) => {
ipfs.util.addFromURL('http://covers.openlibrary.org/book/id/969165.jpg', (err, result) => {
expect(err).to.not.exist()
expect(result[0].hash).to.equal('QmaL9zy7YUfvWmtD5ZXp42buP7P4xmZJWFkm78p8FJqgjg')
done()
})
})
})

describe('Promise API', () => {})
Expand Down