-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Ajax: use anchor tag for parsing urls #1880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ var | |
| 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) | ||
|
|
@@ -40,11 +39,10 @@ var | |
| // Avoid comment-prolog char sequence (#10098); must appease lint and evade compression | ||
| allTypes = "*/".concat( "*" ), | ||
|
|
||
| // Document location | ||
| ajaxLocation = location.href, | ||
| // Anchor tag for parsing the document origin. | ||
| originAnchor = document.createElement( "a" ); | ||
|
|
||
| // Segment location into parts | ||
| ajaxLocParts = rurl.exec( ajaxLocation.toLowerCase() ) || []; | ||
| originAnchor.href = location.href; | ||
|
|
||
| // Base "constructor" for jQuery.ajaxPrefilter and jQuery.ajaxTransport | ||
| function addToPrefiltersOrTransports( structure ) { | ||
|
|
@@ -288,9 +286,9 @@ jQuery.extend({ | |
| etag: {}, | ||
|
|
||
| ajaxSettings: { | ||
| url: ajaxLocation, | ||
| url: location.href, | ||
| type: "GET", | ||
| isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ), | ||
| isLocal: rlocalProtocol.test( location.protocol ), | ||
| global: true, | ||
| processData: true, | ||
| async: true, | ||
|
|
@@ -390,8 +388,8 @@ jQuery.extend({ | |
| responseHeaders, | ||
| // timeout handle | ||
| timeoutTimer, | ||
| // Cross-domain detection vars | ||
| parts, | ||
| // Url cleanup var | ||
| urlAnchor, | ||
| // To know if global events are to be dispatched | ||
| fireGlobals, | ||
| // Loop variable | ||
|
|
@@ -496,23 +494,28 @@ jQuery.extend({ | |
| // Add protocol if not provided (prefilters might expect it) | ||
| // 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 || ajaxLocation ) + "" ).replace( rhash, "" ) | ||
| .replace( rprotocol, ajaxLocParts[ 1 ] + "//" ); | ||
| s.url = ( ( url || s.url || location.href ) + "" ).replace( rhash, "" ) | ||
| .replace( rprotocol, location.protocol + "//" ); | ||
|
|
||
| // Alias method option to type as per ticket #12004 | ||
| s.type = options.method || options.type || s.method || s.type; | ||
|
|
||
| // Extract dataTypes list | ||
| 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 ) { | ||
| 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" ) ) ) | ||
| ); | ||
| urlAnchor = document.createElement( "a" ); | ||
|
|
||
| try { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| urlAnchor.href = s.url; | ||
| urlAnchor.href = urlAnchor.href; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Holy crap. Welcome to our cross-browser hell.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a support comment about that.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll get that.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note sure if this will help, but here's a very similar solution with a giant comment about IE: https://github.com/angular/angular.js/blob/master/src/ng/urlUtils.js#L25
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it seems to boil down to almost the same thing. |
||
| s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !== | ||
| urlAnchor.protocol + "//" + urlAnchor.host; | ||
| } catch ( e ) { | ||
| // If there is an error parsing the URL, assume it is crossDomain. | ||
| s.crossDomain = true; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure if this should just fail closed, or cancel the request. If the URL is malformed, the request will end up failing either way...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, did some head-scratching on this one and left it as-is with an enhanced comment. |
||
| } | ||
| } | ||
|
|
||
| // Convert data if not already a string | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed the indentation here.