<commit_before>(function () {
    'use strict';

    var minimist = require('minimist');
    var url = require('url');
    var http = require('http');
    var pd = require('pretty-data').pd;
    var _ = require('underscore');

    exports.help = function () {
	require('./common').displayHelp('tsu-http');
    };

    exports.execute = function (cmdLine) {
	var args = parseArgs(cmdLine);

	if (args._.length === 0) {
	    console.log('You must provide a URL');
	    process.exit(1);
	} else {
	    sendRequest(args);
	}
    };

    /**
     * Parse command line arguments.
     */
    function parseArgs(args) {
	var argsOptions = {'boolean': 'v',
			   'alias': {'d': 'data',
				     'h': 'header',
				     'm': 'method',
				     'v': 'verbose'}};

	return minimist(args, argsOptions);
    }

    /**
     * Send request.
     */
    function sendRequest(args) {
	var options = createRequestOptions(args);

	var req = http.request(options, function (response) {
	    var verbose = args.verbose || false;
	    var head = args.method && (args.method.toUpperCase() === 'HEAD');

	    if (verbose || head) {
		displayResponseInfos(response, head);
	    }

	    if (!head) {
		displayResponseBody(response);
	    }
	}).on('error', function (e) {
	    console.log('problem with request: ' + e.message);
	});

	if (args.data) {
	    var data = args.data;

	    console.log('writing data:' + data);

	    req.write(data);
	}

	req.end();
    }

    /**
     * create request options.
     */
    function createRequestOptions(args) {
	var options;
	if (args.proxy) {
	    options = createBaseOptionsForProxyAccess(args);
	} else {
	    options = createBaseOptionsForDirectAccess(args);
	}

	addBasicAuth(options, args);
	addHeaders(options, args.header);

	return options;
    }

    /**
     * Create base options when a proxy is used.
     */
    function createBaseOptionsForProxyAccess(args) {
	var proxy = url.parse(args.proxy);
	var target = args._[0];

	return {
	    hostname: proxy.hostname,
	    port: proxy.port,
	    path: target,
	    method: args.method || 'GET',
	    headers: {}
	};
    }

    /**
     * Create base options for direct access.
     */
    function createBaseOptionsForDirectAccess(args) {
	var target = url.parse(args._[0]);

	return {
	    hostname: target.hostname || 'localhost',
	    port: target.port || 80,
	    path: target.path,
	    method: args.method || 'GET',
	    headers: {}
	};
    }

    /**
     * Add basic-auth to request options.
     */
    function addBasicAuth(options, args) {
	var target = url.parse(args._[0]);
	var auth = target.auth || args['basic-auth'];

	if (auth) {
	    addHeader(options, 'Authorization:Basic ' + new Buffer(auth).toString('base64'));
	}
    }

    /**
     * Add a single header to request options.
     */
    function addHeader(options, header) {
	var headerData = header.split(':');
	options.headers[headerData[0]] = headerData[1];
    }

    /**
     * Add all headers to request options.
     */
    function addHeaders(options, header) {
	if (header) {
	    if (_.isArray(header)) {
		_.each(header, function (element) {
		    addHeader(options, element);
		});
	    } else {
		addHeader(options, header);
	    }
	}
    }

    /**
     * Display response informations.
     */
    function displayResponseInfos(res, head) {
	var status = res.statusCode;
	var statusLine = status + ' ' + http.STATUS_CODES[status];
	console.log('Status: ' + statusLine);

	console.log('Headers:\n' + pd.json(res.headers));

	if (!head) {
	    console.log('Body: ');
	}
    }

    /**
     * Display response body.<commit_msg>Fix blocking HEAD request<commit_after>(function () {
    'use strict';

    var minimist = require('minimist');
    var url = require('url');
    var http = require('http');
    var pd = require('pretty-data').pd;
    var _ = require('underscore');

    exports.help = function () {
	require('./common').displayHelp('tsu-http');
    };

    exports.execute = function (cmdLine) {
	var args = parseArgs(cmdLine);

	if (args._.length === 0) {
	    console.log('You must provide a URL');
	    process.exit(1);
	} else {
	    sendRequest(args);
	}
    };

    /**
     * Parse command line arguments.
     */
    function parseArgs(args) {
	var argsOptions = {'boolean': 'v',
			   'alias': {'d': 'data',
				     'h': 'header',
				     'm': 'method',
				     'v': 'verbose'}};

	return minimist(args, argsOptions);
    }

    /**
     * Send request.
     */
    function sendRequest(args) {
	var options = createRequestOptions(args);

	var req = http.request(options, function (response) {
	    var verbose = args.verbose || false;
	    var head = args.method && 'HEAD' === args.method.toUpperCase();
	    
	    displayResponseInfos(response, verbose, head);
	    displayResponseBody(response);
	}).on('error', function (e) {
	    console.log('problem with request: ' + e.message);
	});

	var data = args.data;
	if (data) {
	    req.write(data);
	}

	req.end();
    }

    /**
     * create request options.
     */
    function createRequestOptions(args) {
	var options;
	if (args.proxy) {
	    options = createBaseOptionsForProxyAccess(args);
	} else {
	    options = createBaseOptionsForDirectAccess(args);
	}

	addBasicAuth(options, args);
	addHeaders(options, args.header);

	return options;
    }

    /**
     * Create base options when a proxy is used.
     */
    function createBaseOptionsForProxyAccess(args) {
	var proxy = url.parse(args.proxy);
	var target = args._[0];

	return {
	    hostname: proxy.hostname,
	    port: proxy.port,
	    path: target,
	    method: args.method || 'GET',
	    headers: {}
	};
    }

    /**
     * Create base options for direct access.
     */
    function createBaseOptionsForDirectAccess(args) {
	var target = url.parse(args._[0]);

	return {
	    hostname: target.hostname,
	    port: target.port,
	    path: target.path,
	    method: args.method || 'GET',
	    headers: {}
	};
    }

    /**
     * Add basic-auth to request options.
     */
    function addBasicAuth(options, args) {
	var target = url.parse(args._[0]);
	var auth = target.auth || args['basic-auth'];

	if (auth) {
	    addHeader(options, 'Authorization:Basic ' + new Buffer(auth).toString('base64'));
	}
    }

    /**
     * Add a single header to request options.
     */
    function addHeader(options, header) {
	var headerData = header.split(':');
	options.headers[headerData[0]] = headerData[1];
    }

    /**
     * Add all headers to request options.
     */
    function addHeaders(options, header) {
	if (header) {
	    if (_.isArray(header)) {
		_.each(header, function (element) {
		    addHeader(options, element);
		});
	    } else {
		addHeader(options, header);
	    }
	}
    }

    /**
     * Display response informations.
     * @param response response from server
     * @param verbose indicates if verbose mode is requested
     * @param head indicates if it is a head request
     */
    function displayResponseInfos(response, verbose, head) {
	if (verbose || head) {
	    var status = response.statusCode;
	    var statusLine = status + ' ' + http.STATUS_CODES[status];
	    console.log('Status: ' + statusLine);

	    console.log('Headers:\n' + pd.json(response.headers));

	    if (!head) {
		console.log('Body:');
	    }
	}
    }

    /**
     * Display response body.
     * @param response response from server
{'fraction_non_alphanumeric': 0.15252854812398042, 'fraction_numerical': 0.0027188689505165853, 'mean_word_length': 2.6134577603143416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1495697', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2282, 'n_words': 536}