code_app/sdk/webservice/HttpUser.cpp

910 lines
30 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "HttpUser.h"
#include "WebServer.h"
#include "MsgLoop.h"
#include "Manager.h"
#include "../../base/HGInfo.h"
#include "cJSON.h"
static std::string ToJsonPair(const std::string& key, int value, bool sep)
{
char ret[256];
sprintf(ret, "\"%s\":%d", key.c_str(), value);
std::string retStr = ret;
if (sep)
retStr += ", ";
return retStr;
}
static std::string ToJsonPair(const std::string& key, bool value, bool sep)
{
char ret[256];
sprintf(ret, "\"%s\":%s", key.c_str(), value ? "true" : "false");
std::string retStr = ret;
if (sep)
retStr += ", ";
return retStr;
}
static std::string ToJsonPair(const std::string& key, const std::string& value, bool sep)
{
char ret[256];
if (value.empty())
sprintf(ret, "\"%s\":null", key.c_str());
else
sprintf(ret, "\"%s\":\"%s\"", key.c_str(), value.c_str());
std::string retStr = ret;
if (sep)
retStr += ", ";
return retStr;
}
static int GetJsonIntValue(cJSON *json, const std::string& key)
{
int ret = 0;
cJSON* p = json->child;
while (NULL != p)
{
if (0 == strcmp(p->string, key.c_str()))
{
if (p->type == cJSON_Number)
ret = p->valueint;
break;
}
p = p->next;
}
return ret;
}
static bool GetJsonBoolValue(cJSON* json, const std::string& key)
{
bool ret = false;
cJSON* p = json->child;
while (NULL != p)
{
if (0 == strcmp(p->string, key.c_str()))
{
if (p->type == cJSON_True)
ret = true;
break;
}
p = p->next;
}
return ret;
}
static std::string GetJsonStringValue(cJSON* json, const std::string& key)
{
std::string ret;
cJSON* p = json->child;
while (NULL != p)
{
if (0 == strcmp(p->string, key.c_str()))
{
if (p->type == cJSON_String)
ret = p->valuestring;
break;
}
p = p->next;
}
return ret;
}
#if defined(HG_CMP_MSC)
HttpUser::HttpUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn)
#else
HttpUser::HttpUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn)
#endif
: WebUser(server, id, ip, port, sockConn)
{
}
HttpUser::~HttpUser()
{
}
void HttpUser::HandleCmd(const HttpCmdParam* param)
{
std::string requestMethod = param->head.GetRequestMethod();
std::string requestURIPath = param->head.GetRequestURIPath();
HttpPairs requestURIQueryInfos = param->head.GetRequestURIQueryInfos();
std::string requestHttpVersion = param->head.GetRequestHttpVersion();
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser: method=%s, path=%s, httpVersion=%s",
requestMethod.c_str(), requestURIPath.c_str(), requestHttpVersion.c_str());
if (requestMethod == "POST" && requestURIPath == "/WebScan/getVersionInfo")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
}
std::string devId;
GetManager()->GetCurDevId(devId);
char* result = new char[devId.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devId.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte *)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/getDevices")
{
std::vector<std::string> devNameList;
GetManager()->GetDevNames(devNameList);
std::string devNames = "[";
for (int i = 0; i < (int)devNameList.size(); ++i)
{
devNames += "\"";
devNames += devNameList[i];
devNames += "\"";
if (i != (int)devNameList.size() - 1)
devNames += ",";
}
devNames += "]";
char* result = new char[devNames.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, devNames.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/image/getImageByPid")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
std::vector<std::string> imgNameList;
std::vector<std::string> imgBase64List;
GetManager()->GetImageByDevId(devId, imgNameList, imgBase64List);
std::string imgInfos = "[";
for (int i = 0; i < (int)imgNameList.size(); ++i)
{
imgInfos += "{\"imageName\":\"";
imgInfos += imgNameList[i];
imgInfos += "\",\"src\":\"";
imgInfos += imgBase64List[i];
imgInfos += "\"}";
if (i != imgNameList.size() - 1)
imgInfos += ",";
}
imgInfos += "]";
char *result = new char[imgInfos.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgInfos.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/getParams")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
DevParam devParam;
GetManager()->GetDevParam(devId, devParam);
std::string devParamStr = "{";
devParamStr += ToJsonPair("device", devParam.device, true);
devParamStr += ToJsonPair("autofeeder", devParam.autofeeder, true);
devParamStr += ToJsonPair("pixel", devParam.pixel, true);
devParamStr += ToJsonPair("white", devParam.white, true);
devParamStr += ToJsonPair("discardBlankThre", devParam.discardBlankThre, true);
devParamStr += ToJsonPair("single", devParam.single, true);
devParamStr += ToJsonPair("format", devParam.format, true);
devParamStr += ToJsonPair("resolution", devParam.resolution, true);
devParamStr += ToJsonPair("orentation", devParam.orentation, true);
devParamStr += ToJsonPair("paperType", devParam.paperType, true);
devParamStr += ToJsonPair("splitImage", devParam.splitImage, true);
devParamStr += ToJsonPair("noiseDetachEnable", devParam.noiseDetachEnable, true);
devParamStr += ToJsonPair("noiseDetach", devParam.noiseDetach, true);
devParamStr += "\"upload\":{";
devParamStr += ToJsonPair("uploadMode", devParam.uploadMode, true);
devParamStr += ToJsonPair("httpUrl", devParam.httpUrl, true);
devParamStr += ToJsonPair("fileName", devParam.fileName, true);
devParamStr += ToJsonPair("httpMethod", devParam.httpMethod, true);
devParamStr += ToJsonPair("header", devParam.header, true);
devParamStr += ToJsonPair("param", devParam.param, true);
devParamStr += ToJsonPair("ftpUrl", devParam.ftpUrl, true);
devParamStr += ToJsonPair("ftpPath", devParam.ftpPath, true);
devParamStr += ToJsonPair("ftpUser", devParam.ftpUser, true);
devParamStr += ToJsonPair("ftpPassword", devParam.ftpPassword, true);
devParamStr += ToJsonPair("ftpPort", devParam.ftpPort, true);
devParamStr += ToJsonPair("ftpMode", devParam.ftpMode, false);
devParamStr += "}}";
char* result = new char[devParamStr.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, devParamStr.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/setParams")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char *)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::string devParamStr = HttpHead::GetValue(uriQueryInfos, "params");
DevParam devParam;
cJSON* json = cJSON_Parse(devParamStr.c_str());
if (NULL != json)
{
devParam.autofeeder = GetJsonBoolValue(json, "autofeeder");
devParam.pixel = GetJsonIntValue(json, "pixel");
devParam.white = GetJsonBoolValue(json, "white");
devParam.discardBlankThre = GetJsonIntValue(json, "discardBlankThre");
devParam.single = GetJsonBoolValue(json, "single");
devParam.format = GetJsonStringValue(json, "format");
devParam.resolution = GetJsonIntValue(json, "resolution");
if (0 == devParam.resolution)
devParam.resolution = atoi(GetJsonStringValue(json, "resolution").c_str());
devParam.orentation = GetJsonIntValue(json, "orentation");
devParam.paperType = GetJsonStringValue(json, "paperType");
devParam.splitImage = GetJsonIntValue(json, "splitImage");
devParam.noiseDetachEnable = GetJsonBoolValue(json, "noiseDetachEnable");
devParam.noiseDetach = GetJsonIntValue(json, "noiseDetach");
cJSON* p = json->child;
while (NULL != p)
{
if (0 == strcmp(p->string, "upload"))
{
break;
}
p = p->next;
}
if (NULL != p && p->type == cJSON_Object)
{
devParam.uploadMode = GetJsonIntValue(p, "uploadMode");
devParam.httpUrl = GetJsonStringValue(p, "httpUrl");
devParam.fileName = GetJsonStringValue(p, "fileName");
devParam.httpMethod = GetJsonStringValue(p, "httpMethod");
devParam.header = GetJsonStringValue(p, "header");
devParam.param = GetJsonStringValue(p, "param");
devParam.ftpUrl = GetJsonStringValue(p, "ftpUrl");
devParam.ftpPath = GetJsonStringValue(p, "ftpPath");
if (!devParam.ftpPath.empty() && devParam.ftpPath[0] != '/')
devParam.ftpPath.insert(0, "/");
devParam.ftpUser = GetJsonStringValue(p, "ftpUser");
devParam.ftpPassword = GetJsonStringValue(p, "ftpPassword");
devParam.ftpPort = GetJsonIntValue(p, "ftpPort");
if (0 == devParam.ftpPort)
devParam.ftpPort = atoi(GetJsonStringValue(p, "ftpPort").c_str());
devParam.ftpMode = GetJsonIntValue(p, "ftpMode");
}
cJSON_Delete(json);
}
GetManager()->SetDevParam(devId, devParam);
char result[256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"\"}", 200);
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorOfd")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string majorOfdParam = HttpHead::GetValue(uriQueryInfos, "formDataString");
std::string devId;
bool isAuto = false;
cJSON* json = cJSON_Parse(majorOfdParam.c_str());
if (NULL != json)
{
devId = GetJsonStringValue(json, "pid");
isAuto = GetJsonBoolValue(json, "isAuto");
cJSON_Delete(json);
}
std::string imgBase64;
GetManager()->ExportOfd(devId, isAuto, imgBase64);
char* result = new char[imgBase64.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorOfdFile")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
bool isAuto = ("true" == HttpHead::GetValue(requestURIQueryInfos, "isAuto") ? true : false);
HGByte* data = NULL;
HGUInt size = 0;
GetManager()->ExportOfdFile(devId, isAuto, &data, &size);
SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/zip");
delete[] data;
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorPdf")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string majorOfdParam = HttpHead::GetValue(uriQueryInfos, "formDataString");
std::string devId;
cJSON* json = cJSON_Parse(majorOfdParam.c_str());
if (NULL != json)
{
devId = GetJsonStringValue(json, "pid");
cJSON_Delete(json);
}
std::string imgBase64;
GetManager()->ExportPdf(devId, imgBase64);
char* result = new char[imgBase64.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorPdfFile")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
HGByte* data = NULL;
HGUInt size = 0;
GetManager()->ExportPdfFile(devId, &data, &size);
SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/pdf");
delete[] data;
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorTiff")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::string imgBase64;
GetManager()->ExportTiff(devId, imgBase64);
char* result = new char[imgBase64.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorTiffFile")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
HGByte* data = NULL;
HGUInt size = 0;
GetManager()->ExportTiffFile(devId, &data, &size);
SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/x-tif");
delete[] data;
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/downLoadZip")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::string imgBase64;
GetManager()->ExportZip(devId, imgBase64);
char* result = new char[imgBase64.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/downLoadZipFile")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
HGByte* data = NULL;
HGUInt size = 0;
GetManager()->ExportZipFile(devId, &data, &size);
SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/zip");
delete[] data;
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/uploadImage")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string uploadParamStr = HttpHead::GetValue(uriQueryInfos, "formDataString");
UploadParam uploadParam;
cJSON* json = cJSON_Parse(uploadParamStr.c_str());
if (NULL != json)
{
uploadParam.uploadMode = GetJsonIntValue(json, "uploadMode");
uploadParam.httpUrl = GetJsonStringValue(json, "httpUrl");
uploadParam.fileName = GetJsonStringValue(json, "fileName");
uploadParam.httpMethod = GetJsonStringValue(json, "httpMethod");
uploadParam.header = GetJsonStringValue(json, "header");
uploadParam.param = GetJsonStringValue(json, "param");
uploadParam.ftpUrl = GetJsonStringValue(json, "ftpUrl");
uploadParam.ftpPath = GetJsonStringValue(json, "ftpPath");
if (!uploadParam.ftpPath.empty() && uploadParam.ftpPath[0] != '/')
uploadParam.ftpPath.insert(0, "/");
uploadParam.ftpUser = GetJsonStringValue(json, "ftpUser");
uploadParam.ftpPassword = GetJsonStringValue(json, "ftpPassword");
uploadParam.ftpPort = GetJsonIntValue(json, "ftpPort");
if (0 == uploadParam.ftpPort)
uploadParam.ftpPort = atoi(GetJsonStringValue(json, "ftpPort").c_str());
uploadParam.ftpMode = GetJsonIntValue(json, "ftpMode");
uploadParam.format = GetJsonIntValue(json, "format");
cJSON_Delete(json);
}
bool ret = GetManager()->UploadImage(uploadParam);
char result[256];
if (ret)
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success");
else
sprintf(result, "{\"code\":%d, \"msg\":\"%s\", \"data\":\"%s\"}", 201, "uploadImage failed", "fail");
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/saveImage")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName");
std::string imgBase64 = HttpHead::GetValue(uriQueryInfos, "image");
GetManager()->SaveImage(devId, imgName, imgBase64);
char result[256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success");
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/deleteImage")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName");
GetManager()->DeleteImage(devId, imgName);
char result[256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success");
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/deleteAllImage")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
GetManager()->DeleteAllImage(devId);
char result[256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success");
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/mergeHorizontal")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
bool isHorizontal = ("false" != HttpHead::GetValue(uriQueryInfos, "isHorizontal")) ? true : false;
std::vector<int> imgIndexs;
for (int i = 0; i < (int)uriQueryInfos.size(); ++i)
{
if ("indexs[]" == uriQueryInfos[i].first)
{
imgIndexs.push_back(atoi(uriQueryInfos[i].second.c_str()));
}
}
std::string imgName, imgBase64;
GetManager()->MergeImage(devId, isHorizontal, imgIndexs, imgName, imgBase64);
char *result = new char[256 + imgName.size() + imgBase64.size()];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":{\"imageName\":\"%s\", \"src\":\"%s\"}}",
200, imgName.c_str(), imgBase64.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/bookSort")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::vector<std::string> imgNameList, imgBase64List;
GetManager()->BookSort(devId, imgNameList, imgBase64List);
std::string imgInfos = "[";
for (int i = 0; i < (int)imgNameList.size(); ++i)
{
imgInfos += "{\"imageName\":\"";
imgInfos += imgNameList[i];
imgInfos += "\",\"src\":\"";
imgInfos += imgBase64List[i];
imgInfos += "\"}";
if (i != imgNameList.size() - 1)
imgInfos += ",";
}
imgInfos += "]";
char* result = new char[imgInfos.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgInfos.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/exchangeImage")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
int index1 = -1, index2 = -1;
for (int i = 0; i < (int)uriQueryInfos.size(); ++i)
{
if ("indexs[]" == uriQueryInfos[i].first)
{
if (-1 == index1)
index1 = atoi(uriQueryInfos[i].second.c_str());
else if (-1 == index2)
index2 = atoi(uriQueryInfos[i].second.c_str());
}
}
GetManager()->ExchangeImage(devId, index1, index2);
char result[256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success");
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/getLastBatch")
{
std::string devId;
GetManager()->GetLastBetch(devId);
char* result = new char[devId.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devId.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/getSerialNumber")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
std::string devSerialNo;
GetManager()->GetDevSerialNo(devId, devSerialNo);
char* result = new char[devSerialNo.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devSerialNo.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "GET" && requestURIPath == "/WebScan/getImageByName")
{
std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid");
std::string imgName = HttpHead::GetValue(requestURIQueryInfos, "imageName");
std::string imgBase64;
GetManager()->GetImageBase64(devId, imgName, imgBase64);
char* result = new char[imgBase64.size() + 256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, imgBase64.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/resetPatchIndex")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
}
GetManager()->ResetPatchIndex();
char result[256];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success");
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
}
else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/split")
{
if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded"))
{
std::string query((const char*)param->data, param->size);
HttpPairs uriQueryInfos;
HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos);
std::string devId = HttpHead::GetValue(uriQueryInfos, "pid");
std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName");
bool isHorizontal = ("false" != HttpHead::GetValue(uriQueryInfos, "isHorizontal")) ? true : false;
int x1 = atoi(HttpHead::GetValue(uriQueryInfos, "x1").c_str());
int x2 = atoi(HttpHead::GetValue(uriQueryInfos, "x2").c_str());
int y1 = atoi(HttpHead::GetValue(uriQueryInfos, "y1").c_str());
int y2 = atoi(HttpHead::GetValue(uriQueryInfos, "y2").c_str());
std::string imgName1, imgBase64_1, imgName2, imgBase64_2;
GetManager()->SplitImage(devId, imgName, isHorizontal, x1, y1, x2, y2,
imgName1, imgBase64_1, imgName2, imgBase64_2);
char *result = new char[256 + imgName1.size() + imgBase64_1.size() + imgName2.size() + imgBase64_2.size()];
sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":{\"oneSrc\":\"%s\", \"oneName\":\"%s\", \"twoSrc\":\"%s\", \"twoName\":\"%s\"}}",
200, imgBase64_1.c_str(), imgName1.c_str(), imgBase64_2.c_str(), imgName2.c_str());
SendResponse(requestHttpVersion.c_str(), 200, "OK",
(const HGByte*)result, (HGUInt)strlen(result), "application/json");
delete[] result;
}
}
}
void HttpUser::ThreadFunc()
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser::ThreadFunc");
char chBuffer[2048];
const char* pBuffer = chBuffer;
int nBufferSize = 0;
unsigned char headDataTail[4] = { '\r', '\n', '\r', '\n' };
unsigned int headDataTailLen = 0;
std::string headData;
HttpHead headInfo;
uint8_t* data = NULL;
int dataSize = 0;
bool getData = false;
int getDataSize = 0;
while (1)
{
if (0 == nBufferSize)
{
int len = recv(m_sockConn, chBuffer, 2048, 0);
if (len <= 0)
{
// 这里跳出可能是服务器关闭了socketConn或者客户端关闭了socket或者网络断开
WebMsg msg;
msg.msgId = WEB_MSGID_DISCONNET;
msg.svrType = m_server->GetType();
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break;
}
else
{
pBuffer = chBuffer;
nBufferSize = len;
}
}
assert(nBufferSize > 0);
unsigned char b = *pBuffer;
++pBuffer;
--nBufferSize;
if (!getData)
{
headData.push_back(b);
if (b == headDataTail[headDataTailLen])
{
++headDataTailLen;
}
else
{
headDataTailLen = 0;
if (b == headDataTail[headDataTailLen])
{
++headDataTailLen;
}
}
if (4 == headDataTailLen)
{
headDataTailLen = 0;
if (!headInfo.Parse(headData.c_str()))
{
WebMsg msg;
msg.msgId = WEB_MSGID_DISCONNET;
msg.svrType = m_server->GetType();
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break;
}
headData.clear();
int contentLen = headInfo.GetContentLength();
if (contentLen > 0)
{
data = new uint8_t[contentLen];
dataSize = contentLen;
getData = true;
getDataSize = 0;
}
else
{
HttpCmdParam* param = new HttpCmdParam;
param->head = headInfo;
param->data = NULL;
param->size = 0;
WebMsg msg;
msg.msgId = WEB_MSGID_HTTPCMD;
msg.svrType = m_server->GetType();
msg.usrId = m_id;
msg.param = param;
bool b = GetLoop()->Send(&msg);
if (!b)
{
delete param;
}
headInfo.Clear();
}
}
}
else
{
data[getDataSize] = b;
++getDataSize;
if (getDataSize == dataSize)
{
HttpCmdParam* param = new HttpCmdParam;
param->head = headInfo;
param->data = new HGByte [dataSize];
param->size = dataSize;
memcpy(param->data, data, dataSize);
WebMsg msg;
msg.msgId = WEB_MSGID_HTTPCMD;
msg.svrType = m_server->GetType();
msg.usrId = m_id;
msg.param = param;
bool b = GetLoop()->Send(&msg);
if (!b)
{
delete[] param->data;
param->size = 0;
delete param;
}
delete [] data;
data = NULL;
dataSize = 0;
getData = false;
getDataSize = 0;
headInfo.Clear();
}
}
}
if (NULL != data)
{
delete[] data;
data = NULL;
dataSize = 0;
getData = false;
getDataSize = 0;
}
}
bool HttpUser::SendResponse(const char* httpVersion, HGUInt errCode, const char* errInfo,
const HGByte* data, HGUInt size, const char* contentType)
{
if (NULL == httpVersion || NULL == errInfo)
{
return false;
}
char response[256];
sprintf(response, "%s %u %s\r\n", httpVersion, errCode, errInfo);
char head[256];
if (NULL != data)
{
assert(0 != size);
assert(NULL != contentType);
sprintf(head, "%s: %u\r\n%s: %s\r\n%s: %s\r\n\r\n",
"Content-Length", size,
"Content-Type", contentType,
"Access-Control-Allow-Origin", "null");
}
else
{
assert(0 == size);
sprintf(head, "%s: %s\r\n\r\n",
"Access-Control-Allow-Origin", "null");
}
HGBase_EnterLock(m_cs);
send(m_sockConn, response, (int)strlen(response), 0);
send(m_sockConn, head, (int)strlen(head), 0);
if (NULL != data && 0 != size)
send(m_sockConn, (const char *)data, (int)size, 0);
HGBase_LeaveLock(m_cs);
return true;
}