You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

7 years ago
  1. /**
  2. * Module dependencies.
  3. */
  4. var Polling = require('./polling');
  5. var qs = require('querystring');
  6. var rDoubleSlashes = /\\\\n/g;
  7. var rSlashes = /(\\)?\\n/g;
  8. var util = require('util');
  9. /**
  10. * Module exports.
  11. */
  12. module.exports = JSONP;
  13. /**
  14. * JSON-P polling transport.
  15. *
  16. * @api public
  17. */
  18. function JSONP (req) {
  19. Polling.call(this, req);
  20. this.head = '___eio[' + (req._query.j || '').replace(/[^0-9]/g, '') + '](';
  21. this.foot = ');';
  22. }
  23. /**
  24. * Inherits from Polling.
  25. */
  26. util.inherits(JSONP, Polling);
  27. /**
  28. * Handles incoming data.
  29. * Due to a bug in \n handling by browsers, we expect a escaped string.
  30. *
  31. * @api private
  32. */
  33. JSONP.prototype.onData = function (data) {
  34. // we leverage the qs module so that we get built-in DoS protection
  35. // and the fast alternative to decodeURIComponent
  36. data = qs.parse(data).d;
  37. if ('string' === typeof data) {
  38. // client will send already escaped newlines as \\\\n and newlines as \\n
  39. // \\n must be replaced with \n and \\\\n with \\n
  40. data = data.replace(rSlashes, function (match, slashes) {
  41. return slashes ? match : '\n';
  42. });
  43. Polling.prototype.onData.call(this, data.replace(rDoubleSlashes, '\\n'));
  44. }
  45. };
  46. /**
  47. * Performs the write.
  48. *
  49. * @api private
  50. */
  51. JSONP.prototype.doWrite = function (data, options, callback) {
  52. // we must output valid javascript, not valid json
  53. // see: http://timelessrepo.com/json-isnt-a-javascript-subset
  54. var js = JSON.stringify(data)
  55. .replace(/\u2028/g, '\\u2028')
  56. .replace(/\u2029/g, '\\u2029');
  57. // prepare response
  58. data = this.head + js + this.foot;
  59. Polling.prototype.doWrite.call(this, data, options, callback);
  60. };