You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.4 KiB
137 lines
4.4 KiB
#include "curlRequest.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "curlRequest", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "curlRequest", this, _LOG_ERROR, X )
|
|
//Thread Logging
|
|
#define SLOG( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, X )
|
|
#define SLOG_ERROR( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, _LOG_ERROR, X )
|
|
|
|
curlRequest::curlRequest(CSTR URL, CSTR cntType ){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pBaseObject = NULL;
|
|
m_pBaseParam = NULL;
|
|
m_pPostData = NULL;
|
|
m_pSignalFunc = NULL;
|
|
m_pMemoryBuffer = NULL;
|
|
m_nMemorySize = 0;
|
|
m_pURL = (CSTR)malloc(strlen(URL)+1);
|
|
sprintf(m_pURL, "%s", URL);
|
|
m_pHost = NULL;
|
|
m_pPath = NULL;
|
|
//LOG(m_pURL);
|
|
m_bRequestComplete = false;
|
|
m_pCURL = curl_easy_init();
|
|
if( m_pCURL != NULL){
|
|
struct curl_slist *sList = NULL;
|
|
if( cntType != NULL ) {
|
|
sprintf( m_cContentType, "Content-Type: %s", cntType );
|
|
sList = curl_slist_append( sList, m_cContentType );
|
|
};
|
|
sList = curl_slist_append( sList, "Connection: Keep-Alive" );
|
|
if( sList != NULL ) {
|
|
curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList );
|
|
};
|
|
curl_easy_setopt(m_pCURL, CURLOPT_NOSIGNAL, 1);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_URL, m_pURL);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, m_fWriteMemoryHandler);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_USERAGENT, "Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)");
|
|
};
|
|
};
|
|
|
|
curlRequest::curlRequest(CSTR Host, CSTR Path, CSTR cntType ){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pBaseObject = NULL;
|
|
m_pBaseParam = NULL;
|
|
m_pSignalFunc = NULL;
|
|
m_pMemoryBuffer = NULL;
|
|
m_nMemorySize = 0;
|
|
m_pURL = (CSTR)malloc(strlen(Host) + 1 + strlen(Path));
|
|
sprintf(m_pURL, "%s%s", Host, Path );
|
|
m_pHost = (CSTR)malloc(strlen(Host)+1);
|
|
sprintf(m_pHost, "%s", Host);
|
|
m_pPath = (CSTR)malloc(strlen(Path)+1);
|
|
sprintf(m_pPath, "%s", Path);
|
|
//LOG(m_pURL);
|
|
m_bRequestComplete = false;
|
|
m_pCURL = curl_easy_init();
|
|
if (m_pCURL != NULL){
|
|
struct curl_slist *sList = NULL;
|
|
if( cntType != NULL ) {
|
|
sprintf( m_cContentType, "Content-Type: %s", cntType );
|
|
sList = curl_slist_append( sList, m_cContentType );
|
|
};
|
|
sList = curl_slist_append( sList, "Connection: Keep-Alive" );
|
|
if( sList != NULL ) {
|
|
curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList );
|
|
};
|
|
curl_easy_setopt(m_pCURL, CURLOPT_NOSIGNAL, 1);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_URL, m_pURL);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, m_fWriteMemoryHandler);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
|
|
curl_easy_setopt(m_pCURL, CURLOPT_USERAGENT, "Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)");
|
|
};
|
|
}
|
|
|
|
void curlRequest::setPOST( CSTR postDATA, int size ){
|
|
if (m_pCURL != NULL){
|
|
if( size > 0 ){
|
|
m_pPostData = (CSTR) malloc( size+1 );
|
|
if( m_pPostData == NULL ) return;
|
|
memcpy( m_pPostData, postDATA, size );
|
|
m_pPostData[size] = (char)0;
|
|
} else {
|
|
m_pPostData = (CSTR) malloc(strlen(postDATA)+1);
|
|
if( m_pPostData == NULL ) return;
|
|
sprintf( m_pPostData, "%s", postDATA );
|
|
};
|
|
curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, m_pPostData );
|
|
};
|
|
};
|
|
|
|
curlRequest::~curlRequest(){
|
|
//LOG("Destructor");
|
|
curl_easy_cleanup(m_pCURL);
|
|
if( m_pPostData ) free(m_pPostData );
|
|
if (m_pMemoryBuffer) free(m_pMemoryBuffer);
|
|
};
|
|
|
|
void curlRequest::setHandler(void *baseObject, void *(*start_routine) (void *, void*), void* ptr ){
|
|
//LOG("Set Handler");
|
|
m_pBaseObject = baseObject;
|
|
m_pSignalFunc = start_routine;
|
|
m_pBaseParam = ptr;
|
|
};
|
|
|
|
BYTE *curlRequest::getBuffer(void){
|
|
return m_pMemoryBuffer;
|
|
};
|
|
|
|
size_t curlRequest::m_fWriteMemoryHandler(void *ptr, size_t nsize, size_t nmemb, void *data){
|
|
size_t realsize = nsize * nmemb;
|
|
curlRequest *me = (curlRequest *)data;
|
|
me->m_pMemoryBuffer = (BYTE *)realloc(me->m_pMemoryBuffer, me->m_nMemorySize + realsize + 1); // +1 to transfer String terminator
|
|
if (me->m_pMemoryBuffer == NULL) {
|
|
/* out of memory! */
|
|
return -1;
|
|
};
|
|
memcpy(&(me->m_pMemoryBuffer[me->m_nMemorySize]), ptr, realsize);
|
|
me->m_nMemorySize += realsize;
|
|
me->m_pMemoryBuffer[me->m_nMemorySize] = 0;
|
|
return realsize;
|
|
};
|
|
|
|
void curlRequest::issue(void){
|
|
if( m_pCURL ){
|
|
//LOG("Issued");
|
|
m_bRequestComplete = true;
|
|
if ( m_pSignalFunc != NULL)(*m_pSignalFunc)(m_pBaseObject, m_pBaseParam);
|
|
//LOG("Issue Finished");
|
|
};
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
#undef SLOG
|
|
#undef SLOG_ERROR
|