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.

198 lines
6.0 KiB

7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __GP_CURL_PROTOTYPE____
  2. #define __GP_CURL_PROTOTYPE____
  3. #pragma comment(lib, "wldap32.lib" )
  4. #pragma comment(lib, "crypt32.lib" )
  5. #pragma comment(lib, "Ws2_32.lib")
  6. #ifndef CURL_STATICLIB
  7. #define CURL_STATICLIB
  8. #endif
  9. #include <curl/curl.h>
  10. #include "../../components/base/basicLogCollection.h"
  11. #define DEFAULT_CONTENT_TYPE ((CSTR)"application/xml")
  12. #define DEFAULT_CONTENT_TYPE_JSON ((CSTR)"application/json")
  13. /*struct CURLMsg {
  14. CURLMSG msg; // what this message means
  15. CURL *easy_handle; // the handle it concerns
  16. union {
  17. void *whatever; // message-specific data
  18. CURLcode result; // return code for transfer
  19. } data;
  20. };
  21. */
  22. class curlRequest{
  23. private:
  24. basicLogCollection * m_pLog;
  25. CURL * m_pCURL;
  26. CSTR m_pURL;
  27. char m_cContentType[128];
  28. char m_cContentLength[128];
  29. BYTE * m_pMemoryBuffer;
  30. CSTR m_pPostData;
  31. struct curl_slist * m_HeaderList;
  32. size_t m_nMemorySize;
  33. bool m_bRequestComplete;
  34. void * m_pBaseObject;
  35. void * m_pBaseParam;
  36. char m_cUserPassword[256];
  37. void init_CURL();
  38. void * (*m_pSignalFunc) (void *, void*);
  39. static size_t m_fWriteMemoryHandler(void *ptr, size_t nsize, size_t nmemb, void *data);
  40. #ifdef DEBUG_EXTRA
  41. static int debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr );
  42. #endif
  43. public:
  44. explicit curlRequest( CSTR URL, CSTR cntType );
  45. curlRequest( CSTR Host, CSTR Path, CSTR cntType );
  46. ~curlRequest();
  47. BYTE * getBuffer(void);
  48. size_t getSize(void){ return m_nMemorySize; };
  49. CSTR getURL(void){ return m_pURL; };
  50. void setHandler(void *baseObject, void *(*start_routine) (void *, void*), void *ptr );
  51. void setCredentials( CSTR user, CSTR password, uint8_t AuthMode = 0 );
  52. void setPOST( CSTR postDATA , int size = 0 );
  53. void issue(void);
  54. bool isFinished(void){ return m_bRequestComplete; };
  55. CURL * getEH(void){ return m_pCURL; };
  56. };
  57. typedef vector< curlRequest * > RequestPool;
  58. #ifdef __linux__
  59. typedef vector<pthread_t> RequestThreadPool;
  60. #else
  61. typedef vector<HANDLE> RequestThreadPool;
  62. #endif
  63. #endif
  64. /*
  65. // prototypes
  66. int libcurl_progress_callback (void * clientp, double dltotal,
  67. double dlnow, double ultotal,
  68. double ulnow);
  69. size_t libcurl_read_callback(void * pBuffer, size_t size, size_t nmemb,
  70. void * hFile);
  71. void SSHUpload(char * strFileName, char * strFilePath);
  72. int _tmain(int argc, _TCHAR* argv[])
  73. {
  74. SSHUpload("test.txt", "C:\\");
  75. printf("Press any key to continue...");
  76. _getch();
  77. return 0;
  78. }
  79. void SSHUpload(char * strFileName, char * strFilePath)
  80. {
  81. char strBuffer[1024];
  82. CURL * hCurl;
  83. CURLcode ccCurlResult = CURL_LAST;
  84. curl_off_t cotFileSize;
  85. HANDLE hFile;
  86. LARGE_INTEGER liFileSize;
  87. // check parameters
  88. if((strFileName == NULL || strlen(strFileName) == 0) ||
  89. (strFilePath == NULL || strlen(strFilePath) == 0))
  90. return;
  91. // parse file path
  92. if(strFilePath[strlen(strFilePath) - 1] == '\\')
  93. sprintf_s(strBuffer, 1024, "%s%s", strFilePath, strFileName);
  94. else
  95. sprintf_s(strBuffer, 1024, "%s\\%s", strFilePath, strFileName);
  96. // create a handle to the file
  97. hFile = CreateFileA(strBuffer, // file to open
  98. GENERIC_READ, // open for reading
  99. FILE_SHARE_READ, // share for reading
  100. NULL, // default security
  101. OPEN_EXISTING, // existing file only
  102. FILE_ATTRIBUTE_NORMAL, // normal file
  103. NULL); // no attr. template
  104. if(hFile != INVALID_HANDLE_VALUE)
  105. {
  106. // global libcurl initialisation
  107. ccCurlResult = curl_global_init(CURL_GLOBAL_WIN32);
  108. if(ccCurlResult == 0)
  109. {
  110. // start libcurl easy session
  111. hCurl = curl_easy_init();
  112. if(hCurl)
  113. {
  114. // enable verbose operation
  115. curl_easy_setopt(hCurl, CURLOPT_VERBOSE, TRUE);
  116. // enable uploading
  117. curl_easy_setopt(hCurl, CURLOPT_UPLOAD, TRUE);
  118. // inform libcurl of the file's size
  119. GetFileSizeEx(hFile, &liFileSize);
  120. cotFileSize = liFileSize.QuadPart;
  121. curl_easy_setopt(hCurl,
  122. CURLOPT_INFILESIZE_LARGE,
  123. cotFileSize);
  124. // enable progress report function
  125. curl_easy_setopt(hCurl, CURLOPT_NOPROGRESS, FALSE);
  126. curl_easy_setopt(hCurl,
  127. CURLOPT_PROGRESSFUNCTION,
  128. libcurl_progress_callback);
  129. // use custom read function
  130. curl_easy_setopt(hCurl,
  131. CURLOPT_READFUNCTION,
  132. libcurl_read_callback);
  133. // specify which file to upload
  134. curl_easy_setopt(hCurl, CURLOPT_READDATA, hFile);
  135. // specify full path of uploaded file (i.e. server
  136. // address plus remote path)
  137. sprintf_s(strBuffer,
  138. 1024,
  139. "sftp://123.123.123.123/home/user/%s",
  140. strFileName);
  141. curl_easy_setopt(hCurl, CURLOPT_URL, strBuffer);
  142. // set SSH server port
  143. curl_easy_setopt(hCurl, CURLOPT_PORT, 22);
  144. // set SSH user name and password in libcurl in this
  145. // format "user:password"
  146. curl_easy_setopt(hCurl,
  147. CURLOPT_USERPWD,
  148. "user:password");
  149. // set SSH authentication to user name and password
  150. curl_easy_setopt(hCurl,
  151. CURLOPT_SSH_AUTH_TYPES,
  152. CURLSSH_AUTH_PASSWORD);
  153. // execute command
  154. ccCurlResult = curl_easy_perform(hCurl);
  155. // end libcurl easy session
  156. curl_easy_cleanup(hCurl);
  157. }
  158. }
  159. // release file handle
  160. CloseHandle(hFile);
  161. // global libcurl cleanup
  162. curl_global_cleanup();
  163. if (ccCurlResult == CURLE_OK)
  164. printf("File uploaded successfully.\n");
  165. else
  166. printf("File upload failed. Curl error: %d\n",
  167. ccCurlResult);
  168. }
  169. else
  170. printf("File upload failed! Could not open local file");
  171. }
  172. size_t libcurl_read_callback(void * pBuffer, size_t size,
  173. size_t nmemb, void * hFile)
  174. {
  175. DWORD dwNumberOfBytesRead = 0;
  176. BOOL bResult = ReadFile((HANDLE) hFile, pBuffer, size * nmemb,
  177. &dwNumberOfBytesRead, NULL);
  178. return dwNumberOfBytesRead;
  179. }
  180. int libcurl_progress_callback (void * clientp, double dltotal, double dlnow,
  181. double ultotal, double ulnow)
  182. {
  183. printf("Uploaded: %d / %d\n", (int) ulnow, (int) ultotal);
  184. return 0;
  185. }
  186. NOTE: You must replace some of the information in the sample code:
  187. line 17: enter a file name to be uploaded and its location on your computer
  188. line 93: type in your server’s IP address and the remote path where the file should
  189. be stored (here I assume that you have access to a computer that is
  190. running a SSH server)
  191. line 104: the login credentials for the SSH server
  192. */