From afefb4f3d28f47c0a93cc9dfddfcbadb595a8efb Mon Sep 17 00:00:00 2001 From: jaubourg Date: Sun, 9 Jan 2011 06:01:00 +0100 Subject: [PATCH] Fixes #7465. Reworked the regexp and associated test for cross-domain detection so that it now includes ports. Added cross-domain detection tests for protocol, hostname and port. --- src/ajax.js | 9 +++++++-- test/unit/ajax.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 744476f..207ef13 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -10,7 +10,7 @@ var r20 = /%20/g, rscript = /)<[^<]*)*<\/script>/gi, rselectTextarea = /^(?:select|textarea)/i, rts = /([?&])_=[^&]*/, - rurl = /^(\w+:)?\/\/([^\/?#]+)/, + rurl = /^(\w+:)?\/\/([^\/?#:]+)(?::(\d+))?/, // Slice function sliceFunc = Array.prototype.slice, @@ -520,7 +520,12 @@ jQuery.extend({ // Determine if a cross-domain request is in order var parts = rurl.exec( s.url.toLowerCase() ), loc = location; - s.crossDomain = !!( parts && ( parts[ 1 ] && parts[ 1 ] != loc.protocol || parts[ 2 ] != loc.host ) ); + s.crossDomain = !!( + parts && + ( parts[ 1 ] && parts[ 1 ] != loc.protocol || + parts[ 2 ] != loc.hostname || + ( parts[ 3 ] || 80 ) != ( loc.port || 80 ) ) + ); // Convert data if not already a string if ( s.data && s.processData && typeof s.data != "string" ) { diff --git a/test/unit/ajax.js b/test/unit/ajax.js index adf589e..14f8928 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -358,6 +358,58 @@ test(".ajax() - hash", function() { }); }); +test("jQuery ajax - cross-domain detection", function() { + + expect( 3 ); + + var loc = document.location, + otherPort = loc.port === 666 ? 667 : 666, + otherProtocol = loc.protocol === "http:" ? "https:" : "http:", + protocolFlag, + hostFlag, + portFlag; + + if ( jQuery.ajax({ + url: otherProtocol + "//" + loc.host, + beforeSend: function( _ , s ) { + protocolFlag = 1; + ok( s.crossDomain , "Test different protocols are detected as cross-domain" ); + return false; + } + }) === false ) { + if ( ! protocolFlag ) { + ok( ! jQuery.support.cors , "Test different protocols are detected as cross-domain (no transport)" ); + } + } + + if ( jQuery.ajax({ + url: loc.protocol + '//somewebsitethatdoesnotexist-656329477541.com:' + ( loc.port || 80 ), + beforeSend: function( _ , s ) { + hostFlag = 1; + ok( s.crossDomain , "Test different hostnames are detected as cross-domain" ); + return false; + } + }) === false ) { + if ( ! hostFlag ) { + ok( ! jQuery.support.cors , "Test different hostnames are detected as cross-domain (no transport)" ); + } + } + + if ( jQuery.ajax({ + url: loc.protocol + "//" + loc.hostname + ":" + otherPort, + beforeSend: function( _ , s ) { + portFlag = 1; + ok( s.crossDomain , "Test different ports are detected as cross-domain" ); + return false; + } + }) === false ) { + if ( ! portFlag ) { + ok( ! jQuery.support.cors , "Test different ports are detected as cross-domain (no transport)" ); + } + } + +}); + test(".ajax() - 304", function() { expect( 1 ); stop(); -- 1.7.10.4