Skip to content

Commit

Permalink
Added SSL support. v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Feb 23, 2011
1 parent e0b01dd commit 7a20a34
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 64 deletions.
4 changes: 4 additions & 0 deletions CHANGES
@@ -1,4 +1,8 @@

0.1.0
- added SSL support to HTTP requests
- removed the Client API, in accordance with Node

0.0.6
- added "host" header implicitly to request.read [#1 asutherland]

Expand Down
7 changes: 5 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "q-http",
"description": "Q promise based HTTP client and server interface",
"version": "0.0.7",
"version": "0.1.0",
"homepage": "http://github.com/kriskowal/q-http/",
"author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)",
"bugs": {
Expand All @@ -17,10 +17,13 @@
"main": "q-http.js",
"dependencies": {
"q-util": ">=0.0.1",
"q-io": ">=0.0.1"
"q-io": ">=0.0.4"
},
"repository": {
"type": "git",
"url": "http://github.com/kriskowal/q-http.git"
},
"engines": {
"node": ">=0.4.0"
}
}
91 changes: 29 additions & 62 deletions q-http.js
Expand Up @@ -7,6 +7,7 @@
/*whatsupdoc*/

var HTTP = require("http"); // node
var HTTPS = require("https"); // node
var URL = require("url"); // node
var Q = require("q-util");
var IO = require("q-io");
Expand Down Expand Up @@ -163,59 +164,38 @@ exports.Request = function (_request) {
};

/**
* Creates an HTTP client for issuing requests to
* the given host on the given port.
* @param {Number} port
* @param {String} host
* Issues an HTTP request.
*
* @param {Request {host, port, method, path, headers,
* body}} request (may be a promise)
* @returns {Promise * Response} promise for a response
*/
exports.Client = function (port, host) {
var self = Object.create(exports.Client.prototype);

var _client = HTTP.createClient(port, host);

var error = Q.defer();
_client.on("error", function (_error) {
error.resolve(_error);
});
exports.request = function (request) {
return Q.when(request, function (request) {

/***
* Issues an HTTP request. The request may be
* any object that has `method`, `path`, `headers`
* and `body` properties.
*
* * `method` `String` is optional, defaults to `"GET"`.
* * `path` `String` is optional, defaults to `"/"`.
* * `headers` `Object` is optional, defaults to `{}`.
* * `body` is optional, defaults to `[]`. Body must
* be an object with a `forEach` method that accepts a
* `write` callback. `forEach` may return a promise,
* and may send promises to `write`. `body` may be a
* promise.
*
* The Q HTTP `Server` responder receives a `Request`
* object that is suitable for `request`, and `request`
* returns a `Response` suitable for returning to the
* `Server`.
*
* @param {{method, path, headers, body}}
* @returns {Promise * Response}
*/
self.request = function (request) {
// host, port, method, path, headers, body
var deferred = Q.defer();
Q.when(error.promise, deferred.reject);
var _request = _client.request(
request.method || 'GET',
request.path || '/',
request.headers || {}
);
_request.on('response', function (_response) {
var response = exports.Response(_response);
deferred.resolve(response);
var ssl = request.ssl;
var http = ssl ? HTTPS : HTTP;
var _request = http.request({
"host": request.host,
"port": request.port || (ssl ? 443 : 80),
"path": request.path || "/",
"method": request.method || "GET",
"headers": request.headers || {}
}, function (_response) {
deferred.resolve(exports.Response(_response));
_response.on("error", function (error) {
// XXX find a better way to channel
// this into the response
console.warn(error && error.stack || error);
deferred.reject(error);
});
});

_request.on("error", function (error) {
deferred.reject(error);
});

Q.when(request.body, function (body) {
var end;
if (body) {
Expand All @@ -227,23 +207,8 @@ exports.Client = function (port, host) {
_request.end();
});
});
return deferred.promise;
};

return self;
};

/**
* Issues an HTTP request.
*
* @param {Request {host, port, method, path, headers,
* body}} request (may be a promise)
* @returns {Promise * Response} promise for a response
*/
exports.request = function (request) {
return Q.when(request, function (request) {
var client = exports.Client(request.port || 80, request.host);
return client.request(request);
return deferred.promise;
});
};

Expand All @@ -259,9 +224,11 @@ exports.request = function (request) {
*/
exports.read = function (url) {
url = URL.parse(url);
var ssl = url.protocol === "https:";
return Q.when(exports.request({
"host": url.hostname,
"port": url.port,
"ssl": ssl,
"method": "GET",
"path": (url.pathname || "") + (url.search || ""),
"headers": {
Expand Down

0 comments on commit 7a20a34

Please sign in to comment.