don't send the request using https if the uri is http

This commit is contained in:
Damien Mathieu 2011-09-24 14:10:46 +02:00
parent 99e625968a
commit 32620902a5

View File

@ -1,12 +1,13 @@
var querystring= require('querystring'), var querystring= require('querystring'),
crypto= require('crypto'), crypto= require('crypto'),
https= require('https'), https= require('https'),
http= require('http'),
URL= require('url'), URL= require('url'),
OAuthUtils= require('./_utils'); OAuthUtils= require('./_utils');
exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath) { exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath) {
this._clientId= clientId; this._clientId= clientId;
this._clientSecret= clientSecret; this._clientSecret= clientSecret;
this._baseSite= baseSite; this._baseSite= baseSite;
this._authorizeUrl= authorizePath || "/oauth/authorize"; this._authorizeUrl= authorizePath || "/oauth/authorize";
this._accessTokenUrl= accessTokenPath || "/oauth/access_token"; this._accessTokenUrl= accessTokenPath || "/oauth/access_token";
@ -28,10 +29,15 @@ exports.OAuth2.prototype._getAccessTokenUrl= function() {
exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) { exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) {
var creds = crypto.createCredentials({ }); var creds = crypto.createCredentials({ });
var parsedUrl= URL.parse( url, true ); var parsedUrl= URL.parse( url, true );
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443; if( parsedUrl.protocol == "https:" && !parsedUrl.port ) {
parsedUrl.port= 443;
http_library = https;
} else {
http_library = http;
}
var realHeaders= {}; var realHeaders= {};
if( headers ) { if( headers ) {
for(var key in headers) { for(var key in headers) {
@ -72,7 +78,7 @@ exports.OAuth2.prototype._request= function(method, url, headers, post_body, acc
} }
} }
request = https.request(options, function (response) { request = http_library.request(options, function (response) {
response.on("data", function (chunk) { response.on("data", function (chunk) {
result+= chunk result+= chunk
}); });
@ -94,7 +100,7 @@ exports.OAuth2.prototype._request= function(method, url, headers, post_body, acc
request.write(post_body); request.write(post_body);
} }
request.end(); request.end();
} }
exports.OAuth2.prototype.getAuthorizeUrl= function( params ) { exports.OAuth2.prototype.getAuthorizeUrl= function( params ) {
@ -139,7 +145,7 @@ exports.OAuth2.prototype.getOAuthAccessToken= function(code, params, callback) {
callback(null, access_token, refresh_token); callback(null, access_token, refresh_token);
} }
}); });
} }
// Deprecated // Deprecated
exports.OAuth2.prototype.getProtectedResource= function(url, access_token, callback) { exports.OAuth2.prototype.getProtectedResource= function(url, access_token, callback) {