Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-origin URL redirection workaround #909

Merged
merged 1 commit into from Oct 13, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Cross-origin HTTP redirect workaround

  • Loading branch information
jablko committed Sep 13, 2016
commit 2c193317b115168ffa5aea6e18b489b8715cfc0e
@@ -124,12 +124,7 @@ WebConn.prototype.httpRequest = function (pieceIndex, offset, length, cb) {
range: 'bytes=' + start + '-' + end
}
}
get.concat(opts, function (err, res, data) {
if (hasError) return
if (err) {
hasError = true
return cb(err)
}
function onResponse (res, data) {
if (res.statusCode < 200 || res.statusCode >= 300) {
hasError = true
return cb(new Error('Unexpected HTTP status code ' + res.statusCode))
@@ -147,6 +142,51 @@ WebConn.prototype.httpRequest = function (pieceIndex, offset, length, cb) {
cb(null, ret)
}
}
}
get.concat(opts, function (err, res, data) {
if (hasError) return
if (err) {
// Browsers allow HTTP redirects for simple cross-origin
// requests but not for requests that require preflight.
// Use a simple request to unravel any redirects and get the
// final URL. Retry the original request with the new URL if
// it's different.
//
// This test is imperfect but it's simple and good for common
// cases. It catches all cross-origin cases but matches a few
// same-origin cases too.
if (typeof window === 'undefined' || url.startsWith(window.location.origin + '/')) {
hasError = true
return cb(err)
}

return get.head(url, function (errHead, res) {
if (hasError) return
if (errHead) {
hasError = true
return cb(errHead)
}
if (res.statusCode < 200 || res.statusCode >= 300) {
hasError = true
return cb(new Error('Unexpected HTTP status code ' + res.statusCode))
}
if (res.url === url) {
hasError = true
return cb(err)
}

opts.url = res.url
get.concat(opts, function (err, res, data) {
if (hasError) return
if (err) {
hasError = true
return cb(err)
}
onResponse(res, data)
})
})
}
onResponse(res, data)
})
})
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.