Skip to content

Commit 06cc067

Browse files
committed
modify:can set whether to verify the hash value when uploading files
1 parent 7ec9e2d commit 06cc067

File tree

2 files changed

+127
-100
lines changed

2 files changed

+127
-100
lines changed

XEngine_Source/XEngine_StorageApp/StorageApp_UPLoader.cpp

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ XHTHREAD CALLBACK XEngine_UPLoader_HTTPThread(LPVOID lParam)
1212
{
1313
int nListCount = 0;
1414
RFCCOMPONENTS_HTTP_PKTCLIENT** ppSt_PKTClient;
15-
16-
1715
//获取当前队列池中所有触发上传客户端
1816
RfcComponents_HttpServer_GetPoolEx(xhUPHttp, nThreadPos, &ppSt_PKTClient, &nListCount);
1917
for (int i = 0; i < nListCount; i++)
@@ -103,19 +101,23 @@ BOOL XEngine_Task_HttpUPLoader(LPCTSTR lpszClientAddr, LPCTSTR lpszMsgBuffer, in
103101
int nPosStart = 0;
104102
int nPosEnd = 0;
105103
__int64x nPosCount = 0;
106-
104+
//得到长度大小
107105
if (XEngine_APPHelp_RangeFile(lpszClientAddr, &nPosStart, &nPosEnd, &nPosCount, pptszListHdr, nHdrCount, STORAGE_NETTYPE_HTTPUPLOADER))
108106
{
109-
//是否启用了断点续传
110-
if (!st_ServiceCfg.st_XStorage.bResumable)
107+
//是新的还是断点续传的
108+
if ((0 != nPosStart) || (0 != nPosEnd))
111109
{
112-
st_HDRParam.bIsClose = TRUE;
113-
st_HDRParam.nHttpCode = 416;
110+
//是否启用了断点续传
111+
if (!st_ServiceCfg.st_XStorage.bResumable)
112+
{
113+
st_HDRParam.bIsClose = TRUE;
114+
st_HDRParam.nHttpCode = 416;
114115

115-
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
116-
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
117-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,请求断点续传上传文件失败,服务端关闭了此功能,文件:%s,错误:%lX"), lpszClientAddr, tszFileDir, Session_GetLastError());
118-
return FALSE;
116+
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
117+
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
118+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,请求断点续传上传文件失败,服务端关闭了此功能,文件:%s,错误:%lX"), lpszClientAddr, tszFileDir, Session_GetLastError());
119+
return FALSE;
120+
}
119121
}
120122
}
121123
RfcComponents_HttpServer_GetRecvModeEx(xhUPHttp, lpszClientAddr, &nRVMode, &nRVCount, &nHDSize);
@@ -168,59 +170,71 @@ BOOL XEngine_Task_HttpUPLoader(LPCTSTR lpszClientAddr, LPCTSTR lpszMsgBuffer, in
168170

169171
OPenSsl_Api_Digest(tszFileDir, tszHashStr, &nHashLen, TRUE, st_ServiceCfg.st_XStorage.nHashMode);
170172
BaseLib_OperatorString_StrToHex((char*)tszHashStr, nHashLen, st_ProtocolFile.st_ProtocolFile.tszFileHash);
171-
//验证HASH值
172-
if (XEngine_APPHelp_VerHash(lpszClientAddr, tszFileDir, st_ProtocolFile.st_ProtocolFile.tszFileHash, pptszListHdr, nHdrCount))
173+
//处理结果
174+
if (!XEngine_APPHelp_VerHash(lpszClientAddr, tszFileDir, st_ProtocolFile.st_ProtocolFile.tszFileHash, pptszListHdr, nHdrCount))
175+
{
176+
st_HDRParam.bIsClose = TRUE;
177+
st_HDRParam.nHttpCode = 403;
178+
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
179+
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
180+
return FALSE;
181+
}
182+
BOOL bRet = TRUE;
183+
if (0 != st_ServiceCfg.st_XSql.nSQLType)
173184
{
174-
if (0 != st_ServiceCfg.st_XSql.nSQLType)
185+
if (1 == st_ServiceCfg.st_XSql.nSQLType)
175186
{
176-
BOOL bRet = FALSE;
177-
if (1 == st_ServiceCfg.st_XSql.nSQLType)
178-
{
179-
bRet = XStorage_MySql_FileInsert(&st_ProtocolFile);
180-
}
181-
else
182-
{
183-
bRet = XStorage_SQLite_FileInsert(&st_ProtocolFile);
184-
}
185-
if (bRet)
186-
{
187-
if (st_ServiceCfg.st_XProxy.st_XProxyPass.bUPPass)
188-
{
189-
int nPLen = MAX_PATH;
190-
int nHttpCode = 0;
191-
TCHAR tszProxyStr[MAX_PATH];
192-
SESSION_STORAGEINFO st_StorageInfo;
193-
194-
memset(tszProxyStr, '\0', MAX_PATH);
195-
memset(&st_StorageInfo, '\0', sizeof(SESSION_STORAGEINFO));
196-
197-
Session_UPStroage_GetInfo(lpszClientAddr, &st_StorageInfo);
198-
Protocol_StoragePacket_UPDown(st_StorageInfo.tszFileDir, st_StorageInfo.tszClientAddr, st_StorageInfo.ullRWCount, tszProxyStr, &nPLen, st_ProtocolFile.st_ProtocolFile.tszFileHash);
199-
APIHelp_HttpRequest_Post(st_ServiceCfg.st_XProxy.st_XProxyPass.tszUPPass, tszProxyStr, &nHttpCode);
200-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_NOTICE, _T("上传客户端:%s,请求完成通知返回值:%d,文件:%s,地址:%s"), lpszClientAddr, nHttpCode, st_StorageInfo.tszFileDir, st_ServiceCfg.st_XProxy.st_XProxyPass.tszUPPass);
201-
}
202-
st_HDRParam.bIsClose = TRUE;
203-
st_HDRParam.nHttpCode = 200;
204-
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
205-
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
206-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("上传客户端:%s,请求上传文件成功,文件名:%s,大小:%d"), lpszClientAddr, tszFileDir, nRVCount);
207-
}
208-
else
209-
{
210-
st_HDRParam.bIsClose = TRUE;
211-
st_HDRParam.nHttpCode = 403;
212-
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
213-
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
214-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("上传客户端:%s,请求上传文件失败,插入数据库失败:%s,错误:%lX"), lpszClientAddr, tszFileDir, XStorageDB_GetLastError());
215-
}
187+
bRet = XStorage_MySql_FileInsert(&st_ProtocolFile);
216188
}
217189
else
190+
{
191+
bRet = XStorage_SQLite_FileInsert(&st_ProtocolFile);
192+
}
193+
if (bRet)
218194
{
219195
st_HDRParam.bIsClose = TRUE;
220196
st_HDRParam.nHttpCode = 200;
221197
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
222198
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
223-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN, _T("上传客户端:%s,请求上传文件成功,文件名:%s,大小:%d,数据库没有启用,不插入数据库"), lpszClientAddr, tszFileDir, nRVCount);
199+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("上传客户端:%s,请求上传文件成功,文件名:%s,大小:%d"), lpszClientAddr, tszFileDir, nRVCount);
200+
}
201+
else
202+
{
203+
st_HDRParam.bIsClose = TRUE;
204+
st_HDRParam.nHttpCode = 403;
205+
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
206+
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
207+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("上传客户端:%s,请求上传文件失败,插入数据库失败:%s,错误:%lX"), lpszClientAddr, tszFileDir, XStorageDB_GetLastError());
208+
}
209+
}
210+
else
211+
{
212+
st_HDRParam.bIsClose = TRUE;
213+
st_HDRParam.nHttpCode = 200;
214+
RfcComponents_HttpServer_SendMsgEx(xhUPHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
215+
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPUPLOADER);
216+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN, _T("上传客户端:%s,请求上传文件成功,文件名:%s,大小:%d,数据库没有启用,不插入数据库"), lpszClientAddr, tszFileDir, nRVCount);
217+
}
218+
//PASS代理
219+
if (st_ServiceCfg.st_XProxy.st_XProxyPass.bUPPass && bRet)
220+
{
221+
int nPLen = MAX_PATH;
222+
int nHttpCode = 0;
223+
TCHAR tszProxyStr[MAX_PATH];
224+
SESSION_STORAGEINFO st_StorageInfo;
225+
226+
memset(tszProxyStr, '\0', MAX_PATH);
227+
memset(&st_StorageInfo, '\0', sizeof(SESSION_STORAGEINFO));
228+
229+
Session_UPStroage_GetInfo(lpszClientAddr, &st_StorageInfo);
230+
Protocol_StoragePacket_UPDown(st_StorageInfo.tszFileDir, st_StorageInfo.tszClientAddr, st_StorageInfo.ullRWCount, tszProxyStr, &nPLen, st_ProtocolFile.st_ProtocolFile.tszFileHash);
231+
if (APIHelp_HttpRequest_Post(st_ServiceCfg.st_XProxy.st_XProxyPass.tszUPPass, tszProxyStr, &nHttpCode))
232+
{
233+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("上传客户端:%s,请求完成通知返回值:%d,文件:%s,地址:%s"), lpszClientAddr, nHttpCode, st_StorageInfo.tszFileDir, st_ServiceCfg.st_XProxy.st_XProxyPass.tszUPPass);
234+
}
235+
else
236+
{
237+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,请求完成通知失败,可能服务器不正确:文件:%s,地址:%s"), lpszClientAddr, st_StorageInfo.tszFileDir, st_ServiceCfg.st_XProxy.st_XProxyPass.tszUPPass);
224238
}
225239
}
226240
}

XEngine_Source/XEngine_StorageApp/Storage_APPHelp/Storage_APPHelp.cpp

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ BOOL XEngine_APPHelp_ProxyAuth(LPCTSTR lpszClientAddr, LPCTSTR lpszMethod, LPCTS
108108
}
109109
BOOL XEngine_APPHelp_RangeFile(LPCTSTR lpszClientAddr, int* pInt_SPos, int* pInt_EPos, __int64x* pInt_Count, TCHAR** pptszListHdr, int nHdrCount, int nSDType)
110110
{
111-
LPCTSTR lpszRange = _T("Range");
111+
LPCTSTR lpszRangeStr = _T("Range");
112+
LPCTSTR lpszLengthStr = _T("Content-Length");
112113
LPCTSTR lpszClientType;
113114
TCHAR tszKeyStr[128];
114115
TCHAR tszValueStr[128];
115-
TCHAR tszRangeStr[128];
116+
TCHAR tszFieldStr[128];
116117

117118
memset(tszKeyStr, '\0', sizeof(tszKeyStr));
118119
memset(tszValueStr, '\0', sizeof(tszValueStr));
119-
memset(tszRangeStr, '\0', sizeof(tszRangeStr));
120+
memset(tszFieldStr, '\0', sizeof(tszFieldStr));
120121

121122
if (STORAGE_NETTYPE_HTTPDOWNLOAD == nSDType)
122123
{
@@ -131,61 +132,73 @@ BOOL XEngine_APPHelp_RangeFile(LPCTSTR lpszClientAddr, int* pInt_SPos, int* pInt
131132
lpszClientType = _T("业务客户端");
132133
}
133134
//是否有范围
134-
if (!RfcComponents_HttpHelp_GetField(&pptszListHdr, nHdrCount, lpszRange, tszRangeStr))
135+
if (RfcComponents_HttpHelp_GetField(&pptszListHdr, nHdrCount, lpszRangeStr, tszFieldStr))
135136
{
136-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("%s:%s,请求内容没有范围信息"), lpszClientType, lpszClientAddr);
137-
return FALSE;
138-
}
139-
//是否没有找到
140-
int nBPos = 0; //某些时候有个BYTE
141-
if (NULL != _tcsstr(tszRangeStr,_T("bytes=")))
142-
{
143-
nBPos = 6;
144-
}
145-
if (!BaseLib_OperatorString_GetKeyValue(tszRangeStr + nBPos, "-", tszKeyStr, tszValueStr))
146-
{
147-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("%s:%s,请求内容有范围信息,但是解析失败,内容:%s"), lpszClientType, lpszClientAddr, tszRangeStr);
148-
return FALSE;
149-
}
150-
//得到 1/2 如果有的话
151-
TCHAR tszRangeEnd[128];
152-
TCHAR tszRangeCount[128];
137+
//是否没有找到
138+
int nBPos = 0; //某些时候有个BYTE
139+
if (NULL != _tcsstr(tszFieldStr, _T("bytes=")))
140+
{
141+
nBPos = 6;
142+
}
143+
if (!BaseLib_OperatorString_GetKeyValue(tszFieldStr + nBPos, "-", tszKeyStr, tszValueStr))
144+
{
145+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("%s:%s,请求内容有范围信息,但是解析失败,内容:%s"), lpszClientType, lpszClientAddr, tszFieldStr);
146+
return FALSE;
147+
}
148+
//得到 1/2 如果有的话
149+
TCHAR tszRangeEnd[128];
150+
TCHAR tszRangeCount[128];
153151

154-
memset(tszRangeEnd, '\0', sizeof(tszRangeEnd));
155-
memset(tszRangeCount, '\0', sizeof(tszRangeCount));
156-
if (BaseLib_OperatorString_GetKeyValue(tszValueStr, "/", tszRangeEnd, tszRangeCount))
157-
{
158-
*pInt_SPos = _ttoi(tszKeyStr);
159-
*pInt_EPos = _ttoi(tszRangeEnd);
160-
*pInt_Count = _ttoi64(tszRangeCount);
152+
memset(tszRangeEnd, '\0', sizeof(tszRangeEnd));
153+
memset(tszRangeCount, '\0', sizeof(tszRangeCount));
154+
if (BaseLib_OperatorString_GetKeyValue(tszValueStr, "/", tszRangeEnd, tszRangeCount))
155+
{
156+
*pInt_SPos = _ttoi(tszKeyStr);
157+
*pInt_EPos = _ttoi(tszRangeEnd);
158+
*pInt_Count = _ttoi64(tszRangeCount);
159+
}
160+
else
161+
{
162+
*pInt_SPos = _ttoi(tszKeyStr);
163+
*pInt_EPos = _ttoi(tszValueStr);
164+
}
165+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("%s:%s,客户端的请求设置了数据范围:%d - %d/%lld"), lpszClientType, lpszClientAddr, *pInt_SPos, *pInt_EPos, *pInt_Count);
161166
}
162167
else
163168
{
164-
*pInt_SPos = _ttoi(tszKeyStr);
165-
*pInt_EPos = _ttoi(tszValueStr);
169+
RfcComponents_HttpHelp_GetField(&pptszListHdr, nHdrCount, lpszLengthStr, tszFieldStr);
170+
*pInt_Count = _ttoi64(tszFieldStr);
171+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("%s:%s,请求内容是新的数据,大小:%lld"), lpszClientType, lpszClientAddr, *pInt_Count);
166172
}
167-
168-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("%s:%s,客户端的请求设置了数据范围:%d - %d/%lld"), lpszClientType, lpszClientAddr, *pInt_SPos, *pInt_EPos, *pInt_Count);
169173
return TRUE;
170174
}
171-
BOOL XEngine_APPHelp_VerHash(LPCTSTR lpszClientAddr ,LPCTSTR lpszFileName, LPCTSTR lpszFileHash, TCHAR** pptszListHdr, int nHdrCount)
175+
BOOL XEngine_APPHelp_VerHash(LPCTSTR lpszClientAddr, LPCTSTR lpszFileName, LPCTSTR lpszFileHash, TCHAR** pptszListHdr, int nHdrCount)
172176
{
173-
LPCTSTR lpszKeyStr = _T("FileHash");
174-
TCHAR tszValueStr[MAX_PATH];
175-
memset(tszValueStr, '\0', MAX_PATH);
176-
177-
if (RfcComponents_HttpHelp_GetField(&pptszListHdr, nHdrCount, lpszKeyStr, tszValueStr))
177+
if (st_ServiceCfg.st_XStorage.bUPHash)
178178
{
179-
if (0 != _tcsnicmp(lpszFileHash, tszValueStr, _tcslen(lpszFileHash)))
179+
LPCTSTR lpszKeyStr = _T("FileHash");
180+
TCHAR tszValueStr[MAX_PATH];
181+
memset(tszValueStr, '\0', MAX_PATH);
182+
183+
if (RfcComponents_HttpHelp_GetField(&pptszListHdr, nHdrCount, lpszKeyStr, tszValueStr))
184+
{
185+
if (0 == _tcsnicmp(lpszFileHash, tszValueStr, _tcslen(lpszFileHash)))
186+
{
187+
_tremove(lpszFileName);
188+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,上传的文件信息HASH效验成功,文件:%s 原始HASH:%s,计算HASH:%s"), lpszClientAddr, lpszFileName, tszValueStr, lpszFileHash);
189+
}
190+
else
191+
{
192+
_tremove(lpszFileName);
193+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,上传的文件信息HASH校验失败,无法继续,文件:%s 已被删除,原始HASH:%s,计算HASH:%s"), lpszClientAddr, lpszFileName, tszValueStr, lpszFileHash);
194+
return FALSE;
195+
}
196+
}
197+
else
180198
{
181-
_tremove(lpszFileName);
182-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,上传的文件信息HASH校验失败,无法继续,文件:%s 已被删除,原始HASH:%s,计算HASH:%s"), lpszClientAddr, lpszFileName, tszValueStr, lpszFileHash);
199+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("上传客户端:%s,上传的信息没有附带HASH值,无法验证文件的正确性"), lpszClientAddr);
183200
return FALSE;
184201
}
185202
}
186-
else
187-
{
188-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN, _T("上传客户端:%s,上传的信息没有附带HASH值,无法验证文件的正确性"), lpszClientAddr);
189-
}
190203
return TRUE;
191204
}

0 commit comments

Comments
 (0)