15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'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;
}