<commit_before>///<reference path="../.d.ts"/>

import Url = require("url");
import Future = require("fibers/future");
import helpers = require("./helpers");
import zlib = require("zlib");
import util = require("util");
import progress = require('progress-stream');
import filesize = require('filesize');

export class HttpClient implements Server.IHttpClient {
	private defaultUserAgent: string;

	constructor(private $logger: ILogger,
		private $staticConfig: Config.IStaticConfig,
		private $config: Config.IConfig) {}

	httpRequest(options: any): IFuture<Server.IResponse> {
		return (() => {
			if (_.isString(options)) {
				options = {
					url: options,
					method: "GET"
				}
			}

			var unmodifiedOptions = _.clone(options);

			if (options.url) {
				var urlParts = Url.parse(options.url);
				if (urlParts.protocol) {
					options.proto = urlParts.protocol.slice(0, -1);
				}
				options.host = urlParts.hostname;
				options.port = urlParts.port;
				options.path = urlParts.path;
				delete options.url;
			}

			var requestProto = options.proto || "http";
			delete options.proto;
			var body = options.body;
			delete options.body;
			var pipeTo = options.pipeTo;
			delete options.pipeTo;

			var proto = this.$config.PROXY_TO_FIDDLER ? "http" : requestProto;
			var http = require(proto);

			options.headers = options.headers || {};
			var headers = options.headers;

			if (this.$config.PROXY_TO_FIDDLER) {
				options.path = requestProto + "://" + options.host + options.path;
				headers.Host = options.host;
				options.host = "127.0.0.1";
				options.port = 8888;
			}

			if (!headers.Accept || headers.Accept.indexOf("application/json") < 0) {
				if (headers.Accept) {
					headers.Accept += ", ";
				} else {
					headers.Accept = "";
				}
				headers.Accept += "application/json; charset=UTF-8, */*;q=0.8";
			}

			if (!headers["User-Agent"]) {
				if (!this.defaultUserAgent) {
					this.defaultUserAgent = util.format("AppBuilderCLI/%s (Node.js %s; %s; %s)",
						this.$staticConfig.version,
						process.versions.node, process.platform, process.arch);
					this.$logger.debug("User-Agent: %s", this.defaultUserAgent);
				}

				headers["User-Agent"] = this.defaultUserAgent;
			}

			if (!headers["Accept-Encoding"]) {
				headers["Accept-Encoding"] = "gzip,deflate";
			}

			var result = new Future<Server.IResponse>();

			this.$logger.trace("httpRequest: %s", util.inspect(options));

			var request = http.request(options, (response: Server.IRequestResponseData) => {<commit_msg>pass client name during login<commit_after>///<reference path="../.d.ts"/>

import Url = require("url");
import Future = require("fibers/future");
import helpers = require("./helpers");
import zlib = require("zlib");
import util = require("util");
import progress = require('progress-stream');
import filesize = require('filesize');

export class HttpClient implements Server.IHttpClient {
	private defaultUserAgent: string;

	constructor(private $logger: ILogger,
		private $staticConfig: Config.IStaticConfig,
		private $config: Config.IConfig) {}

	httpRequest(options: any): IFuture<Server.IResponse> {
		return (() => {
			if (_.isString(options)) {
				options = {
					url: options,
					method: "GET"
				}
			}

			var unmodifiedOptions = _.clone(options);

			if (options.url) {
				var urlParts = Url.parse(options.url);
				if (urlParts.protocol) {
					options.proto = urlParts.protocol.slice(0, -1);
				}
				options.host = urlParts.hostname;
				options.port = urlParts.port;
				options.path = urlParts.path;
				delete options.url;
			}

			var requestProto = options.proto || "http";
			delete options.proto;
			var body = options.body;
			delete options.body;
			var pipeTo = options.pipeTo;
			delete options.pipeTo;

			var proto = this.$config.PROXY_TO_FIDDLER ? "http" : requestProto;
			var http = require(proto);

			options.headers = options.headers || {};
			var headers = options.headers;

			if (this.$config.PROXY_TO_FIDDLER) {
				options.path = requestProto + "://" + options.host + options.path;
				headers.Host = options.host;
				options.host = "127.0.0.1";
				options.port = 8888;
			}

			if (!headers.Accept || headers.Accept.indexOf("application/json") < 0) {
				if (headers.Accept) {
					headers.Accept += ", ";
				} else {
					headers.Accept = "";
				}
				headers.Accept += "application/json; charset=UTF-8, */*;q=0.8";
			}

			if (!headers["User-Agent"]) {
				if (!this.defaultUserAgent) {
					//TODO: the user agent client name is also passed explicitly during login and should be kept in sync
					this.defaultUserAgent = util.format("AppBuilderCLI/%s (Node.js %s; %s; %s)",
						this.$staticConfig.version,
						process.versions.node, process.platform, process.arch);
					this.$logger.debug("User-Agent: %s", this.defaultUserAgent);
				}

				headers["User-Agent"] = this.defaultUserAgent;
			}

			if (!headers["Accept-Encoding"]) {
				headers["Accept-Encoding"] = "gzip,deflate";
			}

			var result = new Future<Server.IResponse>();

			this.$logger.trace("httpRequest: %s", util.inspect(options));

			var request = http.request(options, (response: Server.IRequestResponseData) => {
{'fraction_non_alphanumeric': 0.1559721685349826, 'fraction_numerical': 0.006184770003865481, 'mean_word_length': 3.430650684931507, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2075646', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1621, 'n_words': 344}