hexo/node_modules/urllib/lib/httpclient.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
'use strict';
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var utility = require('utility');
var urllib = require('./urllib');
module.exports = HttpClient;
function HttpClient(options) {
EventEmitter.call(this);
options = options || {};
if (options.agent !== undefined) {
this.agent = options.agent;
this.hasCustomAgent = true;
} else {
this.agent = urllib.agent;
this.hasCustomAgent = false;
}
if (options.httpsAgent !== undefined) {
this.httpsAgent = options.httpsAgent;
this.hasCustomHttpsAgent = true;
} else {
this.httpsAgent = urllib.httpsAgent;
this.hasCustomHttpsAgent = false;
}
this.defaultArgs = options.defaultArgs;
}
util.inherits(HttpClient, EventEmitter);
HttpClient.prototype.request = HttpClient.prototype.curl = function (url, args, callback) {
if (typeof args === 'function') {
callback = args;
args = null;
}
args = args || {};
if (this.defaultArgs) {
args = utility.assign({}, [ this.defaultArgs, args ]);
}
args.emitter = this;
args.agent = getAgent(args.agent, this.agent);
args.httpsAgent = getAgent(args.httpsAgent, this.httpsAgent);
return urllib.request(url, args, callback);
};
HttpClient.prototype.requestThunk = function (url, args) {
args = args || {};
if (this.defaultArgs) {
args = utility.assign({}, [ this.defaultArgs, args ]);
}
args.emitter = this;
args.agent = getAgent(args.agent, this.agent);
args.httpsAgent = getAgent(args.httpsAgent, this.httpsAgent);
return urllib.requestThunk(url, args);
};
function getAgent(agent, defaultAgent) {
return agent === undefined ? defaultAgent : agent;
}