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.

235 lines
8.3 KiB

7 years ago
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "curlRequest.h"
  2. #define LOG( X ) m_pLog->Log( "curlRequest", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "curlRequest", this, _LOG_ERROR, X )
  4. //Thread Logging
  5. #define SLOG( X ) basicLogCollection::getInstance()->Log( "curlRequest_T", NULL, X )
  6. #define SLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "curlRequest_T", NULL, _LOG_ERROR, X )
  7. curlRequest::curlRequest(CSTR URL, CSTR cntType ){
  8. m_pLog = basicLogCollection::getInstance();
  9. m_pCURL = NULL;
  10. m_pBaseObject = NULL;
  11. m_pBaseParam = NULL;
  12. m_pPostData = NULL;
  13. m_pSignalFunc = NULL;
  14. m_pMemoryBuffer = NULL;
  15. m_HeaderList = NULL;
  16. m_nMemorySize = 0;
  17. m_bRequestComplete = false;
  18. m_pURL = (CSTR)malloc(strlen(URL)+1);
  19. if( cntType != NULL ) sprintf( m_cContentType, "Content-Type: %s", cntType );
  20. if( m_pURL ) {
  21. sprintf( m_pURL, "%s", URL );
  22. #ifdef DEBUG_EXTRA
  23. LOG(m_pURL);
  24. #endif
  25. init_CURL();
  26. };
  27. };
  28. curlRequest::curlRequest(CSTR Host, CSTR Path, CSTR cntType ){
  29. m_pLog = basicLogCollection::getInstance();
  30. m_pCURL = NULL;
  31. m_pBaseObject = NULL;
  32. m_pBaseParam = NULL;
  33. m_pSignalFunc = NULL;
  34. m_pMemoryBuffer = NULL;
  35. m_HeaderList = NULL;
  36. m_nMemorySize = 0;
  37. m_bRequestComplete = false;
  38. if( cntType != NULL ) sprintf( m_cContentType, "Content-Type: %s", cntType );
  39. m_pURL = (CSTR)malloc(strlen(Host) + 1 + strlen(Path));
  40. if( m_pURL ) {
  41. sprintf( m_pURL, "%s%s", Host, Path );
  42. #ifdef DEBUG_EXTRA
  43. LOG(m_pURL);
  44. #endif
  45. init_CURL();
  46. };
  47. };
  48. curlRequest::~curlRequest(){
  49. #ifdef DEBUG
  50. #endif
  51. curl_easy_cleanup(m_pCURL);
  52. if( m_pURL ) free(m_pURL);
  53. if( m_pPostData ) free(m_pPostData);
  54. if( m_pMemoryBuffer ) free(m_pMemoryBuffer);
  55. };
  56. #ifdef DEBUG_EXTRA
  57. int curlRequest::debug_callback( CURL *handle, curl_infotype type, char *data, size_t size, void *userptr ){
  58. FILE *tdFD = fopen("CURL_DEBUG.TXT","ab+");
  59. if( tdFD ) {
  60. switch( type ) {
  61. case CURLINFO_TEXT:
  62. fprintf( tdFD, "== Info: %s\n", data );
  63. break;
  64. case CURLINFO_HEADER_IN:
  65. fprintf( tdFD, "<= Recpt header\n");
  66. fwrite( data, 1, size, tdFD );
  67. fprintf( tdFD, "\n" );
  68. break;
  69. case CURLINFO_HEADER_OUT:
  70. fprintf( tdFD, "=> Send header\n");
  71. fwrite( data, 1, size, tdFD );
  72. fprintf( tdFD, "\n" );
  73. break;
  74. case CURLINFO_DATA_IN:
  75. fprintf( tdFD, "<= Recpt Data\n" );
  76. fwrite( data, 1, size, tdFD );
  77. fprintf( tdFD, "\n" );
  78. break;
  79. case CURLINFO_DATA_OUT:
  80. fprintf( tdFD, "=> Send Data\n");
  81. fwrite( data, 1, size, tdFD );
  82. fprintf( tdFD, "\n" );
  83. break;
  84. case CURLINFO_SSL_DATA_IN:
  85. fprintf( tdFD, "<= Recpt SSL Data\n" );
  86. fwrite( data, 1, size, tdFD );
  87. fprintf( tdFD, "\n" );
  88. break;
  89. case CURLINFO_SSL_DATA_OUT:
  90. fprintf( tdFD, "=> Send SSL Data\n");
  91. fwrite( data, 1, size, tdFD );
  92. fprintf( tdFD, "\n" );
  93. break;
  94. case CURLINFO_END:
  95. fprintf( tdFD, "End\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n" );
  96. break;
  97. default:
  98. fprintf( tdFD, "~~~~~~~~~ UNKNOWN ~~~~~~~~~~~~\n%s\n", data );
  99. };
  100. fclose( tdFD );
  101. };
  102. return 0;
  103. };
  104. #endif
  105. void curlRequest::init_CURL(){
  106. m_pCURL = curl_easy_init();
  107. if( m_pCURL != NULL ){
  108. m_HeaderList = curl_slist_append( m_HeaderList, m_cContentType );
  109. //m_HeaderList = curl_slist_append( m_HeaderList, "Connection: Keep-Alive" );
  110. //m_HeaderList = curl_slist_append( m_HeaderList, "Connection: Close" );
  111. curl_easy_setopt( m_pCURL, CURLOPT_HTTPHEADER, m_HeaderList );
  112. curl_easy_setopt( m_pCURL, CURLOPT_CONNECTTIMEOUT, 20 );
  113. curl_easy_setopt( m_pCURL, CURLOPT_TIMEOUT, 20 );
  114. curl_easy_setopt( m_pCURL, CURLOPT_SSL_VERIFYPEER, 0 );
  115. curl_easy_setopt( m_pCURL, CURLOPT_SSL_VERIFYHOST, 0 );
  116. #ifdef DEBUG_EXTRA
  117. curl_easy_setopt( m_pCURL, CURLOPT_VERBOSE, 1 );
  118. curl_easy_setopt( m_pCURL, CURLOPT_DEBUGFUNCTION, debug_callback );
  119. curl_easy_setopt( m_pCURL, CURLOPT_DEBUGDATA, this );
  120. #else
  121. curl_easy_setopt( m_pCURL, CURLOPT_VERBOSE, 0 );
  122. #endif
  123. #ifdef __linux__
  124. #if LIBCURL_VERSION_NUM >= 0x073C00 //7.60
  125. curl_easy_setopt( m_pCURL, CURLOPT_TCP_FASTOPEN, 1 );
  126. curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  127. #endif
  128. #endif
  129. curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  130. curl_easy_setopt( m_pCURL, CURLOPT_TCP_NODELAY, 1 );
  131. curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  132. curl_easy_setopt( m_pCURL, CURLOPT_URL, m_pURL );
  133. curl_easy_setopt( m_pCURL, CURLOPT_WRITEFUNCTION, m_fWriteMemoryHandler );
  134. curl_easy_setopt( m_pCURL, CURLOPT_WRITEDATA, this );
  135. curl_easy_setopt( m_pCURL, CURLOPT_USERAGENT, "Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)" );
  136. };
  137. };
  138. void curlRequest::setCredentials( CSTR user, CSTR password, bool use_digest ){
  139. if( !strlen(user) || !strlen(password) ) return;
  140. sprintf(m_cUserPassword, "%s:%s", user, password );
  141. curl_easy_setopt( m_pCURL, CURLOPT_USERPWD, m_cUserPassword );
  142. if( use_digest ) {
  143. curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST );
  144. } else {
  145. curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
  146. };
  147. };
  148. static char *EmptyExpectConstant = (CSTR)"Expect:";
  149. static char *EmptyAcceptConstant = (CSTR)"Accept: */*";
  150. static char *AcceptEncoding = (CSTR)"Accept-Encoding: gzip, deflate";
  151. void curlRequest::setPOST( CSTR postDATA, int size ){
  152. if( m_pCURL != NULL ){
  153. if( !postDATA ){
  154. #ifdef DEBUG
  155. LOG_ERROR("No Post Data Supplied In Post Request!");
  156. #endif
  157. return;
  158. };
  159. if( !size ) size = strlen( postDATA );
  160. m_pPostData = (CSTR) malloc( size+1 );
  161. if( !m_pPostData ){
  162. LOG_ERROR("Memory Allocation Error!");
  163. return;
  164. };
  165. memcpy( m_pPostData, postDATA, size );
  166. m_pPostData[size] = (char)0;
  167. sprintf( m_cContentLength, "Content-Length: %d", size );
  168. m_HeaderList = curl_slist_append( m_HeaderList, EmptyExpectConstant );
  169. m_HeaderList = curl_slist_append( m_HeaderList, EmptyAcceptConstant );
  170. //m_HeaderList = curl_slist_append( m_HeaderList, AcceptEncoding );
  171. m_HeaderList = curl_slist_append( m_HeaderList, m_cContentLength );
  172. curl_easy_setopt( m_pCURL, CURLOPT_HTTPHEADER, m_HeaderList );
  173. curl_easy_setopt( m_pCURL, CURLOPT_POST, 1 );
  174. curl_easy_setopt( m_pCURL, CURLOPT_POSTFIELDS, m_pPostData );
  175. } else LOG_ERROR("Wrong CURL handle!");
  176. };
  177. void curlRequest::setHandler(void *baseObject, void *(*start_routine) (void *, void*), void* ptr ){
  178. #ifdef DEBUG_EXTRA
  179. LOG("Set Handler");
  180. #endif
  181. m_pBaseObject = baseObject;
  182. m_pSignalFunc = start_routine;
  183. m_pBaseParam = ptr;
  184. };
  185. BYTE *curlRequest::getBuffer(void){
  186. return m_pMemoryBuffer;
  187. };
  188. size_t curlRequest::m_fWriteMemoryHandler(void *ptr, size_t nsize, size_t nmemb, void *data){
  189. size_t realsize = nsize * nmemb;
  190. curlRequest *me = (curlRequest *)data;
  191. #ifdef DEBUG_EXTRA
  192. SLOG("*<-D");
  193. #endif
  194. BYTE *tMB = (BYTE*)realloc(me->m_pMemoryBuffer, me->m_nMemorySize + realsize + 1); // +1 to transfer String terminator
  195. if( tMB ){
  196. me->m_pMemoryBuffer = tMB;
  197. } else {
  198. //free(me->m_pMemoryBuffer);
  199. //me->m_pMemoryBuffer = NULL;
  200. //Well be Fried at destruction;
  201. SLOG_ERROR("Out Of Memory!");
  202. return -1;
  203. };
  204. memcpy(&(me->m_pMemoryBuffer[me->m_nMemorySize]), ptr, realsize);
  205. me->m_nMemorySize += realsize;
  206. me->m_pMemoryBuffer[me->m_nMemorySize] = 0;
  207. return realsize;
  208. };
  209. void curlRequest::issue(void){
  210. if( m_pCURL ){
  211. m_bRequestComplete = true;
  212. if ( m_pSignalFunc != NULL)(*m_pSignalFunc)(m_pBaseObject, m_pBaseParam);
  213. };
  214. };
  215. #undef LOG
  216. #undef LOG_ERROR
  217. #undef SLOG
  218. #undef SLOG_ERROR