Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated my custom http lib to match 0.4 api changes.

  • Loading branch information...
commit 8be866ac6c6b7611babbc93d6ff06c45f6f9dccb 1 parent 207f88e
@eschnou eschnou authored
Showing with 41 additions and 43 deletions.
  1. +41 −43 lib/ostatus/http.js
View
84 lib/ostatus/http.js
@@ -22,30 +22,52 @@
* THE SOFTWARE.
*/
-var Http = require('http'),
- Url = require('url');
+var Http = require('http'),
+ Https = require('https'),
+ Url = require('url');
function get(url, callback, headers) {
+ request(url, false, 'GET', headers, callback);
+}
+
+function post(url, reqBody, headers, callback) {
+ request(url, reqBody, 'POST', headers, callback);
+}
+
+function request(url, data, method, headers, callback) {
+ var body = "";
var url = Url.parse(url);
var host = url.hostname;
var path = url.pathname;
var headers = headers ? headers : {};
var secure = (url.protocol == "https:") ? true : false;
var port = secure ? 443 : 80;
+ var client = secure ? Https : Http;
- headers["Host"] = host;
+ // Add the mandatory HTTP Headers
+ headers["host"] = secure ? host + ":443" : host;
+ headers["user-agent"] = "node-ostatus";
+ // If we post data, add a content length header
+ // TODO The +1 is a hack, need to be fixed
+ if (data) {
+ headers["Content-Length"] = data.length + 1;
+ }
+
+ // Add the query string to the path if any
if (url.search != undefined) path += url.search;
- var client = Http.createClient(port, host, secure);
- var request = client.request('GET', path, headers);
- var body = "";
+ // Assemble the request options
+ var options = {
+ host: host,
+ port: port,
+ path: path,
+ method: method,
+ headers: headers
+ };
- client.on('error', function(err) {
- callback(err);
- });
-
- request.on('response', function (response) {
+ // Proceed with the request
+ var request = client.request(options, function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
body += chunk;
@@ -53,43 +75,18 @@ function get(url, callback, headers) {
response.on('end', function (chunk) {
callback(null, response, body);
});
+ }).on('error', function(error) {
+ console.log("HTTP request returned error: " + error.message);
+ callback(error);
});
+ // Send the request
+ if (data) {
+ request.write(data);
+ }
request.end();
}
-function post(url, reqBody, headers, callback) {
- var url = Url.parse(url);
- var host = url.hostname;
- var path = url.pathname;
- var body = body ? body : "";
- var headers = headers ? headers : {};
- var secure = (url.protocol == "https:") ? true : false;
- var port = secure ? 443 : 80;
-
- headers["Host"] = host;
- headers["Content-Length"] = reqBody.length + 1;
-
- if (url.search != undefined) path += url.search;
-
- var client = Http.createClient(port, host, secure);
- var request = client.request('POST', path, headers);
- var resBody = "";
-
- request.on('response', function (response) {
- response.setEncoding('utf8');
- response.on('data', function (chunk) {
- resBody += chunk;
- });
- response.on('end', function (chunk) {
- callback(null, response, resBody);
- });
- });
-
- request.write(reqBody);
- request.end();
-}
-
function response(res, message, code, type) {
var body = message;
var type = type ? type : 'text/html';
@@ -104,4 +101,5 @@ function response(res, message, code, type) {
exports.get = get;
exports.post = post;
+exports.request = request;
exports.response = response;
Please sign in to comment.
Something went wrong with that request. Please try again.