Advertisement
Guest User

Libcurl i windows, plik CPP

a guest
Jan 23rd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1.  
  2. #include <Autolauncher/CURLManager.h>
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #ifdef WIN32
  12. #include <io.h>
  13. #else
  14. #include <unistd.h>
  15. #endif
  16.  
  17. namespace aou {
  18.     //--------------------------------------------------------------------
  19.     namespace exc {
  20.         CURLInitFailed::CURLInitFailed() : aou::exc::Exception("CURLInitFailed") {
  21.  
  22.         }
  23.         //--------------------------------------------------------------------
  24.         CURLDownloadFileFailed::CURLDownloadFileFailed(const std::string& desc) : aou::exc::Exception("CURLDownloadFileFailed", desc) {
  25.  
  26.         }
  27.         //--------------------------------------------------------------------
  28.         CURLUploadFileFailed::CURLUploadFileFailed(const std::string& desc) : aou::exc::Exception("CURLUploadFileFailed", desc) {
  29.            
  30.         }
  31.         //--------------------------------------------------------------------
  32.     }
  33.     //--------------------------------------------------------------------
  34.     CURLManager::CURLManager() {
  35.         curl_state = curl_easy_init();
  36.         curl_global_init(CURL_GLOBAL_ALL);
  37.         if (!curl_state) {
  38.             throw aou::exc::CURLInitFailed();
  39.         }
  40.     }
  41.     //--------------------------------------------------------------------
  42.     CURLManager::~CURLManager() {
  43.         curl_easy_cleanup(curl_state);
  44.         curl_global_cleanup();
  45.         curl_state = nullptr;
  46.     }
  47.     //--------------------------------------------------------------------
  48.     CURLManager& CURLManager::GetInstance() {
  49.         static CURLManager curl_manager;
  50.         return curl_manager;
  51.     }
  52.     //--------------------------------------------------------------------
  53.     void CURLManager::DownloadFileFromHTTP(const std::string& url, const std::string& to_file) {
  54.         ResetCURLOptionsIfNessesaryAndUpdateLastOperationTypeEnum(CURLOperationType::DOWNLOAD_FILE);
  55.         if (curl_state) {
  56.             FILE* file = nullptr;
  57.             file = fopen(to_file.c_str(), "wb");
  58.             curl_easy_setopt(curl_state, CURLOPT_URL, url.c_str());
  59.             curl_easy_setopt(curl_state, CURLOPT_WRITEFUNCTION, CURLManager::curlfunction_write_data);
  60.             curl_easy_setopt(curl_state, CURLOPT_WRITEDATA, file);
  61.             curl_easy_setopt(curl_state, CURLOPT_VERBOSE, 0L);
  62.             CURLcode res;
  63.             res = curl_easy_perform(curl_state);
  64.             if (file)
  65.                 fclose(file);
  66.            
  67.             if (res != CURLE_OK) {
  68.                 throw aou::exc::CURLDownloadFileFailed("Download file failed. CURL returned exception called: ("+std::string(curl_easy_strerror(res))+")");
  69.             }
  70.         } else {
  71.             throw aou::exc::CURLInitFailed();
  72.         }
  73.     }
  74.     //--------------------------------------------------------------------
  75.     void CURLManager::UploadFileToFTP(const std::string& ftp_server_id, const std::string& directory_in_ftp_server, const std::string& from_file) {
  76.             ResetCURLOptionsIfNessesaryAndUpdateLastOperationTypeEnum(CURLOperationType::UPLOAD_FILE);
  77.             if (curl_state) {
  78.                 const FTPServer& ftp_server = GetFTPServer(ftp_server_id);
  79.  
  80.                 FILE* file;
  81.                 file = fopen(from_file.c_str(), "rb");
  82.  
  83.                 curl_easy_setopt(curl_state, CURLOPT_UPLOAD, 1L);
  84.                 curl_easy_setopt(curl_state, CURLOPT_READFUNCTION, CURLManager::curlfunction_read_data);
  85.                 curl_easy_setopt(curl_state, CURLOPT_URL, std::string(ftp_server.url +directory_in_ftp_server).c_str());
  86.                 curl_easy_setopt(curl_state, CURLOPT_READDATA, file);
  87.                 curl_easy_setopt(curl_state, CURLOPT_VERBOSE, 1L);
  88.                 curl_easy_setopt(curl_state, CURLOPT_USERPWD, std::string(ftp_server.login+":"+ftp_server.pass).c_str());
  89.  
  90.  
  91.  
  92.                 CURLcode res;
  93.                 res = curl_easy_perform(curl_state);
  94.  
  95.  
  96.                 if (res != CURLE_OK) {
  97.                     throw aou::exc::CURLUploadFileFailed("Upload file failed. CURL returned exception called: (" + std::string(curl_easy_strerror(res)) + ")");
  98.                 }
  99.                 if (file) {
  100.                     fclose(file);
  101.                 }
  102.             } else {
  103.                 throw aou::exc::CURLInitFailed();
  104.             }
  105.     }
  106.     //--------------------------------------------------------------------
  107.     void CURLManager::AddFTPServer(const std::string& ftp_server_id, const FTPServer& ftp_server) {
  108.         auto insert_pair = ftp_servers.insert(std::make_pair(ftp_server_id, ftp_server));
  109.         if (insert_pair.second == false) {
  110.             throw aou::exc::InsertionFailure("Unable to insert FTPServer");
  111.         }
  112.     }
  113.     //--------------------------------------------------------------------
  114.     void CURLManager::EraseFTPServer(const std::string& ftp_server_id) {
  115.         auto it = ftp_servers.find(ftp_server_id);
  116.         if (it != ftp_servers.end()) {
  117.             ftp_servers.erase(ftp_server_id);
  118.         } else {
  119.             throw aou::exc::ErasionFailure("Unable to erase FTPServer");
  120.         }
  121.     }
  122.     //--------------------------------------------------------------------
  123.     const FTPServer& CURLManager::GetFTPServer(const std::string& ftp_server_id) {
  124.         auto it = ftp_servers.find(ftp_server_id);
  125.         if (it != ftp_servers.end()) {
  126.             return it->second;
  127.         } else {
  128.             throw aou::exc::NotFoundFailure("Unable to find FTPServer");
  129.         }
  130.     }
  131.     //--------------------------------------------------------------------
  132.     void CURLManager::ResetCURLOptionsIfNessesaryAndUpdateLastOperationTypeEnum(CURLOperationType this_operation_type) {
  133.         if (last_operation_type == this_operation_type) {
  134.             //do nothing
  135.         } else {
  136.             curl_easy_reset(curl_state);
  137.             this->last_operation_type = this_operation_type;
  138.         }
  139.     }
  140.     //--------------------------------------------------------------------
  141.     size_t CURLManager::curlfunction_write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  142.             size_t written;
  143.             written = fwrite(ptr, size, nmemb, stream);
  144.             return written;
  145.     }
  146.     //--------------------------------------------------------------------
  147.     size_t CURLManager::curlfunction_read_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  148.         curl_off_t nread;
  149.         /* in real-world cases, this would probably get this data differently
  150.            as this fread() stuff is exactly what the library already would do
  151.            by default internally */
  152.         size_t retcode = fread(ptr, size, nmemb, stream);
  153.  
  154.         nread = (curl_off_t) retcode;
  155.  
  156.         fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  157.                 " bytes from file\n", nread);
  158.         return retcode;
  159.     }
  160.     //--------------------------------------------------------------------
  161. }
  162.  
  163. #ifdef UNIT_TESTS
  164.  
  165. #include <System/UnitTests.h>
  166. #include <System/TestFixtures.h>
  167.  
  168. namespace aou {
  169.     namespace tests {
  170.         //--------------------------------------------------------------------
  171.         TEST(CURLManager)
  172.         CURLManager& curl_manager = CURLManager::GetInstance();
  173.         curl_manager.DownloadFileFromHTTP("http://zapodaj.net/images/c29645adeaff7.png", "test.png");
  174.         FTPServer ftp_server;
  175.         ftp_server.url = "ftp://ftp_którego_nie_poznasz";
  176.         ftp_server.login = "login_którego_nie_poznasz";
  177.         ftp_server.pass = "hasło_którego_nie_poznasz";
  178.        
  179.         curl_manager.AddFTPServer("a", ftp_server);
  180.         curl_manager.UploadFileToFTP("a", "a.png", "test.png");
  181.        
  182.         TEST_END(CURLManager)
  183.         //--------------------------------------------------------------------
  184.     }
  185. }
  186.  
  187. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement