Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / transports / xhr.js
1
2 /**
3  * socket.io
4  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5  * MIT Licensed
6  */
7
8 (function (exports, io, global) {
9
10   /**
11    * Expose constructor.
12    *
13    * @api public
14    */
15
16   exports.XHR = XHR;
17
18   /**
19    * XHR constructor
20    *
21    * @costructor
22    * @api public
23    */
24
25   function XHR (socket) {
26     if (!socket) return;
27
28     io.Transport.apply(this, arguments);
29     this.sendBuffer = [];
30   };
31
32   /**
33    * Inherits from Transport.
34    */
35
36   io.util.inherit(XHR, io.Transport);
37
38   /**
39    * Establish a connection
40    *
41    * @returns {Transport}
42    * @api public
43    */
44
45   XHR.prototype.open = function () {
46     this.socket.setBuffer(false);
47     this.onOpen();
48     this.get();
49
50     // we need to make sure the request succeeds since we have no indication
51     // whether the request opened or not until it succeeded.
52     this.setCloseTimeout();
53
54     return this;
55   };
56
57   /**
58    * Check if we need to send data to the Socket.IO server, if we have data in our
59    * buffer we encode it and forward it to the `post` method.
60    *
61    * @api private
62    */
63
64   XHR.prototype.payload = function (payload) {
65     var msgs = [];
66
67     for (var i = 0, l = payload.length; i < l; i++) {
68       msgs.push(io.parser.encodePacket(payload[i]));
69     }
70
71     this.send(io.parser.encodePayload(msgs));
72   };
73
74   /**
75    * Send data to the Socket.IO server.
76    *
77    * @param data The message
78    * @returns {Transport}
79    * @api public
80    */
81
82   XHR.prototype.send = function (data) {
83     this.post(data);
84     return this;
85   };
86
87   /**
88    * Posts a encoded message to the Socket.IO server.
89    *
90    * @param {String} data A encoded message.
91    * @api private
92    */
93
94   function empty () { };
95
96   XHR.prototype.post = function (data) {
97     var self = this;
98     this.socket.setBuffer(true);
99
100     function stateChange () {
101       if (this.readyState == 4) {
102         this.onreadystatechange = empty;
103         self.posting = false;
104
105         if (this.status == 200){
106           self.socket.setBuffer(false);
107         } else {
108           self.onClose();
109         }
110       }
111     }
112
113     function onload () {
114       this.onload = empty;
115       self.socket.setBuffer(false);
116     };
117
118     this.sendXHR = this.request('POST');
119
120     if (global.XDomainRequest && this.sendXHR instanceof XDomainRequest) {
121       this.sendXHR.onload = this.sendXHR.onerror = onload;
122     } else {
123       this.sendXHR.onreadystatechange = stateChange;
124     }
125
126     this.sendXHR.send(data);
127   };
128
129   /**
130    * Disconnects the established `XHR` connection.
131    *
132    * @returns {Transport}
133    * @api public
134    */
135
136   XHR.prototype.close = function () {
137     this.onClose();
138     return this;
139   };
140
141   /**
142    * Generates a configured XHR request
143    *
144    * @param {String} url The url that needs to be requested.
145    * @param {String} method The method the request should use.
146    * @returns {XMLHttpRequest}
147    * @api private
148    */
149
150   XHR.prototype.request = function (method) {
151     var req = io.util.request(this.socket.isXDomain())
152       , query = io.util.query(this.socket.options.query, 't=' + +new Date);
153
154     req.open(method || 'GET', this.prepareUrl() + query, true);
155
156     if (method == 'POST') {
157       try {
158         if (req.setRequestHeader) {
159           req.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
160         } else {
161           // XDomainRequest
162           req.contentType = 'text/plain';
163         }
164       } catch (e) {}
165     }
166
167     return req;
168   };
169
170   /**
171    * Returns the scheme to use for the transport URLs.
172    *
173    * @api private
174    */
175
176   XHR.prototype.scheme = function () {
177     return this.socket.options.secure ? 'https' : 'http';
178   };
179
180   /**
181    * Check if the XHR transports are supported
182    *
183    * @param {Boolean} xdomain Check if we support cross domain requests.
184    * @returns {Boolean}
185    * @api public
186    */
187
188   XHR.check = function (socket, xdomain) {
189     try {
190       var request = io.util.request(xdomain),
191           usesXDomReq = (global.XDomainRequest && request instanceof XDomainRequest),
192           socketProtocol = (socket && socket.options && socket.options.secure ? 'https:' : 'http:'),
193           isXProtocol = (global.location && socketProtocol != global.location.protocol);
194       if (request && !(usesXDomReq && isXProtocol)) {
195         return true;
196       }
197     } catch(e) {}
198
199     return false;
200   };
201
202   /**
203    * Check if the XHR transport supports cross domain requests.
204    *
205    * @returns {Boolean}
206    * @api public
207    */
208
209   XHR.xdomainCheck = function (socket) {
210     return XHR.check(socket, true);
211   };
212
213 })(
214     'undefined' != typeof io ? io.Transport : module.exports
215   , 'undefined' != typeof io ? io : module.parent.exports
216   , this
217 );