Skip to content

Commit

Permalink
Merge pull request #42 from JacksonTian/refine_httpclient
Browse files Browse the repository at this point in the history
Simply the HTTPClient implementation
  • Loading branch information
fengmk2 committed Oct 15, 2014
2 parents ea4f9fb + 080734f commit c6c7a0d
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ var zlib = require('zlib');
var ua = require('default-user-agent');
var digestAuthHeader = require('digest-header');
var typer = require('media-typer');

var util = require('util');
var EventEmitter = require('events').EventEmitter;

var _Promise;
var _iconv;

var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'package.json')));
var pkg = require('../package.json');

var USER_AGENT = exports.USER_AGENT = ua('node-urllib', pkg.version);

Expand Down Expand Up @@ -342,8 +343,8 @@ exports.requestWithCallback = function (url, args, callback) {

cb(err, data, response);

if (args._events) {
args._events.emit('response', {
if (args.emitter) {
args.emitter.emit('response', {
error: err,
ctx: args.ctx,
req: {
Expand Down Expand Up @@ -624,47 +625,35 @@ function parseJSON(data) {
return result;
}


function HttpClient(options) {
EventEmitter.call(this);
options = options || {};
this.events = null;
this.agent = options.agent || exports.agent;
this.httpsAgent = options.httpsAgent || exports.httpsAgent;
}
util.inherits(HttpClient, EventEmitter);

HttpClient.prototype.request = function (url, args, callback) {
if (typeof args === 'function') {
callback = args;
args = null;
}
args = args || {};
args._events = this.events;
args.emitter = this;
args.agent = args.agent || this.agent;
args.httpsAgent = args.httpsAgent || this.httpsAgent;
return exports.request(url, args, callback);
};

HttpClient.prototype.requestThunk = function (url, args) {
args = args || {};
args._events = this.events;
args.emitter = this;
args.agent = args.agent || this.agent;
args.httpsAgent = args.httpsAgent || this.httpsAgent;
return exports.requestThunk(url, args);
};

HttpClient.prototype.on = function (event, listener) {
if (!this.events) {
this.events = new EventEmitter();
}
this.events.on(event, listener);
};

HttpClient.prototype.removeAllListeners = function (event) {
if (!this.events) {
return;
}
this.events.removeAllListeners(event);
};

exports.HttpClient = HttpClient;

exports.create = function (options) {
Expand Down

0 comments on commit c6c7a0d

Please sign in to comment.