
function HTTPRequest(method, url) {
	if (XMLHttpRequest)
		this.impl = new XMLHttpRequest;
	else if (ActiveXObject) {
		var types = new Array(
			"Msxml2.XMLHTTP.5.0",
			"Msxml2.XMLHTTP.4.0",
			"Msxml2.XMLHTTP.3.0",
			"Msxml2.XMLHTTP",
			"Microsoft.XMLHTTP");
		for (var i = 0; i < types.length; ++i) {
			try {
				this.impl = new ActiveXObject(types[i]);
				break;
			} catch (e) {
			}
		}
	}

	this.onresponse = function(status, text) { };

	var that = this;
	this.impl.onreadystatechange = function() {
		var READY_STATE_COMPLETED = 4;
		if (that.impl.readyState == READY_STATE_COMPLETED) {
			var status = that.impl.status;
			if (!status) // (Firefox 4.0b did this.)
				status = 200;

			that.onresponse(status, that.impl.responseText);

			that.impl = null;
		}
	};

	this.impl.open(method, url, /* async: */ true);
}


// Ordinarily, responses are converted to UTF-16. force_raw() prevents this
// (each raw byte from the response will occupy a character in the resulting
// text. Remember that characters are themselves 1-element strings not numbers;
// use String.charCodeAt(i) to get the numeric value).
HTTPRequest.prototype.force_raw = function() {
	this.impl.overrideMimeType("text/plain; charset=x-user-defined");
}

HTTPRequest.prototype.set_header = function(label, value) {
	this.impl.setRequestHeader(label, value);
}


HTTPRequest.prototype.send = function(data /* = null */) {
	if (typeof(data) == "undefined")
		data = null;

	this.impl.send(data);
}


/* Example usage:

var request = new HTTPRequest("GET", "http://foo.bar/xyz.html");
request.onresponse = function(status, text) {
	if (status == 200) {
		// How to convert to 8-bit data.
		// Requires calling request.force_raw() before sending.
		var data = new Array(text.length);
		for (var i = 0; i < text.length; ++i)
			data[i] = text.charCodeAt(i) & 0xFF;
	} else {
		// Failed.
	}
};
request.send();

*/

