http_utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const request = require("request");
  4. const url = require("url");
  5. const logger_1 = require("./cli/logger");
  6. const config_1 = require("./config");
  7. let logger = new logger_1.Logger('http_utils');
  8. class HttpUtils {
  9. static assignOptions(options) {
  10. Object.assign(HttpUtils.requestOpts, options);
  11. }
  12. static initOptions(url, timeout) {
  13. let options = {
  14. url: url,
  15. // default Linux can be anywhere from 20-120 seconds
  16. // increasing this arbitrarily to 4 minutes
  17. timeout: 240000
  18. };
  19. HttpUtils.optionsSSL(options, HttpUtils.requestOpts.ignoreSSL);
  20. HttpUtils.optionsProxy(options, url, HttpUtils.requestOpts.proxy);
  21. return options;
  22. }
  23. static optionsSSL(options, opt_ignoreSSL) {
  24. if (opt_ignoreSSL) {
  25. logger.info('ignoring SSL certificate');
  26. options.strictSSL = !opt_ignoreSSL;
  27. options.rejectUnauthorized = !opt_ignoreSSL;
  28. }
  29. return options;
  30. }
  31. static optionsProxy(options, requestUrl, opt_proxy) {
  32. if (opt_proxy) {
  33. options.proxy = HttpUtils.resolveProxy(requestUrl, opt_proxy);
  34. if (url.parse(requestUrl).protocol === 'https:') {
  35. options.url = requestUrl.replace('https:', 'http:');
  36. }
  37. }
  38. return options;
  39. }
  40. static optionsHeader(options, key, value) {
  41. if (options.headers == null) {
  42. options.headers = {};
  43. }
  44. options.headers[key] = value;
  45. return options;
  46. }
  47. /**
  48. * Resolves proxy based on values set
  49. * @param fileUrl The url to download the file.
  50. * @param opt_proxy The proxy to connect to to download files.
  51. * @return Either undefined or the proxy.
  52. */
  53. static resolveProxy(fileUrl, opt_proxy) {
  54. let protocol = url.parse(fileUrl).protocol;
  55. let hostname = url.parse(fileUrl).hostname;
  56. if (opt_proxy) {
  57. return opt_proxy;
  58. }
  59. else {
  60. // If the NO_PROXY environment variable exists and matches the host name,
  61. // to ignore the resolve proxy.
  62. // the checks to see if it exists and equal to empty string is to help with testing
  63. let noProxy = config_1.Config.noProxy();
  64. if (noProxy) {
  65. // array of hostnames/domain names listed in the NO_PROXY environment variable
  66. let noProxyTokens = noProxy.split(',');
  67. // check if the fileUrl hostname part does not end with one of the
  68. // NO_PROXY environment variable's hostnames/domain names
  69. for (let noProxyToken of noProxyTokens) {
  70. if (hostname.indexOf(noProxyToken) !== -1) {
  71. return undefined;
  72. }
  73. }
  74. }
  75. // If the HTTPS_PROXY and HTTP_PROXY environment variable is set, use that as the proxy
  76. if (protocol === 'https:') {
  77. return config_1.Config.httpsProxy() || config_1.Config.httpProxy();
  78. }
  79. else if (protocol === 'http:') {
  80. return config_1.Config.httpProxy();
  81. }
  82. }
  83. return undefined;
  84. }
  85. }
  86. HttpUtils.requestOpts = {};
  87. exports.HttpUtils = HttpUtils;
  88. /**
  89. * Request the body from the url.
  90. * @param requestUrl The request url.
  91. * @returns A promise string of the response body.
  92. */
  93. function requestBody(requestUrl) {
  94. const options = HttpUtils.initOptions(requestUrl);
  95. options.followRedirect = true;
  96. return new Promise((resolve, reject) => {
  97. const req = request(options);
  98. req.on('response', response => {
  99. if (response.statusCode === 200) {
  100. let output = '';
  101. response.on('data', (data) => {
  102. output += data;
  103. });
  104. response.on('end', () => {
  105. resolve(output);
  106. });
  107. }
  108. else {
  109. reject(new Error('response status code is not 200'));
  110. }
  111. });
  112. req.on('error', error => {
  113. reject(error);
  114. });
  115. });
  116. }
  117. exports.requestBody = requestBody;
  118. //# sourceMappingURL=http_utils.js.map