http.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module requirements.
  8. */
  9. var Transport = require('../transport')
  10. , parser = require('../parser')
  11. , qs = require('querystring');
  12. /**
  13. * Export the constructor.
  14. */
  15. exports = module.exports = HTTPTransport;
  16. /**
  17. * HTTP interface constructor. For all non-websocket transports.
  18. *
  19. * @api public
  20. */
  21. function HTTPTransport (mng, data, req) {
  22. Transport.call(this, mng, data, req);
  23. };
  24. /**
  25. * Inherits from Transport.
  26. */
  27. HTTPTransport.prototype.__proto__ = Transport.prototype;
  28. /**
  29. * Handles a request.
  30. *
  31. * @api private
  32. */
  33. HTTPTransport.prototype.handleRequest = function (req) {
  34. // Always set the response in case an error is returned to the client
  35. this.response = req.res;
  36. if (req.method == 'POST') {
  37. var buffer = ''
  38. , res = req.res
  39. , origin = req.headers.origin
  40. , headers = { 'Content-Length': 1, 'Content-Type': 'text/plain; charset=UTF-8' }
  41. , self = this;
  42. req.on('data', function (data) {
  43. buffer += data;
  44. if (Buffer.byteLength(buffer) >= self.manager.get('destroy buffer size')) {
  45. buffer = '';
  46. req.connection.destroy();
  47. }
  48. });
  49. req.on('end', function () {
  50. res.writeHead(200, headers);
  51. res.end('1');
  52. self.onData(self.postEncoded ? qs.parse(buffer).d : buffer);
  53. });
  54. // prevent memory leaks for uncompleted requests
  55. req.on('close', function () {
  56. buffer = '';
  57. self.onClose();
  58. });
  59. if (origin) {
  60. // https://developer.mozilla.org/En/HTTP_Access_Control
  61. headers['Access-Control-Allow-Origin'] = origin;
  62. headers['Access-Control-Allow-Credentials'] = 'true';
  63. }
  64. } else {
  65. Transport.prototype.handleRequest.call(this, req);
  66. }
  67. };
  68. /**
  69. * Handles data payload.
  70. *
  71. * @api private
  72. */
  73. HTTPTransport.prototype.onData = function (data) {
  74. var messages = parser.decodePayload(data);
  75. this.log.debug(this.name + ' received data packet', data);
  76. for (var i = 0, l = messages.length; i < l; i++) {
  77. this.onMessage(messages[i]);
  78. }
  79. };
  80. /**
  81. * Closes the request-response cycle
  82. *
  83. * @api private
  84. */
  85. HTTPTransport.prototype.doClose = function () {
  86. this.response.end();
  87. };
  88. /**
  89. * Writes a payload of messages
  90. *
  91. * @api private
  92. */
  93. HTTPTransport.prototype.payload = function (msgs) {
  94. this.write(parser.encodePayload(msgs));
  95. };