2016-09-15 13:59:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
const {EventEmitter} = require('events')
|
2016-09-15 13:59:40 +00:00
|
|
|
const binding = process.atomBinding('net')
|
2016-09-19 09:21:09 +00:00
|
|
|
const {net, Net} = binding
|
|
|
|
const {URLRequest} = net
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
|
|
|
|
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
|
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
class URLResponse extends EventEmitter {
|
2016-09-21 07:23:00 +00:00
|
|
|
constructor(request) {
|
|
|
|
super();
|
|
|
|
this.request = request;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get statusCode() {
|
2016-09-26 12:59:53 +00:00
|
|
|
return this.request._statusCode;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get statusMessage() {
|
2016-09-26 12:59:53 +00:00
|
|
|
return this.request._statusMessage;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get headers() {
|
2016-09-26 12:59:53 +00:00
|
|
|
return this.request._rawResponseHeaders;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get httpVersion() {
|
|
|
|
return `${this.httpVersionMajor}.${this.httpVersionMinor}`;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get httpVersionMajor() {
|
2016-09-26 12:59:53 +00:00
|
|
|
return this.request._httpVersionMajor;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get httpVersionMinor() {
|
2016-09-26 12:59:53 +00:00
|
|
|
return this.request._httpVersionMinor;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get rawHeaders() {
|
2016-09-26 12:59:53 +00:00
|
|
|
return this.request._rawResponseHeaders;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Net.prototype.request = function(options, callback) {
|
2016-09-21 07:23:00 +00:00
|
|
|
let request = new URLRequest(options)
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
if (callback) {
|
|
|
|
request.once('response', callback)
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
return request
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
URLRequest.prototype._init = function() {
|
2016-09-26 12:03:49 +00:00
|
|
|
// Flag to prevent writings after end.
|
|
|
|
this._finished = false;
|
|
|
|
|
|
|
|
// Set when the request uses chuned encoding. Can be switched
|
|
|
|
// to true only once and never set back to false.
|
2016-09-21 15:35:03 +00:00
|
|
|
this._chunkedEncoding = false;
|
2016-09-26 12:03:49 +00:00
|
|
|
|
|
|
|
// Flag to prevent request's headers modifications after
|
|
|
|
// headers flush.
|
|
|
|
this._headersSent = false;
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
URLRequest.prototype.setHeader = function(name, value) {
|
|
|
|
if (typeof name !== 'string')
|
|
|
|
throw new TypeError('`name` should be a string in setHeader(name, value).');
|
|
|
|
if (value === undefined)
|
|
|
|
throw new Error('`value` required in setHeader("' + name + '", value).');
|
|
|
|
if (this._headersSent)
|
|
|
|
throw new Error('Can\'t set headers after they are sent.');
|
|
|
|
|
|
|
|
this._setHeader(name, value)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
URLRequest.prototype.getHeader = function(name) {
|
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw new Error('`name` is required for getHeader(name).');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._getHeader(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
URLRequest.prototype.removeHeader = function(name) {
|
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw new Error('`name` is required for removeHeader(name).');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._headersSent) {
|
|
|
|
throw new Error('Can\'t remove headers after they are sent.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._removeHeader(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
URLRequest.prototype._emitRequestEvent = function(name) {
|
|
|
|
if (name === 'response') {
|
2016-09-19 13:06:13 +00:00
|
|
|
this.response = new URLResponse(this);
|
2016-09-21 07:23:00 +00:00
|
|
|
this.emit(name, this.response);
|
|
|
|
} else {
|
|
|
|
this.emit.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
URLRequest.prototype._emitResponseEvent = function() {
|
|
|
|
this.response.emit.apply(this.response, arguments);
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
URLRequest.prototype._write = function(chunk, encoding, callback, is_last) {
|
|
|
|
|
|
|
|
let chunk_is_string = typeof chunk === 'string';
|
|
|
|
let chunk_is_buffer = chunk instanceof Buffer;
|
|
|
|
if (!chunk_is_string && !chunk_is_buffer) {
|
|
|
|
throw new TypeError('First argument must be a string or Buffer.');
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
if (chunk_is_string) {
|
|
|
|
// We convert all strings into binary buffers.
|
|
|
|
chunk = Buffer.from(chunk, encoding);
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
// Headers are assumed to be sent on first call to _writeBuffer,
|
|
|
|
// i.e. after the first call to write or end.
|
|
|
|
let result = this._writeBuffer(chunk, is_last);
|
|
|
|
|
|
|
|
// Since writing to the network is asynchronous, we conservatively
|
|
|
|
// assume that request headers are written after delivering the first
|
|
|
|
// buffer to the network IO thread.
|
|
|
|
this._headersSent = true;
|
|
|
|
|
|
|
|
// The write callback is fired asynchronously to mimic Node.js.
|
|
|
|
if (callback) {
|
|
|
|
process.nextTick(callback);
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
URLRequest.prototype.write = function(data, encoding, callback) {
|
|
|
|
|
|
|
|
if (this._finished) {
|
|
|
|
let error = new Error('Write after end.');
|
|
|
|
process.nextTick(writeAfterEndNT, this, error, callback);
|
|
|
|
return true;
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
return this._write(data, encoding, callback, false);
|
2016-09-21 15:35:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
URLRequest.prototype.end = function(data, encoding, callback) {
|
|
|
|
if (this._finished) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._finished = true;
|
|
|
|
|
|
|
|
if (typeof data === 'function') {
|
|
|
|
callback = data;
|
|
|
|
encoding = null;
|
|
|
|
data = null;
|
|
|
|
} else if (typeof encoding === 'function') {
|
|
|
|
callback = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = data || '';
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
return this._write(data, encoding, callback, true);
|
2016-09-21 15:35:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-26 12:03:49 +00:00
|
|
|
function writeAfterEndNT(self, error, callback) {
|
|
|
|
self.emit('error', error);
|
|
|
|
if (callback) callback(error);
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
module.exports = net
|
2016-09-15 13:59:40 +00:00
|
|
|
|