Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Commit

Permalink
From Map() to basic {}.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Dec 6, 2016
1 parent 16e0c23 commit 839a3f2
Showing 1 changed file with 64 additions and 62 deletions.
126 changes: 64 additions & 62 deletions lib/internal/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ class Http2Incoming extends Readable {
super(initHttp2IncomingOptions(options));
if (!(stream instanceof Http2Stream))
throw new TypeError('stream argument must be an Http2Stream instance');
if (!(headers instanceof Map))
throw new TypeError('headers argument must be a Map');
if (typeof headers !== 'object')
throw new TypeError('headers argument must be an object');
this[kStream] = stream;
this[kHeaders] = headers;
this[kFinished] = false;
Expand Down Expand Up @@ -679,19 +679,19 @@ class Http2ServerRequest extends Http2Incoming {
}

get method() {
return this.headers.get(constants.HTTP2_HEADER_METHOD);
return this.headers[constants.HTTP2_HEADER_METHOD];
}

get authority() {
return this.headers.get(constants.HTTP2_HEADER_AUTHORITY);
return this.headers[constants.HTTP2_HEADER_AUTHORITY];
}

get scheme() {
return this.headers.get(constants.HTTP2_HEADER_SCHEME);
return this.headers[constants.HTTP2_HEADER_SCHEME];
}

get url() {
return this.headers.get(constants.HTTP2_HEADER_PATH);
return this.headers[constants.HTTP2_HEADER_PATH];
}
}

Expand Down Expand Up @@ -762,18 +762,20 @@ class Http2Outgoing extends Writable {
}

addHeaders(headers) {
if (!headers) return;
const keys = Object.keys(headers);
for (const key of keys)
this.setHeader(key, headers[key]);
var keys;
if (headers) {
keys = Object.keys(headers);
for (var i = 0; i < keys.length; i++)
this.setHeader(keys[i], headers[keys[i]]);
}
return this;
}

addTrailers(headers) {
if (!headers) return;
const keys = Object.keys(headers);
for (const key of keys)
this.setTrailer(key, headers[key]);
for (var i = 0; i < keys.length; i++)
this.setTrailer(keys[i], headers[keys[i]]);
return this;
}

Expand Down Expand Up @@ -920,44 +922,44 @@ class Http2PushResponse extends EventEmitter {
constructor(response) {
super();
this[kResponse] = response;
this[kHeaders] = new Map();
this.headers.set(constants.HTTP2_HEADER_METHOD, 'GET');
this.headers.set(constants.HTTP2_HEADER_AUTHORITY,
response.stream[kRequest].authority);
this.headers.set(constants.HTTP2_HEADER_SCHEME,
response.stream[kRequest].scheme);
this[kHeaders] = Object.create(null);
this.headers[constants.HTTP2_HEADER_METHOD] = 'GET';
this.headers[constants.HTTP2_HEADER_AUTHORITY] =
response.stream[kRequest].authority;
this.headers[constants.HTTP2_HEADER_SCHEME] =
response.stream[kRequest].scheme;
}

get path() {
return this.headers.get(constants.HTTP2_HEADER_PATH);
return this.headers[constants.HTTP2_HEADER_PATH];
}

set path(val) {
this.headers.set(constants.HTTP2_HEADER_PATH, String(val));
this.headers[constants.HTTP2_HEADER_PATH] = String(val);
}

get method() {
return this.headers.get(constants.HTTP2_HEADER_METHOD);
return this.headers[constants.HTTP2_HEADER_METHOD];
}

set method(val) {
this.headers.set(constants.HTTP2_HEADER_METHOD, String(val));
this.headers[constants.HTTP2_HEADER_METHOD] = String(val);
}

get authority() {
return this.headers.get(constants.HTTP2_HEADER_AUTHORITY);
return this.headers[constants.HTTP2_HEADER_AUTHORITY];
}

set authority(val) {
this.headers.set(constants.HTTP2_HEADER_AUTHORITY, String(val));
this.headers[constants.HTTP2_HEADER_AUTHORITY] = String(val);
}

get scheme() {
return this.headers.get(constants.HTTP2_HEADER_SCHEME);
return this.headers[constants.HTTP2_HEADER_SCHEME];
}

set scheme(val) {
this.headers.set(constants.HTTP2_HEADER_SCHEME, String(val));
this.headers[constants.HTTP2_HEADER_SCHEME] = String(val);
}

get headers() {
Expand Down Expand Up @@ -1014,29 +1016,30 @@ function isIllegalConnectionSpecificHeader(name, value) {
}
}

// Converts a ES6 map into an http2.Http2Headers object.
// Converts an object into an http2.Http2Headers object.
// The Http2Headers object maintains an internal array
// of nghttp2_nv objects that contain a copy of the
// header value pairs as an std::vector. To avoid
// that vector having to copy and reallocate, we count
// the number of expected items up front (it's less
// expensive to count than it is to reallocate).
function mapToHeaders(map) {
var size = map.size;
for (const v of map) {
if (Array.isArray(v[1])) {
size += v[1].length - 1;
const keys = Object.keys(map)
var size = keys.length;
for (var i = 0; i < keys.length; i++) {
if (Array.isArray(keys[i])) {
size += keys[i].length - 1;
}
}
const ret = new http2.Http2Headers(size);
if (!(map instanceof Map))
return ret;
for (const v of map) {
const key = String(v[0]);
const value = v[1];

for (i = 0; i < keys.length; i++) {
const key = keys[i]
const value = map[key];
if (Array.isArray(value) && value.length > 0) {
for (const item of value)
ret.add(key, String(item));
for (var k = 0; k < value.length; k++) {
ret.add(key, String(value[k]));
}
} else {
ret.add(key, String(value));
}
Expand Down Expand Up @@ -1165,12 +1168,12 @@ function sessionOnHeaderComplete(stream, flags, headers, category) {
if (finished)
request[kFinished] = true;

if (headers.has('expect')) {
if (headers.expect) {
// If there is an expect header that contains 100-continue,
// and the server has a listener for the checkContinue event,
// emit the checkContinue event instead of the request event.
// This behavior matches the current http/1 API.
if (/^100-continue$/i.test(headers.get('expect'))) {
if (/^100-continue$/i.test(headers.expect)) {
if (server.listenerCount('checkContinue') > 0) {
request[kInFlight] = true;
server.emit('checkContinue', request, response);
Expand Down Expand Up @@ -1552,45 +1555,44 @@ class Http2ClientRequest extends Http2Outgoing {
var authority = options.hostname;
if (options.port)
authority += `:${options.port}`;
const headers = this[kHeaders] = new Map();
const headers = this[kHeaders] = Object.create(null);

headers.set(constants.HTTP2_HEADER_SCHEME,
options.protocol.slice(0, options.protocol.length - 1));
headers.set(constants.HTTP2_HEADER_METHOD, options.method);
headers.set(constants.HTTP2_HEADER_AUTHORITY, authority);
headers.set(constants.HTTP2_HEADER_PATH, options.pathname);
headers[constants.HTTP2_HEADER_SCHEME] = options.protocol.slice(0, options.protocol.length - 1);
headers[constants.HTTP2_HEADER_METHOD] = options.method;
headers[constants.HTTP2_HEADER_AUTHORITY] = authority;
headers[constants.HTTP2_HEADER_PATH] = options.pathname;

if (typeof callback === 'function')
this.once('response', callback);
}

setHeader(name, value) {
name = String(name).toLowerCase().trim();
if (this[kHeaders].has(name)) {
const existing = this[kHeaders].get(name);
const existing = this[kHeaders][name];
if (existing) {
if (Array.isArray(existing)) {
existing.push(String(value));
} else {
this[kHeaders].set(name, [existing, value]);
this[kHeaders][name] = [existing, value];
}
} else {
this[kHeaders].set(name, value);
this[kHeaders][name] = value;
}
}

setTrailer(name, value) {
if (!this[kTrailers])
this[kTrailers] = new Map();
this[kTrailers] = Object.create(null);
name = String(name).toLowerCase().trim();
if (this[kTrailers].has(name)) {
const existing = this[kTrailers].get(name);
const existing = this[kTrailers][name];
if (existing) {
if (Array.isArray(existing)) {
existing.push(String(value));
} else {
this[kTrailers].set(name, [existing, value]);
this[kTrailers][name] = [existing, value];
}
} else {
this[kTrailers].set(name, value);
this[kTrailers][name] = value;
}
}

Expand All @@ -1613,13 +1615,13 @@ class Http2ClientRequest extends Http2Outgoing {

function addTrailers () {
const request = this[kRequest];
if (request[kTrailers] instanceof Map) {
for (const v of request[kTrailers]) {
const key = String(v[0]);
const value = v[1];
if (request[kTrailers]) {
// key is coerced on a string on set
for (var key in request[kTrailers]) {
const value = request[kTrailers][key];
if (Array.isArray(value) && value.length > 0) {
for (const item of value)
this.addTrailer(key, String(item));
for (var i = 0; i < value.length; i++)
this.addTrailer(key, String(value[i]));
} else {
this.addTrailer(key, String(value));
}
Expand All @@ -1633,7 +1635,7 @@ class Http2ClientResponse extends Http2Incoming {
}

get status() {
return this.headers.get(constants.HTTP2_HEADER_STATUS) | 0;
return this.headers[constants.HTTP2_HEADER_STATUS] | 0;
}
}

Expand Down

0 comments on commit 839a3f2

Please sign in to comment.