Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / components / learnboost-engine.io-client / lib / transports / polling-xhr.js
1 /**
2  * Module requirements.
3  */
4
5 var Polling = require('./polling')
6   , util = require('../util')
7   , Emitter = require('../emitter')
8   , debug = require('debug')('engine.io-client:polling-xhr');
9
10 /**
11  * Module exports.
12  */
13
14 module.exports = XHR;
15 module.exports.Request = Request;
16
17 /**
18  * Global reference.
19  */
20
21 var global = 'undefined' != typeof window ? window : global;
22
23 /**
24  * Obfuscated key for Blue Coat.
25  */
26
27 var xobject = global[['Active'].concat('Object').join('X')];
28
29 /**
30  * Empty function
31  */
32
33 function empty(){}
34
35 /**
36  * XHR Polling constructor.
37  *
38  * @param {Object} opts
39  * @api public
40  */
41
42 function XHR(opts){
43   Polling.call(this, opts);
44
45   if (global.location) {
46     this.xd = opts.host != global.location.hostname ||
47       global.location.port != opts.port;
48   }
49 };
50
51 /**
52  * Inherits from Polling.
53  */
54
55 util.inherits(XHR, Polling);
56
57 /**
58  * Opens the socket
59  *
60  * @api private
61  */
62
63 XHR.prototype.doOpen = function(){
64   var self = this;
65   util.defer(function(){
66     Polling.prototype.doOpen.call(self);
67   });
68 };
69
70 /**
71  * Creates a request.
72  *
73  * @param {String} method
74  * @api private
75  */
76
77 XHR.prototype.request = function(opts){
78   opts = opts || {};
79   opts.uri = this.uri();
80   opts.xd = this.xd;
81   return new Request(opts);
82 };
83
84 /**
85  * Sends data.
86  *
87  * @param {String} data to send.
88  * @param {Function} called upon flush.
89  * @api private
90  */
91
92 XHR.prototype.doWrite = function(data, fn){
93   var req = this.request({ method: 'POST', data: data });
94   var self = this;
95   req.on('success', fn);
96   req.on('error', function(err){
97     self.onError('xhr post error', err);
98   });
99   this.sendXhr = req;
100 };
101
102 /**
103  * Starts a poll cycle.
104  *
105  * @api private
106  */
107
108 XHR.prototype.doPoll = function(){
109   debug('xhr poll');
110   var req = this.request();
111   var self = this;
112   req.on('data', function(data){
113     self.onData(data);
114   });
115   req.on('error', function(err){
116     self.onError('xhr poll error', err);
117   });
118   this.pollXhr = req;
119 };
120
121 /**
122  * Request constructor
123  *
124  * @param {Object} options
125  * @api public
126  */
127
128 function Request(opts){
129   this.method = opts.method || 'GET';
130   this.uri = opts.uri;
131   this.xd = !!opts.xd;
132   this.async = false !== opts.async;
133   this.data = undefined != opts.data ? opts.data : null;
134   this.create();
135 }
136
137 /**
138  * Mix in `Emitter`.
139  */
140
141 Emitter(Request.prototype);
142
143 /**
144  * Creates the XHR object and sends the request.
145  *
146  * @api private
147  */
148
149 Request.prototype.create = function(){
150   var xhr = this.xhr = util.request(this.xd);
151   var self = this;
152
153   xhr.open(this.method, this.uri, this.async);
154
155   if ('POST' == this.method) {
156     try {
157       if (xhr.setRequestHeader) {
158         // xmlhttprequest
159         xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
160       } else {
161         // xdomainrequest
162         xhr.contentType = 'text/plain';
163       }
164     } catch (e) {}
165   }
166
167   if (this.xd && global.XDomainRequest && xhr instanceof XDomainRequest) {
168     xhr.onerror = function(e){
169       self.onError(e);
170     };
171     xhr.onload = function(){
172       self.onData(xhr.responseText);
173     };
174     xhr.onprogress = empty;
175   } else {
176     // ie6 check
177     if ('withCredentials' in xhr) {
178       xhr.withCredentials = true;
179     }
180
181     xhr.onreadystatechange = function(){
182       var data;
183
184       try {
185         if (4 != xhr.readyState) return;
186         if (200 == xhr.status || 1223 == xhr.status) {
187           data = xhr.responseText;
188         } else {
189           self.onError(xhr.status);
190         }
191       } catch (e) {
192         self.onError(e);
193       }
194
195       if (undefined !== data) {
196         self.onData(data);
197       }
198     };
199   }
200
201   debug('sending xhr with url %s | data %s', this.uri, this.data);
202   xhr.send(this.data);
203
204   if (xobject) {
205     this.index = Request.requestsCount++;
206     Request.requests[this.index] = this;
207   }
208 };
209
210 /**
211  * Called upon successful response.
212  *
213  * @api private
214  */
215
216 Request.prototype.onSuccess = function(){
217   this.emit('success');
218   this.cleanup();
219 };
220
221 /**
222  * Called if we have data.
223  *
224  * @api private
225  */
226
227 Request.prototype.onData = function(data){
228   this.emit('data', data);
229   this.onSuccess();
230 };
231
232 /**
233  * Called upon error.
234  *
235  * @api private
236  */
237
238 Request.prototype.onError = function(err){
239   this.emit('error', err);
240   this.cleanup();
241 };
242
243 /**
244  * Cleans up house.
245  *
246  * @api private
247  */
248
249 Request.prototype.cleanup = function(){
250   // xmlhttprequest
251   this.xhr.onreadystatechange = empty;
252
253   // xdomainrequest
254   this.xhr.onload = this.xhr.onerror = empty;
255
256   try {
257     this.xhr.abort();
258   } catch(e) {}
259
260   if (xobject) {
261     delete Request.requests[this.index];
262   }
263
264   this.xhr = null;
265 };
266
267 /**
268  * Aborts the request.
269  *
270  * @api public
271  */
272
273 Request.prototype.abort = function(){
274   this.cleanup();
275 };
276
277 if (xobject) {
278   Request.requestsCount = 0;
279   Request.requests = {};
280
281   global.attachEvent('onunload', function(){
282     for (var i in Request.requests) {
283       if (Request.requests.hasOwnProperty(i)) {
284         Request.requests[i].abort();
285       }
286     }
287   });
288 }