Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io / lib / transports / jsonp-polling.js
1
2 /*!
3  * socket.io-node
4  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5  * MIT Licensed
6  */
7
8 /**
9  * Module requirements.
10  */
11
12 var HTTPPolling = require('./http-polling');
13 var jsonpolling_re = /^\d+$/
14
15 /**
16  * Export the constructor.
17  */
18
19 exports = module.exports = JSONPPolling;
20
21 /**
22  * JSON-P polling transport.
23  *
24  * @api public
25  */
26
27 function JSONPPolling (mng, data, req) {
28   HTTPPolling.call(this, mng, data, req);
29
30   this.head = 'io.j[0](';
31   this.foot = ');';
32
33   if (data.query.i && jsonpolling_re.test(data.query.i)) {
34     this.head = 'io.j[' + data.query.i + '](';
35   }
36 };
37
38 /**
39  * Inherits from Transport.
40  */
41
42 JSONPPolling.prototype.__proto__ = HTTPPolling.prototype;
43
44 /**
45  * Transport name
46  *
47  * @api public
48  */
49
50 JSONPPolling.prototype.name = 'jsonppolling';
51
52 /**
53  * Make sure POST are decoded.
54  */
55
56 JSONPPolling.prototype.postEncoded = true;
57
58 /**
59  * Handles incoming data.
60  * Due to a bug in \n handling by browsers, we expect a JSONified string.
61  *
62  * @api private
63  */
64
65 JSONPPolling.prototype.onData = function (data) {
66   try {
67     data = JSON.parse(data);
68   } catch (e) {
69     this.error('parse', 'reconnect');
70     return;
71   }
72
73   HTTPPolling.prototype.onData.call(this, data);
74 };
75
76 /**
77  * Performs the write.
78  *
79  * @api private
80  */
81
82 JSONPPolling.prototype.doWrite = function (data) {
83   HTTPPolling.prototype.doWrite.call(this);
84
85   var data = data === undefined
86       ? '' : this.head + JSON.stringify(data) + this.foot;
87
88   this.response.writeHead(200, {
89       'Content-Type': 'text/javascript; charset=UTF-8'
90     , 'Content-Length': Buffer.byteLength(data)
91     , 'Connection': 'Keep-Alive'
92     , 'X-XSS-Protection': '0'
93   });
94
95   this.response.write(data);
96   this.log.debug(this.name + ' writing', data);
97 };