Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Autolauncher/CURLManager.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #ifdef WIN32
- #include <io.h>
- #else
- #include <unistd.h>
- #endif
- namespace aou {
- //--------------------------------------------------------------------
- namespace exc {
- CURLInitFailed::CURLInitFailed() : aou::exc::Exception("CURLInitFailed") {
- }
- //--------------------------------------------------------------------
- CURLDownloadFileFailed::CURLDownloadFileFailed(const std::string& desc) : aou::exc::Exception("CURLDownloadFileFailed", desc) {
- }
- //--------------------------------------------------------------------
- CURLUploadFileFailed::CURLUploadFileFailed(const std::string& desc) : aou::exc::Exception("CURLUploadFileFailed", desc) {
- }
- //--------------------------------------------------------------------
- }
- //--------------------------------------------------------------------
- CURLManager::CURLManager() {
- curl_state = curl_easy_init();
- curl_global_init(CURL_GLOBAL_ALL);
- if (!curl_state) {
- throw aou::exc::CURLInitFailed();
- }
- }
- //--------------------------------------------------------------------
- CURLManager::~CURLManager() {
- curl_easy_cleanup(curl_state);
- curl_global_cleanup();
- curl_state = nullptr;
- }
- //--------------------------------------------------------------------
- CURLManager& CURLManager::GetInstance() {
- static CURLManager curl_manager;
- return curl_manager;
- }
- //--------------------------------------------------------------------
- void CURLManager::DownloadFileFromHTTP(const std::string& url, const std::string& to_file) {
- ResetCURLOptionsIfNessesaryAndUpdateLastOperationTypeEnum(CURLOperationType::DOWNLOAD_FILE);
- if (curl_state) {
- FILE* file = nullptr;
- file = fopen(to_file.c_str(), "wb");
- curl_easy_setopt(curl_state, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl_state, CURLOPT_WRITEFUNCTION, CURLManager::curlfunction_write_data);
- curl_easy_setopt(curl_state, CURLOPT_WRITEDATA, file);
- curl_easy_setopt(curl_state, CURLOPT_VERBOSE, 0L);
- CURLcode res;
- res = curl_easy_perform(curl_state);
- if (file)
- fclose(file);
- if (res != CURLE_OK) {
- throw aou::exc::CURLDownloadFileFailed("Download file failed. CURL returned exception called: ("+std::string(curl_easy_strerror(res))+")");
- }
- } else {
- throw aou::exc::CURLInitFailed();
- }
- }
- //--------------------------------------------------------------------
- void CURLManager::UploadFileToFTP(const std::string& ftp_server_id, const std::string& directory_in_ftp_server, const std::string& from_file) {
- ResetCURLOptionsIfNessesaryAndUpdateLastOperationTypeEnum(CURLOperationType::UPLOAD_FILE);
- if (curl_state) {
- const FTPServer& ftp_server = GetFTPServer(ftp_server_id);
- FILE* file;
- file = fopen(from_file.c_str(), "rb");
- curl_easy_setopt(curl_state, CURLOPT_UPLOAD, 1L);
- curl_easy_setopt(curl_state, CURLOPT_READFUNCTION, CURLManager::curlfunction_read_data);
- curl_easy_setopt(curl_state, CURLOPT_URL, std::string(ftp_server.url +directory_in_ftp_server).c_str());
- curl_easy_setopt(curl_state, CURLOPT_READDATA, file);
- curl_easy_setopt(curl_state, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl_state, CURLOPT_USERPWD, std::string(ftp_server.login+":"+ftp_server.pass).c_str());
- CURLcode res;
- res = curl_easy_perform(curl_state);
- if (res != CURLE_OK) {
- throw aou::exc::CURLUploadFileFailed("Upload file failed. CURL returned exception called: (" + std::string(curl_easy_strerror(res)) + ")");
- }
- if (file) {
- fclose(file);
- }
- } else {
- throw aou::exc::CURLInitFailed();
- }
- }
- //--------------------------------------------------------------------
- void CURLManager::AddFTPServer(const std::string& ftp_server_id, const FTPServer& ftp_server) {
- auto insert_pair = ftp_servers.insert(std::make_pair(ftp_server_id, ftp_server));
- if (insert_pair.second == false) {
- throw aou::exc::InsertionFailure("Unable to insert FTPServer");
- }
- }
- //--------------------------------------------------------------------
- void CURLManager::EraseFTPServer(const std::string& ftp_server_id) {
- auto it = ftp_servers.find(ftp_server_id);
- if (it != ftp_servers.end()) {
- ftp_servers.erase(ftp_server_id);
- } else {
- throw aou::exc::ErasionFailure("Unable to erase FTPServer");
- }
- }
- //--------------------------------------------------------------------
- const FTPServer& CURLManager::GetFTPServer(const std::string& ftp_server_id) {
- auto it = ftp_servers.find(ftp_server_id);
- if (it != ftp_servers.end()) {
- return it->second;
- } else {
- throw aou::exc::NotFoundFailure("Unable to find FTPServer");
- }
- }
- //--------------------------------------------------------------------
- void CURLManager::ResetCURLOptionsIfNessesaryAndUpdateLastOperationTypeEnum(CURLOperationType this_operation_type) {
- if (last_operation_type == this_operation_type) {
- //do nothing
- } else {
- curl_easy_reset(curl_state);
- this->last_operation_type = this_operation_type;
- }
- }
- //--------------------------------------------------------------------
- size_t CURLManager::curlfunction_write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
- size_t written;
- written = fwrite(ptr, size, nmemb, stream);
- return written;
- }
- //--------------------------------------------------------------------
- size_t CURLManager::curlfunction_read_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
- curl_off_t nread;
- /* in real-world cases, this would probably get this data differently
- as this fread() stuff is exactly what the library already would do
- by default internally */
- size_t retcode = fread(ptr, size, nmemb, stream);
- nread = (curl_off_t) retcode;
- fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
- " bytes from file\n", nread);
- return retcode;
- }
- //--------------------------------------------------------------------
- }
- #ifdef UNIT_TESTS
- #include <System/UnitTests.h>
- #include <System/TestFixtures.h>
- namespace aou {
- namespace tests {
- //--------------------------------------------------------------------
- TEST(CURLManager)
- CURLManager& curl_manager = CURLManager::GetInstance();
- curl_manager.DownloadFileFromHTTP("http://zapodaj.net/images/c29645adeaff7.png", "test.png");
- FTPServer ftp_server;
- ftp_server.url = "ftp://ftp_którego_nie_poznasz";
- ftp_server.login = "login_którego_nie_poznasz";
- ftp_server.pass = "hasło_którego_nie_poznasz";
- curl_manager.AddFTPServer("a", ftp_server);
- curl_manager.UploadFileToFTP("a", "a.png", "test.png");
- TEST_END(CURLManager)
- //--------------------------------------------------------------------
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement