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.

69 lines
1.2 KiB

7 years ago
  1. /**
  2. * Module dependencies.
  3. */
  4. var Polling = require('./polling');
  5. var util = require('util');
  6. /**
  7. * Module exports.
  8. */
  9. module.exports = XHR;
  10. /**
  11. * Ajax polling transport.
  12. *
  13. * @api public
  14. */
  15. function XHR (req) {
  16. Polling.call(this, req);
  17. }
  18. /**
  19. * Inherits from Polling.
  20. */
  21. util.inherits(XHR, Polling);
  22. /**
  23. * Overrides `onRequest` to handle `OPTIONS`..
  24. *
  25. * @param {http.IncomingMessage}
  26. * @api private
  27. */
  28. XHR.prototype.onRequest = function (req) {
  29. if ('OPTIONS' === req.method) {
  30. var res = req.res;
  31. var headers = this.headers(req);
  32. headers['Access-Control-Allow-Headers'] = 'Content-Type';
  33. res.writeHead(200, headers);
  34. res.end();
  35. } else {
  36. Polling.prototype.onRequest.call(this, req);
  37. }
  38. };
  39. /**
  40. * Returns headers for a response.
  41. *
  42. * @param {http.IncomingMessage} request
  43. * @param {Object} extra headers
  44. * @api private
  45. */
  46. XHR.prototype.headers = function (req, headers) {
  47. headers = headers || {};
  48. if (req.headers.origin) {
  49. headers['Access-Control-Allow-Credentials'] = 'true';
  50. headers['Access-Control-Allow-Origin'] = req.headers.origin;
  51. } else {
  52. headers['Access-Control-Allow-Origin'] = '*';
  53. }
  54. return Polling.prototype.headers.call(this, req, headers);
  55. };