2016-09-15 13:59:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const url = require('url')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { EventEmitter } = require('events')
|
2019-11-27 01:01:13 +00:00
|
|
|
const { Readable, Writable } = require('stream')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { app } = require('electron')
|
2019-03-18 19:37:06 +00:00
|
|
|
const { Session } = process.electronBinding('session')
|
2019-11-27 01:01:13 +00:00
|
|
|
const { net, Net, _isValidHeaderName, _isValidHeaderValue } = process.electronBinding('net')
|
|
|
|
const { URLLoader } = net
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
Object.setPrototypeOf(URLLoader.prototype, EventEmitter.prototype)
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
const kSupportedProtocols = new Set(['http:', 'https:'])
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-04-02 14:41:19 +00:00
|
|
|
// set of headers that Node.js discards duplicates for
|
|
|
|
// see https://nodejs.org/api/http.html#http_message_headers
|
|
|
|
const discardableDuplicateHeaders = new Set([
|
|
|
|
'content-type',
|
|
|
|
'content-length',
|
|
|
|
'user-agent',
|
|
|
|
'referer',
|
|
|
|
'host',
|
|
|
|
'authorization',
|
|
|
|
'proxy-authorization',
|
|
|
|
'if-modified-since',
|
|
|
|
'if-unmodified-since',
|
|
|
|
'from',
|
|
|
|
'location',
|
|
|
|
'max-forwards',
|
|
|
|
'retry-after',
|
|
|
|
'etag',
|
|
|
|
'last-modified',
|
|
|
|
'server',
|
|
|
|
'age',
|
|
|
|
'expires'
|
|
|
|
])
|
|
|
|
|
2016-10-06 12:14:05 +00:00
|
|
|
class IncomingMessage extends Readable {
|
2019-11-27 01:01:13 +00:00
|
|
|
constructor (responseHead) {
|
2016-09-29 13:24:28 +00:00
|
|
|
super()
|
2019-11-27 01:01:13 +00:00
|
|
|
this._shouldPush = false
|
|
|
|
this._data = []
|
|
|
|
this._responseHead = responseHead
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get statusCode () {
|
2019-11-27 01:01:13 +00:00
|
|
|
return this._responseHead.statusCode
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get statusMessage () {
|
2019-11-27 01:01:13 +00:00
|
|
|
return this._responseHead.statusMessage
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get headers () {
|
2019-04-02 14:41:19 +00:00
|
|
|
const filteredHeaders = {}
|
2019-11-27 01:01:13 +00:00
|
|
|
const rawHeaders = this._responseHead.headers
|
2019-04-02 14:41:19 +00:00
|
|
|
Object.keys(rawHeaders).forEach(header => {
|
|
|
|
if (header in filteredHeaders && discardableDuplicateHeaders.has(header)) {
|
|
|
|
// do nothing with discardable duplicate headers
|
|
|
|
} else {
|
|
|
|
if (header === 'set-cookie') {
|
|
|
|
// keep set-cookie as an array per Node.js rules
|
|
|
|
// see https://nodejs.org/api/http.html#http_message_headers
|
|
|
|
filteredHeaders[header] = rawHeaders[header]
|
|
|
|
} else {
|
|
|
|
// for non-cookie headers, the values are joined together with ', '
|
|
|
|
filteredHeaders[header] = rawHeaders[header].join(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return filteredHeaders
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersion () {
|
|
|
|
return `${this.httpVersionMajor}.${this.httpVersionMinor}`
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersionMajor () {
|
2019-11-27 01:01:13 +00:00
|
|
|
return this._responseHead.httpVersion.major
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersionMinor () {
|
2019-11-27 01:01:13 +00:00
|
|
|
return this._responseHead.httpVersion.minor
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
get rawTrailers () {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('HTTP trailers are not supported')
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
get trailers () {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('HTTP trailers are not supported')
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_storeInternalData (chunk) {
|
2019-11-27 01:01:13 +00:00
|
|
|
this._data.push(chunk)
|
|
|
|
this._pushInternalData()
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_pushInternalData () {
|
2019-11-27 01:01:13 +00:00
|
|
|
while (this._shouldPush && this._data.length > 0) {
|
|
|
|
const chunk = this._data.shift()
|
|
|
|
this._shouldPush = this.push(chunk)
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_read () {
|
2019-11-27 01:01:13 +00:00
|
|
|
this._shouldPush = true
|
2016-10-06 12:14:05 +00:00
|
|
|
this._pushInternalData()
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
/** Writable stream that buffers up everything written to it. */
|
|
|
|
class SlurpStream extends Writable {
|
|
|
|
constructor () {
|
|
|
|
super()
|
|
|
|
this._data = Buffer.alloc(0)
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
_write (chunk, encoding, callback) {
|
|
|
|
this._data = Buffer.concat([this._data, chunk])
|
|
|
|
callback()
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
data () { return this._data }
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
class ChunkedBodyStream extends Writable {
|
|
|
|
constructor (clientRequest) {
|
2016-09-29 13:24:28 +00:00
|
|
|
super()
|
2019-11-27 01:01:13 +00:00
|
|
|
this._clientRequest = clientRequest
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_write (chunk, encoding, callback) {
|
|
|
|
if (this._downstream) {
|
|
|
|
this._downstream.write(chunk).then(callback, callback)
|
2016-09-27 10:33:51 +00:00
|
|
|
} else {
|
2019-11-27 01:01:13 +00:00
|
|
|
// the contract of _write is that we won't be called again until we call
|
|
|
|
// the callback, so we're good to just save a single chunk.
|
|
|
|
this._pendingChunk = chunk
|
|
|
|
this._pendingCallback = callback
|
|
|
|
|
|
|
|
// The first write to a chunked body stream begins the request.
|
|
|
|
this._clientRequest._startRequest()
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_final (callback) {
|
|
|
|
this._downstream.done()
|
|
|
|
callback()
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
startReading (pipe) {
|
|
|
|
if (this._downstream) {
|
|
|
|
throw new Error('two startReading calls???')
|
|
|
|
}
|
|
|
|
this._downstream = pipe
|
|
|
|
if (this._pendingChunk) {
|
|
|
|
const doneWriting = (maybeError) => {
|
|
|
|
const cb = this._pendingCallback
|
|
|
|
delete this._pendingCallback
|
|
|
|
delete this._pendingChunk
|
|
|
|
cb(maybeError)
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
this._downstream.write(this._pendingChunk).then(doneWriting, doneWriting)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
function parseOptions (options) {
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = url.parse(options)
|
|
|
|
} else {
|
|
|
|
options = { ...options }
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
const method = (options.method || 'GET').toUpperCase()
|
|
|
|
let urlStr = options.url
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!urlStr) {
|
|
|
|
const urlObj = {}
|
|
|
|
const protocol = options.protocol || 'http:'
|
|
|
|
if (!kSupportedProtocols.has(protocol)) {
|
|
|
|
throw new Error('Protocol "' + protocol + '" not supported')
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
urlObj.protocol = protocol
|
2017-03-23 19:37:54 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (options.host) {
|
|
|
|
urlObj.host = options.host
|
|
|
|
} else {
|
|
|
|
if (options.hostname) {
|
|
|
|
urlObj.hostname = options.hostname
|
2016-10-25 13:47:54 +00:00
|
|
|
} else {
|
2019-11-27 01:01:13 +00:00
|
|
|
urlObj.hostname = 'localhost'
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
|
|
|
|
if (options.port) {
|
|
|
|
urlObj.port = options.port
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (options.path && / /.test(options.path)) {
|
|
|
|
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
|
|
|
// with an additional rule for ignoring percentage-escaped characters
|
|
|
|
// but that's a) hard to capture in a regular expression that performs
|
|
|
|
// well, and b) possibly too restrictive for real-world usage. That's
|
|
|
|
// why it only scans for spaces because those are guaranteed to create
|
|
|
|
// an invalid request.
|
|
|
|
throw new TypeError('Request path contains unescaped characters')
|
|
|
|
}
|
|
|
|
const pathObj = url.parse(options.path || '/')
|
|
|
|
urlObj.pathname = pathObj.pathname
|
|
|
|
urlObj.search = pathObj.search
|
|
|
|
urlObj.hash = pathObj.hash
|
|
|
|
urlStr = url.format(urlObj)
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
const redirectPolicy = options.redirect || 'follow'
|
|
|
|
if (!['follow', 'error', 'manual'].includes(redirectPolicy)) {
|
|
|
|
throw new Error('redirect mode should be one of follow, error or manual')
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (options.headers != null && typeof options.headers !== 'object') {
|
|
|
|
throw new TypeError('headers must be an object')
|
|
|
|
}
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
const urlLoaderOptions = {
|
|
|
|
method: method,
|
|
|
|
url: urlStr,
|
|
|
|
redirectPolicy,
|
|
|
|
extraHeaders: options.headers || {}
|
|
|
|
}
|
|
|
|
for (const [name, value] of Object.entries(urlLoaderOptions.extraHeaders)) {
|
|
|
|
if (!_isValidHeaderName(name)) {
|
|
|
|
throw new Error(`Invalid header name: '${name}'`)
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!_isValidHeaderValue(value.toString())) {
|
|
|
|
throw new Error(`Invalid value for header '${name}': '${value}'`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.session) {
|
|
|
|
if (options.session instanceof Session) {
|
|
|
|
urlLoaderOptions.session = options.session
|
|
|
|
} else {
|
|
|
|
throw new TypeError('`session` should be an instance of the Session class')
|
|
|
|
}
|
|
|
|
} else if (options.partition) {
|
|
|
|
if (typeof options.partition === 'string') {
|
|
|
|
urlLoaderOptions.partition = options.partition
|
|
|
|
} else {
|
|
|
|
throw new TypeError('`partition` should be a string')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return urlLoaderOptions
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
class ClientRequest extends Writable {
|
|
|
|
constructor (options, callback) {
|
|
|
|
super({ autoDestroy: true })
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!app.isReady()) {
|
|
|
|
throw new Error('net module can only be used after app is ready')
|
|
|
|
}
|
2016-10-20 09:43:26 +00:00
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
if (callback) {
|
|
|
|
this.once('response', callback)
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
const { redirectPolicy, ...urlLoaderOptions } = parseOptions(options)
|
|
|
|
this._urlLoaderOptions = urlLoaderOptions
|
|
|
|
this._redirectPolicy = redirectPolicy
|
|
|
|
this._started = false
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
set chunkedEncoding (value) {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._started) {
|
|
|
|
throw new Error('chunkedEncoding can only be set before the request is started')
|
|
|
|
}
|
|
|
|
if (typeof this._chunkedEncoding !== 'undefined') {
|
|
|
|
throw new Error('chunkedEncoding can only be set once')
|
|
|
|
}
|
|
|
|
this._chunkedEncoding = !!value
|
|
|
|
if (this._chunkedEncoding) {
|
|
|
|
this._body = new ChunkedBodyStream(this)
|
|
|
|
this._urlLoaderOptions.body = (pipe) => {
|
|
|
|
this._body.startReading(pipe)
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
setHeader (name, value) {
|
|
|
|
if (typeof name !== 'string') {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new TypeError('`name` should be a string in setHeader(name, value)')
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2017-03-30 16:24:40 +00:00
|
|
|
if (value == null) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('`value` required in setHeader("' + name + '", value)')
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._started || this._firstWrite) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('Can\'t set headers after they are sent')
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!_isValidHeaderName(name)) {
|
|
|
|
throw new Error(`Invalid header name: '${name}'`)
|
|
|
|
}
|
|
|
|
if (!_isValidHeaderValue(value.toString())) {
|
|
|
|
throw new Error(`Invalid value for header '${name}': '${value}'`)
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const key = name.toLowerCase()
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoaderOptions.extraHeaders[key] = value
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
getHeader (name) {
|
2017-03-30 19:37:40 +00:00
|
|
|
if (name == null) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('`name` is required for getHeader(name)')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const key = name.toLowerCase()
|
2019-11-27 01:01:13 +00:00
|
|
|
return this._urlLoaderOptions.extraHeaders[key]
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
removeHeader (name) {
|
2017-03-30 19:37:40 +00:00
|
|
|
if (name == null) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('`name` is required for removeHeader(name)')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._started || this._firstWrite) {
|
2019-06-14 23:26:07 +00:00
|
|
|
throw new Error('Can\'t remove headers after they are sent')
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
const key = name.toLowerCase()
|
2019-11-27 01:01:13 +00:00
|
|
|
delete this._urlLoaderOptions.extraHeaders[key]
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_write (chunk, encoding, callback) {
|
|
|
|
this._firstWrite = true
|
|
|
|
if (!this._body) {
|
|
|
|
this._body = new SlurpStream()
|
|
|
|
this._body.on('finish', () => {
|
|
|
|
this._urlLoaderOptions.body = this._body.data()
|
|
|
|
this._startRequest()
|
|
|
|
})
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
// TODO: is this the right way to forward to another stream?
|
|
|
|
this._body.write(chunk, encoding, callback)
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_final (callback) {
|
|
|
|
if (this._body) {
|
|
|
|
// TODO: is this the right way to forward to another stream?
|
|
|
|
this._body.end(callback)
|
|
|
|
} else {
|
|
|
|
// end() called without a body, go ahead and start the request
|
|
|
|
this._startRequest()
|
|
|
|
callback()
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_startRequest () {
|
|
|
|
this._started = true
|
|
|
|
const stringifyValues = (obj) => {
|
|
|
|
const ret = {}
|
|
|
|
for (const k in obj) {
|
|
|
|
ret[k] = obj[k].toString()
|
|
|
|
}
|
|
|
|
return ret
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
const opts = { ...this._urlLoaderOptions, extraHeaders: stringifyValues(this._urlLoaderOptions.extraHeaders) }
|
|
|
|
this._urlLoader = new URLLoader(opts)
|
|
|
|
this._urlLoader.on('response-started', (event, finalUrl, responseHead) => {
|
|
|
|
const response = this._response = new IncomingMessage(responseHead)
|
|
|
|
this.emit('response', response)
|
|
|
|
})
|
|
|
|
this._urlLoader.on('data', (event, data) => {
|
|
|
|
this._response._storeInternalData(Buffer.from(data))
|
|
|
|
})
|
|
|
|
this._urlLoader.on('complete', () => {
|
|
|
|
if (this._response) { this._response._storeInternalData(null) }
|
|
|
|
})
|
|
|
|
this._urlLoader.on('error', (event, netErrorString) => {
|
|
|
|
const error = new Error(netErrorString)
|
|
|
|
if (this._response) this._response.destroy(error)
|
|
|
|
this._die(error)
|
|
|
|
})
|
2016-09-29 13:24:28 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('login', (event, authInfo, callback) => {
|
|
|
|
const handled = this.emit('login', authInfo, callback)
|
|
|
|
if (!handled) {
|
|
|
|
// If there were no listeners, cancel the authentication request.
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this._urlLoader.on('redirect', (event, redirectInfo, headers) => {
|
|
|
|
const { statusCode, newMethod, newUrl } = redirectInfo
|
|
|
|
if (this._redirectPolicy === 'error') {
|
|
|
|
this._die(new Error(`Attempted to redirect, but redirect policy was 'error'`))
|
|
|
|
} else if (this._redirectPolicy === 'manual') {
|
|
|
|
let _followRedirect = false
|
|
|
|
this._followRedirectCb = () => { _followRedirect = true }
|
|
|
|
try {
|
|
|
|
this.emit('redirect', statusCode, newMethod, newUrl, headers)
|
|
|
|
} finally {
|
|
|
|
this._followRedirectCb = null
|
|
|
|
if (!_followRedirect) {
|
|
|
|
this._die(new Error('Redirect was cancelled'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (this._redirectPolicy === 'follow') {
|
|
|
|
// Calling followRedirect() when the redirect policy is 'follow' is
|
|
|
|
// allowed but does nothing. (Perhaps it should throw an error
|
|
|
|
// though...? Since the redirect will happen regardless.)
|
|
|
|
try {
|
|
|
|
this._followRedirectCb = () => {}
|
|
|
|
this.emit('redirect', statusCode, newMethod, newUrl, headers)
|
|
|
|
} finally {
|
|
|
|
this._followRedirectCb = null
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._die(new Error(`Unexpected redirect policy '${this._redirectPolicy}'`))
|
|
|
|
}
|
|
|
|
})
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('upload-progress', (event, position, total) => {
|
|
|
|
this._uploadProgress = { active: true, started: true, current: position, total }
|
|
|
|
this.emit('upload-progress', position, total) // Undocumented, for now
|
|
|
|
})
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('download-progress', (event, current) => {
|
|
|
|
if (this._response) {
|
|
|
|
this._response.emit('download-progress', current) // Undocumented, for now
|
|
|
|
}
|
|
|
|
})
|
2016-09-26 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
followRedirect () {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._followRedirectCb) {
|
|
|
|
this._followRedirectCb()
|
|
|
|
} else {
|
|
|
|
throw new Error('followRedirect() called, but was not waiting for a redirect')
|
|
|
|
}
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
abort () {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!this._aborted) {
|
|
|
|
process.nextTick(() => { this.emit('abort') })
|
|
|
|
}
|
|
|
|
this._aborted = true
|
|
|
|
this._die()
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
2018-07-27 14:15:48 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_die (err) {
|
|
|
|
this.destroy(err)
|
|
|
|
if (this._urlLoader) {
|
|
|
|
this._urlLoader.cancel()
|
|
|
|
if (this._response) this._response.destroy(err)
|
|
|
|
}
|
2018-07-27 14:15:48 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
getUploadProgress () {
|
|
|
|
return { ...this._uploadProgress } || { active: false }
|
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
Net.prototype.request = function (options, callback) {
|
|
|
|
return new ClientRequest(options, callback)
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
net.ClientRequest = ClientRequest
|
2016-09-27 08:21:11 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
module.exports = net
|