diff --git a/backends/twitter/libtwitcurl/oauthlib.cpp b/backends/twitter/libtwitcurl/oauthlib.cpp index d3403d428545dc6965a61858c988bfff3ee4029b..00732766304c652d4ba74b47b3e95c4d3bf986b6 100644 --- a/backends/twitter/libtwitcurl/oauthlib.cpp +++ b/backends/twitter/libtwitcurl/oauthlib.cpp @@ -1,3 +1,4 @@ +#include "twitcurlurls.h" #include "oauthlib.h" #include "HMAC_SHA1.h" #include "base64.h" @@ -43,16 +44,16 @@ oAuth::~oAuth() *--*/ oAuth oAuth::clone() { - oAuth cloneObj; - cloneObj.m_consumerKey = m_consumerKey; - cloneObj.m_consumerSecret = m_consumerSecret; - cloneObj.m_oAuthTokenKey = m_oAuthTokenKey; - cloneObj.m_oAuthTokenSecret = m_oAuthTokenSecret; - cloneObj.m_oAuthPin = m_oAuthPin; - cloneObj.m_nonce = m_nonce; - cloneObj.m_timeStamp = m_timeStamp; - cloneObj.m_oAuthScreenName = m_oAuthScreenName; - return cloneObj; + oAuth cloneObj; + cloneObj.m_consumerKey = m_consumerKey; + cloneObj.m_consumerSecret = m_consumerSecret; + cloneObj.m_oAuthTokenKey = m_oAuthTokenKey; + cloneObj.m_oAuthTokenSecret = m_oAuthTokenSecret; + cloneObj.m_oAuthPin = m_oAuthPin; + cloneObj.m_nonce = m_nonce; + cloneObj.m_timeStamp = m_timeStamp; + cloneObj.m_oAuthScreenName = m_oAuthScreenName; + return cloneObj; } @@ -254,7 +255,7 @@ void oAuth::generateNonceTimeStamp() char szRand[oAuthLibDefaults::OAUTHLIB_BUFFSIZE]; memset( szTime, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE ); memset( szRand, 0, oAuthLibDefaults::OAUTHLIB_BUFFSIZE ); - srand( time( NULL ) ); + srand( (unsigned int)time( NULL ) ); sprintf( szRand, "%x", rand()%1000 ); sprintf( szTime, "%ld", time( NULL ) ); @@ -285,35 +286,22 @@ void oAuth::buildOAuthRawDataKeyValPairs( const std::string& rawData, oAuthKeyValuePairs& rawDataKeyValuePairs ) { /* Raw data if it's present. Data should already be urlencoded once */ - if( rawData.length() ) + if( rawData.empty() ) { - size_t nSep = std::string::npos; - size_t nPos = std::string::npos; - std::string dataKeyVal; - std::string dataKey; - std::string dataVal; - - /* This raw data part can contain many key value pairs: key1=value1&key2=value2&key3=value3 */ - std::string dataPart = rawData; - while( std::string::npos != ( nSep = dataPart.find_first_of("&") ) ) - { - /* Extract first key=value pair */ - dataKeyVal = dataPart.substr( 0, nSep ); - - /* Split them */ - nPos = dataKeyVal.find_first_of( "=" ); - if( std::string::npos != nPos ) - { - dataKey = dataKeyVal.substr( 0, nPos ); - dataVal = dataKeyVal.substr( nPos + 1 ); - - /* Put this key=value pair in map */ - rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal; - } - dataPart = dataPart.substr( nSep + 1 ); - } + return; + } - /* For the last key=value */ + size_t nSep = std::string::npos; + size_t nPos = std::string::npos; + std::string dataKeyVal; + std::string dataKey; + std::string dataVal; + + /* This raw data part can contain many key value pairs: key1=value1&key2=value2&key3=value3 */ + std::string dataPart = rawData; + while( std::string::npos != ( nSep = dataPart.find_first_of("&") ) ) + { + /* Extract first key=value pair */ dataKeyVal = dataPart.substr( 0, nSep ); /* Split them */ @@ -326,6 +314,21 @@ void oAuth::buildOAuthRawDataKeyValPairs( const std::string& rawData, /* Put this key=value pair in map */ rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal; } + dataPart = dataPart.substr( nSep + 1 ); + } + + /* For the last key=value */ + dataKeyVal = dataPart.substr( 0, nSep ); + + /* Split them */ + nPos = dataKeyVal.find_first_of( "=" ); + if( std::string::npos != nPos ) + { + dataKey = dataKeyVal.substr( 0, nPos ); + dataVal = dataKeyVal.substr( nPos + 1 ); + + /* Put this key=value pair in map */ + rawDataKeyValuePairs[dataKey] = urlencodeData ? urlencode( dataVal ) : dataVal; } } @@ -390,7 +393,7 @@ bool oAuth::buildOAuthTokenKeyValuePairs( const bool includeOAuthVerifierPin, /* Version */ keyValueMap[oAuthLibDefaults::OAUTHLIB_VERSION_KEY] = std::string( "1.0" ); - return ( keyValueMap.size() ) ? true : false; + return !keyValueMap.empty(); } /*++ @@ -417,7 +420,7 @@ bool oAuth::getSignature( const eOAuthHttpRequestType eType, std::string sigBase; /* Initially empty signature */ - oAuthSignature.assign( "" ); + oAuthSignature = ""; /* Build a string using key-value pairs */ paramsSeperator = "&"; @@ -481,7 +484,7 @@ bool oAuth::getSignature( const eOAuthHttpRequestType eType, /* Do an url encode */ oAuthSignature = urlencode( base64Str ); - return ( oAuthSignature.length() ) ? true : false; + return !oAuthSignature.empty(); } /*++ @@ -511,7 +514,7 @@ bool oAuth::getOAuthHeader( const eOAuthHttpRequestType eType, std::string pureUrl( rawUrl ); /* Clear header string initially */ - oAuthHttpHeader.assign( "" ); + oAuthHttpHeader = ""; rawKeyValuePairs.clear(); /* If URL itself contains ?key=value, then extract and put them in map */ @@ -551,7 +554,7 @@ bool oAuth::getOAuthHeader( const eOAuthHttpRequestType eType, oAuthHttpHeader.assign( oAuthLibDefaults::OAUTHLIB_AUTHHEADER_STRING ); oAuthHttpHeader.append( rawParams ); - return ( oAuthHttpHeader.length() ) ? true : false; + return !oAuthHttpHeader.empty(); } /*++ @@ -571,48 +574,50 @@ bool oAuth::getStringFromOAuthKeyValuePairs( const oAuthKeyValuePairs& rawParamM std::string& rawParams, const std::string& paramsSeperator ) { - rawParams.assign( "" ); - if( rawParamMap.size() ) + rawParams = ""; + if( rawParamMap.empty() ) { - oAuthKeyValueList keyValueList; - std::string dummyStr; + return false; + } + + oAuthKeyValueList keyValueList; + std::string dummyStr; - /* Push key-value pairs to a list of strings */ - keyValueList.clear(); - oAuthKeyValuePairs::const_iterator itMap = rawParamMap.begin(); - for( ; itMap != rawParamMap.end(); itMap++ ) + /* Push key-value pairs to a list of strings */ + keyValueList.clear(); + oAuthKeyValuePairs::const_iterator itMap = rawParamMap.begin(); + for( ; itMap != rawParamMap.end(); itMap++ ) + { + dummyStr.assign( itMap->first ); + dummyStr.append( "=" ); + if( paramsSeperator == "," ) + { + dummyStr.append( "\"" ); + } + dummyStr.append( itMap->second ); + if( paramsSeperator == "," ) { - dummyStr.assign( itMap->first ); - dummyStr.append( "=" ); - if( paramsSeperator == "," ) - { - dummyStr.append( "\"" ); - } - dummyStr.append( itMap->second ); - if( paramsSeperator == "," ) - { - dummyStr.append( "\"" ); - } - keyValueList.push_back( dummyStr ); + dummyStr.append( "\"" ); } + keyValueList.push_back( dummyStr ); + } - /* Sort key-value pairs based on key name */ - keyValueList.sort(); + /* Sort key-value pairs based on key name */ + keyValueList.sort(); - /* Now, form a string */ - dummyStr.assign( "" ); - oAuthKeyValueList::iterator itKeyValue = keyValueList.begin(); - for( ; itKeyValue != keyValueList.end(); itKeyValue++ ) + /* Now, form a string */ + dummyStr = ""; + oAuthKeyValueList::iterator itKeyValue = keyValueList.begin(); + for( ; itKeyValue != keyValueList.end(); itKeyValue++ ) + { + if( dummyStr.length() ) { - if( dummyStr.length() ) - { - dummyStr.append( paramsSeperator ); - } - dummyStr.append( itKeyValue->c_str() ); - } - rawParams.assign( dummyStr ); + dummyStr.append( paramsSeperator ); + } + dummyStr.append( itKeyValue->c_str() ); } - return ( rawParams.length() ) ? true : false; + rawParams = dummyStr; + return !rawParams.empty(); } /*++ @@ -628,45 +633,49 @@ bool oAuth::getStringFromOAuthKeyValuePairs( const oAuthKeyValuePairs& rawParamM *--*/ bool oAuth::extractOAuthTokenKeySecret( const std::string& requestTokenResponse ) { - if( requestTokenResponse.length() ) + if( requestTokenResponse.empty() ) { - size_t nPos = std::string::npos; - std::string strDummy; + return false; + } - /* Get oauth_token key */ - nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKEN_KEY ); - if( std::string::npos != nPos ) - { - nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKEN_KEY.length() + strlen( "=" ); - strDummy = requestTokenResponse.substr( nPos ); - nPos = strDummy.find( "&" ); - if( std::string::npos != nPos ) - { - m_oAuthTokenKey = strDummy.substr( 0, nPos ); - } - } + size_t nPos = std::string::npos; + std::string strDummy; - /* Get oauth_token_secret */ - nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY ); + /* Get oauth_token key */ + nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKEN_KEY ); + if( std::string::npos != nPos ) + { + nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKEN_KEY.length() + strlen( "=" ); + strDummy = requestTokenResponse.substr( nPos ); + nPos = strDummy.find( "&" ); if( std::string::npos != nPos ) { - nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY.length() + strlen( "=" ); - strDummy = requestTokenResponse.substr( nPos ); - nPos = strDummy.find( "&" ); - if( std::string::npos != nPos ) - { - m_oAuthTokenSecret = strDummy.substr( 0, nPos ); - } + m_oAuthTokenKey = strDummy.substr( 0, nPos ); } + } - /* Get screen_name */ - nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY ); + /* Get oauth_token_secret */ + nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY ); + if( std::string::npos != nPos ) + { + nPos = nPos + oAuthLibDefaults::OAUTHLIB_TOKENSECRET_KEY.length() + strlen( "=" ); + strDummy = requestTokenResponse.substr( nPos ); + nPos = strDummy.find( "&" ); if( std::string::npos != nPos ) { - nPos = nPos + oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY.length() + strlen( "=" ); - strDummy = requestTokenResponse.substr( nPos ); - m_oAuthScreenName = strDummy; + m_oAuthTokenSecret = strDummy.substr( 0, nPos ); } } + + /* Get screen_name */ + nPos = requestTokenResponse.find( oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY ); + if( std::string::npos != nPos ) + { + nPos = nPos + oAuthLibDefaults::OAUTHLIB_SCREENNAME_KEY.length() + strlen( "=" ); + strDummy = requestTokenResponse.substr( nPos ); + m_oAuthScreenName = strDummy; + } + return true; } +