Skip to content

Instantly share code, notes, and snippets.

@srveit
Created August 19, 2010 23:36
Show Gist options
  • Save srveit/539213 to your computer and use it in GitHub Desktop.
Save srveit/539213 to your computer and use it in GitHub Desktop.
/*global clearTimeout: false, setTimeout: false
*/
var sys = require("sys");
var http = require("http");
var events = require("events");
var user_agent_name = "VSS/4.0.0";
var createRequestWithHeaders = function (request, httpClient, method, path,
headers, timeout) {
var httpRequest = httpClient.request(method, path, headers);
var responseEncoding = "binary";
var timed_out = false;
var timeout_id;
var errorHandler = function (exception) {
if (!timed_out) {
if (timeout_id) {
clearTimeout(timeout_id);
}
httpClient.end();
request.emit("error", exception);
}
};
httpRequest.
addListener('response', function (httpResponse) {
var body = "";
httpResponse.setEncoding(responseEncoding);
httpResponse.
addListener("data", function (data) {
body += data;
}).
addListener("end", function () {
var parsedBody;
if (httpResponse.headers &&
httpResponse.headers['content-type'] === 'audio/x-wav') {
parsedBody = body;
} else {
parsedBody = JSON.parse(body);
}
if (!timed_out) {
if (timeout_id) {
clearTimeout(timeout_id);
}
httpClient.end();
request.emit("parsedResponse", parsedBody, httpResponse);
}
}).
addListener("error", errorHandler);
}).
addListener("error", errorHandler);
if (timeout) {
var timeout_fn = function () {
timed_out = true;
httpClient.end();
request.emit("error", {message: "timed_out"});
};
timeout_id = setTimeout(timeout_fn, timeout);
}
httpClient.addListener("error", errorHandler);
return httpRequest;
};
var createRequest = function (httpClient, method, path, accept, contentType,
body, timeout) {
var headers = {
"User-Agent": user_agent_name,
Host: httpClient.host + ":" + httpClient.port,
Accept: accept
};
var request = new events.EventEmitter();
if (contentType) {
headers["Content-Type"] = contentType;
headers["Content-Length"] = body ? body.length : 0;
}
var httpRequest = createRequestWithHeaders(request, httpClient, method, path,
headers, timeout);
request.end = function () {
httpRequest.end();
};
if (body) {
httpRequest.write(body, "binary");
}
return request;
};
var createClient = function (port, host, secure, credentials) {
var client = new events.EventEmitter();
var httpClient = http.createClient(port, host, secure, credentials);
client.request = function (method, path, accept, contentType, body,
timeout) {
return createRequest(httpClient, method, path, accept, contentType, body,
timeout);
};
client.encodedRequest = function (method, path, accept, contentType, body,
timeout) {
if (contentType === 'application/json' && body) {
body = JSON.stringify(body);
}
return createRequest(httpClient, method, path, accept, contentType, body,
timeout);
};
client.end = function () {
httpClient.end();
};
return client;
};
exports.createClient = createClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment