-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUploadFiles.cpp
More file actions
206 lines (172 loc) · 6.1 KB
/
Copy pathHttpUploadFiles.cpp
File metadata and controls
206 lines (172 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "HttpUploadFiles.h"
#include <atlbase.h>
#include <atlstr.h>
#define UNREAD 0xFFFFFFFF
#define BOUNDARYPART L"--387C5529D89343DAB0A4B68591D754B1"
CHttpUploadFiles::CHttpUploadFiles(void)
{
m_ReadInfo.eType = EHeader;
m_ReadInfo.dwReadIndex = 0;
}
CHttpUploadFiles::~CHttpUploadFiles(void)
{
}
DWORD CHttpUploadFiles::GetDataSize()
{
if ( m_strUploadFileHeaderUTF8.empty() ) {
return 0;
}
DWORD dwFileSize = 0;
HANDLE hFile = CreateFileW( m_wstrFilePath.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
do {
if ( INVALID_HANDLE_VALUE == hFile ) {
break;
}
LARGE_INTEGER lgFileSize = {0};
if ( FALSE == GetFileSizeEx(hFile, &lgFileSize) ) {
break;
}
if ( lgFileSize.HighPart > 0 || lgFileSize.LowPart > 0x00FFFFFF) {
// ÏÞÖÆ´óС
break;
}
dwFileSize = lgFileSize.LowPart;
}while(0);
if ( INVALID_HANDLE_VALUE != hFile ) {
CloseHandle(hFile);
hFile = NULL;
}
DWORD dwDataSize = 0;
if ( 0 != dwFileSize ) {
dwDataSize = dwFileSize + m_strUploadFileHeaderUTF8.length();
}
return dwDataSize;
}
BOOL CHttpUploadFiles::GetData( LPVOID lpBuffer, DWORD dwBufferSize, DWORD& dwWrite )
{
if ( m_strUploadFileHeaderUTF8.empty() ) {
return FALSE;
}
if ( EHeader == m_ReadInfo.eType ) {
if ( FALSE == ReadFromString(m_strUploadFileHeaderUTF8, lpBuffer, dwBufferSize, m_ReadInfo.dwReadIndex, dwWrite ) ) {
return FALSE;
}
m_ReadInfo.dwReadIndex += dwWrite;
if ( m_ReadInfo.dwReadIndex == m_strUploadFileHeaderUTF8.length() ) {
m_ReadInfo.eType = EFile;
m_ReadInfo.dwReadIndex = 0;
return TRUE;
}
}
else if ( EFile == m_ReadInfo.eType ){
OVERLAPPED ov;
memset(&ov, 0, sizeof(ov));
ov.Offset = m_ReadInfo.dwReadIndex;
HANDLE hFile = CreateFileW( m_wstrFilePath.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
BOOL bContinue = FALSE;
DWORD dwFileSize = 0;
do {
if ( INVALID_HANDLE_VALUE == hFile ) {
dwWrite = 0;
break;
}
LARGE_INTEGER lgFileSize = {0};
if ( FALSE == GetFileSizeEx(hFile, &lgFileSize) ) {
break;
}
if ( FALSE == ReadFile(hFile, lpBuffer, dwBufferSize, &dwWrite, &ov)) {
break;
}
dwFileSize = lgFileSize.LowPart;
bContinue = TRUE;
} while (0);
if ( INVALID_HANDLE_VALUE != hFile ) {
CloseHandle(hFile);
hFile = NULL;
}
m_ReadInfo.dwReadIndex += dwWrite;
if ( m_ReadInfo.dwReadIndex == dwFileSize ) {
m_ReadInfo.dwReadIndex = 0;
bContinue = FALSE;
}
return bContinue;
}
return TRUE;
}
BOOL CHttpUploadFiles::TransDataToServer( const std::wstring wstrUrl, VecStParam& VecExtInfo,
const std::wstring& wstrFilePath, const std::wstring& wstrFileKey)
{
m_wstrBlockStart = L"--";
m_wstrBlockStart += BOUNDARYPART;
m_wstrBlockStart += L"\r\n";
m_strBlockStartUTF8 = CW2A(m_wstrBlockStart.c_str(), CP_UTF8);
m_wstrBlockEnd = L"\r\n--";
m_wstrBlockEnd += BOUNDARYPART;
m_wstrBlockEnd += L"--\r\n";
m_wstrNewHeader = L"Content-Type: multipart/form-data; boundary=";
m_wstrNewHeader += BOUNDARYPART;
m_wstrNewHeader += L"\r\n";
m_wstrFilePath = wstrFilePath;
std::wstring wstrFileName = m_wstrFilePath;
int nPos = m_wstrFilePath.rfind('\\');
if ( -1 == nPos ) {
nPos = m_wstrFilePath.rfind('/');
}
if ( -1 != nPos ) {
wstrFileName = m_wstrFilePath.substr( nPos + 1, m_wstrFilePath.length() - nPos - 1);
}
std::wstring wstrUploadFileHeader;
wstrUploadFileHeader = m_wstrBlockStart;
wstrUploadFileHeader += L"Content-Disposition: form-data; name=\"";
wstrUploadFileHeader += wstrFileKey;
wstrUploadFileHeader += L"\";";
wstrUploadFileHeader += L"filename=\"";
wstrUploadFileHeader += wstrFileName;
wstrUploadFileHeader += L"\"\r\n";
wstrUploadFileHeader += L"Content-Type:application/octet-stream\r\n\r\n";
m_strUploadFileHeaderUTF8 = CW2A(wstrUploadFileHeader.c_str(), CP_UTF8);
m_VecExtInfo.assign(VecExtInfo.begin(), VecExtInfo.end());
return TransmiteData(wstrUrl, eUpload, 30 * 1000);
}
BOOL CHttpUploadFiles::ModifyRequestHeader( HINTERNET hRequest )
{
return ::WinHttpAddRequestHeaders(hRequest, m_wstrNewHeader.c_str(),
m_wstrNewHeader.length(), WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);
}
std::wstring CHttpUploadFiles::GenerateExtInfo( const VecStParam& VecExtInfo )
{
std::wstring wstrInfo = L"\r\n";
for ( VecStParamCIter it = VecExtInfo.begin(); it != VecExtInfo.end(); it++ ) {
wstrInfo += m_wstrBlockStart;
wstrInfo += L"Content-Disposition:form-data;";
wstrInfo += L"name=";
wstrInfo += L"\"";
wstrInfo += it->wstrKey;
wstrInfo += L"\"";
wstrInfo += L"\r\n\r\n";
wstrInfo += it->wstrValue;
wstrInfo += L"\r\n";
}
wstrInfo += m_wstrBlockEnd;
return wstrInfo;
}
VOID CHttpUploadFiles::AddExtInfo( VecStParam& VecExtInfo )
{
for ( VecStParamCIter it = m_VecExtInfo.begin(); it != m_VecExtInfo.end(); it++) {
VecExtInfo.push_back(*it);
}
}
BOOL CHttpUploadFiles::ReadFromString( const std::string& strContent,
LPVOID lpBuffer, DWORD dwBufferSize, DWORD dwIndex, DWORD& dwWrite )
{
if ( strContent.length() > dwIndex + dwBufferSize) {
dwWrite = dwBufferSize;
}
else {
dwWrite = strContent.length() - dwIndex;
}
if ( 0 != memcpy_s( lpBuffer, dwBufferSize, strContent.c_str() + dwIndex, dwWrite) ) {
return FALSE;
}
return TRUE;
}