Reworked script and xhr abort logic to take advantage of the fact jXHR.abort will...
[jquery.git] / src / ajax / xhr.js
1 (function( jQuery ) {
2
3 var // Next fake timer id
4         xhrPollingId = jQuery.now(),
5
6         // Callbacks hashtable
7         xhrs = {},
8
9         // XHR pool
10         xhrPool = [],
11
12         // #5280: see below
13         xhrUnloadAbortInstalled;
14
15
16 jQuery.ajax.transport( function( s , determineDataType ) {
17
18         // Cross domain only allowed if supported through XMLHttpRequest
19         if ( ! s.crossDomain || jQuery.support.cors ) {
20
21                 var callback;
22
23                 return {
24
25                         send: function(headers, complete) {
26
27                                 // #5280: we need to abort on unload or IE will keep connections alive
28                                 if ( ! xhrUnloadAbortInstalled ) {
29
30                                         xhrUnloadAbortInstalled = 1;
31
32                                         jQuery(window).bind( "unload" , function() {
33
34                                                 // Abort all pending requests
35                                                 jQuery.each(xhrs, function(_, xhr) {
36                                                         if ( xhr.onreadystatechange ) {
37                                                                 xhr.onreadystatechange( 1 );
38                                                         }
39                                                 });
40
41                                         });
42                                 }
43
44                                 // Get a new xhr
45                                 var xhr = xhrPool.pop() || s.xhr(),
46                                         handle;
47
48                                 // Open the socket
49                                 // Passing null username, generates a login popup on Opera (#2865)
50                                 if ( s.username ) {
51                                         xhr.open(s.type, s.url, s.async, s.username, s.password);
52                                 } else {
53                                         xhr.open(s.type, s.url, s.async);
54                                 }
55
56                                 // Requested-With header
57                                 // Not set for crossDomain requests with no content
58                                 // (see why at http://trac.dojotoolkit.org/ticket/9486)
59                                 // Won't change header if already provided
60                                 if ( ! ( s.crossDomain && ! s.hasContent ) && ! headers["x-requested-with"] ) {
61                                         headers["x-requested-with"] = "XMLHttpRequest";
62                                 }
63
64                                 // Need an extra try/catch for cross domain requests in Firefox 3
65                                 try {
66
67                                         jQuery.each(headers, function(key,value) {
68                                                 xhr.setRequestHeader(key,value);
69                                         });
70
71                                 } catch(_) {}
72
73                                 // Do send the request
74                                 try {
75                                         xhr.send( ( s.hasContent && s.data ) || null );
76                                 } catch(e) {
77                                         // Store back into pool
78                                         xhrPool.push( xhr );
79                                         complete(0, "error", "" + e);
80                                         return;
81                                 }
82
83                                 // Listener
84                                 callback = function( isAbort ) {
85
86                                         // Was never called and is aborted or complete
87                                         if ( callback && ( isAbort || xhr.readyState === 4 ) ) {
88
89                                                 // Only called once
90                                                 callback = 0;
91
92                                                 // Do not keep as active anymore
93                                                 // and store back into pool
94                                                 if (handle) {
95                                                         xhr.onreadystatechange = jQuery.noop;
96                                                         delete xhrs[ handle ];
97                                                         handle = undefined;
98                                                         xhrPool.push( xhr );
99                                                 }
100
101                                                 // If it's an abort
102                                                 if ( isAbort ) {
103
104                                                         // Abort it manually if needed
105                                                         if ( xhr.readyState !== 4 ) {
106                                                                 xhr.abort();
107                                                         }
108                                                 } else {
109
110                                                         // Get info
111                                                         var status = xhr.status,
112                                                                 statusText,
113                                                                 response,
114                                                                 responseHeaders = xhr.getAllResponseHeaders();
115
116                                                         try { // Firefox throws an exception when accessing statusText for faulty cross-domain requests
117
118                                                                 statusText = xhr.statusText;
119
120                                                         } catch( e ) {
121
122                                                                 statusText = ""; // We normalize with Webkit giving an empty statusText
123
124                                                         }
125
126                                                         // Filter status for non standard behaviours
127                                                         // (so many they seem to be the actual "standard")
128                                                         status =
129                                                                 // Opera returns 0 when it should be 304
130                                                                 // Webkit returns 0 for failing cross-domain no matter the real status
131                                                                 status === 0 ?
132                                                                         (
133                                                                                 ! s.crossDomain || statusText ? // Webkit, Firefox: filter out faulty cross-domain requests
134                                                                                 (
135                                                                                         responseHeaders ? // Opera: filter out real aborts #6060
136                                                                                         304
137                                                                                         :
138                                                                                         0
139                                                                                 )
140                                                                                 :
141                                                                                 302 // We assume 302 but could be anything cross-domain related
142                                                                         )
143                                                                         :
144                                                                         (
145                                                                                 status == 1223 ?        // IE sometimes returns 1223 when it should be 204 (see #1450)
146                                                                                         204
147                                                                                         :
148                                                                                         status
149                                                                         );
150
151                                                         // Guess response & update dataType accordingly
152                                                         response =
153                                                                 determineDataType(
154                                                                         s,
155                                                                         xhr.getResponseHeader("content-type"),
156                                                                         xhr.responseText,
157                                                                         xhr.responseXML );
158
159                                                         // Call complete
160                                                         complete(status,statusText,response,responseHeaders);
161                                                 }
162                                         }
163                                 };
164
165                                 // if we're in sync mode
166                                 // or it's in cache and has been retrieved directly (IE6 & IE7)
167                                 // we need to manually fire the callback
168                                 if ( ! s.async || xhr.readyState === 4 ) {
169
170                                         callback();
171
172                                 } else {
173
174                                         // Listener is externalized to handle abort on unload
175                                         handle = xhrPollingId++;
176                                         xhrs[ handle ] = xhr;
177                                         xhr.onreadystatechange = function() {
178                                                 callback();
179                                         };
180                                 }
181                         },
182
183                         abort: function() {
184                                 if ( callback ) {
185                                         callback(1);
186                                 }
187                         }
188                 };
189         }
190 });
191
192 })( jQuery );