Disable ssl in wget commands on old macos

This commit is contained in:
Oleg Korshul
2023-05-05 22:17:49 +03:00
parent 6d45b90ac0
commit 3cb1503632
2 changed files with 305 additions and 274 deletions

View File

@ -24,6 +24,8 @@ core_linux {
SOURCES += $$PWD/src/FileTransporter_curl.cpp
}
core_mac {
use_v8:DEFINES += OLD_MACOS_SYSTEM
OBJECTIVE_SOURCES += $$PWD/src/FileTransporter_mac.mm
LIBS += -framework AppKit
}

View File

@ -49,323 +49,352 @@
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 "";
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);
}
return url.substr(pos);
}
int download_external(const std::wstring& sUrl, const std::wstring& sOutput, std::function<void(int)> func_onProgress = nullptr, std::function<bool(void)> func_checkAborted = nullptr)
{
pid_t pid;
int nReturnCode = -1;
std::string sUrlA = U_TO_UTF8(sUrl);
//sUrlA =("\"" + sUrlA + "\"");
std::string sOutputA = U_TO_UTF8(sOutput);
//sOutputA =("\"" + sOutputA + "\"");
int download_external(const std::wstring& sUrl, const std::wstring& sOutput, std::function<void(int)> func_onProgress = nullptr, std::function<bool(void)> func_checkAborted = nullptr)
{
pid_t pid;
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"))
{
int pipefd[2];
if(func_onProgress)
pipe(pipefd);
std::wstring sCurlBin = L"";
if (NSFile::CFileBinary::Exists(L"/usr/bin/curl"))
sCurlBin = L"/usr/bin/curl";
if (NSFile::CFileBinary::Exists(L"/usr/local/bin/curl"))
sCurlBin = L"/usr/local/bin/curl";
pid = fork(); // create child process
int status;
if (0 != nReturnCode && !sCurlBin.empty())
{
int pipefd[2];
if(func_onProgress)
pipe(pipefd);
switch (pid)
{
case -1: // error
break;
pid = fork(); // create child process
int status;
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();
func_onProgress == NULL ? nargs[5] = "--silent" : nargs[5] = "--progress-bar";
nargs[6] = "-L";
nargs[7] = "--connect-timeout";
nargs[8] = "10";
nargs[9] = NULL;
switch (pid)
{
case -1: // error
break;
const char* nenv[3];
nenv[0] = "LD_PRELOAD=";
nenv[1] = "LD_LIBRARY_PATH=";
nenv[2] = NULL;
case 0: // child process
{
std::string sProgramBinA = U_TO_UTF8(sCurlBin);
if(func_onProgress)
{
close(pipefd[0]); // close reading end in the child
const char* nargs[10];
nargs[0] = sProgramBinA.c_str();
nargs[1] = "--url";
nargs[2] = sUrlA.c_str();
nargs[3] = "--output";
nargs[4] = sOutputA.c_str();
func_onProgress == NULL ? nargs[5] = "--silent" : nargs[5] = "--progress-bar";
nargs[6] = "-L";
nargs[7] = "--connect-timeout";
nargs[8] = "10";
nargs[9] = NULL;
dup2(pipefd[1], 1); // send stdout to the pipe
dup2(pipefd[1], 2); // send stderr to the pipe
const char* nenv[3];
nenv[0] = "LD_PRELOAD=";
nenv[1] = "LD_LIBRARY_PATH=";
nenv[2] = NULL;
close(pipefd[1]); // this descriptor is no longer needed
}
if(func_onProgress)
{
close(pipefd[0]); // close reading end in the child
execve("/usr/bin/curl", (char * const *)nargs, (char * const *)nenv);
exit(EXIT_SUCCESS);
break;
}
default: // parent process, pid now contains the child pid
if(func_onProgress)
{
close(pipefd[1]);
// close the write end of the pipe in the parent
size_t size = 81;
char buffer[size];
std::string str;
ssize_t res = 1;
std::regex r(R"(\d+(?:\.\d+)?%)");
std::smatch sm;
std::string percentFull;
std::string percent;
int percentInt;
dup2(pipefd[1], 1); // send stdout to the pipe
dup2(pipefd[1], 2); // send stderr to the pipe
while (1)
{
if(func_checkAborted && func_checkAborted())
{
kill(pid, SIGTERM);
//while (-1 == waitpid(pid, &status, 0)); // wait for child to complete
return nReturnCode;
}
close(pipefd[1]); // this descriptor is no longer needed
}
str.clear();
res = read(pipefd[0], buffer, sizeof(buffer));
execve("/usr/bin/curl", (char * const *)nargs, (char * const *)nenv);
exit(EXIT_SUCCESS);
break;
}
default: // parent process, pid now contains the child pid
if(func_onProgress)
{
close(pipefd[1]);
// close the write end of the pipe in the parent
size_t size = 81;
char buffer[size];
std::string str;
ssize_t res = 1;
std::regex r(R"(\d+(?:\.\d+)?%)");
std::smatch sm;
std::string percentFull;
std::string percent;
int percentInt;
if(res == 0)
break;
while (1)
{
if(func_checkAborted && func_checkAborted())
{
kill(pid, SIGTERM);
//while (-1 == waitpid(pid, &status, 0)); // wait for child to complete
return nReturnCode;
}
str.append(buffer);
str.clear();
res = read(pipefd[0], buffer, sizeof(buffer));
if(regex_search(str, sm, r))
{
percentFull = sm.str();
percent = percentFull.substr(0, percentFull.find("."));
percentInt = std::stoi(percent);
if(res == 0)
break;
if(percentInt >= 0 && percentInt <= 100)
func_onProgress(percentInt);
}
str.append(buffer);
if(str.find("100.0%") != std::string::npos)
break;
if(regex_search(str, sm, r))
{
percentFull = sm.str();
percent = percentFull.substr(0, percentFull.find("."));
percentInt = std::stoi(percent);
}
}
else {
int waitres;
while (1) // wait for child to complete
{
if(func_checkAborted && func_checkAborted())
{
kill(pid, SIGTERM);
return nReturnCode;
}
else if((waitres = waitpid(pid, &status, WNOHANG)) > 0)
{
if (WIFEXITED(status))
{
nReturnCode = WEXITSTATUS(status);
}
break;
}
}
}
}
}
if(percentInt >= 0 && percentInt <= 100)
func_onProgress(percentInt);
}
if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/wget"))
{
std::string sUrlValidateA = wget_url_validate(sUrlA);
if(str.find("100.0%") != std::string::npos)
break;
pid = fork(); // create child process
int status;
}
}
else {
int waitres;
while (1) // wait for child to complete
{
if(func_checkAborted && func_checkAborted())
{
kill(pid, SIGTERM);
return nReturnCode;
}
else if((waitres = waitpid(pid, &status, WNOHANG)) > 0)
{
if (WIFEXITED(status))
{
nReturnCode = WEXITSTATUS(status);
}
break;
}
}
}
}
}
switch (pid)
{
case -1: // error
break;
std::wstring sWgetBin = L"";
if (NSFile::CFileBinary::Exists(L"/usr/bin/wget"))
sWgetBin = L"/usr/bin/wget";
if (NSFile::CFileBinary::Exists(L"/usr/local/bin/wget"))
sWgetBin = L"/usr/local/bin/wget";
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;
if (0 != nReturnCode && !sWgetBin.empty())
{
std::string sUrlValidateA = wget_url_validate(sUrlA);
const char* nenv[2];
nenv[0] = "LD_PRELOAD=";
nenv[1] = NULL;
pid = fork(); // create child process
int status;
execve("/usr/bin/wget", (char * const *)nargs, (char * const *)nenv);
exit(EXIT_SUCCESS);
break;
}
default: // parent process, pid now contains the child pid
int waitres;
while (1) // wait for child to complete
{
if(func_checkAborted && func_checkAborted())
{
kill(pid, SIGTERM);
return nReturnCode;
}
else if((waitres = waitpid(pid, &status, WNOHANG)) > 0)
{
if (WIFEXITED(status))
{
nReturnCode = WEXITSTATUS(status);
}
break;
}
}
}
}
switch (pid)
{
case -1: // error
break;
if (0 == nReturnCode)
{
if (!NSFile::CFileBinary::Exists(sOutput))
nReturnCode = -1;
}
case 0: // child process
{
std::string sProgramBinA = U_TO_UTF8(sWgetBin);
return nReturnCode;
}
#ifndef OLD_MACOS_SYSTEM
const char* nargs[8];
#else
const char* nargs[9];
#endif
int uploaddata_external(const std::wstring &sUploadUrl, const unsigned char* cData, const int nSize)
{
int nReturnCode = -1;
NSFile::CFileBinary oFileData;
std::wstring tempFileName = NSFile::CFileBinary::GetTempPath() + L"/tmpFileForUpload";
oFileData.CreateFileW(tempFileName);
oFileData.WriteFile(cData, nSize);
oFileData.CloseFile();
nargs[0] = sProgramBinA.c_str();
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";
std::string sUploadUrlA = U_TO_UTF8(sUploadUrl);
#ifndef OLD_MACOS_SYSTEM
nargs[7] = NULL;
#else
nargs[7] = "--no-check-certificate";
nargs[8] = NULL;
#endif
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
int waitres;
while (1) // wait for child to complete
{
if(func_checkAborted && func_checkAborted())
{
kill(pid, SIGTERM);
return nReturnCode;
}
else if((waitres = waitpid(pid, &status, WNOHANG)) > 0)
{
if (WIFEXITED(status))
{
nReturnCode = WEXITSTATUS(status);
}
break;
}
}
}
}
if (0 == nReturnCode)
{
if (!NSFile::CFileBinary::Exists(sOutput))
nReturnCode = -1;
}
return nReturnCode;
}
int uploaddata_external(const std::wstring &sUploadUrl, const unsigned char* cData, const int nSize)
{
int nReturnCode = -1;
NSFile::CFileBinary oFileData;
std::wstring tempFileName = NSFile::CFileBinary::GetTempPath() + L"/tmpFileForUpload";
oFileData.CreateFileW(tempFileName);
oFileData.WriteFile(cData, nSize);
oFileData.CloseFile();
std::string sUploadUrlA = U_TO_UTF8(sUploadUrl);
if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/curl"))
{
pid_t pid = fork(); // create child process
int status;
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;
switch (pid)
{
case -1: // error
break;
case 0: // child process
{
//curl --request POST --data-binary "@template_entry.xml" $URL
auto pathTofile(std::string("@") + std::string(tempFileName.begin(), tempFileName.end()));
const char* nargs[9];
nargs[0] = "/usr/bin/curl";
nargs[1] = "--request";
nargs[2] = "POST";
nargs[3] = "--data-binary";
nargs[4] = pathTofile.c_str();
nargs[5] = sUploadUrlA.c_str();
nargs[6] = "--connect-timeout";
nargs[7] = "10";
nargs[8] = NULL;
case 0: // child process
{
//curl --request POST --data-binary "@template_entry.xml" $URL
auto pathTofile(std::string("@") + std::string(tempFileName.begin(), tempFileName.end()));
const char* nargs[9];
nargs[0] = "/usr/bin/curl";
nargs[1] = "--request";
nargs[2] = "POST";
nargs[3] = "--data-binary";
nargs[4] = pathTofile.c_str();
nargs[5] = sUploadUrlA.c_str();
nargs[6] = "--connect-timeout";
nargs[7] = "10";
nargs[8] = NULL;
const char* nenv[3];
nenv[0] = "LD_PRELOAD=";
nenv[1] = "LD_LIBRARY_PATH=";
nenv[2] = 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;
}
}
NSFile::CFileBinary::Remove(tempFileName);
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;
}
}
NSFile::CFileBinary::Remove(tempFileName);
return nReturnCode;
}
return nReturnCode;
}
int uploadfile_external(const std::wstring &sUploadUrl, const std::wstring &sUploadFilePath)
{
int nReturnCode = -1;
int uploadfile_external(const std::wstring &sUploadUrl, const std::wstring &sUploadFilePath)
{
int nReturnCode = -1;
std::string sUploadUrlA = U_TO_UTF8(sUploadUrl);
std::string sUploadFilePathA = U_TO_UTF8(sUploadFilePath);
std::string sUploadFileNameA = U_TO_UTF8(NSFile::GetFileName(sUploadFilePath));
std::string sUploadUrlA = U_TO_UTF8(sUploadUrl);
std::string sUploadFilePathA = U_TO_UTF8(sUploadFilePath);
std::string sUploadFileNameA = U_TO_UTF8(NSFile::GetFileName(sUploadFilePath));
if (0 != nReturnCode && NSFile::CFileBinary::Exists(L"/usr/bin/curl"))
{
pid_t pid = fork(); // create child process
int status;
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;
switch (pid)
{
case -1: // error
break;
case 0: // child process
{
//curl -v -F filename=image.jpg -F upload=@/home/vladimir/Pictures/Test.png $URL
auto filename(std::string("filename=") + sUploadFileNameA);
auto upload(std::string("upload=@") + sUploadFilePathA);
const char* nargs[10];
nargs[0] = "/usr/bin/curl";
nargs[1] = "-v";
nargs[2] = "-F";
nargs[3] = filename.c_str();
nargs[4] = "-F";
nargs[5] = upload.c_str();
nargs[6] = sUploadUrlA.c_str();
nargs[7] = "--connect-timeout";
nargs[8] = "10";
nargs[9] = NULL;
case 0: // child process
{
//curl -v -F filename=image.jpg -F upload=@/home/vladimir/Pictures/Test.png $URL
auto filename(std::string("filename=") + sUploadFileNameA);
auto upload(std::string("upload=@") + sUploadFilePathA);
const char* nargs[10];
nargs[0] = "/usr/bin/curl";
nargs[1] = "-v";
nargs[2] = "-F";
nargs[3] = filename.c_str();
nargs[4] = "-F";
nargs[5] = upload.c_str();
nargs[6] = sUploadUrlA.c_str();
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;
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;
}
}
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;
}
}
return nReturnCode;
}
}
return nReturnCode;
}
}
}
#endif