diff --git a/lib/Typertext/Http/HttpUrl.ts b/lib/Typertext/Http/HttpUrl.ts index 8a21bbe..2479389 100644 --- a/lib/Typertext/Http/HttpUrl.ts +++ b/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()); + } } } \ No newline at end of file diff --git a/lib/Typertext/Transport/TransportChooser.ts b/lib/Typertext/Transport/TransportChooser.ts index 7fd4f99..0cd9e0a 100644 --- a/lib/Typertext/Transport/TransportChooser.ts +++ b/lib/Typertext/Transport/TransportChooser.ts @@ -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 (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 = ""; + 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 {}; } }