-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Conversation
In some cases I would like to set a header to authenticate a user for getting a vector tile. It needs to be set per source. * Added the following format to the source style headers: [["HEADER-NAME", "HEADER-VALUE"]] * Added headers to ajax getArrayBuffer call
I understand this is a quite controversial pull request. Is this something that you guys can think of supporting or should I go with patching this locally? |
Thanks @gyllen! My apologies that this has slipped through the cracks. Here are the next steps towards merging this PR:
|
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, true); | ||
xhr.responseType = 'arraybuffer'; | ||
xhr.onerror = function(e) { | ||
callback(e); | ||
}; | ||
|
||
if (headers !== undefined && headers !== null) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@lucaswoj Any news on this issue? (sorry for bugging you) |
I think it's unlikely that we would want to add Could request customization of this sort fit into the Source extensibility framework we started to establish with #2667? (cc @anandthakker) |
Interesting question! I'll let @anandthakker answer. I haven't thought much about how it'd play with raster, image, and video sources. Sounds like something it should support in theory 😄 |
Yep, this should definitely be doable via a custom source. It will require some experimentation / hackery, because the custom source interface is still private and undocumented while it's being vetted and stabilized. In the case of a It's a bit more involved -- but still possible -- for vector sources, because in that case, you'd need something like the following: my_vector_source.js: var VectorTileSource = require('mapbox-gl/source/vector_tile_source')
module.exports = MyVectorSource // MyVectorSource inherits from gl-js's vector_tile_source.js
function MyVectorSource () { VectorTileSource.apply(this, arguments) }
MyVectorSource.prototype = inherit(VectorTileSource, {
/* override VectorTileSource methods, changing it to include a `type: 'my-vector-source'` property in the params it's sending to the worker via dispatcher.send(...). */
}
module.exports.workerSourceURL = URL.createObjectURL(webworkify(require('./my_vector_worker'), {bare: true}) my_vector_worker.js: var VectorTileWorkerSource = require('mapbox-gl/source/vector_tile_worker_source')
module.exports = function (self) {
self.registerWorkerSource('my-vector-source', MyWorkerSource)
}
function MyWorkerSource (actor, style) {
var vectorWorker = new VectorTileWorkerSource(actor, style, loadVectorData)
this.loadTile = vectorWorker.loadTile.bind(vectorWorker)
this.reloadTile = vectorWorker.reloadTile.bind(vectorWorker)
/* and so on with abortTile, removeTile, redoPlacement */
}
function loadVectorData (params, callback) {
// params.url is a particular tile url.
// callback should be called with (err, { tile: VectorTile object, rawTileData: binary pbf data ArrayBuffer })
} |
@anandthakker @lucaswoj @jfirebaugh Thanks for taking some time reviewing this. The custom source interface makes much more sense than adding a global property. |
Custom vector source for such a simple task seems hacky... Could we expose a point of extension for our xhr methods so they could be patched? |
@mourner I feel like that would very likely end up being more hacky, at least in this use case, since:
^ given that headers need to be different per source, it seems to me like patching it at I do agree that the custom source approach feels like overkill, but that's partly because the custom source interface is still not as streamlined as it could be. See also #3051 |
@anandthakker yeah, I assumed something like: if (url.indexOf(myAwesomeServer) >= 0) xhr.setRequestHeader('foo', 'bar'); |
I guess that's not too bad 😄 . However, there's another issue with exposing |
I have the same case as @gyllen : Need to set http headers for VT requests. What is the correct way to do this currently? A lot of things have changed since this discussion, but unfortunately no exposed API methods to set headers for particular VT sources. |
In some cases I would like to set a header to authenticate a user for getting a vector tile. It needs to be set per source and authentication is often done in the header.
I added the following format to the source style.
Added headers to ajax getArrayBuffer call.
Not sure about the what source format or if this is something that you want to support.