Skip to content

Commit

Permalink
chore(logging): add logging for curls (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina authored May 17, 2017
1 parent dca9ad9 commit 77491ce
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
30 changes: 28 additions & 2 deletions lib/binaries/config_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as request from 'request';
import * as url from 'url';
import * as xml2js from 'xml2js';

import {Logger} from '../cli/logger';
import {Config} from '../config';
import {HttpUtils} from '../http_utils';

let logger = new Logger('config_source');

export abstract class ConfigSource {
ostype = Config.osType();
osarch = Config.osArch();
Expand Down Expand Up @@ -69,10 +73,21 @@ export abstract class XmlConfigSource extends ConfigSource {
return new Promise<string>((resolve, reject) => {
let options = HttpUtils.initOptions(this.xmlUrl);

let curl = this.getFileName() + ' ' + options.url;
if (HttpUtils.requestOpts.proxy) {
let pathUrl = url.parse(options.url.toString()).path;
let host = url.parse(options.url.toString()).host;
let newFileUrl = url.resolve(HttpUtils.requestOpts.proxy, pathUrl);
curl = this.getFileName() + ' \'' + newFileUrl + '\' -H \'host:' + host + '\'';
}
if (HttpUtils.requestOpts.ignoreSSL) {
curl = 'k ' + curl;
}
logger.info('curl -o' + curl);

let req = request(options);
req.on('response', response => {
if (response.statusCode === 200) {
// logger.info('curl -v ' + options.url);
let output = '';
response.on('data', (data) => {
output += data;
Expand Down Expand Up @@ -144,10 +159,21 @@ export abstract class GithubApiConfigSource extends JsonConfigSource {
options = HttpUtils.optionsHeader(options, 'Host', 'api.github.com');
options = HttpUtils.optionsHeader(options, 'User-Agent', 'request');

let curl = this.getFileName() + ' ' + options.url;
if (HttpUtils.requestOpts.proxy) {
let pathUrl = url.parse(options.url.toString()).path;
let host = url.parse(options.url.toString()).host;
let newFileUrl = url.resolve(HttpUtils.requestOpts.proxy, pathUrl);
curl = this.getFileName() + ' \'' + newFileUrl + '\' -H \'host:' + host + '\'';
}
if (HttpUtils.requestOpts.ignoreSSL) {
curl = 'k ' + curl;
}
logger.info('curl -o' + curl);

let req = request(options);
req.on('response', response => {
if (response.statusCode === 200) {
// logger.info('curl -v ' + options.url);
let output = '';
response.on('data', (data) => {
output += data;
Expand Down
14 changes: 14 additions & 0 deletions lib/files/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as q from 'q';
import * as request from 'request';
import * as url from 'url';

import {Binary} from '../binaries';
import {Logger} from '../cli';
Expand Down Expand Up @@ -48,6 +49,19 @@ export class Downloader {
response.destroy();
resolve(false);
} else {
let curl = outputDir + '/' + fileName + ' ' + options.url;
if (HttpUtils.requestOpts.proxy) {
let pathUrl = url.parse(options.url.toString()).path;
let host = url.parse(options.url.toString()).host;
let newFileUrl = url.resolve(HttpUtils.requestOpts.proxy, pathUrl);
curl = outputDir + '/' + fileName + ' \'' + newFileUrl +
'\' -H \'host:' + host + '\'';
}
if (HttpUtils.requestOpts.ignoreSSL) {
curl = 'k ' + curl;
}
logger.info('curl -o' + curl);

// only pipe if the headers are different length
file = fs.createWriteStream(filePath);
req.pipe(file);
Expand Down
9 changes: 4 additions & 5 deletions lib/http_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ export declare interface RequestOptionsValue {
ignoreSSL?: boolean;
}

let requestOpts: RequestOptionsValue = {};

export class HttpUtils {
static requestOpts: RequestOptionsValue = {};
static assignOptions(options: RequestOptionsValue): void {
Object.assign(requestOpts, options);
Object.assign(HttpUtils.requestOpts, options);
}

static initOptions(url: string, timeout?: number): OptionsWithUrl {
Expand All @@ -25,8 +24,8 @@ export class HttpUtils {
// increasing this arbitrarily to 4 minutes
timeout: 240000
};
HttpUtils.optionsSSL(options, requestOpts.ignoreSSL);
HttpUtils.optionsProxy(options, url, requestOpts.proxy);
HttpUtils.optionsSSL(options, HttpUtils.requestOpts.ignoreSSL);
HttpUtils.optionsProxy(options, url, HttpUtils.requestOpts.proxy);
return options;
}

Expand Down

0 comments on commit 77491ce

Please sign in to comment.