Update chooser to properly return a standard XHR

This commit is contained in:
Kegan Myers 2014-04-15 11:23:57 -05:00
parent 217e2b2909
commit e5476e81f6
2 changed files with 74 additions and 8 deletions

View File

@ -166,5 +166,41 @@ module Typertext.Http {
((this.port == HttpUrl.DefaultPort(this.protocol)) ? "" : ":" + this.port) + this.path +
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());
}
}
}

View File

@ -1,16 +1,46 @@
module Typertext.Transport {
export class TransportChooser {
static Transport(method:Typertext.Http.HttpMethod, request:Typertext.Http.HttpUrl, postData:Typertext.Http.HttpPostData, callback:Typertext.Http.HttpResponseHandler):GenericTransport {
var ieLte9 = false;
var isXdomain = false;
var isXprotocol = false;
import HttpMethod = Typertext.Http.HttpMethod;
import HttpUrl = Typertext.Http.HttpUrl
import HttpPostData = Typertext.Http.HttpPostData;
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
if (!ieLte9) {
return new XDR(method, request, postData, callback);
} else if (isXdomain && !isXprotocol) {
export class TransportChooser {
/**
*
* @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);
}
//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 {};
}
}