From 3ffb86880872b192acc312a8ce624a24f3ad94e2 Mon Sep 17 00:00:00 2001 From: luoliangyi <87842688@qq.com> Date: Fri, 24 Jun 2022 13:52:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90cgi=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E7=9A=84http=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/windows/HGSolution.sln | 17 ++ build/windows/HGUpload/HGUpload.vcxproj | 177 ++++++++++++++++++ .../windows/HGWebService/HGWebService.vcxproj | 8 +- sdk/upload/main.cpp | 111 +++++++++++ sdk/webservice/ManagerV2.cpp | 58 ++++-- test/webservice/demo.html | 16 ++ third_party/cgic/cgic.c | 24 +-- 7 files changed, 375 insertions(+), 36 deletions(-) create mode 100644 build/windows/HGUpload/HGUpload.vcxproj create mode 100644 sdk/upload/main.cpp diff --git a/build/windows/HGSolution.sln b/build/windows/HGSolution.sln index 3d59d72b..8e0b40c4 100644 --- a/build/windows/HGSolution.sln +++ b/build/windows/HGSolution.sln @@ -44,6 +44,15 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HGTwainUser", "HGTwainUser\ {F85F4457-1B42-46E7-BA86-F088D6D5994F} = {F85F4457-1B42-46E7-BA86-F088D6D5994F} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HGUpload", "HGUpload\HGUpload.vcxproj", "{FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}" + ProjectSection(ProjectDependencies) = postProject + {F85F4457-1B42-46E7-BA86-F088D6D5994F} = {F85F4457-1B42-46E7-BA86-F088D6D5994F} + {EFAE5F69-A4C2-4A07-8BE6-68714D86BCE2} = {EFAE5F69-A4C2-4A07-8BE6-68714D86BCE2} + {4954F36B-A0A3-4550-9C4E-3B3D210F5CE4} = {4954F36B-A0A3-4550-9C4E-3B3D210F5CE4} + {5D85F2AC-FACD-436C-A67B-E13056DD0C03} = {5D85F2AC-FACD-436C-A67B-E13056DD0C03} + {4909ACEA-80FF-482E-9FA2-5E8534789A82} = {4909ACEA-80FF-482E-9FA2-5E8534789A82} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -108,6 +117,14 @@ Global {EFAE5F69-A4C2-4A07-8BE6-68714D86BCE2}.Release|x64.Build.0 = Release|x64 {EFAE5F69-A4C2-4A07-8BE6-68714D86BCE2}.Release|x86.ActiveCfg = Release|Win32 {EFAE5F69-A4C2-4A07-8BE6-68714D86BCE2}.Release|x86.Build.0 = Release|Win32 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Debug|x64.ActiveCfg = Debug|x64 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Debug|x64.Build.0 = Debug|x64 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Debug|x86.ActiveCfg = Debug|Win32 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Debug|x86.Build.0 = Debug|Win32 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Release|x64.ActiveCfg = Release|x64 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Release|x64.Build.0 = Release|x64 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Release|x86.ActiveCfg = Release|Win32 + {FC39DAEA-2BB2-45AE-A7F3-F3879692A66B}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/build/windows/HGUpload/HGUpload.vcxproj b/build/windows/HGUpload/HGUpload.vcxproj new file mode 100644 index 00000000..e0fe30ea --- /dev/null +++ b/build/windows/HGUpload/HGUpload.vcxproj @@ -0,0 +1,177 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + 16.0 + Win32Proj + {fc39daea-2bb2-45ae-a7f3-f3879692a66b} + HGUpload + 10.0 + + + + Application + true + v142 + MultiByte + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + upload + .cgi + + + false + upload + .cgi + + + true + upload + .cgi + + + false + upload + .cgi + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + MultiThreadedDebug + ../../../modules/;../../../third_party/cgic/;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + MultiThreaded + ../../../modules/;../../../third_party/cgic/;%(AdditionalIncludeDirectories) + + + Console + true + true + true + /LTCG %(AdditionalOptions) + + + copy $(OutDir)upload.cgi $(SolutionDir)..\..\..\release\win\x86\Release\ + + + + + Level3 + true + _DEBUG;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + MultiThreadedDebug + ../../../modules/;../../../third_party/cgic/;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + MultiThreaded + ../../../modules/;../../../third_party/cgic/;%(AdditionalIncludeDirectories) + + + Console + true + true + true + /LTCG %(AdditionalOptions) + + + copy $(OutDir)upload.cgi $(SolutionDir)..\..\..\release\win\x64\Release\ + + + + + + \ No newline at end of file diff --git a/build/windows/HGWebService/HGWebService.vcxproj b/build/windows/HGWebService/HGWebService.vcxproj index b1baf59e..3ae0ac8e 100644 --- a/build/windows/HGWebService/HGWebService.vcxproj +++ b/build/windows/HGWebService/HGWebService.vcxproj @@ -129,7 +129,7 @@ Level3 true - WIN32;_DEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;CURL_STATICLIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;_DEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;CURL_STATICLIB;_CRT_SECURE_NO_WARNINGS;OEM_HUAGAO;%(PreprocessorDefinitions) true MultiThreadedDebug ../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../third_party/sqlite/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories) @@ -148,7 +148,7 @@ true true true - WIN32;NDEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;CURL_STATICLIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + WIN32;NDEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;CURL_STATICLIB;_CRT_SECURE_NO_WARNINGS;OEM_HUAGAO;%(PreprocessorDefinitions) true MultiThreaded ../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../third_party/sqlite/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories) @@ -169,7 +169,7 @@ Level3 true - _DEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _DEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;_CRT_SECURE_NO_WARNINGS;OEM_HUAGAO;%(PreprocessorDefinitions) true MultiThreadedDebug ../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../third_party/sqlite/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories) @@ -187,7 +187,7 @@ true true true - NDEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + NDEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;ZIP_STATIC;_CRT_SECURE_NO_WARNINGS;OEM_HUAGAO;%(PreprocessorDefinitions) true MultiThreaded ../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../third_party/sqlite/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories) diff --git a/sdk/upload/main.cpp b/sdk/upload/main.cpp new file mode 100644 index 00000000..c34876e3 --- /dev/null +++ b/sdk/upload/main.cpp @@ -0,0 +1,111 @@ +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "base/HGUtility.h" +#include +#include "cgic.h" + +int cgiMain() +{ + char mimiType[32] = "text/html; charset=utf-8"; + char fileName[32] = "filename"; + char remoteFileName[32] = "remote_filename"; + + cgiHeaderContentType(mimiType); + + char* scriptPath = getenv("PATH_INFO"); + fprintf(cgiOut, "scriptPath: %s
\n", scriptPath); + char* scriptFullPath = getenv("PATH_TRANSLATED"); + fprintf(cgiOut, "scriptFullPath: %s
\n", scriptFullPath); + if (NULL == scriptPath || NULL == scriptFullPath) + { + fprintf(cgiOut, "

get PATH_INFO or PATH_TRANSLATED failed.

\n"); + return cgiFormNotFound; + } + + char stdScriptPath[256]; + HGBase_StandardiseFileName(scriptPath, stdScriptPath, 256); + char* p = strstr(scriptFullPath, stdScriptPath); + if (NULL == p) + { + fprintf(cgiOut, "

PATH_INFO or PATH_TRANSLATED error.

\n"); + return cgiFormNotFound; + } + + std::string rootPath(scriptFullPath, p - scriptFullPath); + + fprintf(cgiOut, "rootPath: "); + cgiHtmlEscape(rootPath.c_str()); + fprintf(cgiOut, "
\n"); + + char name[512]; + if (cgiFormFileName(fileName, name, sizeof(name)) != cgiFormSuccess) + { + fprintf(cgiOut, "

no filename.

\n"); + return cgiFormNoFileName; + } + + fprintf(cgiOut, "filename: "); + cgiHtmlEscape(name); + fprintf(cgiOut, "
\n"); + + int size = 0; + cgiFormFileSize(fileName, &size); + if (size <= 0) + { + fprintf(cgiOut, "

bad file size.

\n"); + return cgiFormEmpty; + } + + fprintf(cgiOut, "file size: %d bytes
\n", size); + + char remoteFilePath[512]; + if (cgiFormString(remoteFileName, remoteFilePath, sizeof(remoteFilePath)) != cgiFormSuccess) + { + fprintf(cgiOut, "

no remote filename.

\n"); + return cgiFormNoFileName; + } + + fprintf(cgiOut, "remote_filepath: "); + cgiHtmlEscape(remoteFilePath); + fprintf(cgiOut, "
\n"); + + cgiFilePtr file = NULL; + if (cgiFormFileOpen(fileName, &file) != cgiFormSuccess) + { + fprintf(cgiOut, "

Could not open the file.

\n"); + return cgiFormOpenFailed; + } + + std::string savePath = rootPath + remoteFilePath; + char stdSavePath[256]; + HGBase_StandardiseFileName(savePath.c_str(), stdSavePath, 256); + char stdSaveDir[256]; + HGBase_GetFilePath(stdSavePath, stdSaveDir, 256); + HGBase_CreateDir(stdSaveDir); + + fprintf(cgiOut, "savePath: "); + cgiHtmlEscape(stdSavePath); + fprintf(cgiOut, "
\n"); + + FILE *fd = fopen(stdSavePath, "wb+"); + if (fd == NULL) + { + fprintf(cgiOut, "

create file failed.

\n"); + cgiFormFileClose(file); + return cgiFormOpenFailed; + } + + char tmp[1024]; + int got = 0; + while (cgiFormFileRead(file, tmp, 1024, &got) == cgiFormSuccess) + { + fwrite(tmp, got, sizeof(char), fd); //把读出的content写入新文件 + } + + fclose(fd); + cgiFormFileClose(file); + + //打印输出 + fprintf(cgiOut, "

upload file success.

\n"); + return cgiFormSuccess; +} \ No newline at end of file diff --git a/sdk/webservice/ManagerV2.cpp b/sdk/webservice/ManagerV2.cpp index ba285168..827babbc 100644 --- a/sdk/webservice/ManagerV2.cpp +++ b/sdk/webservice/ManagerV2.cpp @@ -2830,7 +2830,41 @@ namespace ver_2 int ManagerV2::HttpUpload(const std::string& host, int port, const std::string& path, const std::string& filePath, const std::string& remoteFilePath) { - return -1; + curl_global_init(CURL_GLOBAL_ALL); + + int ret = -1; + CURL* curl = curl_easy_init(); + if (curl) + { + struct curl_httppost* formpost = NULL; + struct curl_httppost* lastptr = NULL; + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_FILE, filePath.c_str(), CURLFORM_END); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "remote_filename", CURLFORM_COPYCONTENTS, remoteFilePath.c_str(), CURLFORM_END); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END); + + char url[512]; + sprintf(url, "http://%s:%d%s", host.c_str(), port, path.c_str()); + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + + /* Perform the request, res will get the return code */ + CURLcode res = curl_easy_perform(curl); + /* Check for errors */ + if (res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res)); + else + ret = 0; + + /* always cleanup */ + curl_easy_cleanup(curl); + + /* then cleanup the formpost chain */ + curl_formfree(formpost); + } + + curl_global_cleanup(); + return ret; } static size_t read_callback(char* ptr, size_t size, size_t nmemb, void* stream) @@ -2870,22 +2904,6 @@ namespace ver_2 CURL* curl = curl_easy_init(); if (NULL != curl) { - //char tmpName[256]; - //HGBase_GetUuid(tmpName, 256); - char remotePath[256]; - HGBase_GetFilePath(remoteFilePath.c_str(), remotePath, 256); - char remoteName[256]; - HGBase_GetFileName(remoteFilePath.c_str(), remoteName, 256); - - //char ftp_rnfr[512]; - //sprintf(ftp_rnfr, "RNFR %s", tmpName); - //char ftp_rnto[512]; - //sprintf(ftp_rnto, "RNTO %s", remoteName); - - //struct curl_slist* headerlist = NULL; - //headerlist = curl_slist_append(headerlist, ftp_rnfr); - //headerlist = curl_slist_append(headerlist, ftp_rnto); - /* we want to use our own read function */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); @@ -2894,12 +2912,12 @@ namespace ver_2 char url[512]; if (!user.empty() && !password.empty()) { - sprintf(url, "ftp://%s:%s@%s:%d%s%s", user.c_str(), password.c_str(), - host.c_str(), port, remotePath, remoteName); + sprintf(url, "ftp://%s:%s@%s:%d%s", user.c_str(), password.c_str(), + host.c_str(), port, remoteFilePath.c_str()); } else { - sprintf(url, "ftp://%s:%d%s%s", host.c_str(), port, remotePath, remoteName); + sprintf(url, "ftp://%s:%d%s", host.c_str(), port, remoteFilePath.c_str()); } curl_easy_setopt(curl, CURLOPT_URL, url); diff --git a/test/webservice/demo.html b/test/webservice/demo.html index 267b416b..563949a5 100644 --- a/test/webservice/demo.html +++ b/test/webservice/demo.html @@ -360,6 +360,18 @@ })); } + function HttpUpload() + { + socket.send(JSON.stringify({ + 'func':'upload_local_file', + 'file_path':'D:\\1.jpg', + 'remote_file_path':'/savedir/123.jpg', + 'upload_mode':'http', + 'http_host':'192.168.100.148', + 'http_port':8001 + })); + } + function MergeLocalImage() { socket.send(JSON.stringify({ @@ -831,10 +843,13 @@ + + + @@ -876,6 +891,7 @@ + diff --git a/third_party/cgic/cgic.c b/third_party/cgic/cgic.c index 0af957f5..f7b9f0e7 100644 --- a/third_party/cgic/cgic.c +++ b/third_party/cgic/cgic.c @@ -31,7 +31,7 @@ #include #include -#ifdef WIN32 +#ifdef _WIN32 #include /* cgic 2.01 */ @@ -39,7 +39,7 @@ #else #include -#endif /* WIN32 */ +#endif /* _WIN32 */ #include "cgic.h" #define cgiStrEq(a, b) (!strcmp((a), (b))) @@ -200,12 +200,12 @@ int main(int argc, char *argv[]) { fprintf(dout, "%s\n", cgiContentType); CGICDEBUGEND #endif /* CGICDEBUG */ -#ifdef WIN32 +#ifdef _WIN32 /* 1.07: Must set stdin and stdout to binary mode */ /* 2.0: this is particularly crucial now and must not be removed */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); -#endif /* WIN32 */ +#endif /* _WIN32 */ cgiFormEntryFirst = 0; cgiIn = stdin; cgiOut = stdout; @@ -274,7 +274,7 @@ int main(int argc, char *argv[]) { } else if (cgiStrEqNc(cgiRequestMethod, "get")) { /* The spec says this should be taken care of by the server, but... it isn't */ - cgiContentLength = strlen(cgiQueryString); + cgiContentLength = (int)strlen(cgiQueryString); if (cgiParseGetFormInput() != cgiParseSuccess) { #ifdef CGICDEBUG CGICDEBUGSTART @@ -371,7 +371,7 @@ int mpRead(mpStreamPtr mpp, char *buffer, int len) } } if (len) { - int fgot = fread(buffer, 1, len, cgiIn); + int fgot = (int)fread(buffer, 1, len, cgiIn); if (fgot >= 0) { mpp->offset += (got + fgot); return got + fgot; @@ -634,7 +634,7 @@ static cgiParseResultType getTempFile(FILE **tFile) length is strlen(cgiTempDir) + a short unique pattern. */ char tfileName[1024]; -#ifndef WIN32 +#ifndef _WIN32 /* Unix. Use the robust 'mkstemp' function to create a temporary file that is truly unique, with permissions that are truly safe. The @@ -726,7 +726,7 @@ cgiParseResultType afterNextBoundary(mpStreamPtr mpp, FILE *outf, char **outP, if (first) { workingBoundary = workingBoundaryData + 2; } - workingBoundaryLength = strlen(workingBoundary); + workingBoundaryLength = (int)strlen(workingBoundary); while (1) { got = mpRead(mpp, d, 1); if (got != 1) { @@ -1060,7 +1060,7 @@ static cgiParseResultType cgiParseFormInput(char *data, int length) { } n->attr = attr; n->value = value; - n->valueLength = strlen(n->value); + n->valueLength = (int)strlen(n->value); n->fileName = (char *) malloc(1); if (!n->fileName) { free(attr); @@ -1345,7 +1345,7 @@ cgiFormResultType cgiFormFileRead( if (!cfp) { return cgiFormOpenFailed; } - got = fread(buffer, 1, bufferSize, cfp->in); + got = (int)fread(buffer, 1, bufferSize, cfp->in); if (got <= 0) { return cgiFormEOF; } @@ -2187,7 +2187,7 @@ cgiEnvironmentResultType cgiReadEnvironment(char *filename) { if (tryr > ((int) sizeof(buffer))) { tryr = sizeof(buffer); } - got = fread(buffer, 1, tryr, in); + got = (int)fread(buffer, 1, tryr, in); if (got <= 0) { result = cgiEnvironmentIO; fclose(out); @@ -2329,7 +2329,7 @@ static cgiFormEntry *cgiFormEntryFindNext() { } static int cgiFirstNonspaceChar(char *s) { - int len = strspn(s, " \n\r\t"); + int len = (int)strspn(s, " \n\r\t"); return s[len]; }