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.

193 lines
5.6 KiB

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