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.

188 lines
5.4 KiB

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