Skip to content

Commit

Permalink
fix: preload request uses opt-out hint
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Jun 27, 2018
1 parent 6ea409d commit 2bfda53
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { optionDefaults, storeMissingOptions } = require('./options')
const { initState, offlinePeerCount } = require('./state')
const { createIpfsPathValidator, urlAtPublicGw } = require('./ipfs-path')
const createDnsLink = require('./dns-link')
const { createRequestModifier } = require('./ipfs-request')
const { createRequestModifier, redirectOptOutHint } = require('./ipfs-request')
const { initIpfsClient, destroyIpfsClient } = require('./ipfs-client')
const { createIpfsUrlProtocolHandler } = require('./ipfs-protocol')
const createNotifier = require('./notifier')
Expand Down Expand Up @@ -239,7 +239,9 @@ module.exports = async function init () {
// asynchronous HTTP HEAD request preloads triggers content without downloading it
return new Promise((resolve, reject) => {
const http = new XMLHttpRequest()
http.open('HEAD', urlAtPublicGw(path, state.pubGwURLString))
// Make sure preload request is excluded from global redirect
const preloadUrl = urlAtPublicGw(`${path}#${redirectOptOutHint}`, state.pubGwURLString)
http.open('HEAD', preloadUrl)
http.onreadystatechange = function () {
if (this.readyState === this.DONE) {
console.info(`[ipfs-companion] preloadAtPublicGateway(${path}):`, this.statusText)
Expand Down
3 changes: 2 additions & 1 deletion test/functional/lib/ipfs-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('modifyRequest', function () {
expect(modifyRequest(request)).to.equal(undefined)
})
it(`should be left untouched if URL includes opt-out hint (${nodeType} node)`, function () {
// A safe way for preloading data at arbitrary gateways - it should arrive at original destination
const request = url2request('https://google.com/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?x-ipfs-no-redirect#hashTest')
expect(modifyRequest(request)).to.equal(undefined)
expect(redirectOptOutHint).to.equal('x-ipfs-no-redirect')
Expand All @@ -87,7 +88,7 @@ describe('modifyRequest', function () {
expect(modifyRequest(request)).to.equal(undefined)
})
it(`should be left untouched if its is a preload request (${nodeType} node)`, function () {
// HTTP HEAD is often used for preloading data at arbitrary gateways - it should arrive at original destination
// HTTP HEAD is a legacy way for preloading data at arbitrary gateways - it should arrive at original destination
const headRequest = {url: 'https://google.com/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?argTest#hashTest', method: 'HEAD'}
expect(modifyRequest(headRequest)).to.equal(undefined)
})
Expand Down

0 comments on commit 2bfda53

Please sign in to comment.