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.

192 lines
5.5 KiB

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