Update chooser to properly return a standard XHR
This commit is contained in:
parent
217e2b2909
commit
e5476e81f6
|
@ -166,5 +166,41 @@ module Typertext.Http {
|
||||||
((this.port == HttpUrl.DefaultPort(this.protocol)) ? "" : ":" + this.port) + this.path +
|
((this.port == HttpUrl.DefaultPort(this.protocol)) ? "" : ":" + this.port) + this.path +
|
||||||
HttpUrl.EncodeQueryString(this.queryString);
|
HttpUrl.EncodeQueryString(this.queryString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the port number that this url uses
|
||||||
|
*
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
public GetPort():number {
|
||||||
|
return this.port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the domain that this url uses
|
||||||
|
*
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
public GetDomain():string {
|
||||||
|
return this.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the protocol that this url uses
|
||||||
|
*
|
||||||
|
* @returns {HttpProtocol}
|
||||||
|
*/
|
||||||
|
public GetProtocol():HttpProtocol {
|
||||||
|
return this.protocol;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether or not a given URL is in the same domain
|
||||||
|
*
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public CrossOriginCheck(url:HttpUrl):boolean {
|
||||||
|
return (this.domain === url.GetDomain() && this.port === url.GetPort() && this.protocol === url.GetProtocol());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,16 +1,46 @@
|
||||||
module Typertext.Transport {
|
module Typertext.Transport {
|
||||||
export class TransportChooser {
|
import HttpMethod = Typertext.Http.HttpMethod;
|
||||||
static Transport(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl, postData:Typertext.Http.HttpPostData, callback:Typertext.Http.HttpResponseHandler):GenericTransport {
|
import HttpUrl = Typertext.Http.HttpUrl
|
||||||
var ieLte9 = false;
|
import HttpPostData = Typertext.Http.HttpPostData;
|
||||||
var isXdomain = false;
|
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
|
||||||
var isXprotocol = false;
|
|
||||||
|
|
||||||
if (!ieLte9) {
|
export class TransportChooser {
|
||||||
return new XDR(method, request, postData, callback);
|
/**
|
||||||
} else if (isXdomain && !isXprotocol) {
|
*
|
||||||
|
* @param {HttpMethod} method
|
||||||
|
* @param {HttpUrl} request
|
||||||
|
* @param {HttpPostData} postData
|
||||||
|
* @param {HttpResponseHandler} callback
|
||||||
|
* @returns {GenericTransport}
|
||||||
|
*/
|
||||||
|
static Transport(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):GenericTransport {
|
||||||
|
//Prepare to test if we are in IE
|
||||||
|
var ieTestDiv = document.createElement("div");
|
||||||
|
ieTestDiv.innerHTML = "<!--[if lte IE 7]><i></i><![endif]-->";
|
||||||
|
|
||||||
|
if (ieTestDiv.getElementsByTagName("i").length === 1) {
|
||||||
|
//There is currently no supported transport for IE lte 7
|
||||||
|
throw {};
|
||||||
|
}
|
||||||
|
|
||||||
|
//Now test how we should proceed normally
|
||||||
|
ieTestDiv.innerHTML = "<!--[if lte IE 9]><i></i><![endif]-->";
|
||||||
|
var ieLte9 = (ieTestDiv.getElementsByTagName("i").length === 1);
|
||||||
|
var origin = HttpUrl.FromUrl(window.location.href);
|
||||||
|
|
||||||
|
//If this is a CORS request in a modern browser
|
||||||
|
if (!origin.CrossOriginCheck(url) || !ieLte9) {
|
||||||
|
//Just use a standard XHR request
|
||||||
return new XHR(method, request, postData, callback);
|
return new XHR(method, request, postData, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Otherwise if we aren't cross protocol
|
||||||
|
if (origin.GetProtocol() === request.GetProtocol()) {
|
||||||
|
//Use IE's silly XDomainRequest
|
||||||
|
return new XDR(method, request, postData, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Otherwise there is no supported transport
|
||||||
throw {};
|
throw {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue