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.

136 lines
4.4 KiB

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 ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, X )
  6. #define SLOG_ERROR( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, _LOG_ERROR, X )
  7. curlRequest::curlRequest(CSTR URL, CSTR cntType ){
  8. m_pLog = basicLogCollection::getInstance();
  9. m_pBaseObject = NULL;
  10. m_pBaseParam = NULL;
  11. m_pPostData = NULL;
  12. m_pSignalFunc = NULL;
  13. m_pMemoryBuffer = NULL;
  14. m_nMemorySize = 0;
  15. m_pURL = (CSTR)malloc(strlen(URL)+1);
  16. sprintf(m_pURL, "%s", URL);
  17. m_pHost = NULL;
  18. m_pPath = NULL;
  19. //LOG(m_pURL);
  20. m_bRequestComplete = false;
  21. m_pCURL = curl_easy_init();
  22. if( m_pCURL != NULL){
  23. struct curl_slist *sList = NULL;
  24. if( cntType != NULL ) {
  25. sprintf( m_cContentType, "Content-Type: %s", cntType );
  26. sList = curl_slist_append( sList, m_cContentType );
  27. };
  28. sList = curl_slist_append( sList, "Connection: Keep-Alive" );
  29. if( sList != NULL ) {
  30. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList );
  31. };
  32. curl_easy_setopt(m_pCURL, CURLOPT_NOSIGNAL, 1);
  33. curl_easy_setopt(m_pCURL, CURLOPT_URL, m_pURL);
  34. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, m_fWriteMemoryHandler);
  35. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  36. curl_easy_setopt(m_pCURL, CURLOPT_USERAGENT, "Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)");
  37. };
  38. };
  39. curlRequest::curlRequest(CSTR Host, CSTR Path, CSTR cntType ){
  40. m_pLog = basicLogCollection::getInstance();
  41. m_pBaseObject = NULL;
  42. m_pBaseParam = NULL;
  43. m_pSignalFunc = NULL;
  44. m_pMemoryBuffer = NULL;
  45. m_nMemorySize = 0;
  46. m_pURL = (CSTR)malloc(strlen(Host) + 1 + strlen(Path));
  47. sprintf(m_pURL, "%s%s", Host, Path );
  48. m_pHost = (CSTR)malloc(strlen(Host)+1);
  49. sprintf(m_pHost, "%s", Host);
  50. m_pPath = (CSTR)malloc(strlen(Path)+1);
  51. sprintf(m_pPath, "%s", Path);
  52. //LOG(m_pURL);
  53. m_bRequestComplete = false;
  54. m_pCURL = curl_easy_init();
  55. if (m_pCURL != NULL){
  56. struct curl_slist *sList = NULL;
  57. if( cntType != NULL ) {
  58. sprintf( m_cContentType, "Content-Type: %s", cntType );
  59. sList = curl_slist_append( sList, m_cContentType );
  60. };
  61. sList = curl_slist_append( sList, "Connection: Keep-Alive" );
  62. if( sList != NULL ) {
  63. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList );
  64. };
  65. curl_easy_setopt(m_pCURL, CURLOPT_NOSIGNAL, 1);
  66. curl_easy_setopt(m_pCURL, CURLOPT_URL, m_pURL);
  67. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, m_fWriteMemoryHandler);
  68. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  69. curl_easy_setopt(m_pCURL, CURLOPT_USERAGENT, "Mozilla/5.0 (PC; U; Intel; Linux; en) AppleWebKit/420+ (KHTML, like Gecko)");
  70. };
  71. }
  72. void curlRequest::setPOST( CSTR postDATA, int size ){
  73. if (m_pCURL != NULL){
  74. if( size > 0 ){
  75. m_pPostData = (CSTR) malloc( size+1 );
  76. if( m_pPostData == NULL ) return;
  77. memcpy( m_pPostData, postDATA, size );
  78. m_pPostData[size] = (char)0;
  79. } else {
  80. m_pPostData = (CSTR) malloc(strlen(postDATA)+1);
  81. if( m_pPostData == NULL ) return;
  82. sprintf( m_pPostData, "%s", postDATA );
  83. };
  84. curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, m_pPostData );
  85. };
  86. };
  87. curlRequest::~curlRequest(){
  88. //LOG("Destructor");
  89. curl_easy_cleanup(m_pCURL);
  90. if( m_pPostData ) free(m_pPostData );
  91. if (m_pMemoryBuffer) free(m_pMemoryBuffer);
  92. };
  93. void curlRequest::setHandler(void *baseObject, void *(*start_routine) (void *, void*), void* ptr ){
  94. //LOG("Set Handler");
  95. m_pBaseObject = baseObject;
  96. m_pSignalFunc = start_routine;
  97. m_pBaseParam = ptr;
  98. };
  99. BYTE *curlRequest::getBuffer(void){
  100. return m_pMemoryBuffer;
  101. };
  102. size_t curlRequest::m_fWriteMemoryHandler(void *ptr, size_t nsize, size_t nmemb, void *data){
  103. size_t realsize = nsize * nmemb;
  104. curlRequest *me = (curlRequest *)data;
  105. me->m_pMemoryBuffer = (BYTE *)realloc(me->m_pMemoryBuffer, me->m_nMemorySize + realsize + 1); // +1 to transfer String terminator
  106. if (me->m_pMemoryBuffer == NULL) {
  107. /* out of memory! */
  108. return -1;
  109. };
  110. memcpy(&(me->m_pMemoryBuffer[me->m_nMemorySize]), ptr, realsize);
  111. me->m_nMemorySize += realsize;
  112. me->m_pMemoryBuffer[me->m_nMemorySize] = 0;
  113. return realsize;
  114. };
  115. void curlRequest::issue(void){
  116. if( m_pCURL ){
  117. //LOG("Issued");
  118. m_bRequestComplete = true;
  119. if ( m_pSignalFunc != NULL)(*m_pSignalFunc)(m_pBaseObject, m_pBaseParam);
  120. //LOG("Issue Finished");
  121. };
  122. };
  123. #undef LOG
  124. #undef LOG_ERROR
  125. #undef SLOG
  126. #undef SLOG_ERROR