Skip to content

Commit

Permalink
Merge pull request #1 from Stoehr-Sauer/feature_Tools_CustomRequestHe…
Browse files Browse the repository at this point in the history
…aders

Feature tools custom request headers
  • Loading branch information
susares committed Nov 8, 2018
2 parents ca8da8d + 1243419 commit 930482b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions dist/preview release/what's new.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- AssetContainer dispose method ([TrevorDev](https://github.com/TrevorDev))
- Loading texture with KTX will fallback to non-KTX loader if KTX loader fails ([TrevorDev](https://github.com/TrevorDev))
- `Layer` are now supported in `RenderTargetTexture` ([Sebavan](https://github.com/Sebavan))
- Added `Tools.CustomRequestHeaders`, `Tools.UseCustomRequestHeaders`, `Tools.InjectCustomRequestHeaders` to send Custom Request Headers alongside XMLHttpRequest's i.e. when loading files (Tools.Loadfile) from resources requiring special headers like 'Authorization' ([susares](https://github.com/susares))

### glTF Loader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ module BABYLON {
}, false);

try {
if (Tools.UseCustomRequestHeaders) {
Tools.InjectCustomRequestHeaders(xhr);
}
xhr.send();
}
catch (ex) {
Expand Down Expand Up @@ -164,4 +167,4 @@ module BABYLON {
this._animate = value;
}
}
}
}
15 changes: 15 additions & 0 deletions src/Offline/babylon.database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ module BABYLON {
// It could fail when coupled with HTML5 Offline API
var retryManifestURL = this._currentSceneUrl + ".manifest";
xhr.open("GET", retryManifestURL, true);
if (Tools.UseCustomRequestHeaders) {
Tools.InjectCustomRequestHeaders(xhr);
}
xhr.send();
}
else {
Expand All @@ -150,6 +153,10 @@ module BABYLON {
}, false);

try {
if (Tools.UseCustomRequestHeaders) {
Tools.InjectCustomRequestHeaders(xhr);
}

xhr.send();
}
catch (ex) {
Expand Down Expand Up @@ -377,6 +384,10 @@ module BABYLON {
image.src = url;
}, false);

if (Tools.CustomRequestHeaders) {
Tools.InjectCustomRequestHeaders(xhr);
}

xhr.send();
}
else {
Expand Down Expand Up @@ -652,6 +663,10 @@ module BABYLON {
callback();
}, false);

if (Tools.UseCustomRequestHeaders) {
Tools.InjectCustomRequestHeaders(xhr);
}

xhr.send();
}
else {
Expand Down
31 changes: 31 additions & 0 deletions src/Tools/babylon.tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ module BABYLON {
*/
public static BaseUrl = "";

/**
* Enable/Disable Custom HTTP Request Headers globally.
* default = false
* @see CustomRequestHeaders
*/
public static UseCustomRequestHeaders: boolean = false;

/**
* Custom HTTP Request Headers to be sent with XMLHttpRequests
* i.e. when loading files, where the server/service expects an Authorization header.
* @see InjectCustomRequestHeaders injects them to an XMLHttpRequest
*/
public static CustomRequestHeaders: { [key: string]: string } = {};

/**
* Gets or sets the retry strategy to apply when an error happens while loading an asset
*/
Expand Down Expand Up @@ -943,6 +957,10 @@ module BABYLON {

request.addEventListener("readystatechange", onReadyStateChange);

if (Tools.UseCustomRequestHeaders) {
Tools.InjectCustomRequestHeaders(request);
}

request.send();
};

Expand Down Expand Up @@ -1881,6 +1899,19 @@ module BABYLON {
}
}

/**
* Injects the @see CustomRequestHeaders into the given request
* @param request the request that should be used for injection
*/
public static InjectCustomRequestHeaders(request: XMLHttpRequest): void {
for (let key in Tools.CustomRequestHeaders) {
const val = Tools.CustomRequestHeaders[key];
if (val) {
request.setRequestHeader(key, val);
}
}
}

/**
* Starts a performance counter
*/
Expand Down

0 comments on commit 930482b

Please sign in to comment.