|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const http2 = require('http2'); |
| 4 | +const Stream = require('stream'); |
| 5 | +const util = require('util'); |
| 6 | +const net = require('net'); |
| 7 | +const tls = require('tls'); |
| 8 | +const parse = require('url').parse; |
| 9 | + |
| 10 | +const { |
| 11 | + HTTP2_HEADER_PATH, |
| 12 | + HTTP2_HEADER_STATUS, |
| 13 | + HTTP2_HEADER_METHOD, |
| 14 | + HTTP2_HEADER_AUTHORITY, |
| 15 | + HTTP2_HEADER_HOST, |
| 16 | + HTTP2_HEADER_SET_COOKIE, |
| 17 | + NGHTTP2_CANCEL, |
| 18 | +} = http2.constants; |
| 19 | + |
| 20 | + |
| 21 | +function setProtocol(protocol) { |
| 22 | + return { |
| 23 | + request: function (options) { |
| 24 | + return new Request(protocol, options); |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function Request(protocol, options) { |
| 30 | + Stream.call(this); |
| 31 | + const defaultPort = protocol === 'https:' ? 443 : 80; |
| 32 | + const defaultHost = 'localhost' |
| 33 | + const port = options.port || defaultPort; |
| 34 | + const host = options.host || defaultHost; |
| 35 | + |
| 36 | + delete options.port |
| 37 | + delete options.host |
| 38 | + |
| 39 | + this.method = options.method.toUpperCase(); |
| 40 | + this.path = options.path; |
| 41 | + this.protocol = protocol; |
| 42 | + this.host = host; |
| 43 | + |
| 44 | + delete options.method |
| 45 | + delete options.path |
| 46 | + |
| 47 | + const sessionOptions = Object.assign({}, options); |
| 48 | + if (options.socketPath) { |
| 49 | + sessionOptions.socketPath = options.socketPath; |
| 50 | + sessionOptions.createConnection = this.createUnixConnection.bind(this); |
| 51 | + } |
| 52 | + |
| 53 | + this._headers = {}; |
| 54 | + |
| 55 | + const session = http2.connect(`${protocol}//${host}:${port}`, sessionOptions); |
| 56 | + this.setHeader('host', `${host}:${port}`) |
| 57 | + |
| 58 | + session.on('error', (err) => this.emit('error', err)); |
| 59 | + |
| 60 | + this.session = session; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Inherit from `Stream` (which inherits from `EventEmitter`). |
| 65 | + */ |
| 66 | +util.inherits(Request, Stream); |
| 67 | + |
| 68 | +Request.prototype.createUnixConnection = function (authority, options) { |
| 69 | + switch (this.protocol) { |
| 70 | + case 'http:': |
| 71 | + return net.connect(options.socketPath); |
| 72 | + case 'https:': |
| 73 | + options.ALPNProtocols = ['h2']; |
| 74 | + options.servername = this.host; |
| 75 | + options.allowHalfOpen = true; |
| 76 | + return tls.connect(options.socketPath, options); |
| 77 | + default: |
| 78 | + throw new Error('Unsupported protocol', this.protocol); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +Request.prototype.setNoDelay = function (bool) { |
| 83 | + // We can not use setNoDelay with HTTP/2. |
| 84 | + // Node 10 limits http2session.socket methods to ones safe to use with HTTP/2. |
| 85 | + // See also https://nodejs.org/api/http2.html#http2_http2session_socket |
| 86 | +} |
| 87 | + |
| 88 | +Request.prototype.getFrame = function () { |
| 89 | + if (this.frame) { |
| 90 | + return this.frame; |
| 91 | + } |
| 92 | + |
| 93 | + const method = { |
| 94 | + [HTTP2_HEADER_PATH]: this.path, |
| 95 | + [HTTP2_HEADER_METHOD]: this.method, |
| 96 | + } |
| 97 | + |
| 98 | + let headers = this.mapToHttp2Header(this._headers); |
| 99 | + |
| 100 | + headers = Object.assign(headers, method); |
| 101 | + |
| 102 | + const frame = this.session.request(headers); |
| 103 | + frame.once('response', (headers, flags) => { |
| 104 | + headers = this.mapToHttpHeader(headers); |
| 105 | + frame.headers = headers; |
| 106 | + frame.status = frame.statusCode = headers[HTTP2_HEADER_STATUS]; |
| 107 | + this.emit('response', frame); |
| 108 | + }); |
| 109 | + |
| 110 | + this._headerSent = true; |
| 111 | + |
| 112 | + frame.once('drain', () => this.emit('drain')); |
| 113 | + frame.on('error', (err) => this.emit('error', err)); |
| 114 | + frame.on('close', () => this.session.close()); |
| 115 | + |
| 116 | + this.frame = frame; |
| 117 | + return frame; |
| 118 | +} |
| 119 | + |
| 120 | +Request.prototype.mapToHttpHeader = function (headers) { |
| 121 | + const keys = Object.keys(headers); |
| 122 | + const http2Headers = {}; |
| 123 | + for (var i = 0; i < keys.length; i++) { |
| 124 | + let key = keys[i]; |
| 125 | + let value = headers[key]; |
| 126 | + key = key.toLowerCase(); |
| 127 | + switch (key) { |
| 128 | + case HTTP2_HEADER_SET_COOKIE: |
| 129 | + value = Array.isArray(value) ? value : [value]; |
| 130 | + break; |
| 131 | + default: |
| 132 | + break; |
| 133 | + } |
| 134 | + http2Headers[key] = value; |
| 135 | + } |
| 136 | + return http2Headers; |
| 137 | +} |
| 138 | + |
| 139 | +Request.prototype.mapToHttp2Header = function (headers) { |
| 140 | + const keys = Object.keys(headers); |
| 141 | + const http2Headers = {}; |
| 142 | + for (var i = 0; i < keys.length; i++) { |
| 143 | + let key = keys[i]; |
| 144 | + let value = headers[key]; |
| 145 | + key = key.toLowerCase(); |
| 146 | + switch (key) { |
| 147 | + case HTTP2_HEADER_HOST: |
| 148 | + key = HTTP2_HEADER_AUTHORITY; |
| 149 | + value = /^http\:\/\/|^https\:\/\//.test(value) ? parse(value).host : value; |
| 150 | + break; |
| 151 | + default: |
| 152 | + break; |
| 153 | + } |
| 154 | + http2Headers[key] = value; |
| 155 | + } |
| 156 | + return http2Headers; |
| 157 | +} |
| 158 | + |
| 159 | +Request.prototype.setHeader = function (name, value) { |
| 160 | + this._headers[name.toLowerCase()] = value; |
| 161 | +} |
| 162 | + |
| 163 | +Request.prototype.getHeader = function (name) { |
| 164 | + return this._headers[name.toLowerCase()]; |
| 165 | +} |
| 166 | + |
| 167 | +Request.prototype.write = function (data, encoding) { |
| 168 | + const frame = this.getFrame(); |
| 169 | + return frame.write(data, encoding); |
| 170 | +}; |
| 171 | + |
| 172 | +Request.prototype.pipe = function (stream, options) { |
| 173 | + const frame = this.getFrame(); |
| 174 | + return frame.pipe(stream, options); |
| 175 | +} |
| 176 | + |
| 177 | +Request.prototype.end = function (data) { |
| 178 | + const frame = this.getFrame(); |
| 179 | + frame.end(data); |
| 180 | +} |
| 181 | + |
| 182 | +Request.prototype.abort = function (data) { |
| 183 | + const frame = this.getFrame(); |
| 184 | + frame.close(NGHTTP2_CANCEL); |
| 185 | + this.session.destroy(); |
| 186 | +} |
| 187 | + |
| 188 | +exports.setProtocol = setProtocol; |
0 commit comments