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