Commit de7ae8cd authored by Ben Toews's avatar Ben Toews Committed by Dave Methvin

Ajax: use anchor tag for parsing urls

Fixes gh-1875
Closes gh-1880
(cherry picked from commit 5a75278e4c5359e07303fc4d8e78a1cf94f6ad65)

Conflicts:
	src/ajax.js
parent 9d1d90e7
...@@ -12,12 +12,11 @@ define([ ...@@ -12,12 +12,11 @@ define([
var var
rhash = /#.*$/, rhash = /#.*$/,
rts = /([?&])_=[^&]*/, rts = /([?&])_=[^&]*/,
rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg, // IE leaves an \r character at EOL rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg,
// #7653, #8125, #8152: local protocol detection // #7653, #8125, #8152: local protocol detection
rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/, rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
rnoContent = /^(?:GET|HEAD)$/, rnoContent = /^(?:GET|HEAD)$/,
rprotocol = /^\/\//, rprotocol = /^\/\//,
rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/,
/* Prefilters /* Prefilters
* 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example) * 1) They are useful to introduce custom dataTypes (see ajax/jsonp.js for an example)
...@@ -40,11 +39,10 @@ var ...@@ -40,11 +39,10 @@ var
// Avoid comment-prolog char sequence (#10098); must appease lint and evade compression // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression
allTypes = "*/".concat( "*" ), allTypes = "*/".concat( "*" ),
// Document location // Anchor tag for parsing the document origin
ajaxLocation = location.href, originAnchor = document.createElement( "a" );
// Segment location into parts originAnchor.href = location.href;
ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || [];
// Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport
function addToPrefiltersOrTransports( structure ) { function addToPrefiltersOrTransports( structure ) {
...@@ -287,9 +285,9 @@ jQuery.extend({ ...@@ -287,9 +285,9 @@ jQuery.extend({
etag: {}, etag: {},
ajaxSettings: { ajaxSettings: {
url: ajaxLocation, url: location.href,
type: "GET", type: "GET",
isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ), isLocal: rlocalProtocol.test( location.protocol ),
global: true, global: true,
processData: true, processData: true,
async: true, async: true,
...@@ -381,8 +379,7 @@ jQuery.extend({ ...@@ -381,8 +379,7 @@ jQuery.extend({
// Force options to be an object // Force options to be an object
options = options || {}; options = options || {};
var // Cross-domain detection vars var
parts,
// Loop variable // Loop variable
i, i,
// URL without anti-cache param // URL without anti-cache param
...@@ -391,7 +388,8 @@ jQuery.extend({ ...@@ -391,7 +388,8 @@ jQuery.extend({
responseHeadersString, responseHeadersString,
// timeout handle // timeout handle
timeoutTimer, timeoutTimer,
// Url cleanup var
urlAnchor,
// To know if global events are to be dispatched // To know if global events are to be dispatched
fireGlobals, fireGlobals,
...@@ -498,9 +496,8 @@ jQuery.extend({ ...@@ -498,9 +496,8 @@ jQuery.extend({
// Add protocol if not provided (prefilters might expect it) // Add protocol if not provided (prefilters might expect it)
// Handle falsy url in the settings object (#10093: consistency with old signature) // Handle falsy url in the settings object (#10093: consistency with old signature)
// We also use the url parameter if available // We also use the url parameter if available
s.url = ( ( url || s.url || ajaxLocation ) + "" ) s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" )
.replace( rhash, "" ) .replace( rprotocol, location.protocol + "//" );
.replace( rprotocol, ajaxLocParts[ 1 ] + "//" );
// Alias method option to type as per ticket #12004 // Alias method option to type as per ticket #12004
s.type = options.method || options.type || s.method || s.type; s.type = options.method || options.type || s.method || s.type;
...@@ -508,14 +505,24 @@ jQuery.extend({ ...@@ -508,14 +505,24 @@ jQuery.extend({
// Extract dataTypes list // Extract dataTypes list
s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ]; s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().match( rnotwhite ) || [ "" ];
// A cross-domain request is in order when we have a protocol:host:port mismatch // A cross-domain request is in order when the origin doesn't match the current origin
if ( s.crossDomain == null ) { if ( s.crossDomain == null ) {
parts = rurl.exec( s.url.toLowerCase() ); urlAnchor = document.createElement( "a" );
s.crossDomain = !!( parts &&
( parts[ 1 ] !== ajaxLocParts[ 1 ] || parts[ 2 ] !== ajaxLocParts[ 2 ] || // Support: IE8-11+
( parts[ 3 ] || ( parts[ 1 ] === "http:" ? "80" : "443" ) ) !== // IE throws exception if url is malformed, e.g. http://example.com:80x/
( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? "80" : "443" ) ) ) try {
); urlAnchor.href = s.url;
// Support: IE8-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;
}
} }
// Convert data if not already a string // Convert data if not already a string
......
...@@ -296,7 +296,7 @@ module( "ajax", { ...@@ -296,7 +296,7 @@ module( "ajax", {
} }
]); ]);
ajaxTest( "jQuery.ajax() - cross-domain detection", 7, function() { ajaxTest( "jQuery.ajax() - cross-domain detection", 8, function() {
function request( url, title, crossDomainOrOptions ) { function request( url, title, crossDomainOrOptions ) {
return jQuery.extend( { return jQuery.extend( {
dataType: "jsonp", dataType: "jsonp",
...@@ -346,6 +346,10 @@ module( "ajax", { ...@@ -346,6 +346,10 @@ module( "ajax", {
{ {
crossDomain: true crossDomain: true
} }
),
request(
" http://otherdomain.com",
"Cross-domain url with leading space is detected as cross-domain"
) )
]; ];
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment