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.

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