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.

35 lines
815 B

4 years ago
  1. 'use strict';
  2. var Headers = function() {
  3. this.clear();
  4. };
  5. Headers.prototype.ALLOWED_DUPLICATES = ['set-cookie', 'set-cookie2', 'warning', 'www-authenticate'];
  6. Headers.prototype.clear = function() {
  7. this._sent = {};
  8. this._lines = [];
  9. };
  10. Headers.prototype.set = function(name, value) {
  11. if (value === undefined) return;
  12. name = this._strip(name);
  13. value = this._strip(value);
  14. var key = name.toLowerCase();
  15. if (!this._sent.hasOwnProperty(key) || this.ALLOWED_DUPLICATES.indexOf(key) >= 0) {
  16. this._sent[key] = true;
  17. this._lines.push(name + ': ' + value + '\r\n');
  18. }
  19. };
  20. Headers.prototype.toString = function() {
  21. return this._lines.join('');
  22. };
  23. Headers.prototype._strip = function(string) {
  24. return string.toString().replace(/^ */, '').replace(/ *$/, '');
  25. };
  26. module.exports = Headers;