Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function Caseless (dict) {
  2. this.dict = dict || {}
  3. }
  4. Caseless.prototype.set = function (name, value, clobber) {
  5. if (typeof name === 'object') {
  6. for (var i in name) {
  7. this.set(i, name[i], value)
  8. }
  9. } else {
  10. if (typeof clobber === 'undefined') clobber = true
  11. var has = this.has(name)
  12. if (!clobber && has) this.dict[has] = this.dict[has] + ',' + value
  13. else this.dict[has || name] = value
  14. return has
  15. }
  16. }
  17. Caseless.prototype.has = function (name) {
  18. var keys = Object.keys(this.dict)
  19. , name = name.toLowerCase()
  20. ;
  21. for (var i=0;i<keys.length;i++) {
  22. if (keys[i].toLowerCase() === name) return keys[i]
  23. }
  24. return false
  25. }
  26. Caseless.prototype.get = function (name) {
  27. name = name.toLowerCase()
  28. var result, _key
  29. var headers = this.dict
  30. Object.keys(headers).forEach(function (key) {
  31. _key = key.toLowerCase()
  32. if (name === _key) result = headers[key]
  33. })
  34. return result
  35. }
  36. Caseless.prototype.swap = function (name) {
  37. var has = this.has(name)
  38. if (has === name) return
  39. if (!has) throw new Error('There is no header than matches "'+name+'"')
  40. this.dict[name] = this.dict[has]
  41. delete this.dict[has]
  42. }
  43. Caseless.prototype.del = function (name) {
  44. var has = this.has(name)
  45. return delete this.dict[has || name]
  46. }
  47. module.exports = function (dict) {return new Caseless(dict)}
  48. module.exports.httpify = function (resp, headers) {
  49. var c = new Caseless(headers)
  50. resp.setHeader = function (key, value, clobber) {
  51. if (typeof value === 'undefined') return
  52. return c.set(key, value, clobber)
  53. }
  54. resp.hasHeader = function (key) {
  55. return c.has(key)
  56. }
  57. resp.getHeader = function (key) {
  58. return c.get(key)
  59. }
  60. resp.removeHeader = function (key) {
  61. return c.del(key)
  62. }
  63. resp.headers = c.dict
  64. return c
  65. }