1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | module.exports = Headers;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function Headers(headers) {
|
17 |
|
18 | var self = this;
|
19 | this._headers = {};
|
20 |
|
21 |
|
22 | if (headers instanceof Headers) {
|
23 | headers = headers.raw();
|
24 | }
|
25 |
|
26 |
|
27 | for (var prop in headers) {
|
28 | if (!headers.hasOwnProperty(prop)) {
|
29 | continue;
|
30 | }
|
31 |
|
32 | if (typeof headers[prop] === 'string') {
|
33 | this.set(prop, headers[prop]);
|
34 |
|
35 | } else if (typeof headers[prop] === 'number' && !isNaN(headers[prop])) {
|
36 | this.set(prop, headers[prop].toString());
|
37 |
|
38 | } else if (headers[prop] instanceof Array) {
|
39 | headers[prop].forEach(function(item) {
|
40 | self.append(prop, item.toString());
|
41 | });
|
42 | }
|
43 | }
|
44 |
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | Headers.prototype.get = function(name) {
|
54 | var list = this._headers[name.toLowerCase()];
|
55 | return list ? list[0] : null;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Headers.prototype.getAll = function(name) {
|
65 | if (!this.has(name)) {
|
66 | return [];
|
67 | }
|
68 |
|
69 | return this._headers[name.toLowerCase()];
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | Headers.prototype.set = function(name, value) {
|
80 | this._headers[name.toLowerCase()] = [value];
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | Headers.prototype.append = function(name, value) {
|
91 | if (!this.has(name)) {
|
92 | this.set(name, value);
|
93 | return;
|
94 | }
|
95 |
|
96 | this._headers[name.toLowerCase()].push(value);
|
97 | };
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | Headers.prototype.has = function(name) {
|
106 | return this._headers.hasOwnProperty(name.toLowerCase());
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | Headers.prototype['delete'] = function(name) {
|
116 | delete this._headers[name.toLowerCase()];
|
117 | };
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | Headers.prototype.raw = function() {
|
125 | return this._headers;
|
126 | };
|