From c71da950ee5a88418f643b5a1011f9e8f6d05388 Mon Sep 17 00:00:00 2001 From: Vladimir Gorshenkov Date: Thu, 13 May 2021 18:43:25 +0300 Subject: [PATCH] [Common] add network lib --- Common/3dParty/ixwebsocket/ixwebsocket.pri | 12 +- .../FileTransporter/include/FileTransporter.h | 97 ++++ .../FileTransporter/src/FileTransporter.cpp | 177 ++++++++ .../src/FileTransporter_curl.cpp | 243 ++++++++++ .../src/FileTransporter_mac.mm | 136 ++++++ .../src/FileTransporter_private.h | 199 ++++++++ .../src/FileTransporter_win.cpp | 424 ++++++++++++++++++ .../FileTransporter/src/transport_external.h | 168 +++++++ .../WebSocket/include}/websocket.h | 47 +- .../WebSocket/src/base/WebWorkerBase.h} | 44 +- .../src/ixwebsocket}/ixwebsocket_internal.cpp | 73 +-- .../src/ixwebsocket}/ixwebsocket_internal.h | 47 +- .../src/manager}/managerWebSocket.cpp | 51 ++- .../src/socketrocket/socketRocket_internal.h} | 44 +- .../socketrocket}/socketRocket_internal.mm | 85 ++-- .../src/socketrocket}/socketRocket_objc.h | 10 +- .../src/socketrocket}/socketRocket_objc.mm | 49 +- Common/{ => Network}/WebSocket/websocket.pri | 0 Common/Network/network.pro | 118 +++++ 19 files changed, 1800 insertions(+), 224 deletions(-) create mode 100644 Common/Network/FileTransporter/include/FileTransporter.h create mode 100644 Common/Network/FileTransporter/src/FileTransporter.cpp create mode 100644 Common/Network/FileTransporter/src/FileTransporter_curl.cpp create mode 100644 Common/Network/FileTransporter/src/FileTransporter_mac.mm create mode 100644 Common/Network/FileTransporter/src/FileTransporter_private.h create mode 100644 Common/Network/FileTransporter/src/FileTransporter_win.cpp create mode 100644 Common/Network/FileTransporter/src/transport_external.h rename Common/{WebSocket => Network/WebSocket/include}/websocket.h (62%) rename Common/{WebSocket/socketRocket_internal.h => Network/WebSocket/src/base/WebWorkerBase.h} (64%) rename Common/{WebSocket => Network/WebSocket/src/ixwebsocket}/ixwebsocket_internal.cpp (54%) rename Common/{WebSocket => Network/WebSocket/src/ixwebsocket}/ixwebsocket_internal.h (70%) rename Common/{WebSocket => Network/WebSocket/src/manager}/managerWebSocket.cpp (66%) rename Common/{WebSocket/WebWorkerBase.h => Network/WebSocket/src/socketrocket/socketRocket_internal.h} (72%) rename Common/{WebSocket => Network/WebSocket/src/socketrocket}/socketRocket_internal.mm (58%) rename Common/{WebSocket => Network/WebSocket/src/socketrocket}/socketRocket_objc.h (89%) rename Common/{WebSocket => Network/WebSocket/src/socketrocket}/socketRocket_objc.mm (76%) rename Common/{ => Network}/WebSocket/websocket.pri (100%) create mode 100644 Common/Network/network.pro diff --git a/Common/3dParty/ixwebsocket/ixwebsocket.pri b/Common/3dParty/ixwebsocket/ixwebsocket.pri index 8f5ed8b125..de90adfc68 100644 --- a/Common/3dParty/ixwebsocket/ixwebsocket.pri +++ b/Common/3dParty/ixwebsocket/ixwebsocket.pri @@ -57,11 +57,11 @@ core_windows { LIBS += $$PWD/../ixwebsocket/IXWebSocket/build/windows/$$CORE_BUILDS_PLATFORM_PREFIX/lib/ixwebsocket.lib LIBS += $$PWD/../openssl/build/$$CORE_BUILDS_PLATFORM_PREFIX/lib/libssl.lib LIBS += $$PWD/../openssl/build/$$CORE_BUILDS_PLATFORM_PREFIX/lib/libcrypto.lib - LIBS += -lwsock32 - LIBS += -lws2_32 - LIBS += -lIphlpapi - LIBS += -lcrypt32 - LIBS += -lUser32 - LIBS += -lshlwapi + LIBS += -lwsock32 + LIBS += -lws2_32 + LIBS += -lIphlpapi + LIBS += -lcrypt32 + LIBS += -lUser32 + LIBS += -lshlwapi } diff --git a/Common/Network/FileTransporter/include/FileTransporter.h b/Common/Network/FileTransporter/include/FileTransporter.h new file mode 100644 index 0000000000..cd75ed785e --- /dev/null +++ b/Common/Network/FileTransporter/include/FileTransporter.h @@ -0,0 +1,97 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ +#pragma once + +#include +#include "../../../kernel_config.h" + +namespace NSNetwork +{ + namespace NSFileTransport + { + typedef void (*CFileTransporter_OnComplete)(int error); + // cancel: 1, else 0 + typedef int (*CFileTransporter_OnProgress)(int percent); + + class CFileTransporter_private; + class KERNEL_DECL CFileTransporter + { + protected: + // создаем в зависимости от платформы + CFileTransporter_private* m_pInternal; + + #ifdef _MAC + static bool m_bIsARCEnabled; + #endif + + public: + CFileTransporter(std::wstring &sDownloadFileUrl, bool bDelete = true); //download constructor + CFileTransporter(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize); //upload constructor + virtual ~CFileTransporter(); + + void SetDownloadFilePath(const std::wstring& sPath); + std::wstring GetDownloadFilePath(); + bool IsFileDownloaded(); + void SetDownloadProp(std::wstring &sDownloadFileUrl, bool bDelete = true); + bool DownloadSync(); + void DownloadAsync(); + + void SetUploadProp(std::wstring &url, unsigned char* data, const int size); + bool UploadSync(); + void UploadAsync(); + + std::wstring& GetResponse(); + + void Start(int lPriority); + void Suspend(); + void Resume(); + void Stop(); + + int IsSuspended(); + int IsRunned(); + int GetError(); + + int GetPriority(); + + void CheckSuspend(); + + //events + void SetEvent_OnProgress(CFileTransporter_OnProgress); + void SetEvent_OnComplete(CFileTransporter_OnComplete); + + #ifdef _MAC + static void SetARCEnabled(const bool& enabled); + static bool GetARCEnabled(); + #endif + }; + } +} diff --git a/Common/Network/FileTransporter/src/FileTransporter.cpp b/Common/Network/FileTransporter/src/FileTransporter.cpp new file mode 100644 index 0000000000..89a0e7e5b7 --- /dev/null +++ b/Common/Network/FileTransporter/src/FileTransporter.cpp @@ -0,0 +1,177 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ +#pragma once + +#include "./FileTransporter_private.h" +#include "../include/FileTransporter.h" + +namespace NSNetwork +{ + namespace NSFileTransport + { + CFileTransporter::CFileTransporter(std::wstring &sDownloadFileUrl, bool bDelete) + { + m_pInternal = new CFileTransporter_private(sDownloadFileUrl, bDelete); + } + + CFileTransporter::CFileTransporter(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + m_pInternal = new CFileTransporter_private(sUploadPathUrl, cData, nSize); + } + + CFileTransporter::~CFileTransporter() + { + Stop(); + if (NULL != m_pInternal) + delete m_pInternal; + } + + void CFileTransporter::SetDownloadFilePath(const std::wstring& sPath) + { + return m_pInternal->SetDownloadFilePath(sPath); + } + + std::wstring CFileTransporter::GetDownloadFilePath() + { + return m_pInternal->GetDownloadFilePath(); + } + + bool CFileTransporter::IsFileDownloaded() + { + return m_pInternal->IsFileDownloaded(); + } + + void CFileTransporter::SetDownloadProp(std::wstring &sDownloadFileUrl, bool bDelete) + { + m_pInternal->SetDownloadProp(sDownloadFileUrl, bDelete); + } + + bool CFileTransporter::DownloadSync() + { + return m_pInternal->TransferSync(); + } + + void CFileTransporter::DownloadAsync() + { + m_pInternal->TransferAsync(); + } + + void CFileTransporter::SetUploadProp(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + m_pInternal->SetUploadProp(sUploadPathUrl, cData, nSize); + } + + bool CFileTransporter::UploadSync() + { + return m_pInternal->TransferSync(); + } + + void CFileTransporter::UploadAsync() + { + m_pInternal->TransferAsync(); + } + + std::wstring& CFileTransporter::GetResponse() + { + return m_pInternal->GetResponse(); + } + + void CFileTransporter::Start(int lPriority) + { + return m_pInternal->Start(lPriority); + } + + void CFileTransporter::Suspend() + { + return m_pInternal->Suspend(); + } + + void CFileTransporter::Resume() + { + return m_pInternal->Resume(); + } + + void CFileTransporter::Stop() + { + return m_pInternal->Stop(); + } + + int CFileTransporter::IsSuspended() + { + return m_pInternal->IsSuspended(); + } + + int CFileTransporter::IsRunned() + { + return m_pInternal->IsRunned(); + } + + int CFileTransporter::GetError() + { + return m_pInternal->GetError(); + } + + int CFileTransporter::GetPriority() + { + return m_pInternal->GetPriority(); + } + + void CFileTransporter::CheckSuspend() + { + return m_pInternal->CheckSuspend(); + } + + void CFileTransporter::SetEvent_OnProgress(CFileTransporter_OnProgress func) + { + m_pInternal->GetInternal()->m_func_onProgress = func; + } + + void CFileTransporter::SetEvent_OnComplete(CFileTransporter_OnComplete func) + { + m_pInternal->GetInternal()->m_func_onComplete = func; + } + + #ifdef _MAC + bool CFileTransporter::m_bIsARCEnabled = false; + + void CFileTransporter::SetARCEnabled(const bool& enabled) + { + m_bIsARCEnabled = enabled; + } + + bool CFileTransporter::GetARCEnabled() + { + return m_bIsARCEnabled; + } + #endif + } +} diff --git a/Common/Network/FileTransporter/src/FileTransporter_curl.cpp b/Common/Network/FileTransporter/src/FileTransporter_curl.cpp new file mode 100644 index 0000000000..8aa8fb9d19 --- /dev/null +++ b/Common/Network/FileTransporter/src/FileTransporter_curl.cpp @@ -0,0 +1,243 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ + +#include "FileTransporter_private.h" + +#include +#include +#include "../../DesktopEditor/common/Directory.h" + +#ifndef USE_EXTERNAL_TRANSPORT + +#include +#include +#include +#include + +#else + +#include "transport_external.h" + +#endif + +namespace NSNetwork +{ + namespace NSFileTransport + { + class CFileTransporterBaseCURL : public CFileTransporterBase + { + public : + CFileTransporterBaseCURL(std::wstring &sDownloadFileUrl, bool bDelete = true) + : CFileTransporterBase(sDownloadFileUrl, bDelete) + { + } + CFileTransporterBaseCURL(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + : CFileTransporterBase(sUploadPathUrl, cData, nSize) + { + } + virtual ~CFileTransporterBaseCURL() + { + if (m_bDelete && !m_sFilePath.empty()) + { + std::string sFilePath = U_TO_UTF8(m_sFilePath); + unlink(sFilePath.c_str()); + } + } + + #ifndef USE_EXTERNAL_TRANSPORT + static size_t write_data(void *ptr, size_t size, size_t nmemb, int fd) { + size_t written = write(fd, ptr, size * nmemb); + return written; + } + + static size_t write_data_to_string(char *contents, size_t size, size_t nmemb, void *userp) + { + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; + } + + virtual int DownloadFile() + { + CURL *curl; + int fp; + CURLcode res; + std::string sUrl = U_TO_UTF8(m_sFileUrl); + std::string sOut; + const char *url = sUrl.c_str(); + curl = curl_easy_init(); + if (curl) + { + fp = createUniqueTempFile(sOut); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + #if defined(__linux__) + //в linux нет встроенных в систему корневых сертификатов, поэтому отключаем проверку + //http://curl.haxx.se/docs/sslcerts.html + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + #endif + /* tell libcurl to follow redirection(default false) */ + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + /* some servers don't like requests that are made without a user-agent field, so we provide one */ + curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"); + res = curl_easy_perform(curl); + /* always cleanup */ + curl_easy_cleanup(curl); + close(fp); + } + + m_bComplete = (CURLE_OK == res); + if (m_bComplete) + { + if (m_sFilePath.empty()) + m_sFilePath = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sOut.c_str(), sOut.length()); + else + NSFile::CFileBinary::Move(UTF8_TO_U(sOut), m_sFilePath); + } + //int nRes = execl("/usr/bin/wget", stringWstingToUtf8String (m_sFileUrl).c_str(), "-P", stringWstingToUtf8String (m_sFilePath).c_str(), (char *)NULL); + //m_bComplete = nRes >= 0; + + return m_bComplete ? 0 : 1; + } + + virtual int UploadFile() + { + CURL *curl; + CURLcode res; + std::string sUrl = U_TO_UTF8(m_sUploadUrl); + const char *url = sUrl.c_str(); + struct curl_slist *headerlist = NULL; + std::string readBuffer; + + /* get a curl handle */ + curl = curl_easy_init(); + if(curl) { + + headerlist = curl_slist_append(headerlist, "Content-Type: application/octet-stream"); + + curl_easy_setopt(curl, CURLOPT_POST, true); + curl_easy_setopt(curl, CURLOPT_HEADER, true); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); + /* First set the URL that is about to receive our POST. This URL can + just as well be a https:// URL if that is what should receive the + data. */ + curl_easy_setopt(curl, CURLOPT_URL, url); + /* Now specify the POST data */ + /* size of the POST data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, m_nSize); + /* binary data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, m_cData); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_to_string); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + + #if defined(__linux__) + //в linux нет встроенных в систему корневых сертификатов, поэтому отключаем проверку + //http://curl.haxx.se/docs/sslcerts.html + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + #endif + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + + long http_code = 0; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + + if (res == CURLE_OK) + { + if (http_code == 200 || http_code == 1223) + { + size_t startLenghtPos = readBuffer.find("Content-Length:") + sizeof("Content-Length:"); + size_t endLenghtPos = readBuffer.substr(startLenghtPos, readBuffer.length()).find("\r"); + std::string dataSize = readBuffer.substr(startLenghtPos, endLenghtPos); + readBuffer = readBuffer.substr(readBuffer.length() - std::stoi(dataSize)); + NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)readBuffer.c_str(), (LONG)readBuffer.length(), m_sResponse); + } + else + { + res = CURLE_HTTP_RETURNED_ERROR; + } + } + + /* always cleanup */ + curl_easy_cleanup(curl); + } + m_bComplete = (CURLE_OK == res); + + return m_bComplete ? 0 : 1; + } + + protected: + int createUniqueTempFile (std::string &filename) + { + std::string sTempPath = NSFile::CUtf8Converter::GetUtf8StringFromUnicode(NSDirectory::GetTempPath()); + sTempPath += "/fileXXXXXX"; + int fd = mkstemp(const_cast (sTempPath.c_str())); + if (-1 != fd) + filename = sTempPath; + return fd; + } + #else + virtual int DownloadFile() override + { + if (m_sFilePath.empty()) + { + m_sFilePath = NSFile::CFileBinary::CreateTempFileWithUniqueName(NSDirectory::GetTempPath(), L"DW"); + if (NSFile::CFileBinary::Exists(m_sFilePath)) + NSFile::CFileBinary::Remove(m_sFilePath); + } + return download_external(m_sDownloadFileUrl, m_sFilePath); + } + virtual int UploadFile() override + { + if (!m_sUploadPathUrl.empty() && m_cData != NULL && m_nSize != 0) + { + return upload_external(m_sUploadPathUrl, m_cData, m_nSize); + } + + else return -1; + } + + #endif + }; + + CFileTransporter_private::CFileTransporter_private(std::wstring &sDownloadFileUrl, bool bDelete) + { + m_pInternal = new CFileTransporterBaseCURL(sDownloadFileUrl, bDelete); + } + + CFileTransporter_private::CFileTransporter_private(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + m_pInternal = new CFileTransporterBaseCURL(sUploadPathUrl, cData, nSize); + } + } +} diff --git a/Common/Network/FileTransporter/src/FileTransporter_mac.mm b/Common/Network/FileTransporter/src/FileTransporter_mac.mm new file mode 100644 index 0000000000..232b335042 --- /dev/null +++ b/Common/Network/FileTransporter/src/FileTransporter_mac.mm @@ -0,0 +1,136 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ + +#include "FileTransporter_private.h" +#include "FileTransporter.h" + +#ifdef USE_EXTERNAL_DOWNLOAD +#include "transport_external.h" +#endif + +#if _IOS + #import +#else + #include +#endif + +namespace NSNetwork +{ + namespace NSFileTransport + { + static NSString* StringWToNSString ( const std::wstring& Str ) + { + NSString* pString = [ [ NSString alloc ] + initWithBytes : (char*)Str.data() + length : Str.size() * sizeof(wchar_t) + encoding : CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE ) ]; + return pString; + } + class CFileTransporterBaseCocoa : public CFileTransporterBase + { + public : + CFileTransporterBaseCocoa(std::wstring sFileUrl, bool bDelete = true) + : CFileTransporterBase(sFileUrl, bDelete) + { + } + virtual ~CFileTransporterBaseCocoa() + { + } + + virtual int DownloadFile() + { + if (m_sFilePath.empty()) + { + m_sFilePath = NSFile::CFileBinary::CreateTempFileWithUniqueName(NSFile::CFileBinary::GetTempPath(), L"DWD"); + if (NSFile::CFileBinary::Exists(m_sFilePath)) + NSFile::CFileBinary::Remove(m_sFilePath); + } + + #ifdef USE_EXTERNAL_DOWNLOAD + int nExternalTransport = Transport_external(m_sFileUrl, m_sFilePath); + if (0 == nExternalTransport) + return 0; + #endif + + NSString* stringURL = StringWToNSString(m_sFileUrl); + NSString *escapedURL = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + NSURL *url = [NSURL URLWithString:escapedURL]; + NSData *urlData = [NSData dataWithContentsOfURL:url]; + if ( urlData ) + { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString *documentsDirectory = [paths objectAtIndex:0]; + + NSString *filePath = StringWToNSString ( m_sFilePath ); + [urlData writeToFile:filePath atomically:YES]; + + #if defined(_IOS) + return 0; + #else + #ifndef _ASC_USE_ARC_ + if (!CFileTransporter::GetARCEnabled()) + { + [stringURL release]; + //[url release]; + [urlData release]; + } + #endif + #endif + return 0; + } + + #if defined(_IOS) + return 1; + #else + #ifndef _ASC_USE_ARC_ + if (!CFileTransporter::GetARCEnabled()) + { + [stringURL release]; + //[url release]; + } + #endif + #endif + return 1; + } + }; + + CFileTransporter_private::CFileTransporter_private(std::wstring sFileUrl, bool bDelete) + { + m_pInternal = new CFileTransporterBaseCocoa(sFileUrl, bDelete); + } + + CFileTransporter_private::CFileTransporter_private(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + m_pInternal = new CFileTransporterBaseCocoa(sUploadPathUrl, cData, nSize); + } + } +} diff --git a/Common/Network/FileTransporter/src/FileTransporter_private.h b/Common/Network/FileTransporter/src/FileTransporter_private.h new file mode 100644 index 0000000000..0503810418 --- /dev/null +++ b/Common/Network/FileTransporter/src/FileTransporter_private.h @@ -0,0 +1,199 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ +#pragma once + +#include "../../../../DesktopEditor/common/File.h" +#include "../../../../DesktopEditor/graphics/BaseThread.h" +#include "../include/FileTransporter.h" + +namespace NSNetwork +{ + namespace NSFileTransport + { + class CFileTransporterBase + { + public : + CFileTransporterBase(std::wstring &sDownloadFileUrl, bool bDelete) + { + m_sFilePath = L""; + m_sDownloadFileUrl = sDownloadFileUrl; + m_bComplete = false; + m_bDelete = bDelete; + m_bIsUpload = false; + + m_sUploadPathUrl = L""; + m_cData = NULL; + m_nSize = 0; + m_sResponse = L""; + + + m_func_onComplete = NULL; + m_func_onProgress = NULL; + } + + CFileTransporterBase(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + m_sFilePath = L""; + m_sDownloadFileUrl = L""; + m_bComplete = false; + m_bDelete = true; + m_bIsUpload = true; + + m_sUploadPathUrl = sUploadPathUrl; + m_cData = cData; + m_nSize = nSize; + m_sResponse = L""; + + + m_func_onComplete = NULL; + m_func_onProgress = NULL; + } + virtual ~CFileTransporterBase () + { + if ( m_sFilePath.length() > 0 && m_bDelete ) + { + NSFile::CFileBinary::Remove(m_sFilePath); + m_sFilePath = L""; + } + } + + virtual int DownloadFile() = 0; + virtual int UploadFile() = 0; + + public: + std::wstring m_sFilePath; // Путь к сохраненному файлу на диске + std::wstring m_sDownloadFileUrl; // Ссылка на скачивание файла + + bool m_bComplete; // Закачался файл или нет + bool m_bDelete; // Удалять ли файл в деструкторе + bool m_bIsUpload; // Если хотим выгрузку данных + + std::wstring m_sUploadPathUrl; // URL для выгрузки данных + unsigned char* m_cData; // Данные в сыром виде + int m_nSize; // Размер данных + std::wstring m_sResponse; // Ответ сервера + + CFileTransporter_OnComplete m_func_onComplete; + CFileTransporter_OnProgress m_func_onProgress; + }; + + class CFileTransporter_private : public NSThreads::CBaseThread + { + protected: + // создаем в зависимости от платформы + CFileTransporterBase* m_pInternal; + + public: + CFileTransporterBase* GetInternal() + { + return m_pInternal; + } + + public: + CFileTransporter_private(std::wstring &sDownloadFileUrl, bool bDelete = true); + CFileTransporter_private(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize); + virtual ~CFileTransporter_private() + { + Stop(); + if (NULL != m_pInternal) + delete m_pInternal; + } + + void SetDownloadProp(std::wstring &sDownloadFileUrl, bool bDelete = true) + { + m_pInternal->m_sDownloadFileUrl = sDownloadFileUrl; + m_pInternal->m_bDelete = bDelete; + m_pInternal->m_bIsUpload = false; + } + + void SetDownloadFilePath(const std::wstring& sPath) + { + m_pInternal->m_sFilePath = sPath; + } + + std::wstring GetDownloadFilePath() + { + return m_pInternal->m_sFilePath; + } + + bool IsFileDownloaded() + { + return m_pInternal->m_bComplete; + } + + void SetUploadProp(std::wstring &sDownloadFileUrl, unsigned char* cData, const int nSize) + { + m_pInternal->m_sUploadPathUrl = sDownloadFileUrl; + m_pInternal->m_cData = cData; + m_pInternal->m_nSize = nSize; + m_pInternal->m_bIsUpload = true; + } + + std::wstring& GetResponse() + { + return m_pInternal->m_sResponse; + } + + bool TransferSync() + { + this->Start( 1 ); + while ( this->IsRunned() ) + { + NSThreads::Sleep( 10 ); + } + return IsFileDownloaded(); + } + + void TransferAsync() + { + this->Start( 1 ); + } + + protected : + + virtual DWORD ThreadProc () + { + m_pInternal->m_bComplete = false; + + int hrResultAll = m_pInternal->m_bIsUpload?m_pInternal->UploadFile():m_pInternal->DownloadFile(); + if (0 == hrResultAll) + m_pInternal->m_bComplete = true; + + if (m_pInternal->m_func_onComplete) + m_pInternal->m_func_onComplete(hrResultAll); + + m_bRunThread = FALSE; + return 0; + } + }; + } +} diff --git a/Common/Network/FileTransporter/src/FileTransporter_win.cpp b/Common/Network/FileTransporter/src/FileTransporter_win.cpp new file mode 100644 index 0000000000..ee513eb947 --- /dev/null +++ b/Common/Network/FileTransporter/src/FileTransporter_win.cpp @@ -0,0 +1,424 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ + +#include "FileTransporter_private.h" + + +#include +#pragma comment(lib, "Wininet") +#pragma comment(lib, "Ole32.lib") + +//------------------------------------------------------------------------------------------------------ + +// Константа для максимального числа символов в строке +#define MAX_SIZE 256 +// Константа для максимального числа загружаемых байт +#define DOWNLOAD_FILE_SIZE 32768 +#define MAX_SINGLE_DOWNLOAD_FILE_SIZE 524288 + + +// Константа для получения размера файла +#define CONTENT_RANGE L"bytes 0-0/" +// Константа для колличества символов у CONTENT_RANGE +#define CONTENT_RANGE_SIZE ( 11/*sizeof ( CONTENT_RANGE )*/ - 1 ) + +namespace NSNetwork +{ + namespace NSFileTransport + { + class CFileTransporterBaseWin : public CFileTransporterBase + { + public : + CFileTransporterBaseWin(std::wstring &sDownloadFileUrl, bool bDelete = true) : + CFileTransporterBase(sDownloadFileUrl, bDelete) + { + m_pFile = NULL; + } + + CFileTransporterBaseWin(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) : + CFileTransporterBase(sUploadPathUrl, cData, nSize) + { + m_pFile = NULL; + } + + virtual ~CFileTransporterBaseWin() + { + if ( m_pFile ) + { + ::fclose( m_pFile ); + m_pFile = NULL; + } + } + + virtual int DownloadFile() override + { + CoInitialize ( NULL ); + if ( /*S_OK != _DownloadFile ( m_sFileUrl )*/TRUE ) + { + HRESULT hrResultAll = DownloadFileAll(m_sDownloadFileUrl, m_sFilePath); + + if (S_OK != hrResultAll) + { + hrResultAll = (true == DownloadFilePS(m_sDownloadFileUrl, m_sFilePath)) ? S_OK : S_FALSE; + CoUninitialize (); + return hrResultAll; + } + } + + CoUninitialize (); + m_bComplete = true; + return S_OK; + } + + virtual int UploadFile() override + { + //stub + return S_OK; + } + + protected: + FILE *m_pFile; // Хэндл на временный файл + unsigned int _DownloadFile(std::wstring sFileUrl) + { + // Проверяем состояние соединения + if ( FALSE == InternetGetConnectedState ( 0, 0 ) ) + return S_FALSE; + + wchar_t sTempPath[MAX_PATH], sTempFile[MAX_PATH]; + if ( 0 == GetTempPathW( MAX_PATH, sTempPath ) ) + return S_FALSE; + + if ( 0 == GetTempFileNameW( sTempPath, L"CSS", 0, sTempFile ) ) + return S_FALSE; + + m_pFile = ::_wfopen( sTempFile, L"wb" ); + if ( !m_pFile ) + return S_FALSE; + + m_sFilePath = std::wstring( sTempFile ); + + // Открываем сессию + HINTERNET hInternetSession = InternetOpenW ( L"Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); + if ( NULL == hInternetSession ) + return S_FALSE; + + // Заголовок запроса ( пока содержит 0 байт ( необходимо для проверки ) ) + std::wstring sHTTPHdr = L"Range: bytes=0-0"; + // Открываем ссылку для проверки на ее существование, а также на возможность чтения частями + HINTERNET hInternetOpenURL = InternetOpenUrlW ( hInternetSession, sFileUrl.c_str(), sHTTPHdr.c_str(), -1, INTERNET_FLAG_RESYNCHRONIZE, 0 ); + if ( NULL != hInternetOpenURL ) + { + // Открытие произошло, проверяем ответ + if ( TRUE == QueryStatusCode ( hInternetOpenURL, TRUE ) ) + { + // Запрос прошел удачно, проверяем возможность чтения частями и получаем размер данных + LONGLONG nFileSize = IsAccept_Ranges ( hInternetOpenURL ); + // Закрываем хендл + InternetCloseHandle ( hInternetOpenURL ); + if ( -1 == nFileSize ) + { + // Чтение частями недоступно + // Закрываем хендл соединения + InternetCloseHandle ( hInternetSession ); + // Закрываем файл (сделается на DownloadAll) + // Попробуем записать его целиком + return S_FALSE; + } + else + { + // Чтение частями доступно + LONGLONG nStartByte = 0; + while ( true ) + { + // Если закачали весь файл - то выходим + if ( nStartByte == nFileSize - 1 ) + { + // Закрываем хендл соединения + InternetCloseHandle ( hInternetSession ); + return S_OK; + } + LONGLONG nEndByte = nStartByte + DOWNLOAD_FILE_SIZE; + // Если файл заканчивается, то загружаем меньшее колличество байт ( на 1 меньше, чем размер, т.к. начинается с 0 ) + if ( nEndByte >= nFileSize ) + nEndByte = nFileSize - 1; + + // Буффер для закачки + BYTE arrBuffer [ DOWNLOAD_FILE_SIZE ] = { 0 }; + DWORD dwBytesDownload = DownloadFilePath ( hInternetSession, arrBuffer, nStartByte, nEndByte, sFileUrl ); + + nStartByte = nEndByte; + if ( -1 == dwBytesDownload ) + { + // Ничего не прочиталось - это плохо!!!! + // Закрываем хендл соединения + InternetCloseHandle ( hInternetSession ); + // Закрываем файл (сделается на DownloadAll) + // Попробуем записать его целиком + return S_FALSE; + } + + // Пишем в файл + ::fwrite( (BYTE*)arrBuffer, 1, dwBytesDownload, m_pFile ); + ::fflush( m_pFile ); + + NSThreads::Sleep(10); + } + } + } + else + { + // Закрываем хендл соединения + InternetCloseHandle ( hInternetSession ); + // Закрываем файл (сделается на DownloadAll) + // Попробуем записать его целиком + return S_FALSE; + } + } + else + { + // Закрываем хендл соединения + InternetCloseHandle ( hInternetSession ); + // Закрываем файл (сделается на DownloadAll) + // Попробуем записать его целиком + return S_FALSE; + } + + // Закрываем хендл соединения + InternetCloseHandle ( hInternetSession ); + + return S_OK; + } + DWORD DownloadFilePath ( HINTERNET hInternet, LPBYTE pBuffer, LONGLONG nStartByte, LONGLONG nEndByte, std::wstring sFileURL ) + { + // Неоткрытая сессия + if ( NULL == hInternet ) + return -1; + + // Пришли непонятные параметры + if ( nStartByte > nEndByte || !pBuffer ) + return -1; + + // Заголовок запроса ( содержит nEndByte - nStartByte байт ) + std::wstring sHTTPHdr = L"Range: bytes=" + std::to_wstring(nStartByte) + L"-" + std::to_wstring(nEndByte); + // Открываем ссылку для закачки + HINTERNET hInternetOpenURL = InternetOpenUrlW ( hInternet, sFileURL.c_str(), sHTTPHdr.c_str(), -1, INTERNET_FLAG_RESYNCHRONIZE, 0 ); + if ( NULL == hInternetOpenURL ) + return -1; + // Открытие произошло, проверяем ответ + if ( FALSE == QueryStatusCode ( hInternetOpenURL, TRUE ) ) + { + // Закрываем хендл соединения + InternetCloseHandle ( hInternetOpenURL ); + return -1; + } + + // Какое колличество байт прочитано + DWORD dwBytesRead = 0; + // Читаем файл + if ( FALSE == InternetReadFile ( hInternetOpenURL, pBuffer, DOWNLOAD_FILE_SIZE, &dwBytesRead ) ) + { + // Закрываем хендл соединения + InternetCloseHandle ( hInternetOpenURL ); + return -1; + } + + // Закрываем хендл соединения + InternetCloseHandle ( hInternetOpenURL ); + + return dwBytesRead; + } + + BOOL QueryStatusCode ( HINTERNET hInternet, BOOL bIsRanges ) + { + // Зачем проверять у неоткрытой сессии что-то + if ( NULL == hInternet ) + return FALSE; + + // Результат ответа + INT nResult = 0; + // Размер данных ответа ( должно быть = 4 ) + DWORD dwLengthDataSize = 4; + + // Делаем запрос, если не проходит - то возвращаем FALSE + if ( FALSE == HttpQueryInfo ( hInternet, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &nResult, &dwLengthDataSize, NULL ) ) + return FALSE; + + // Запрос прошел, теперь проверяем код ответа + if ( HTTP_STATUS_NOT_FOUND == nResult ) + { + // Объект не найден, плохая ссылка или что-то еще + return FALSE; + } + else if ( ( HTTP_STATUS_OK != nResult && FALSE == bIsRanges ) || ( HTTP_STATUS_PARTIAL_CONTENT != nResult && TRUE == bIsRanges ) ) + { + // Запрос не прошел по какой-то причине + return FALSE; + } + + // Все отлично, запрос прошел + return TRUE; + } + // Проверяет, доступно ли для ресурса чтение частями и возвращает -1 если не доступно и размер данных, если доступно + LONGLONG IsAccept_Ranges ( HINTERNET hInternet ) + { + // Зачем проверять у неоткрытой сессии что-то + if ( NULL == hInternet ) + return -1; + + // Результат ответа + wchar_t arrResult [ MAX_SIZE ] = { 0 }; + // Размер данных ответа + DWORD dwLengthDataSize = sizeof ( arrResult ); + + // Делаем запрос, если не проходит - то возвращаем FALSE + if ( FALSE == HttpQueryInfoW ( hInternet, HTTP_QUERY_CONTENT_RANGE, &arrResult, &dwLengthDataSize, NULL ) ) + { + // Получаем последнюю ошибку + DWORD dwLastError = GetLastError (); + if ( dwLastError == ERROR_HTTP_HEADER_NOT_FOUND ) + { + // Не пришел заголовок, значит ресурс не поддерживает чтение частями + return -1; + } + + // Возникла какая-то другая ошибка - возвращаем FALSE + return -1; + } + + // Если размер 0, то заголовка нет + if ( 0 >= dwLengthDataSize ) + return -1; + + // Приведем к std::wstring + std::wstring strResult ( arrResult ); + + // Содержит размер данных + LONGLONG nFileSize = 0; + + try + { + // Ищем индекс размера данных в строке + INT nStartIndex = (INT)strResult.find ( CONTENT_RANGE ); + if ( -1 == nStartIndex ) + return -1; + + // Оставляем в строке только размер данных + strResult = strResult.substr( nStartIndex + CONTENT_RANGE_SIZE ); + // Теперь получим размер данных, переводя стринг в LONGLONG + nFileSize = _wtoi64 ( strResult.c_str() ); + // Т.к. реально нумерация с 0 ( поэтому добавляем еще 1 байт ) + if ( 0 < nFileSize ) + nFileSize += 1; + } + catch ( ... ) + { + // не нашли возвращаем ошибку + return -1; + } + + // Все отлично, ресурс поддерживает чтение частями, возвращаем размер + return nFileSize; + } + + HRESULT DownloadFileAll(std::wstring sFileURL, std::wstring strFileOutput) + { + if ( m_pFile ) + { + ::fclose( m_pFile ); + m_pFile = NULL; + } + // Скачиваем файл + return URLDownloadToFileW (NULL, sFileURL.c_str(), strFileOutput.c_str(), NULL, NULL); + } + + bool DownloadFilePS(const std::wstring& sFileURL, const std::wstring& strFileOutput) + { + STARTUPINFO sturtupinfo; + ZeroMemory(&sturtupinfo,sizeof(STARTUPINFO)); + sturtupinfo.cb = sizeof(STARTUPINFO); + + std::wstring sFileDst = strFileOutput; + size_t posn = 0; + while (std::wstring::npos != (posn = sFileDst.find('\\', posn))) + { + sFileDst.replace(posn, 1, L"/"); + posn += 1; + } + + std::wstring sApp = L"powershell.exe –c \"(new-object System.Net.WebClient).DownloadFile('" + sFileURL + L"','" + sFileDst + L"')\""; + wchar_t* pCommandLine = new wchar_t[sApp.length() + 1]; + memcpy(pCommandLine, sApp.c_str(), sApp.length() * sizeof(wchar_t)); + pCommandLine[sApp.length()] = (wchar_t)'\0'; + + HANDLE ghJob = CreateJobObject(NULL, NULL); + + if (ghJob) + { + JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; + + // Configure all child processes associated with the job to terminate when the + jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + if ( 0 == SetInformationJobObject( ghJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli))) + { + CloseHandle(ghJob); + ghJob = NULL; + } + } + + PROCESS_INFORMATION processinfo; + ZeroMemory(&processinfo,sizeof(PROCESS_INFORMATION)); + BOOL bResult = CreateProcessW(NULL, pCommandLine, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &sturtupinfo, &processinfo); + + if (bResult && ghJob) + { + AssignProcessToJobObject(ghJob, processinfo.hProcess); + } + + ::WaitForSingleObject(processinfo.hProcess, INFINITE); + + RELEASEARRAYOBJECTS(pCommandLine); + + return NSFile::CFileBinary::Exists(sFileDst); + } + }; + + CFileTransporter_private::CFileTransporter_private(std::wstring &sDownloadFileUrl, bool bDelete) + { + m_pInternal = new CFileTransporterBaseWin(sDownloadFileUrl, bDelete); + } + + CFileTransporter_private::CFileTransporter_private(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + m_pInternal = new CFileTransporterBaseWin(sUploadPathUrl, cData, nSize); + } + } +} diff --git a/Common/Network/FileTransporter/src/transport_external.h b/Common/Network/FileTransporter/src/transport_external.h new file mode 100644 index 0000000000..df04dbe613 --- /dev/null +++ b/Common/Network/FileTransporter/src/transport_external.h @@ -0,0 +1,168 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2021 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ + +#include +#include +#include "../../DesktopEditor/common/Directory.h" + +#ifdef USE_EXTERNAL_TRANSPORT + +#include +#include + +namespace NSNetwork +{ + namespace NSFileTransport + { + std::string wget_url_validate(const std::string& url) + { + std::string::size_type pos = 0; + const char* url_ptr = url.c_str(); + while ('-' == *url_ptr++) // '\0' => break + ++pos; + if (*url_ptr == '\0') + return ""; + + return url.substr(pos); + } + + int download_external(const std::wstring& sUrl, const std::wstring& sOutput) + { + int nReturnCode = -1; + + std::string sUrlA = U_TO_UTF8(sUrl); + //sUrlA =("\"" + sUrlA + "\""); + std::string sOutputA = U_TO_UTF8(sOutput); + //sOutputA =("\"" + sOutputA + "\""); + + if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/curl")) + { + pid_t pid = fork(); // create child process + int status; + + switch (pid) + { + case -1: // error + break; + + case 0: // child process + { + const char* nargs[10]; + nargs[0] = "/usr/bin/curl"; + nargs[1] = "--url"; + nargs[2] = sUrlA.c_str(); + nargs[3] = "--output"; + nargs[4] = sOutputA.c_str(); + nargs[5] = "--silent"; + nargs[6] = "-L"; + nargs[7] = "--connect-timeout"; + nargs[8] = "10"; + nargs[9] = NULL; + + const char* nenv[3]; + nenv[0] = "LD_PRELOAD="; + nenv[1] = "LD_LIBRARY_PATH="; + nenv[2] = NULL; + + execve("/usr/bin/curl", (char * const *)nargs, (char * const *)nenv); + exit(EXIT_SUCCESS); + break; + } + default: // parent process, pid now contains the child pid + while (-1 == waitpid(pid, &status, 0)); // wait for child to complete + if (WIFEXITED(status)) + { + nReturnCode = WEXITSTATUS(status); + } + break; + } + } + + if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/wget")) + { + std::string sUrlValidateA = wget_url_validate(sUrlA); + + pid_t pid = fork(); // create child process + int status; + + switch (pid) + { + case -1: // error + break; + + case 0: // child process + { + const char* nargs[8]; + nargs[0] = "/usr/bin/wget"; + nargs[1] = sUrlValidateA.c_str(); + nargs[2] = "-O"; + nargs[3] = sOutputA.c_str(); + nargs[4] = "-q"; + nargs[5] = "--connect-timeout=10"; + nargs[6] = "--tries=2"; + nargs[7] = NULL; + + const char* nenv[2]; + nenv[0] = "LD_PRELOAD="; + nenv[1] = NULL; + + execve("/usr/bin/wget", (char * const *)nargs, (char * const *)nenv); + exit(EXIT_SUCCESS); + break; + } + default: // parent process, pid now contains the child pid + while (-1 == waitpid(pid, &status, 0)); // wait for child to complete + if (WIFEXITED(status)) + { + nReturnCode = WEXITSTATUS(status); + } + break; + } + } + + if (0 == nReturnCode) + { + if (!NSFile::CFileBinary::Exists(sOutput)) + nReturnCode = -1; + } + + return nReturnCode; + } + + int upload_external(std::wstring &sUploadPathUrl, unsigned char* cData, const int nSize) + { + //stub + return -1; + } + } +} +#endif diff --git a/Common/WebSocket/websocket.h b/Common/Network/WebSocket/include/websocket.h similarity index 62% rename from Common/WebSocket/websocket.h rename to Common/Network/WebSocket/include/websocket.h index aef5960071..acb20a693d 100644 --- a/Common/WebSocket/websocket.h +++ b/Common/Network/WebSocket/include/websocket.h @@ -1,5 +1,5 @@ /* - * (c) Copyright Ascensio System SIA 2010-2019 + * (c) Copyright Ascensio System SIA 2010-2021 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) @@ -33,33 +33,36 @@ #ifndef _WEB_SOCKET_H_ #define _WEB_SOCKET_H_ -#include "../kernel_config.h" +#include "../../../kernel_config.h" #include #include -namespace NSWebSocket +namespace NSNetwork { - class IWebSocket + namespace NSWebSocket { - public: - virtual void open() = 0; - virtual void send(const std::string& message) = 0; - virtual void close() = 0; - virtual void setUrl(const std::string& url) = 0; - virtual ~IWebSocket() {} - }; + class IWebSocket + { + public: + virtual void open() = 0; + virtual void send(const std::string& message) = 0; + virtual void close() = 0; + virtual void setUrl(const std::string& url) = 0; + virtual ~IWebSocket() {} + }; - class IListener - { - public: - virtual void onMessage(const std::string& message) = 0; - virtual void onOpen() = 0; - virtual void onError(const std::string& error) = 0; - virtual void onClose(int code, const std::string& reason) = 0; - virtual ~IListener() {} - }; + class IListener + { + public: + virtual void onMessage(const std::string& message) = 0; + virtual void onOpen() = 0; + virtual void onError(const std::string& error) = 0; + virtual void onClose(int code, const std::string& reason) = 0; + virtual ~IListener() {} + }; - KERNEL_DECL std::shared_ptr createWebsocket(const std::string& type, std::shared_ptr listener, const std::string& url = ""); + KERNEL_DECL std::shared_ptr createWebsocket(const std::string& type, std::shared_ptr listener, const std::string& url = ""); + } } -#endif /* _WEB_SOCKET_H_ */ +#endif /* _WEB_SOCKET_H_ */ \ No newline at end of file diff --git a/Common/WebSocket/socketRocket_internal.h b/Common/Network/WebSocket/src/base/WebWorkerBase.h similarity index 64% rename from Common/WebSocket/socketRocket_internal.h rename to Common/Network/WebSocket/src/base/WebWorkerBase.h index 99eb58fb83..df4c144e5e 100644 --- a/Common/WebSocket/socketRocket_internal.h +++ b/Common/Network/WebSocket/src/base/WebWorkerBase.h @@ -30,31 +30,33 @@ * */ -#ifndef _SOCKET_ROCKET_H_ -#define _SOCKET_ROCKET_H_ +#ifndef _WEB_WORKER_BASE_H_ +#define _WEB_WORKER_BASE_H_ -#include "WebWorkerBase.h" +#include "../../include/websocket.h" -namespace NSWebSocket +namespace NSNetwork { - struct SocketRocketImpl; - class CSocketRocket: public CWebWorkerBase + namespace NSWebSocket { - - private: + class CWebWorkerBase: public IWebSocket + { + protected: + std::shared_ptr listener; + std::string url; - SocketRocketImpl* impl; - - public: - - CSocketRocket(const std::string& url, std::shared_ptr listener); - ~CSocketRocket(); - virtual void open() override; - virtual void send(const std::string& message) override; - virtual void close() override; - virtual void setUrl(const std::string& url) override; - - }; + public: + CWebWorkerBase(const std::string& url, std::shared_ptr listener) + { + this->url = url; + this->listener = listener; + } + virtual void open() override {} + virtual void send(const std::string& message) override {} + virtual void close() override {} + virtual void setUrl(const std::string& url) override {this->url = url;} + }; + } } -#endif /* _SOCKET_ROCKET_H_ */ +#endif /* _WEB_WORKER_BASE_H_ */ \ No newline at end of file diff --git a/Common/WebSocket/ixwebsocket_internal.cpp b/Common/Network/WebSocket/src/ixwebsocket/ixwebsocket_internal.cpp similarity index 54% rename from Common/WebSocket/ixwebsocket_internal.cpp rename to Common/Network/WebSocket/src/ixwebsocket/ixwebsocket_internal.cpp index f8f1905f7e..249b0e8a6c 100644 --- a/Common/WebSocket/ixwebsocket_internal.cpp +++ b/Common/Network/WebSocket/src/ixwebsocket/ixwebsocket_internal.cpp @@ -1,5 +1,5 @@ /* - * (c) Copyright Ascensio System SIA 2010-2019 + * (c) Copyright Ascensio System SIA 2010-2021 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) @@ -32,48 +32,49 @@ #include "ixwebsocket_internal.h" -namespace NSWebSocket +namespace NSNetwork { - - void CIXWebSocket::open() + namespace NSWebSocket { - ix::SocketTLSOptions tls; - tls.caFile = "NONE"; - webSocket.setTLSOptions(tls); - webSocket.setUrl(url); - std::function f = std::bind(&CIXWebSocket::receive, this, std::placeholders::_1); - webSocket.setOnMessageCallback(f); - webSocket.start(); - } + void CIXWebSocket::open() + { + ix::SocketTLSOptions tls; + tls.caFile = "NONE"; + webSocket.setTLSOptions(tls); + webSocket.setUrl(url); + std::function f = std::bind(&CIXWebSocket::receive, this, std::placeholders::_1); + webSocket.setOnMessageCallback(f); + webSocket.start(); + } - void CIXWebSocket::receive(const ix::WebSocketMessagePtr& msg) - { - if (msg->type == ix::WebSocketMessageType::Message) + void CIXWebSocket::receive(const ix::WebSocketMessagePtr& msg) { - CWebWorkerBase::listener->onMessage(msg->str); + if (msg->type == ix::WebSocketMessageType::Message) + { + CWebWorkerBase::listener->onMessage(msg->str); + } + else if (msg->type == ix::WebSocketMessageType::Open) + { + CWebWorkerBase::listener->onOpen(); + } + else if (msg->type == ix::WebSocketMessageType::Error) + { + CWebWorkerBase::listener->onError(msg->errorInfo.reason); + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + CWebWorkerBase::listener->onClose(msg->closeInfo.code, msg->closeInfo.reason); + } } - else if (msg->type == ix::WebSocketMessageType::Open) + + void CIXWebSocket::send(const std::string& message) { - CWebWorkerBase::listener->onOpen(); + webSocket.send(message); } - else if (msg->type == ix::WebSocketMessageType::Error) + + void CIXWebSocket::close() { - CWebWorkerBase::listener->onError(msg->errorInfo.reason); - } - else if (msg->type == ix::WebSocketMessageType::Close) - { - CWebWorkerBase::listener->onClose(msg->closeInfo.code, msg->closeInfo.reason); + webSocket.stop(); } } - - void CIXWebSocket::send(const std::string& message) - { - webSocket.send(message); - } - - void CIXWebSocket::close() - { - webSocket.stop(); - } - -} +} \ No newline at end of file diff --git a/Common/WebSocket/ixwebsocket_internal.h b/Common/Network/WebSocket/src/ixwebsocket/ixwebsocket_internal.h similarity index 70% rename from Common/WebSocket/ixwebsocket_internal.h rename to Common/Network/WebSocket/src/ixwebsocket/ixwebsocket_internal.h index 60790ec7f7..b5a3ac1fa7 100644 --- a/Common/WebSocket/ixwebsocket_internal.h +++ b/Common/Network/WebSocket/src/ixwebsocket/ixwebsocket_internal.h @@ -1,5 +1,5 @@ /* - * (c) Copyright Ascensio System SIA 2010-2019 + * (c) Copyright Ascensio System SIA 2010-2021 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) @@ -33,34 +33,37 @@ #ifndef _IX_WEB_SOCKET_H_ #define _IX_WEB_SOCKET_H_ -#include "WebWorkerBase.h" +#include "../base/WebWorkerBase.h" #include #include -namespace NSWebSocket +namespace NSNetwork { - class CIXWebSocket: public CWebWorkerBase + namespace NSWebSocket { - - private: - - ix::WebSocket webSocket; - - public: - - CIXWebSocket(const std::string& url, std::shared_ptr listener): CWebWorkerBase(url, listener) + class CIXWebSocket: public CWebWorkerBase { - ix::initNetSystem(); - } - - public: - - virtual void open() override; - virtual void send(const std::string& message) override; - virtual void close() override; - void receive(const ix::WebSocketMessagePtr& msg); - }; + private: + + ix::WebSocket webSocket; + + public: + + CIXWebSocket(const std::string& url, std::shared_ptr listener): CWebWorkerBase(url, listener) + { + ix::initNetSystem(); + } + + public: + + virtual void open() override; + virtual void send(const std::string& message) override; + virtual void close() override; + void receive(const ix::WebSocketMessagePtr& msg); + + }; + } } #endif /* _IX_WEB_SOCKET_H_ */ diff --git a/Common/WebSocket/managerWebSocket.cpp b/Common/Network/WebSocket/src/manager/managerWebSocket.cpp similarity index 66% rename from Common/WebSocket/managerWebSocket.cpp rename to Common/Network/WebSocket/src/manager/managerWebSocket.cpp index 4757bd28d2..8ae7b8be15 100644 --- a/Common/WebSocket/managerWebSocket.cpp +++ b/Common/Network/WebSocket/src/manager/managerWebSocket.cpp @@ -1,5 +1,5 @@ /* - * (c) Copyright Ascensio System SIA 2010-2019 + * (c) Copyright Ascensio System SIA 2010-2021 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) @@ -30,33 +30,36 @@ * */ -#include "websocket.h" +#include "../../include/websocket.h" #ifdef USE_IXWEBSOCKET -#include "ixwebsocket_internal.h" +#include "../ixwebsocket/ixwebsocket_internal.h" #elif defined USE_SOCKETROCKET -#include "socketRocket_internal.h" +#include "../socketrocket/socketRocket_internal.h" #endif -namespace NSWebSocket +namespace NSNetwork { - std::shared_ptr createWebsocket(const std::string& type, std::shared_ptr listener, const std::string& url) - { - if(type == "ixwebsocket") + namespace NSWebSocket + { + std::shared_ptr createWebsocket(const std::string& type, std::shared_ptr listener, const std::string& url) { - #ifdef USE_IXWEBSOCKET - return std::make_shared(url, listener); - #else - return nullptr; - #endif + if(type == "ixwebsocket") + { + #ifdef USE_IXWEBSOCKET + return std::make_shared(url, listener); + #else + return nullptr; + #endif + } + if(type == "socketRocket") + { + #ifdef USE_SOCKETROCKET + return std::make_shared(url, listener); + #else + return nullptr; + #endif + } + else return nullptr; } - if(type == "socketRocket") - { - #ifdef USE_SOCKETROCKET - return std::make_shared(url, listener); - #else - return nullptr; - #endif - } - else return nullptr; - } -} + } +} \ No newline at end of file diff --git a/Common/WebSocket/WebWorkerBase.h b/Common/Network/WebSocket/src/socketrocket/socketRocket_internal.h similarity index 72% rename from Common/WebSocket/WebWorkerBase.h rename to Common/Network/WebSocket/src/socketrocket/socketRocket_internal.h index 66b99e071f..8bb61f52ee 100644 --- a/Common/WebSocket/WebWorkerBase.h +++ b/Common/Network/WebSocket/src/socketrocket/socketRocket_internal.h @@ -1,5 +1,5 @@ /* - * (c) Copyright Ascensio System SIA 2010-2019 + * (c) Copyright Ascensio System SIA 2010-2021 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) @@ -30,32 +30,34 @@ * */ -#ifndef _WEB_WORKER_BASE_H_ -#define _WEB_WORKER_BASE_H_ +#ifndef _SOCKET_ROCKET_H_ +#define _SOCKET_ROCKET_H_ -#include "websocket.h" +#include "../base/WebWorkerBase.h" -namespace NSWebSocket +namespace NSNetwork { - - class CWebWorkerBase: public IWebSocket + namespace NSWebSocket { - protected: - std::shared_ptr listener; - std::string url; + struct SocketRocketImpl; + class CSocketRocket: public CWebWorkerBase + { + + private: + SocketRocketImpl* impl; + public: - CWebWorkerBase(const std::string& url, std::shared_ptr listener) - { - this->url = url; - this->listener = listener; - } - virtual void open() override {} - virtual void send(const std::string& message) override {} - virtual void close() override {} - virtual void setUrl(const std::string& url) override {this->url = url;} - }; + CSocketRocket(const std::string& url, std::shared_ptr listener); + ~CSocketRocket(); + virtual void open() override; + virtual void send(const std::string& message) override; + virtual void close() override; + virtual void setUrl(const std::string& url) override; + + }; + } } -#endif /* _WEB_WORKER_BASE_H_ */ +#endif /* _SOCKET_ROCKET_H_ */ \ No newline at end of file diff --git a/Common/WebSocket/socketRocket_internal.mm b/Common/Network/WebSocket/src/socketrocket/socketRocket_internal.mm similarity index 58% rename from Common/WebSocket/socketRocket_internal.mm rename to Common/Network/WebSocket/src/socketrocket/socketRocket_internal.mm index d11c6c927d..55f2c057a8 100644 --- a/Common/WebSocket/socketRocket_internal.mm +++ b/Common/Network/WebSocket/src/socketrocket/socketRocket_internal.mm @@ -32,50 +32,53 @@ #include "socketRocket_internal.h" #import "socketRocket_objc.h" -namespace NSWebSocket +namespace NSNetwork { - - struct SocketRocketImpl + namespace NSWebSocket { - SocketRocketObjC* wrapped; - }; - CSocketRocket::CSocketRocket(const std::string& url, std::shared_ptr listener): CWebWorkerBase(url, listener) - { - impl = new SocketRocketImpl(); - impl->wrapped = [[SocketRocketObjC alloc] init]; + struct SocketRocketImpl + { + SocketRocketObjC* wrapped; + }; - [impl->wrapped setUrl:[NSString stringWithAString:url]]; + CSocketRocket::CSocketRocket(const std::string& url, std::shared_ptr listener): CWebWorkerBase(url, listener) + { + impl = new SocketRocketImpl(); + impl->wrapped = [[SocketRocketObjC alloc] init]; + + [impl->wrapped setUrl:[NSString stringWithAString:url]]; + + IListener* ptr = listener.get(); + [impl->wrapped setListener: ptr]; + } + + CSocketRocket::~CSocketRocket() + { + if (impl) + [impl->wrapped release]; + delete impl; + } + + void CSocketRocket::open() + { + [impl->wrapped open]; + } + + void CSocketRocket::send(const std::string& message) + { + [impl->wrapped send:[NSString stringWithAString:message]]; + } + + void CSocketRocket::close() + { + [impl->wrapped close]; + } + + void CSocketRocket::setUrl(const std::string& url) + { + [impl->wrapped setUrl:[NSString stringWithAString:url]]; + } - IListener* ptr = listener.get(); - [impl->wrapped setListener: ptr]; } - - CSocketRocket::~CSocketRocket() - { - if (impl) - [impl->wrapped release]; - delete impl; - } - - void CSocketRocket::open() - { - [impl->wrapped open]; - } - - void CSocketRocket::send(const std::string& message) - { - [impl->wrapped send:[NSString stringWithAString:message]]; - } - - void CSocketRocket::close() - { - [impl->wrapped close]; - } - - void CSocketRocket::setUrl(const std::string& url) - { - [impl->wrapped setUrl:[NSString stringWithAString:url]]; - } - -} +} \ No newline at end of file diff --git a/Common/WebSocket/socketRocket_objc.h b/Common/Network/WebSocket/src/socketrocket/socketRocket_objc.h similarity index 89% rename from Common/WebSocket/socketRocket_objc.h rename to Common/Network/WebSocket/src/socketrocket/socketRocket_objc.h index b7135e400e..bba35c8bcf 100644 --- a/Common/WebSocket/socketRocket_objc.h +++ b/Common/Network/WebSocket/src/socketrocket/socketRocket_objc.h @@ -34,12 +34,12 @@ //#define _SOCKET_ROCKET_OBJC_H_ #import -#include "websocket.h" +#include "../../include/websocket.h" @interface SocketRocketObjC: NSObject { - NSWebSocket::IListener* m_listener; + NSNetwork::NSWebSocket::IListener* m_listener; NSString* m_url; } @@ -47,12 +47,12 @@ - (void) open; -- (void) send : (NSString *)name; +- (void) send: (NSString *)name; - (void) close; -- (void) setListener: (NSWebSocket::IListener *)listener; +- (void) setListener: (NSNetwork::NSWebSocket::IListener *)listener; - (void) setUrl: (NSString *)url; @end - /* _SOCKET_ROCKET_OBJC_H_ */ + /* _SOCKET_ROCKET_OBJC_H_ */ \ No newline at end of file diff --git a/Common/WebSocket/socketRocket_objc.mm b/Common/Network/WebSocket/src/socketrocket/socketRocket_objc.mm similarity index 76% rename from Common/WebSocket/socketRocket_objc.mm rename to Common/Network/WebSocket/src/socketrocket/socketRocket_objc.mm index 5ab7cd766a..0eaa465547 100644 --- a/Common/WebSocket/socketRocket_objc.mm +++ b/Common/Network/WebSocket/src/socketrocket/socketRocket_objc.mm @@ -32,7 +32,7 @@ #import "socketRocket_objc.h" -#import "../../DesktopEditor/Common/Mac/NSString+StringUtils.h" +#import "../../../../../DesktopEditor/Common/Mac/NSString+StringUtils.h" @implementation SocketRocketObjC @@ -65,11 +65,10 @@ - (void)send:(NSString*)message { - #if _LOGGER_SOCKETS - NSLog(@"------------------- SEND TO SOCKET -------------------"); - NSLog(@"%@", message); + NSLog(@"------------------- SEND TO SOCKET -------------------"); + NSLog(@"%@", message); #endif @@ -102,40 +101,39 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { - #if _LOGGER_SOCKETS NSLog(@"------------------- SOCKET RECEIVE MESSAGE -------------------"); NSLog(@"%@", message); #endif + NSString *s = (NSString *)message; if (s) { m_listener->onMessage(s.stdstring); - } - + } } - (void)webSocketDidOpen:(SRWebSocket *)webSocket { - m_listener->onOpen(); + #if _LOGGER_SOCKETS - NSLog(@"------------------- SOCKET OPEN -------------------"); - // NSLog(@"URL : %@", webSocket.url); - // NSLog(@"description : %@", webSocket.description); - // NSLog(@"hash : %lu", (unsigned long)webSocket.hash); - // NSLog(@"---------------------------------------------------"); + NSLog(@"------------------- SOCKET OPEN -------------------"); + NSLog(@"URL : %@", webSocket.url); + NSLog(@"description : %@", webSocket.description); + NSLog(@"hash : %lu", (unsigned long)webSocket.hash); + NSLog(@"---------------------------------------------------"); -#endif - +#endif } - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { m_listener->onError(error.localizedDescription.stdstring); + #if _LOGGER_SOCKETS NSLog(@"---------------------------------------------------------"); @@ -147,27 +145,26 @@ - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { - - m_listener->onClose(code, reason.stdstring); + m_listener->onClose(code, reason.stdstring); + #if _LOGGER_SOCKETS - NSLog(@"---------------------------------------------------------"); - NSLog(@"------------------- SOCKET CLOSE : %@ -----------", reason); - NSLog(@"---------------------------------------------------------"); + NSLog(@"---------------------------------------------------------"); + NSLog(@"------------------- SOCKET CLOSE : %@ -----------", reason); + NSLog(@"---------------------------------------------------------"); #endif } - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload -{ - +{ #if _LOGGER_SOCKETS - NSString *str = [[NSString alloc] initWithData:pongPayload encoding:NSUTF8StringEncoding]; + NSString *str = [[NSString alloc] initWithData:pongPayload encoding:NSUTF8StringEncoding]; - NSLog(@"---------------------------------------------------"); - NSLog(@"------------------- SOCKET PONG : %@ ---------", str); - NSLog(@"---------------------------------------------------"); + NSLog(@"---------------------------------------------------"); + NSLog(@"------------------- SOCKET PONG : %@ ---------", str); + NSLog(@"---------------------------------------------------"); #endif } diff --git a/Common/WebSocket/websocket.pri b/Common/Network/WebSocket/websocket.pri similarity index 100% rename from Common/WebSocket/websocket.pri rename to Common/Network/WebSocket/websocket.pri diff --git a/Common/Network/network.pro b/Common/Network/network.pro new file mode 100644 index 0000000000..24136dfa70 --- /dev/null +++ b/Common/Network/network.pro @@ -0,0 +1,118 @@ +TARGET = network +TEMPLATE = lib + +# Исключаем зависимость от QT +CONFIG -= qt +QT -= core gui +# Вместо shared используем plugin, чтобы к целевому файлу не добавлялась информция о версии(вместо libnetwork.so.1.0.0 получаем libnetwork.so) +CONFIG += plugin warn_on + +CORE_ROOT_DIR = $$PWD/../../../core +PWD_ROOT_DIR = $$PWD +include(../base.pri) + +# WEBSOCKET +include(./WebSocket/websocket.pri) + +ixwebsocket { + + DEFINES += USE_IXWEBSOCKET + + include(../3dParty/ixwebsocket/ixwebsocket.pri) + + HEADERS += \ + ./WebSocket/include/websocket.h \ + ./WebSocket/src/base/WebWorkerBase.h \ + ./WebSocket/src/ixwebsocket/ixwebsocket_internal.h \ + + SOURCES += \ + ./WebSocket/src/ixwebsocket/ixwebsocket_internal.cpp \ + ./WebSocket/src/manager/managerWebSocket.cpp \ + +} + +socketrocket { + + DEFINES += USE_SOCKETROCKET + + include(../3dParty/socketrocket/socketrocket.pri) + + HEADERS += \ + ./WebSocket/include/websocket.h \ + ./WebSocket/src/base/WebWorkerBase.h \ + ./WebSocket/src/socketrocket/socketRocket_objc.h \ + ./WebSocket/src/socketrocket/socketRocket_internal.h \ + + OBJECTIVE_SOURCES += \ + ./WebSocket/src/socketrocket/socketRocket_objc.mm \ + ./WebSocket/src/socketrocket/socketRocket_internal.mm \ + ./WebSocket/src/manager/managerWebSocket.cpp \ + +} + +# FileTransporter +HEADERS += \ + ./FileTransporter/include/FileTransporter.h \ + ./FileTransporter/src/FileTransporter_private.h \ + ./FileTransporter/src/transport_external.h + +SOURCES += ./FileTransporter/src/FileTransporter.cpp + +core_windows { + SOURCES += \ + ./FileTransporter/src/FileTransporter_win.cpp + + LIBS += -lAdvapi32 + LIBS += -lurlmon + LIBS += -lRpcrt4 + LIBS += -lShell32 +} +core_linux { + CONFIG += use_external_transport + + use_external_transport { + DEFINES += USE_EXTERNAL_TRANSPORT + } else { + include(../3dParty/curl/curl.pri) + } + + SOURCES += \ + ./FileTransporter/src/FileTransporter_curl.cpp +} +core_mac { + DEFINES += USE_EXTERNAL_TRANSPORT + OBJECTIVE_SOURCES += \ + ./FileTransporter/src/FileTransporter_mac.mm + + LIBS += -framework AppKit +} + +core_ios { + OBJECTIVE_SOURCES += \ + ./FileTransporter/src/FileTransporter_mac.mm \ + ../../DesktopEditor/common/File_ios.mm + + LIBS += -framework Foundation + +} + +core_android { + DEFINES += USE_FILE32API + SOURCES += ./FileTransporter/src/FileTransporter_curl.cpp + + USE_EXTERNAL_TRANSPORT { + DEFINES += USE_EXTERNAL_TRANSPORT + } else { + include(../3dParty/curl/curl.pri) + } + + DEFINES += NOT_USE_PTHREAD_CANCEL +} + +use_mac_extensions { + INCLUDEPATH += $$CORE_ROOT_DIR/DesktopEditor/common/Mac + OBJECTIVE_SOURCES += \ + $$CORE_ROOT_DIR/DesktopEditor/common/Mac/NSString+StringUtils.mm \ + $$CORE_ROOT_DIR/DesktopEditor/common/Mac/NSFileManager+Utils.m +} +