#include "curlRequest.h" //#define DEBUG_EXTRA #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) basicLogCollection::getInstance()->Log("curlRequest_T", NULL, X) #define SLOG_ERROR(X) basicLogCollection::getInstance()->Log("curlRequest_T", NULL, _LOG_ERROR, X) curlRequest::curlRequest(CSTR URL, CSTR cntType) { m_pLog = basicLogCollection::getInstance(); m_pCURL = NULL; m_pBaseObject = NULL; m_pBaseParam = NULL; m_pPostData = NULL; m_pSignalFunc = NULL; m_pMemoryBuffer = NULL; m_HeaderList = NULL; m_nMemorySize = 0; m_bRequestComplete = false; m_pURL = (CSTR) malloc(strlen(URL) + 1); m_cContentType[0] = (char) 0; if (cntType != NULL) { snprintf(m_cContentType, sizeof(m_cContentType), "Content-Type: %s", cntType); } if (m_pURL) { sprintf(m_pURL, "%s", URL); #ifdef DEBUG_EXTRA LOG(m_pURL); #endif init_CURL(); }; } curlRequest::curlRequest(CSTR Host, CSTR Path, CSTR cntType) { m_pLog = basicLogCollection::getInstance(); m_pCURL = NULL; m_pBaseObject = NULL; m_pBaseParam = NULL; m_pSignalFunc = NULL; m_pPostData = NULL; // was uninitialised: destructor freed a garbage pointer m_pMemoryBuffer = NULL; m_HeaderList = NULL; m_nMemorySize = 0; m_bRequestComplete = false; m_cContentType[0] = (char) 0; if (cntType != NULL) { snprintf(m_cContentType, sizeof(m_cContentType), "Content-Type: %s", cntType); } m_pURL = (CSTR) malloc(strlen(Host) + 1 + strlen(Path)); if (m_pURL) { sprintf(m_pURL, "%s%s", Host, Path); #ifdef DEBUG_EXTRA LOG(m_pURL); #endif init_CURL(); }; } curlRequest::~curlRequest() { #ifdef DEBUG #endif if (m_pCURL) { curl_easy_cleanup(m_pCURL); m_pCURL = NULL; } // The header list is not owned by the easy handle - it must be freed explicitly if (m_HeaderList) { curl_slist_free_all(m_HeaderList); m_HeaderList = NULL; } if (m_pURL) { free(m_pURL); } if (m_pPostData) { free(m_pPostData); } if (m_pMemoryBuffer) { free(m_pMemoryBuffer); } } #ifdef DEBUG_EXTRA int curlRequest::debug_callback(CURL * handle, curl_infotype type, char * data, size_t size, void * userptr) { FILE * tdFD = fopen("CURL_DEBUG.TXT", "ab+"); if (tdFD) { switch (type) { case CURLINFO_TEXT: fprintf(tdFD, "== Info: %s\n", data); break; case CURLINFO_HEADER_IN: fprintf(tdFD, "<= Recpt header\n"); fwrite(data, 1, size, tdFD); fprintf(tdFD, "\n"); break; case CURLINFO_HEADER_OUT: fprintf(tdFD, "=> Send header\n"); fwrite(data, 1, size, tdFD); fprintf(tdFD, "\n"); break; case CURLINFO_DATA_IN: fprintf(tdFD, "<= Recpt Data\n"); fwrite(data, 1, size, tdFD); fprintf(tdFD, "\n"); break; case CURLINFO_DATA_OUT: fprintf(tdFD, "=> Send Data\n"); fwrite(data, 1, size, tdFD); fprintf(tdFD, "\n"); break; case CURLINFO_SSL_DATA_IN: fprintf(tdFD, "<= Recpt SSL Data\n"); fwrite(data, 1, size, tdFD); fprintf(tdFD, "\n"); break; case CURLINFO_SSL_DATA_OUT: fprintf(tdFD, "=> Send SSL Data\n"); fwrite(data, 1, size, tdFD); fprintf(tdFD, "\n"); break; case CURLINFO_END: fprintf(tdFD, "End\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); break; default : fprintf(tdFD, "~~~~~~~~~ UNKNOWN ~~~~~~~~~~~~\n%s\n", data); }; fclose(tdFD); }; return 0; } #endif void curlRequest::init_CURL() { m_pCURL = curl_easy_init(); if (m_pCURL != NULL) { if (m_cContentType[0]) { m_HeaderList = curl_slist_append(m_HeaderList, m_cContentType); } //m_HeaderList = curl_slist_append( m_HeaderList, "Connection: Keep-Alive" ); m_HeaderList = curl_slist_append( m_HeaderList, "accept: application/json" ); // m_HeaderList = curl_slist_append( m_HeaderList, "Connection: Close" ); curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, m_HeaderList); curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 20); curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 20); curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0); #ifdef DEBUG_EXTRA curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 1); curl_easy_setopt(m_pCURL, CURLOPT_DEBUGFUNCTION, debug_callback); curl_easy_setopt(m_pCURL, CURLOPT_DEBUGDATA, this); #else curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0); #endif #ifdef __linux__ #if LIBCURL_VERSION_NUM >= 0x073C00 // 7.60 curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1); curl_easy_setopt(m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0); #endif #endif curl_easy_setopt(m_pCURL, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1); 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::setCredentials(CSTR user, CSTR password, uint8_t AuthMode){ if (! strlen(user) || ! strlen(password)){ return; } sprintf( m_cUserPassword, "%s:%s", user, password ); curl_easy_setopt( m_pCURL, CURLOPT_USERPWD, m_cUserPassword); switch (AuthMode){ case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break; case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break; case 3: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER ); break; //Does not works on 3D case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break; case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break; case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break; case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break; case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break; case 9: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY); break; //case 10:curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break; default: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); }; } static char * EmptyExpectConstant = (CSTR) "Expect:"; static char * EmptyAcceptConstant = (CSTR) "Accept: */*"; //static char * AcceptEncoding = (CSTR) "Accept-Encoding: gzip, deflate"; void curlRequest::setPOST(CSTR postDATA, int size) { if (m_pCURL != NULL) { if (! postDATA) { #ifdef DEBUG LOG_ERROR("No Post Data Supplied In Post Request!"); #endif return; }; if (! size) { size = strlen(postDATA); } if (m_pPostData) { free(m_pPostData); } m_pPostData = (CSTR) malloc(size + 1); if (! m_pPostData) { LOG_ERROR("Memory Allocation Error!"); return; }; memcpy(m_pPostData, postDATA, size); m_pPostData[size] = (char) 0; sprintf(m_cContentLength, "Content-Length: %d", size); m_HeaderList = curl_slist_append(m_HeaderList, EmptyExpectConstant); m_HeaderList = curl_slist_append(m_HeaderList, EmptyAcceptConstant); // m_HeaderList = curl_slist_append( m_HeaderList, AcceptEncoding ); m_HeaderList = curl_slist_append(m_HeaderList, m_cContentLength); curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, m_HeaderList); curl_easy_setopt(m_pCURL, CURLOPT_POST, 1); curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, m_pPostData); } else { LOG_ERROR("Wrong CURL handle!"); } } void curlRequest::setHandler(void * baseObject, void * (*start_routine)(void *, void *), void * ptr) { #ifdef DEBUG_EXTRA LOG("Set Handler"); #endif 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; #ifdef DEBUG_EXTRA SLOG("*<-D"); #endif BYTE * tMB = (BYTE *) realloc(me->m_pMemoryBuffer, me->m_nMemorySize + realsize + 1); // +1 to transfer String terminator if (tMB) { me->m_pMemoryBuffer = tMB; } else { // free(me->m_pMemoryBuffer); // me->m_pMemoryBuffer = NULL; // Well be Fried at destruction; SLOG_ERROR("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) { m_bRequestComplete = true; if (m_pSignalFunc != NULL) { (*m_pSignalFunc)(m_pBaseObject, m_pBaseParam); } }; } #undef LOG #undef LOG_ERROR #undef SLOG #undef SLOG_ERROR //#undef DEBUG_EXTRA