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.

232 lines
8.2 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. curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  125. #endif
  126. curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  127. curl_easy_setopt( m_pCURL, CURLOPT_TCP_FASTOPEN, 1 );
  128. curl_easy_setopt( m_pCURL, CURLOPT_TCP_NODELAY, 1 );
  129. curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  130. curl_easy_setopt( m_pCURL, CURLOPT_URL, m_pURL );
  131. curl_easy_setopt( m_pCURL, CURLOPT_WRITEFUNCTION, m_fWriteMemoryHandler );
  132. curl_easy_setopt( m_pCURL, CURLOPT_WRITEDATA, this );
  133. curl_easy_setopt( m_pCURL, CURLOPT_USERAGENT, "Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)" );
  134. };
  135. };
  136. void curlRequest::setCredentials( CSTR user, CSTR password, bool use_digest ){
  137. if( !strlen(user) || !strlen(password) ) return;
  138. sprintf(m_cUserPassword, "%s:%s", user, password );
  139. curl_easy_setopt( m_pCURL, CURLOPT_USERPWD, m_cUserPassword );
  140. if( use_digest ) {
  141. curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST );
  142. } else {
  143. curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
  144. };
  145. };
  146. static char *EmptyExpectConstant = (CSTR)"Expect:";
  147. static char *EmptyAcceptConstant = (CSTR)"Accept: */*";
  148. static char *AcceptEncoding = (CSTR)"Accept-Encoding: gzip, deflate";
  149. void curlRequest::setPOST( CSTR postDATA, int size ){
  150. if( m_pCURL != NULL ){
  151. if( !postDATA ){
  152. #ifdef DEBUG
  153. LOG_ERROR("No Post Data Supplied In Post Request!");
  154. #endif
  155. return;
  156. };
  157. if( !size ) size = strlen( postDATA );
  158. m_pPostData = (CSTR) malloc( size+1 );
  159. if( !m_pPostData ){
  160. LOG_ERROR("Memory Allocation Error!");
  161. return;
  162. };
  163. memcpy( m_pPostData, postDATA, size );
  164. m_pPostData[size] = (char)0;
  165. sprintf( m_cContentLength, "Content-Length: %d", size );
  166. m_HeaderList = curl_slist_append( m_HeaderList, EmptyExpectConstant );
  167. m_HeaderList = curl_slist_append( m_HeaderList, EmptyAcceptConstant );
  168. //m_HeaderList = curl_slist_append( m_HeaderList, AcceptEncoding );
  169. m_HeaderList = curl_slist_append( m_HeaderList, m_cContentLength );
  170. curl_easy_setopt( m_pCURL, CURLOPT_HTTPHEADER, m_HeaderList );
  171. curl_easy_setopt( m_pCURL, CURLOPT_POST, 1 );
  172. curl_easy_setopt( m_pCURL, CURLOPT_POSTFIELDS, m_pPostData );
  173. } else LOG_ERROR("Wrong CURL handle!");
  174. };
  175. void curlRequest::setHandler(void *baseObject, void *(*start_routine) (void *, void*), void* ptr ){
  176. #ifdef DEBUG_EXTRA
  177. LOG("Set Handler");
  178. #endif
  179. m_pBaseObject = baseObject;
  180. m_pSignalFunc = start_routine;
  181. m_pBaseParam = ptr;
  182. };
  183. BYTE *curlRequest::getBuffer(void){
  184. return m_pMemoryBuffer;
  185. };
  186. size_t curlRequest::m_fWriteMemoryHandler(void *ptr, size_t nsize, size_t nmemb, void *data){
  187. size_t realsize = nsize * nmemb;
  188. curlRequest *me = (curlRequest *)data;
  189. #ifdef DEBUG_EXTRA
  190. SLOG("*<-D");
  191. #endif
  192. BYTE *tMB = (BYTE*)realloc(me->m_pMemoryBuffer, me->m_nMemorySize + realsize + 1); // +1 to transfer String terminator
  193. if( tMB ){
  194. me->m_pMemoryBuffer = tMB;
  195. } else {
  196. //free(me->m_pMemoryBuffer);
  197. //me->m_pMemoryBuffer = NULL;
  198. //Well be Fried at destruction;
  199. SLOG_ERROR("Out Of Memory!");
  200. return -1;
  201. };
  202. memcpy(&(me->m_pMemoryBuffer[me->m_nMemorySize]), ptr, realsize);
  203. me->m_nMemorySize += realsize;
  204. me->m_pMemoryBuffer[me->m_nMemorySize] = 0;
  205. return realsize;
  206. };
  207. void curlRequest::issue(void){
  208. if( m_pCURL ){
  209. m_bRequestComplete = true;
  210. if ( m_pSignalFunc != NULL)(*m_pSignalFunc)(m_pBaseObject, m_pBaseParam);
  211. };
  212. };
  213. #undef LOG
  214. #undef LOG_ERROR
  215. #undef SLOG
  216. #undef SLOG_ERROR