Skip to content

Commit

Permalink
Changed version label from h2 to http2 for more verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
parthverma1 committed Jun 20, 2024
1 parent fffca4f commit 0d9d4bc
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 57 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function initParams (uri, options, callback) {
params.callback = callback || params.callback

// Disable http/2 when using custom agents that don't handle different versions separately
if (params.agents && !(params.agents.http1 || params.agents.auto || params.agents.h2)) {
if (params.agents && !(params.agents.http1 || params.agents.auto || params.agents.http2)) {
params.protocolVersion = 'http1'
}

Expand Down
8 changes: 6 additions & 2 deletions lib/autohttp/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AutoHttp2Agent extends EventEmitter {
connection && connection.socket && socketCb(connection.socket)
return
}
cb(null, 'h2', connection)
cb(null, 'http2', connection)
socketCb(connection.socket)
return
}
Expand Down Expand Up @@ -138,7 +138,7 @@ class AutoHttp2Agent extends EventEmitter {
newOptions,
socket
)
cb(null, 'h2', connection)
cb(null, 'http2', connection)
} catch (e) {
cb(e)
}
Expand Down Expand Up @@ -173,6 +173,10 @@ class AutoHttp2Agent extends EventEmitter {
})
}

/*
* This function has been borrowed from Node.js HTTPS Agent implementation
* Ref: v20.15.0 https://github.com/nodejs/node/blob/6bf148e12b00a3ec596f4c123ec35445a48ab209/lib/https.js
*/
getName (options) {
let name = options.host || 'localhost'

Expand Down
12 changes: 6 additions & 6 deletions lib/autohttp/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ class MultiProtocolRequest extends EventEmitter {

const agent = options.agent || globalAgent
// Request agent to perform alpn and return either an http agent or https agent
// Pass the request to the agent, the agent then calls the callback with http or h2 argument based on the result of alpn negotiation
// Pass the request to the agent, the agent then calls the callback with http or http2 argument based on the result
// of alpn negotiation
agent.createConnection(this, options, (err, proto, req) => {
if (err) {
this.emit('error', err)
return
}
if (proto === 'h2') {
if (proto === 'http2') {
this.onHttp2(req)
}
if (proto === 'http1') {
Expand Down Expand Up @@ -115,12 +116,11 @@ class MultiProtocolRequest extends EventEmitter {
}

get _header () {
if (this._req && this._req._header) {
return this._req._header
}
return new Promise((resolve, reject) => {
const action = () => resolve(this._req._header)
if (this._req) {
action()
return
}
this[kJobs].push(action)
})
}
Expand Down
21 changes: 13 additions & 8 deletions lib/autohttp/utils/headers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const {constants} = require('http2')
/*
* The following code has been borrowed from the Node.js project
* v20.15.0 /Users/[email protected]/node/lib/internal/http2/compat.js
*/
const {constants = {}} = require('http2')

const kValidPseudoHeaders = new Set([
constants.HTTP2_HEADER_STATUS,
constants.HTTP2_HEADER_METHOD,
constants.HTTP2_HEADER_AUTHORITY,
constants.HTTP2_HEADER_SCHEME,
constants.HTTP2_HEADER_PATH
])

function assertValidPseudoHeader (header) {
const kValidPseudoHeaders = new Set([
constants.HTTP2_HEADER_STATUS,
constants.HTTP2_HEADER_METHOD,
constants.HTTP2_HEADER_AUTHORITY,
constants.HTTP2_HEADER_SCHEME,
constants.HTTP2_HEADER_PATH
])
if (!kValidPseudoHeaders.has(header)) {
throw new Error('Invalid PseudoHeader ' + header)
}
Expand Down
4 changes: 4 additions & 0 deletions lib/http2/http2Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class Http2Agent extends EventEmitter {
return connection
}

/*
* This function has been borrowed from Node.js HTTPS Agent implementation
* Ref: v20.15.0 https://github.com/nodejs/node/blob/6bf148e12b00a3ec596f4c123ec35445a48ab209/lib/https.js
*/
getName (options) {
let name = options.host || 'localhost'

Expand Down
24 changes: 12 additions & 12 deletions lib/http2/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Http2Request extends EventEmitter {

this._client.once('error', this.onError)
this.stream.on('response', (response) => {
this.emit('response', new ResponseProxy(response, this))
this.emit('response', new ResponseProxy(response, this.stream))
})

this.stream.on('end', () => {
Expand Down Expand Up @@ -153,18 +153,18 @@ function request (options) {
}

class ResponseProxy extends EventEmitter {
constructor (response, request) {
constructor (response, stream) {
super()
this.httpVersion = '2.0'
this.req = request
this.reqStream = stream
this.response = response
this.on = this.on.bind(this)
this.registerRequestListeners()
}

registerRequestListeners () {
this.req.stream.on('error', (e) => this.emit('error', e))
this.req.stream.on('close', () => {
this.reqStream.on('error', (e) => this.emit('error', e))
this.reqStream.on('close', () => {
this.emit('close')
})
}
Expand All @@ -177,13 +177,13 @@ class ResponseProxy extends EventEmitter {
// that forwards the data event to the response object.
// If there is no listener attached and we use the event forwarding pattern above, the data event will still be emitted
// but with no listeners attached to it, thus causing data loss.
this.req.stream.on('data', (chunk) => {
this.reqStream.on('data', (chunk) => {
this.emit('data', chunk)
})
}

if (eventName === 'end') {
this.req.stream.on('end', listener)
this.reqStream.on('end', listener)
}
return this
}
Expand All @@ -201,23 +201,23 @@ class ResponseProxy extends EventEmitter {
}

pause () {
this.req.stream.pause()
this.reqStream.pause()
}

resume () {
this.req.stream.resume()
this.reqStream.resume()
}

pipe (dest) {
this.req.stream.pipe(dest)
this.reqStream.pipe(dest)
}

setEncoding (encoding) {
this.req.stream.setEncoding(encoding)
this.reqStream.setEncoding(encoding)
}

destroy () {
this.req.stream.destroy()
this.reqStream.destroy()
}
}

Expand Down
2 changes: 1 addition & 1 deletion request.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ Request.prototype.init = function (options) {
}

var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
var defaultModules = {'http:': { h2: http, http1: http, auto: http }, 'https:': { http1: https, h2: http2, auto: autohttp2 }}
var defaultModules = {'http:': { http2: http, http1: http, auto: http }, 'https:': { http1: https, http2: http2, auto: autohttp2 }}
var httpModules = self.httpModules || {}

// If user defines httpModules, respect if they have different httpModules for different http versions, else use the tls specific http module
Expand Down
6 changes: 3 additions & 3 deletions tests/test-body-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function addTest (name, data) {
s.on('/' + name, data.resp)
data.uri = s.url + '/' + name
request(
{ ...data, protocolVersion: 'h2', strictSSL: false },
{ ...data, protocolVersion: 'http2', strictSSL: false },
function (err, resp, body) {
t.equal(err, null)
if (data.expectBody && Buffer.isBuffer(data.expectBody)) {
Expand Down Expand Up @@ -163,7 +163,7 @@ tape('testBinaryFile', function (t) {
uri: 'https://localhost:' + s.port,
method: 'POST',
strictSSL: false,
protocolVersion: 'h2',
protocolVersion: 'http2',
body: fs.createReadStream(path.join(__dirname, 'raw.file'))
},
function (err, res, body) {
Expand All @@ -190,7 +190,7 @@ tape('typed array', function (t) {
body: data,
encoding: null,
strictSSL: false,
protocolVersion: 'h2'
protocolVersion: 'http2'
},
function (err, res, body) {
t.error(err)
Expand Down
24 changes: 12 additions & 12 deletions tests/test-brotli-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ tape('transparently supports brotli decoding to callbacks', function (t) {
var options = {
url: s.url + '/foo',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request.get(options, function (err, res, body) {
Expand All @@ -104,7 +104,7 @@ tape('transparently supports brotli decoding to pipes', function (t) {
var options = {
url: s.url + '/foo',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
var chunks = []
Expand All @@ -130,7 +130,7 @@ tape(
url: s.url + '/foo',
headers: headers,
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request.get(options, function (err, res, body) {
Expand All @@ -147,7 +147,7 @@ tape('does not decode user-requested encoding by default', function (t) {
var options = {
url: s.url + '/foo',
headers: headers,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request.get(options, function (err, res, body) {
Expand All @@ -164,7 +164,7 @@ tape('does not decode brotli encoding when "gzip" option is set', function (t) {
url: s.url + '/foo',
headers: headers,
gzip: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request.get(options, function (err, res, body) {
Expand All @@ -182,7 +182,7 @@ tape('supports character encoding with brotli encoding', function (t) {
headers: headers,
brotli: true,
encoding: 'utf8',
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
var strings = []
Expand All @@ -205,7 +205,7 @@ tape('transparently supports brotli error to callbacks', function (t) {
var options = {
url: s.url + '/error',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request.get(options, function (err, res, body) {
Expand All @@ -220,7 +220,7 @@ tape('transparently supports brotli error to pipes', function (t) {
var options = {
url: s.url + '/error',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request
Expand All @@ -241,7 +241,7 @@ tape('pause when streaming from a brotli request object', function (t) {
var options = {
url: s.url + '/chunks',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
request.get(options, function (err, res, body) {
Expand All @@ -256,7 +256,7 @@ tape('pause before streaming from a brotli request object', function (t) {
var options = {
url: s.url + '/foo',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}
var r = request.get(options)
Expand All @@ -279,7 +279,7 @@ tape('do not try to pipe HEAD request responses', function (t) {
method: 'HEAD',
url: s.url + '/foo',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}

Expand All @@ -294,7 +294,7 @@ tape('do not try to pipe responses with no body', function (t) {
var options = {
url: s.url + '/foo',
brotli: true,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
}

Expand Down
6 changes: 3 additions & 3 deletions tests/test-cookies-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ tape('after server sends a cookie', function (t) {
method: 'GET',
url: validUrl,
jar: jar1,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
},
function (error, response, body) {
Expand All @@ -78,7 +78,7 @@ tape('after server sends a malformed cookie', function (t) {
method: 'GET',
url: malformedUrl,
jar: jar,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
},
function (error, response, body) {
Expand All @@ -100,7 +100,7 @@ tape('after server sends a cookie for a different domain', function (t) {
method: 'GET',
url: invalidUrl,
jar: jar2,
protocolVersion: 'h2',
protocolVersion: 'http2',
strictSSL: false
},
function (error, response, body) {
Expand Down
Loading

0 comments on commit 0d9d4bc

Please sign in to comment.