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.

79 lines
1.6 KiB

  1. /*!
  2. * Connect
  3. * Copyright(c) 2011 TJ Holowaychuk
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var http = require('http')
  10. , res = http.ServerResponse.prototype
  11. , setHeader = res.setHeader
  12. , _renderHeaders = res._renderHeaders
  13. , writeHead = res.writeHead;
  14. // apply only once
  15. if (!res._hasConnectPatch) {
  16. /**
  17. * Provide a public "header sent" flag
  18. * until node does.
  19. *
  20. * @return {Boolean}
  21. * @api public
  22. */
  23. res.__defineGetter__('headerSent', function(){
  24. return this._header;
  25. });
  26. /**
  27. * Set header `field` to `val`, special-casing
  28. * the `Set-Cookie` field for multiple support.
  29. *
  30. * @param {String} field
  31. * @param {String} val
  32. * @api public
  33. */
  34. res.setHeader = function(field, val){
  35. var key = field.toLowerCase()
  36. , prev;
  37. // special-case Set-Cookie
  38. if (this._headers && 'set-cookie' == key) {
  39. if (prev = this.getHeader(field)) {
  40. val = Array.isArray(prev)
  41. ? prev.concat(val)
  42. : [prev, val];
  43. }
  44. // charset
  45. } else if ('content-type' == key && this.charset) {
  46. val += '; charset=' + this.charset;
  47. }
  48. return setHeader.call(this, field, val);
  49. };
  50. /**
  51. * Proxy to emit "header" event.
  52. */
  53. res._renderHeaders = function(){
  54. if (!this._emittedHeader) this.emit('header');
  55. this._emittedHeader = true;
  56. return _renderHeaders.call(this);
  57. };
  58. res.writeHead = function(){
  59. if (!this._emittedHeader) this.emit('header');
  60. this._emittedHeader = true;
  61. return writeHead.apply(this, arguments);
  62. };
  63. res._hasConnectPatch = true;
  64. }