123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- #include "stdafx.h"
- #include "CurlClient.h"
- #include "CharEncoding.h"
- #include <sys/stat.h>
- CCurlClient::CCurlClient(void)
- {
- m_bDebug = FALSE;
- m_headers = NULL;
- }
- CCurlClient::~CCurlClient(void)
- {
-
- curl_global_cleanup();
- }
- INT CCurlClient::Initialize()
- {
-
- CURLcode res = ::curl_global_init( CURL_GLOBAL_ALL );
- if( CURLE_OK != res )
- {
- fprintf( stderr, "curl_global_init failed: %d \n", res );
- return -1;
- }
- return 0;
- }
- static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
- {
- if(itype == CURLINFO_TEXT)
- {
-
-
- }
- else if(itype == CURLINFO_HEADER_IN)
- {
-
-
- }
- else if(itype == CURLINFO_HEADER_OUT)
- {
-
-
- }
- else if(itype == CURLINFO_DATA_IN)
- {
-
-
- }
- else if(itype == CURLINFO_DATA_OUT)
- {
-
-
- }
- return 0;
- }
- size_t CCurlClient::OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream)
- {
- if( NULL == stream || NULL == ptr )
- return -1;
- stream->append((char*)ptr, size * nmemb);
- return nmemb;
- }
- int CCurlClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, long time_out )
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
- }
-
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
-
- curl_easy_setopt(curl, CURLOPT_POST, 1);
-
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
-
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
-
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
-
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30000);
-
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
-
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
-
- res = curl_easy_perform(curl);
-
-
- curl_easy_cleanup(curl);
-
- ClearHeaders();
- return res;
- }
- int CCurlClient::Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out )
- {
- if ( lpUrl == NULL || lpPost == NULL )
- return CURLE_FAILED_INIT;
- string strUrl;
- string strPost;
- string strResponse;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
- CharEncoding::UNICODE2ASCII((LPWCH)lpPost, strPost);
- int res = Post(strUrl, strPost, strResponse) ;
- if ( CURLE_OK == res )
- {
- CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
- return CURLE_OK;
- }
- return res;
- #else
- strUrl = lpUrl;
- strPost = lpPost;
- int res = Post(strUrl, strPost, strResponse, time_out) ;
- if ( CURLE_OK == res )
- {
- sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
- return CURLE_OK;
- }
- return res;
- #endif
- }
- int CCurlClient::Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out )
- {
- if ( strUrl.IsEmpty() || strPost.IsEmpty() )
- return CURLE_FAILED_INIT;
- string url;
- string post;
- string response;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
- CharEncoding::UNICODE2ASCII((LPWCH)strPost.GetString(), post);
- int res = Post(url, post, response) ;
- if ( CURLE_OK == res )
- {
- WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
- if ( pResult )
- {
- strResponse = pResult;
- delete []pResult;
- pResult = NULL;
- return CURLE_OK;
- }
- }
- return res;
- #else
- url = strUrl.GetString();
- post = strPost.GetString();
- int res = Post(url, post, response, time_out) ;
- if ( CURLE_OK == res )
- {
- strResponse = response.c_str();
- return CURLE_OK;
- }
- return res;
- #endif
- }
- int CCurlClient::Get(const std::string & strUrl, std::string & strResponse, long time_out )
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
- }
-
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
-
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
-
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
-
-
-
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-
- curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5000);
-
- curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
-
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
-
- ClearHeaders();
- return res;
- }
- int CCurlClient::Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out )
- {
- if ( lpUrl == NULL )
- return CURLE_FAILED_INIT;
- string strUrl;
- string strResponse;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
- int res = Get(strUrl, strResponse) ;
- if ( CURLE_OK == res )
- {
- CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
- return CURLE_OK;
- }
- return res;
- #else
- strUrl = lpUrl;
- int res = Get(strUrl, strResponse, time_out) ;
- if ( CURLE_OK == res )
- {
- sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
- return CURLE_OK;
- }
- return res;
- #endif
- }
- int CCurlClient::Get(IN CString& strUrl, OUT CString& strResponse, long time_out )
- {
- if ( strUrl.IsEmpty() )
- return CURLE_FAILED_INIT;
- string url;
- string post;
- string response;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
- int res = Get(url, response) ;
- if ( CURLE_OK == res )
- {
- WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
- if ( pResult )
- {
- strResponse = pResult;
- delete []pResult;
- pResult = NULL;
- return CURLE_OK;
- }
- }
- return res;
- #else
- url = strUrl.GetString();
- int res = Get(url, response, time_out) ;
- if ( CURLE_OK == res )
- {
- strResponse = response.c_str();
- return CURLE_OK;
- }
- return res;
- #endif
- }
- CURLcode CCurlClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, long time_out )
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
- }
-
-
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
-
- curl_easy_setopt(curl, CURLOPT_POST, 1);
-
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
-
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
-
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
-
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- if(NULL == pCaPath || pCaPath[0] == '\0')
- {
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- else
- {
-
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
- }
-
- curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5000);
-
- curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
-
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
-
-
- res = curl_easy_perform(curl);
-
-
- curl_easy_cleanup(curl);
-
- ClearHeaders();
- return res;
- }
- int CCurlClient::Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath , long time_out )
- {
- if ( lpUrl == NULL || lpPost == NULL )
- return CURLE_FAILED_INIT;
- string strUrl;
- string strPost;
- string strCapath;
- string strResponse;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
- CharEncoding::UNICODE2ASCII((LPWCH)lpPost, strPost);
- CharEncoding::UNICODE2ASCII((LPWCH)lpCaPath, strCapath);
- int res = Posts(strUrl, strPost, strResponse, strCapath.c_str()) ;
- if ( CURLE_OK == res )
- {
- CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
- return CURLE_OK;
- }
- return res;
- #else
- strUrl = lpUrl;
- strPost = lpPost;
- strCapath = lpCaPath;
- int res = Posts(strUrl, strPost, strResponse, strCapath.c_str(), time_out ) ;
- if ( CURLE_OK == res )
- {
- sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
- return CURLE_OK;
- }
- return res;
- #endif
- }
- int CCurlClient::Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath , long time_out )
- {
- if ( strUrl.IsEmpty() || strPost.IsEmpty() )
- return CURLE_FAILED_INIT;
- string url;
- string post;
- string capth;
- string response;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
- CharEncoding::UNICODE2ASCII((LPWCH)strPost.GetString(), post);
- CharEncoding::UNICODE2ASCII((LPWCH)strCaPath.GetString(), capth);
- int res = Posts(url, post, response, capth.c_str()) ;
- if ( CURLE_OK == res )
- {
- WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
- if ( pResult )
- {
- strResponse = pResult;
- delete []pResult;
- pResult = NULL;
- return CURLE_OK;
- }
- }
- return res;
- #else
- url = strUrl.GetString();
- post = strPost.GetString();
- capth = strCaPath.GetString();
- int res = Posts(url, post, response, capth.c_str(), time_out) ;
- if ( CURLE_OK == res )
- {
- strResponse = response.c_str();
- return CURLE_OK;
- }
- return res;
- #endif
- }
- int CCurlClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath, long time_out )
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if(NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- if(m_bDebug)
- {
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
- }
- curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- if(NULL == pCaPath || pCaPath[0] == '\0')
- {
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- else
- {
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
- }
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5000);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
-
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
-
- ClearHeaders();
- return res;
- }
- int CCurlClient::Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath , long time_out )
- {
- if ( lpUrl == NULL )
- return CURLE_FAILED_INIT;
- string strUrl;
- string strCapath;
- string strResponse;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
- CharEncoding::UNICODE2ASCII((LPWCH)lpCaPath, strCapath);
- int res = Gets(strUrl, strResponse, strCapath.c_str()) ;
- if ( CURLE_OK == res )
- {
- CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
- return CURLE_OK;
- }
- return res;
- #else
- strUrl = lpUrl;
- strCapath = lpCaPath;
- int res = Gets(strUrl, strResponse, strCapath.c_str(), time_out) ;
- if ( CURLE_OK == res )
- {
- sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
- return CURLE_OK;
- }
- return res;
- #endif
- }
- int CCurlClient::Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath , long time_out )
- {
- if ( strUrl.IsEmpty() )
- return CURLE_FAILED_INIT;
- string url;
- string post;
- string capth;
- string response;
- #ifdef UNICODE
- CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
- CharEncoding::UNICODE2ASCII((LPWCH)strCaPath.GetString(), capth);
- int res = Gets(url, response, capth.c_str()) ;
- if ( CURLE_OK == res )
- {
- WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
- if ( pResult )
- {
- strResponse = pResult;
- delete []pResult;
- pResult = NULL;
- return CURLE_OK;
- }
- }
- return res;
- #else
- url = strUrl.GetString();
- capth = strCaPath.GetString();
- int res = Gets(url, response, capth.c_str(), time_out) ;
- if ( CURLE_OK == res )
- {
- strResponse = response.c_str();
- return CURLE_OK;
- }
- return res;
- #endif
- }
- void CCurlClient::SetDebug(bool bDebug)
- {
- m_bDebug = bDebug;
- }
- void CCurlClient::SetHeaders(const std::string headers)
- {
- m_headers = curl_slist_append(m_headers, headers.c_str());
- }
- bool CCurlClient::DownloadEx(const std::string& url, const std::string& path, long time_out )
- {
- FILE* pf = NULL;
- curl_off_t local_file_len = -1;
- long file_size = 0;
- CURLcode res = CURLE_GOT_NOTHING;
- int c = 0;
- struct stat file_info;
- int use_resume = 0;
-
- if (stat(path.c_str(), &file_info) == 0)
- {
- local_file_len = file_info.st_size;
- use_resume = 1;
- }
-
- int nErr = _tfopen_s(&pf, path.c_str(), "ab+");
- if (pf)
- {
- CURL* curl = curl_easy_init();
- if (NULL == curl)
- {
-
- return false;
- }
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, time_out);
-
- curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, OnGetContentLength);
- curl_easy_setopt(curl, CURLOPT_HEADERDATA, &file_size);
- if (url.find("https://") != std::string::npos)
- {
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
- }
-
- curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, use_resume ? local_file_len : 0);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, pf);
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteFile);
- curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- res = curl_easy_perform(curl);
- fclose(pf);
- curl_easy_cleanup(curl);
- if (res == CURLE_OK)
- return true;
- }
- return false;
- }
- size_t CCurlClient::OnGetContentLength(void* ptr, size_t size, size_t nmemb, void* stream)
- {
- int r;
- long len = 0;
-
-
- r = sscanf_s((char*)ptr, "Content-Length: %ld\n", &len);
- if (r)
- *((long*)stream) = len;
- return size * nmemb;
- }
- size_t CCurlClient::OnWriteFile(const void* ptr, size_t size, size_t nmemb, void* stream)
- {
- if (NULL == stream || NULL == ptr)
- return -1;
- return fwrite(ptr, size, nmemb, (FILE*)stream);
- }
- int CCurlClient::FormPosts(std::string url, std::multimap<std::string, std::string> form_data, std::string& result, long time_out)
- {
- CURLcode res;
- CURL* curl = curl_easy_init();
- if (NULL == curl)
- {
- return CURLE_FAILED_INIT;
- }
- if (m_bDebug)
- {
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
- }
-
- std::string post;
- CURLFORMcode rmcode;
- struct curl_httppost* formpost = NULL;
- struct curl_httppost* lastptr = NULL;
- std::multimap<std::string, std::string>::iterator it = form_data.begin();
- #if 0
- for (; it != form_data.end(); )
- {
- post.append(it->first);
- post.append("=");
- post.append(it->second);
- if (++it != form_data.end())
- post.append("&");
- else
- break;
- }
-
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
-
- res = curl_easy_setopt(curl, CURLOPT_POST, 1);
- #else
- for (; it != form_data.end(); it++)
- {
- rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
- }
-
- res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
-
-
-
-
-
- #endif
-
- res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-
- res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
-
- res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
-
- res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&result);
-
- res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
-
- res = curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
-
- res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
-
- res = curl_easy_perform(curl);
-
- curl_easy_cleanup(curl);
-
- ClearHeaders();
-
- curl_formfree(formpost);
- return res;
- }
|