|
24 | 24 | const {
|
25 | 25 | ObjectDefineProperty,
|
26 | 26 | ObjectSetPrototypeOf,
|
| 27 | + Symbol |
27 | 28 | } = primordials;
|
28 | 29 |
|
29 | 30 | const Stream = require('stream');
|
30 | 31 |
|
| 32 | +const kHeaders = Symbol('kHeaders'); |
| 33 | +const kHeadersCount = Symbol('kHeadersCount'); |
| 34 | +const kTrailers = Symbol('kTrailers'); |
| 35 | +const kTrailersCount = Symbol('kTrailersCount'); |
| 36 | + |
31 | 37 | function readStart(socket) {
|
32 | 38 | if (socket && !socket._paused && socket.readable)
|
33 | 39 | socket.resume();
|
@@ -58,9 +64,11 @@ function IncomingMessage(socket) {
|
58 | 64 | this.httpVersionMinor = null;
|
59 | 65 | this.httpVersion = null;
|
60 | 66 | this.complete = false;
|
61 |
| - this.headers = {}; |
| 67 | + this[kHeaders] = null; |
| 68 | + this[kHeadersCount] = 0; |
62 | 69 | this.rawHeaders = [];
|
63 |
| - this.trailers = {}; |
| 70 | + this[kTrailers] = null; |
| 71 | + this[kTrailersCount] = 0; |
64 | 72 | this.rawTrailers = [];
|
65 | 73 |
|
66 | 74 | this.aborted = false;
|
@@ -93,6 +101,44 @@ ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
|
93 | 101 | }
|
94 | 102 | });
|
95 | 103 |
|
| 104 | +ObjectDefineProperty(IncomingMessage.prototype, 'headers', { |
| 105 | + get: function() { |
| 106 | + if (!this[kHeaders]) { |
| 107 | + this[kHeaders] = {}; |
| 108 | + |
| 109 | + const src = this.rawHeaders; |
| 110 | + const dst = this[kHeaders]; |
| 111 | + |
| 112 | + for (let n = 0; n < this[kHeadersCount]; n += 2) { |
| 113 | + this._addHeaderLine(src[n + 0], src[n + 1], dst); |
| 114 | + } |
| 115 | + } |
| 116 | + return this[kHeaders]; |
| 117 | + }, |
| 118 | + set: function(val) { |
| 119 | + this[kHeaders] = val; |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +ObjectDefineProperty(IncomingMessage.prototype, 'trailers', { |
| 124 | + get: function() { |
| 125 | + if (!this[kTrailers]) { |
| 126 | + this[kTrailers] = {}; |
| 127 | + |
| 128 | + const src = this.rawTrailers; |
| 129 | + const dst = this[kTrailers]; |
| 130 | + |
| 131 | + for (let n = 0; n < this[kTrailersCount]; n += 2) { |
| 132 | + this._addHeaderLine(src[n + 0], src[n + 1], dst); |
| 133 | + } |
| 134 | + } |
| 135 | + return this[kTrailers]; |
| 136 | + }, |
| 137 | + set: function(val) { |
| 138 | + this[kTrailers] = val; |
| 139 | + } |
| 140 | +}); |
| 141 | + |
96 | 142 | IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
|
97 | 143 | if (callback)
|
98 | 144 | this.on('timeout', callback);
|
@@ -131,14 +177,18 @@ function _addHeaderLines(headers, n) {
|
131 | 177 | let dest;
|
132 | 178 | if (this.complete) {
|
133 | 179 | this.rawTrailers = headers;
|
134 |
| - dest = this.trailers; |
| 180 | + this[kTrailersCount] = n; |
| 181 | + dest = this[kTrailers]; |
135 | 182 | } else {
|
136 | 183 | this.rawHeaders = headers;
|
137 |
| - dest = this.headers; |
| 184 | + this[kHeadersCount] = n; |
| 185 | + dest = this[kHeaders]; |
138 | 186 | }
|
139 | 187 |
|
140 |
| - for (let i = 0; i < n; i += 2) { |
141 |
| - this._addHeaderLine(headers[i], headers[i + 1], dest); |
| 188 | + if (dest) { |
| 189 | + for (let i = 0; i < n; i += 2) { |
| 190 | + this._addHeaderLine(headers[i], headers[i + 1], dest); |
| 191 | + } |
142 | 192 | }
|
143 | 193 | }
|
144 | 194 | }
|
|
0 commit comments