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

Allow user to set headers for XHR vector tile get #2918

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion bench/benchmarks/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function preloadAssets(stylesheet, callback) {
}

function getTile(url, callback) {
ajax.getArrayBuffer(url, function(err, response) {
ajax.getArrayBuffer(url, null, function(err, response) {
assets.tiles[url] = response;
callback(err, response);
});
Expand Down
5 changes: 3 additions & 2 deletions js/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = VectorTileSource;
function VectorTileSource(id, options, dispatcher) {
this.id = id;
this.dispatcher = dispatcher;
util.extend(this, util.pick(options, ['url', 'scheme', 'tileSize']));
util.extend(this, util.pick(options, ['headers', 'url', 'scheme', 'tileSize']));
this._options = util.extend({ type: 'vector' }, options);

if (this.tileSize !== 512) {
Expand Down Expand Up @@ -55,7 +55,8 @@ VectorTileSource.prototype = util.inherit(Evented, {
overscaling: overscaling,
angle: this.map.transform.angle,
pitch: this.map.transform.pitch,
showCollisionBoxes: this.map.showCollisionBoxes
showCollisionBoxes: this.map.showCollisionBoxes,
headers: this.headers
};

if (tile.workerID) {
Expand Down
2 changes: 1 addition & 1 deletion js/source/vector_tile_worker_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ VectorTileWorkerSource.prototype = {
* @param {string} params.url The URL of the tile PBF to load.
*/
loadVectorData: function (params, callback) {
var xhr = ajax.getArrayBuffer(params.url, done.bind(this));
var xhr = ajax.getArrayBuffer(params.url, params.headers, done.bind(this));
return function abort () { xhr.abort(); };
function done(err, data) {
if (err) { return callback(err); }
Expand Down
2 changes: 1 addition & 1 deletion js/symbol/glyph_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ GlyphSource.prototype.loadRange = function(fontstack, range, callback) {
var rangeName = (range * 256) + '-' + (range * 256 + 255);
var url = glyphUrl(fontstack, rangeName, this.url);

getArrayBuffer(url, function(err, data) {
getArrayBuffer(url, null, function(err, data) {
var glyphs = !err && new Glyphs(new Protobuf(new Uint8Array(data)));
for (var i = 0; i < loading[range].length; i++) {
loading[range][i](err, range, glyphs);
Expand Down
2 changes: 1 addition & 1 deletion js/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports.getJSON = function(url, callback) {
});
};

exports.getArrayBuffer = function(url, callback) {
exports.getArrayBuffer = function(url, headers, callback) {
if (cache[url]) return cached(cache[url], callback);
return request({url: url, encoding: null}, function(error, response, body) {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
Expand Down
11 changes: 9 additions & 2 deletions js/util/browser/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ exports.getJSON = function(url, callback) {
return xhr;
};

exports.getArrayBuffer = function(url, callback) {
exports.getArrayBuffer = function(url, headers, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onerror = function(e) {
callback(e);
};

if (headers !== undefined && headers !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be Array.isArray(headers) instead? This code will fail if given a truthy non-array value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still won't fail because i < undefined will always be false so the loop will effectively be noop.

for (var i = 0; i < headers.length; i++) {
xhr.setRequestHeader(headers[i][0], headers[i][1]);
}
}

xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300 && xhr.response) {
callback(null, xhr.response);
Expand All @@ -49,7 +56,7 @@ function sameOrigin(url) {
}

exports.getImage = function(url, callback) {
return exports.getArrayBuffer(url, function(err, imgData) {
return exports.getArrayBuffer(url, null, function(err, imgData) {
if (err) return callback(err);
var img = new Image();
img.onload = function() {
Expand Down