/
HttpReqSocket.cpp
executable file
·268 lines (224 loc) · 5.72 KB
/
HttpReqSocket.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// HttpReqSocket.cpp : implementation file
//
#include "stdafx.h"
#include "CdCoverCreator2.h"
#include "HttpReqSocket.h"
#include <ras.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHttpReqSocket
CHttpReqSocket::CHttpReqSocket()
{
}
CHttpReqSocket::~CHttpReqSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CHttpReqSocket, CSocket)
//{{AFX_MSG_MAP(CHttpReqSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CHttpReqSocket member functions
int CHttpReqSocket::Receive(void* lpBuf, int nBufLen, int nFlags)
{
if (m_pbBlocking != NULL)
{
WSASetLastError(WSAEINPROGRESS);
return FALSE;
}
int nResult, i = 0;
while ((nResult = CAsyncSocket::Receive(lpBuf, nBufLen, nFlags)) == SOCKET_ERROR)
{
if (GetLastError() == WSAEWOULDBLOCK)
{
if (!PumpMessages(FD_READ))
return SOCKET_ERROR;
}
else
return SOCKET_ERROR;
i++;
if (i > 5)
return SOCKET_ERROR;
}
return nResult;
}
CString CHttpReqSocket::GetHTTPContent(CString strHost, CString strRequest,
bool* pbCancelDownload, int& nError,
int nApproxContentLength,
CProgressCtrl *pProgress, CWnd* pStatus,
CString strFilename)
{
// init sockets
AfxSocketInit ();
CHttpReqSocket socket;
nError = 0;
// try to create the socket
if (!socket.Create ())
{
nError = WSAGetLastError ();
return "";
}
// try to connect to server
if (!socket.Connect (strHost, 80))
{
nError = socket.GetLastError ();
socket.Close ();
return "";
}
// wait for response
socket.Send (strRequest, strRequest.GetLength ());
CString strRecv;
char szBuf[4096 + 1];
int nPos, nContLen = -1;
int nLen = 0;
// create and open file, if stream is to be written to a file
// (i.e. strFilename is not empty)
CFile file;
bool bUseFile = !strFilename.IsEmpty ();
bool bHeaderRead = false;
if (bUseFile)
file.Open (strFilename, CFile::modeCreate | CFile::modeWrite);
// init progress control
if (pProgress != NULL)
pProgress->SetRange32 (0, nApproxContentLength);
MSG msg;
CString strStatus;
// read http stream
for (int nRead = 1; nRead > 0; )
{
if (*pbCancelDownload)
break;
// read from the socket
nRead = socket.Receive (szBuf, 4096);
switch (nRead)
{
case 0:
continue;
case SOCKET_ERROR:
nError = socket.GetLastError ();
break;
default:
if (bUseFile)
{
// write stream to file
if (!bHeaderRead)
{
// try to get the Content-Length header
char* pBuf = strstr (szBuf, "Content-Length:");
if (pBuf)
nContLen = atoi (pBuf + 15);
if (pProgress != NULL)
pProgress->SetRange32 (0, nContLen);
// cut off the header (should not be saved to the file)
pBuf = strstr (szBuf, "\r\n\r\n");
int nHeaderLen = (int) (pBuf - szBuf);
file.Write (pBuf + 4, nRead - nHeaderLen - 4);
// file.Write (szBuf, nRead);
// return the header
LPSTR pRecvBuf = strRecv.GetBuffer (nHeaderLen);
strncpy (pRecvBuf, szBuf, nHeaderLen);
pRecvBuf[nHeaderLen] = '\0';
strRecv.ReleaseBuffer ();
bHeaderRead = true;
}
else
file.Write (szBuf, nRead);
}
else
{
// save stream as string to return
szBuf[nRead] = '\0';
strRecv += szBuf;
// try to get the Content-Length header
// and set the progress control's range appropriately
if (!bHeaderRead)
{
if ((nPos = strRecv.Find ("Content-Length:")) >= 0)
{
nContLen = atoi (strRecv.Mid (nPos + 15));
if (pProgress != NULL)
pProgress->SetRange32 (0, nContLen);
}
bHeaderRead = true;
}
}
}
nLen += nRead;
if (nContLen >= 0)
if (nLen >= nContLen)
nRead = 0;
if (pProgress != NULL)
pProgress->SetPos (nLen);
if (pStatus != NULL)
{
strStatus.Format (IDS_DOWNLOADSTATUS, (double) nLen / 1024.0, (double) nContLen / 1024.0);
pStatus->SetWindowText (strStatus);
}
while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
DispatchMessage (&msg);
}
socket.Close ();
if (bUseFile)
file.Close ();
return strRecv;
}
bool CHttpReqSocket::IsInternetConnected ()
{
return true; // MCH 17.09.04 (what the heck...)
//////////////////////////////////////////////////////////////
DWORD dwState;
InternetGetConnectedState (&dwState, 0);
if (dwState & INTERNET_CONNECTION_LAN)
return true;
else
{
bool bConnected = false;
RASCONN rasConn;
rasConn.dwSize = sizeof (RASCONN);
DWORD dwBufSize = sizeof (RASCONN);
DWORD dwNumConns;
if (RasEnumConnections (&rasConn, &dwBufSize, &dwNumConns) == 0)
{
// RasEnumConnections succeeded
RASCONNSTATUS rasConnStatus;
for (UINT i = 0; i < dwNumConns; i++)
{
rasConnStatus.dwSize = sizeof (RASCONNSTATUS);
RasGetConnectStatus (rasConn.hrasconn, &rasConnStatus);
if (rasConnStatus.rasconnstate == RASCS_Connected)
{
bConnected = true;
break;
}
}
}
return bConnected;
}
}
CString CHttpReqSocket::URLEncode(CString str)
{
CString strEnc;
CString s;
char c;
for (int i = 0; i < str.GetLength (); i++)
{
c = str.GetAt (i);
if (c == ' ')
strEnc += "+";
else if (c <= 41 || c == 47 || (c >= 58 && c <= 64) || (c >= 91 && c <= 94) || c == 96 || c >= 123)
{
s.Format ("%%%02X", (unsigned char) c);
strEnc += s;
}
else
strEnc += c;
}
return strEnc;
}