|
|
#ifndef __GP_CURL_PROTOTYPE____
#define __GP_CURL_PROTOTYPE____
#pragma comment(lib, "wldap32.lib" )
#pragma comment(lib, "crypt32.lib" )
#pragma comment(lib, "Ws2_32.lib")
#ifndef CURL_STATICLIB
#define CURL_STATICLIB
#endif
#include <curl/curl.h>
#include "../../components/base/basicLogCollection.h"
#define DEFAULT_CONTENT_TYPE ((CSTR)"application/xml")
#define DEFAULT_CONTENT_TYPE_JSON ((CSTR)"application/json")
/*struct CURLMsg {
CURLMSG msg; // what this message means
CURL *easy_handle; // the handle it concerns
union { void *whatever; // message-specific data
CURLcode result; // return code for transfer
} data;};*/
class curlRequest{ private: basicLogCollection * m_pLog; CURL * m_pCURL; CSTR m_pURL; char m_cContentType[128]; char m_cContentLength[128]; BYTE * m_pMemoryBuffer; CSTR m_pPostData; struct curl_slist * m_HeaderList; size_t m_nMemorySize; bool m_bRequestComplete; void * m_pBaseObject; void * m_pBaseParam; char m_cUserPassword[256]; void init_CURL(); void * (*m_pSignalFunc) (void *, void*); static size_t m_fWriteMemoryHandler(void *ptr, size_t nsize, size_t nmemb, void *data);#ifdef DEBUG_EXTRA
static int debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr );#endif
public: explicit curlRequest( CSTR URL, CSTR cntType ); curlRequest( CSTR Host, CSTR Path, CSTR cntType ); ~curlRequest(); BYTE * getBuffer(void); size_t getSize(void){ return m_nMemorySize; }; CSTR getURL(void){ return m_pURL; }; void setHandler(void *baseObject, void *(*start_routine) (void *, void*), void *ptr ); void setCredentials( CSTR user, CSTR password, uint8_t AuthMode = 0 ); void setPOST( CSTR postDATA , int size = 0 ); void issue(void); bool isFinished(void){ return m_bRequestComplete; }; CURL * getEH(void){ return m_pCURL; };};
typedef vector< curlRequest * > RequestPool;#ifdef __linux__
typedef vector<pthread_t> RequestThreadPool;#else
typedef vector<HANDLE> RequestThreadPool;#endif
#endif
/*
// prototypes
int libcurl_progress_callback (void * clientp, double dltotal,double dlnow, double ultotal,double ulnow);size_t libcurl_read_callback(void * pBuffer, size_t size, size_t nmemb,void * hFile);void SSHUpload(char * strFileName, char * strFilePath);int _tmain(int argc, _TCHAR* argv[]){SSHUpload("test.txt", "C:\\");printf("Press any key to continue...");_getch();return 0;}void SSHUpload(char * strFileName, char * strFilePath){char strBuffer[1024];CURL * hCurl;CURLcode ccCurlResult = CURL_LAST;curl_off_t cotFileSize;HANDLE hFile;LARGE_INTEGER liFileSize;// check parameters
if((strFileName == NULL || strlen(strFileName) == 0) ||(strFilePath == NULL || strlen(strFilePath) == 0))return;// parse file path
if(strFilePath[strlen(strFilePath) - 1] == '\\')sprintf_s(strBuffer, 1024, "%s%s", strFilePath, strFileName);elsesprintf_s(strBuffer, 1024, "%s\\%s", strFilePath, strFileName);// create a handle to the file
hFile = CreateFileA(strBuffer, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if(hFile != INVALID_HANDLE_VALUE){// global libcurl initialisation
ccCurlResult = curl_global_init(CURL_GLOBAL_WIN32);if(ccCurlResult == 0){// start libcurl easy session
hCurl = curl_easy_init();if(hCurl){// enable verbose operation
curl_easy_setopt(hCurl, CURLOPT_VERBOSE, TRUE);// enable uploading
curl_easy_setopt(hCurl, CURLOPT_UPLOAD, TRUE);// inform libcurl of the file's size
GetFileSizeEx(hFile, &liFileSize);cotFileSize = liFileSize.QuadPart;curl_easy_setopt(hCurl,CURLOPT_INFILESIZE_LARGE,cotFileSize);// enable progress report function
curl_easy_setopt(hCurl, CURLOPT_NOPROGRESS, FALSE);curl_easy_setopt(hCurl,CURLOPT_PROGRESSFUNCTION,libcurl_progress_callback);// use custom read function
curl_easy_setopt(hCurl,CURLOPT_READFUNCTION,libcurl_read_callback);// specify which file to upload
curl_easy_setopt(hCurl, CURLOPT_READDATA, hFile);// specify full path of uploaded file (i.e. server
// address plus remote path)
sprintf_s(strBuffer,1024,"sftp://123.123.123.123/home/user/%s",strFileName);curl_easy_setopt(hCurl, CURLOPT_URL, strBuffer);// set SSH server port
curl_easy_setopt(hCurl, CURLOPT_PORT, 22);// set SSH user name and password in libcurl in this
// format "user:password"
curl_easy_setopt(hCurl,CURLOPT_USERPWD,"user:password");// set SSH authentication to user name and password
curl_easy_setopt(hCurl,CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_PASSWORD);// execute command
ccCurlResult = curl_easy_perform(hCurl);// end libcurl easy session
curl_easy_cleanup(hCurl);}}// release file handle
CloseHandle(hFile);// global libcurl cleanup
curl_global_cleanup();if (ccCurlResult == CURLE_OK)printf("File uploaded successfully.\n");elseprintf("File upload failed. Curl error: %d\n",ccCurlResult);}elseprintf("File upload failed! Could not open local file");}size_t libcurl_read_callback(void * pBuffer, size_t size,size_t nmemb, void * hFile){DWORD dwNumberOfBytesRead = 0;BOOL bResult = ReadFile((HANDLE) hFile, pBuffer, size * nmemb,&dwNumberOfBytesRead, NULL);return dwNumberOfBytesRead;}int libcurl_progress_callback (void * clientp, double dltotal, double dlnow,double ultotal, double ulnow){printf("Uploaded: %d / %d\n", (int) ulnow, (int) ultotal);return 0;}NOTE: You must replace some of the information in the sample code: line 17: enter a file name to be uploaded and its location on your computer line 93: type in your server’s IP address and the remote path where the file shouldbe stored (here I assume that you have access to a computer that isrunning a SSH server) line 104: the login credentials for the SSH server
*/
|