htmlfile.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <[email protected]>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module requirements.
  8. */
  9. var HTTPTransport = require('./http');
  10. /**
  11. * Export the constructor.
  12. */
  13. exports = module.exports = HTMLFile;
  14. /**
  15. * HTMLFile transport constructor.
  16. *
  17. * @api public
  18. */
  19. function HTMLFile (mng, data, req) {
  20. HTTPTransport.call(this, mng, data, req);
  21. };
  22. /**
  23. * Inherits from Transport.
  24. */
  25. HTMLFile.prototype.__proto__ = HTTPTransport.prototype;
  26. /**
  27. * Transport name
  28. *
  29. * @api public
  30. */
  31. HTMLFile.prototype.name = 'htmlfile';
  32. /**
  33. * Handles the request.
  34. *
  35. * @api private
  36. */
  37. HTMLFile.prototype.handleRequest = function (req) {
  38. HTTPTransport.prototype.handleRequest.call(this, req);
  39. if (req.method == 'GET') {
  40. req.res.writeHead(200, {
  41. 'Content-Type': 'text/html; charset=UTF-8'
  42. , 'Connection': 'keep-alive'
  43. , 'Transfer-Encoding': 'chunked'
  44. });
  45. req.res.write(
  46. '<html><body>'
  47. + '<script>var _ = function (msg) { parent.s._(msg, document); };</script>'
  48. + new Array(174).join(' ')
  49. );
  50. }
  51. };
  52. /**
  53. * Performs the write.
  54. *
  55. * @api private
  56. */
  57. HTMLFile.prototype.write = function (data) {
  58. // escape all forward slashes. see GH-1251
  59. data = '<script>_(' + JSON.stringify(data).replace(/\//g, '\\/') + ');</script>';
  60. if (this.response.write(data)) {
  61. this.drained = true;
  62. }
  63. this.log.debug(this.name + ' writing', data);
  64. };