Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow option to select encoding type #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
return orderedParameters;
}

exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) {
exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback, encoding ) {
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params);

if( !post_content_type ) {
Expand Down Expand Up @@ -327,6 +327,9 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
if( (method == "POST" || method == "PUT") && ( post_body == null && extra_params != null) ) {
post_body= querystring.stringify(extra_params);
}

if (encoding == null)
encoding = 'utf8';

headers["Content-length"]= post_body ? Buffer.byteLength(post_body) : 0;
headers["Content-Type"]= post_content_type;
Expand Down Expand Up @@ -360,7 +363,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
} else {
// Follow 301 or 302 redirects with Location HTTP header
if((response.statusCode == 301 || response.statusCode == 302) && response.headers && response.headers.location) {
self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback);
self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback, encoding);
}
else {
callback({ statusCode: response.statusCode, data: data }, data, response);
Expand All @@ -370,7 +373,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}

request.on('response', function (response) {
response.setEncoding('utf8');
response.setEncoding(encoding);
response.on('data', function (chunk) {
data+=chunk;
});
Expand Down Expand Up @@ -420,7 +423,7 @@ exports.OAuth.prototype.setClientOptions= function(options) {
this._clientOptions= mergedOptions;
};

exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback, encoding) {
var extraParams= {};
if( typeof oauth_verifier == "function" ) {
callback= oauth_verifier;
Expand All @@ -442,19 +445,19 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s
}

// Deprecated
exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", null, callback );
exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback, encoding) {
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", null, callback, encoding );
}

exports.OAuth.prototype.delete= function(url, oauth_token, oauth_token_secret, callback) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "DELETE", url, null, "", null, callback );
exports.OAuth.prototype.delete= function(url, oauth_token, oauth_token_secret, callback, encoding) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "DELETE", url, null, "", null, callback, encoding );
}

exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback, encoding) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback, encoding );
}

exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, encoding) {
var extra_params= null;
if( typeof post_content_type == "function" ) {
callback= post_content_type;
Expand All @@ -465,16 +468,16 @@ exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_tok
extra_params= post_body;
post_body= null;
}
return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback );
return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback, encoding );
}


exports.OAuth.prototype.put= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
return this._putOrPost("PUT", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback);
exports.OAuth.prototype.put= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, encoding) {
return this._putOrPost("PUT", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, encoding);
}

exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
return this._putOrPost("POST", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback);
exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, encoding) {
return this._putOrPost("POST", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, encoding);
}

/**
Expand All @@ -491,13 +494,13 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos
* getOAuthRequestToken( callbackFunction )
*
* 2) As above but allows for provision of extra parameters to be sent as part of the query to the server.
* getOAuthRequestToken( extraParams, callbackFunction )
* getOAuthRequestToken( extraParams, callback, encodingFunction )
*
* N.B. This method will HTTP POST verbs by default, if you wish to override this behaviour you will
* need to provide a requestTokenHttpMethod option when creating the client.
*
**/
exports.OAuth.prototype.getOAuthRequestToken= function( extraParams, callback ) {
exports.OAuth.prototype.getOAuthRequestToken= function( extraParams, callback, encoding ) {
if( typeof extraParams == "function" ){
callback = extraParams;
extraParams = {};
Expand Down