<commit_before>var _cl = require('./curllib');
var curllib = new _cl.CurlLib();

// console.log("curlllib:", curllib);

function CurlRequest(options) {
    this._options = options;
    this._headers = { };

    var k;
    for (k in this._options.headers) {
	this.setHeader(k, this._options.headers[k]);
    }
}

CurlRequest.prototype = {
    getHeader: function(name) {
	return this._headers[name.toLowerCase()];
    }, 
    removeHeader: function(name) {
	delete this._headers[name.toLowerCase()];
    }, 
    setHeader: function(name, value) {
	this._headers[name.toLowerCase()] = value;
    }, 
    write: function(data) {
	data = data || '';
	this._options.body += data;
    }, 
    end: function(data) {
	this.write(data);
	var _ep = this._options.protocol + '://' + this._options.host + 
	    ':' + this._options.port + this._options.path;
	var _h = [ ];
	var k;
	for (k in this._headers) {
	    _h.push(k + ': ' + this._headers[k]);
	}

	var ret = curllib.run(this._options.method, _ep, 
			      _h, this._options.body);

	if (ret.error) {
	    throw new Error(ret.error);
	}

	ret.body = '';
	if (ret.body_length) {
	    var _b = new Buffer(ret.body_length);
	    ret.body = curllib.body(_b);
	}

	function _parse_headers(headers) {
	    var _sre = /HTTP\/[0-9].[0-9] ([0-9]{3})/;
	    var _hre = /([^:]+):([\s]*)([^\r\n]*)/;
	    var statusCode = 200;
	    var _h = { };
	    headers.forEach(function(line) {
		var _m = line.match(_sre);
		if (_m) {
		    statusCode = _m[1];
		}
		else {
		    _m = line.match(_hre);
		    if (_m) {
			_h[_m[1]] = _m[3];
		    }
		}
	    });
	    return {
		statusCode: statusCode, 
		headers: _h
	    };
	}

	var _ph = _parse_headers(ret.headers);
	ret.statusCode = _ph.statusCode;
	ret.headers    = _ph.headers;

	return ret;
    }
};

exports.request = function(options) {
    options.protocol = options.protocol || 'http';
    options.port = options.port || (options.protocol === 'https' ? 443 : 80);
    options.method = options.method || 'GET';
    options.path = options.path || '/';
    options.headers = options.headers || { };
    options.host = options.host || '127.0.0.1';
    options.body = options.body || '';

    return new CurlRequest(options);
};<commit_msg>force method to be uppercase to avoid vague errors when it's lowercase<commit_after>var _cl = require('./curllib');
var curllib = new _cl.CurlLib();

// console.log("curlllib:", curllib);

function CurlRequest(options) {
    this._options = options;
    this._headers = { };

    var k;
    for (k in this._options.headers) {
	this.setHeader(k, this._options.headers[k]);
    }
}

CurlRequest.prototype = {
    getHeader: function(name) {
	return this._headers[name.toLowerCase()];
    }, 
    removeHeader: function(name) {
	delete this._headers[name.toLowerCase()];
    }, 
    setHeader: function(name, value) {
	this._headers[name.toLowerCase()] = value;
    }, 
    write: function(data) {
	data = data || '';
	this._options.body += data;
    }, 
    end: function(data) {
	this.write(data);
	var _ep = this._options.protocol + '://' + this._options.host + 
	    ':' + this._options.port + this._options.path;
	var _h = [ ];
	var k;
	for (k in this._headers) {
	    _h.push(k + ': ' + this._headers[k]);
	}

	var ret = curllib.run(this._options.method, _ep, 
			      _h, this._options.body);

	if (ret.error) {
	    throw new Error(ret.error);
	}

	ret.body = '';
	if (ret.body_length) {
	    var _b = new Buffer(ret.body_length);
	    ret.body = curllib.body(_b);
	}

	function _parse_headers(headers) {
	    var _sre = /HTTP\/[0-9].[0-9] ([0-9]{3})/;
	    var _hre = /([^:]+):([\s]*)([^\r\n]*)/;
	    var statusCode = 200;
	    var _h = { };
	    headers.forEach(function(line) {
		var _m = line.match(_sre);
		if (_m) {
		    statusCode = _m[1];
		}
		else {
		    _m = line.match(_hre);
		    if (_m) {
			_h[_m[1]] = _m[3];
		    }
		}
	    });
	    return {
		statusCode: statusCode, 
		headers: _h
	    };
	}

	var _ph = _parse_headers(ret.headers);
	ret.statusCode = _ph.statusCode;
	ret.headers    = _ph.headers;

	return ret;
    }
};

exports.request = function(options) {
    options.method = options.method || 'GET';
    options.method = options.method.toUpperCase();

    options.protocol = options.protocol || 'http';
    options.port = options.port || (options.protocol === 'https' ? 443 : 80);
    options.path = options.path || '/';
    options.headers = options.headers || { };
    options.host = options.host || '127.0.0.1';
    options.body = options.body || '';

    return new CurlRequest(options);
};
{'fraction_non_alphanumeric': 0.1806196440342782, 'fraction_numerical': 0.01054713249835201, 'mean_word_length': 2.752679307502061, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6146147', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1652, 'n_words': 313}