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