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.

220 lines
5.8 KiB

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