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