Browse Source

Update chooser to properly return a standard XHR

master
Kegan Myers 8 years ago
parent
commit
e5476e81f6
  1. 36
      lib/Typertext/Http/HttpUrl.ts
  2. 44
      lib/Typertext/Transport/TransportChooser.ts

36
lib/Typertext/Http/HttpUrl.ts

@ -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());
}
}
}

44
lib/Typertext/Transport/TransportChooser.ts

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