]> source.dussan.org Git - jquery.git/commitdiff
Revert "Ajax: use anchor tag for parsing urls"
authorTimmy Willison <timmywillisn@gmail.com>
Tue, 5 Jan 2016 21:43:18 +0000 (16:43 -0500)
committerTimmy Willison <timmywillisn@gmail.com>
Wed, 6 Jan 2016 19:05:17 +0000 (14:05 -0500)
This reverts commit de7ae8cd17a22b0275217a6ca0345d9d5a97c6dd.

src/ajax.js
test/unit/ajax.js

index 45e8cfe217765317689bdd68295db8f0b2ec5d1e..c7013c0939a0ae6c4796acd69d76c0a8cef87e02 100644 (file)
@@ -15,12 +15,15 @@ define( [
 var
        rhash = /#.*$/,
        rts = /([?&])_=[^&]*/,
-       rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
+
+       // IE leaves an \r character at EOL
+       rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg,
 
        // #7653, #8125, #8152: local protocol detection
        rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
        rnoContent = /^(?:GET|HEAD)$/,
        rprotocol = /^\/\//,
+       rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/,
 
        /* Prefilters
         * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
@@ -43,10 +46,11 @@ var
        // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
        allTypes = "*/".concat( "*" ),
 
-       // Anchor tag for parsing the document origin
-       originAnchor = document.createElement( "a" );
+       // Document location
+       ajaxLocation = location.href,
 
-       originAnchor.href = location.href;
+       // Segment location into parts
+       ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || [];
 
 // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
 function addToPrefiltersOrTransports( structure ) {
@@ -295,9 +299,9 @@ jQuery.extend( {
        etag: {},
 
        ajaxSettings: {
-               url: location.href,
+               url: ajaxLocation,
                type: "GET",
-               isLocal: rlocalProtocol.test( location.protocol ),
+               isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ),
                global: true,
                processData: true,
                async: true,
@@ -391,6 +395,9 @@ jQuery.extend( {
 
                var
 
+                       // Cross-domain detection vars
+                       parts,
+
                        // Loop variable
                        i,
 
@@ -403,9 +410,6 @@ jQuery.extend( {
                        // timeout handle
                        timeoutTimer,
 
-                       // Url cleanup var
-                       urlAnchor,
-
                        // To know if global events are to be dispatched
                        fireGlobals,
 
@@ -524,8 +528,9 @@ jQuery.extend( {
                // Add protocol if not provided (#5866: IE7 issue with protocol-less urls)
                // Handle falsy url in the settings object (#10093: consistency with old signature)
                // We also use the url parameter if available
-               s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" )
-                       .replace( rprotocol, location.protocol + "//" );
+               s.url = ( ( url || s.url || ajaxLocation ) + "" )
+                       .replace( rhash, "" )
+                       .replace( rprotocol, ajaxLocParts[ 1 ] + "//" );
 
                // Alias method option to type as per ticket #12004
                s.type = options.method || options.type || s.method || s.type;
@@ -533,26 +538,14 @@ jQuery.extend( {
                // Extract dataTypes list
                s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
 
-               // A cross-domain request is in order when the origin doesn't match the current origin
+               // A cross-domain request is in order when we have a protocol:host:port mismatch
                if ( s.crossDomain == null ) {
-                       urlAnchor = document.createElement( "a" );
-
-                       // Support: IE6-11+
-                       // IE throws exception if url is malformed, e.g. http://example.com:80x/
-                       try {
-                               urlAnchor.href = s.url;
-
-                               // Support: IE6-11+
-                               // Anchor's host property isn't correctly set when s.url is relative
-                               urlAnchor.href = urlAnchor.href;
-                               s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !==
-                                       urlAnchor.protocol + "//" + urlAnchor.host;
-                       } catch ( e ) {
-
-                               // If there is an error parsing the URL, assume it is crossDomain,
-                               // it can be rejected by the transport if it is invalid
-                               s.crossDomain = true;
-                       }
+                       parts = rurl.exec( s.url.toLowerCase() );
+                       s.crossDomain = !!( parts &&
+                               ( parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] ||
+                                       ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? "80" : "443" ) ) !==
+                                               ( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? "80" : "443" ) ) )
+                       );
                }
 
                // Convert data if not already a string
@@ -651,9 +644,9 @@ jQuery.extend( {
                strAbort = "abort";
 
                // Install callbacks on deferreds
-               completeDeferred.add( s.complete );
-               jqXHR.done( s.success );
-               jqXHR.fail( s.error );
+               for ( i in { success: 1, error: 1, complete: 1 } ) {
+                       jqXHR[ i ]( s[ i ] );
+               }
 
                // Get transport
                transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR );
index 9abc5e61338909e10a78b1fc7edfe42874204b0a..d74909f887c13950c96219479bbafa06392cc0f6 100644 (file)
@@ -374,7 +374,7 @@ QUnit.module( "ajax", {
                ];
        } );
 
-       ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function( assert ) {
+       ajaxTest( "jQuery.ajax() - cross-domain detection", 7, function( assert ) {
                function request( url, title, crossDomainOrOptions ) {
                        return jQuery.extend( {
                                dataType: "jsonp",
@@ -424,10 +424,6 @@ QUnit.module( "ajax", {
                                {
                                        crossDomain: true
                                }
-                       ),
-                       request(
-                               " http://otherdomain.com",
-                               "Cross-domain url with leading space is detected as cross-domain"
                        )
                ];
        } );