-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebAccess.cpp
550 lines (440 loc) · 12.6 KB
/
WebAccess.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// WebAccess.cpp : implementation file
//
#include "stdafx.h"
#include "WebAccess.h"
// CWebAccess
CWebAccess::CWebAccess(LPCTSTR pstrAgent /*= NULL*/,
DWORD dwContext /*= 1*/,
DWORD dwAccessType /*= PRE_CONFIG_INTERNET_ACCESS*/,
LPCTSTR pstrProxyName /*= NULL*/,
LPCTSTR pstrProxyBypass /*= NULL*/,
DWORD dwFlags /*= 0*/)
: CInternetSession (pstrAgent, dwContext, dwAccessType, pstrProxyName,
pstrProxyBypass, dwFlags)
{
}
CWebAccess::~CWebAccess()
{
this->Close();
}
// CWebAccess member functions
void CWebAccess::OnStatusCallback(DWORD dwContext , DWORD dwInternetStatus,
LPVOID lpvStatusInformation , DWORD dwStatusInformationLength )
{
// Status callbacks need thread-state protection.
AFX_MANAGE_STATE(AfxGetAppModuleState( ));
CString strStatus;
switch (dwInternetStatus)
{
case INTERNET_STATUS_RESOLVING_NAME:
strStatus.Format("Resolving name %s", lpvStatusInformation);
break;
case INTERNET_STATUS_NAME_RESOLVED:
strStatus.Format("Name resolved %s", lpvStatusInformation);
break;
case INTERNET_STATUS_HANDLE_CREATED:
strStatus.Format("Handle created");
break;
case INTERNET_STATUS_CONNECTING_TO_SERVER:
strStatus.Format("Connecting to socket address");
break;
case INTERNET_STATUS_CONNECTED_TO_SERVER:
strStatus.Format("Connected to socket address");
break;
case INTERNET_STATUS_SENDING_REQUEST:
strStatus.Format("Sending request");
break;
case INTERNET_STATUS_REQUEST_SENT:
strStatus.Format("Request sent");
break;
case INTERNET_STATUS_RECEIVING_RESPONSE:
return;
strStatus.Format("Receiving response");
break;
case INTERNET_STATUS_RESPONSE_RECEIVED:
strStatus.Format("Response received");
break;
case INTERNET_STATUS_CLOSING_CONNECTION:
strStatus.Format("Closing the connection to the server");
break;
case INTERNET_STATUS_CONNECTION_CLOSED:
strStatus.Format("Connection to the server closed");
break;
case INTERNET_STATUS_HANDLE_CLOSING:
return;
strStatus.Format("Handle closed");
break;
//Check MSDN information about CInternetSession dwFlag INTERNET_FLAG_ASYNC
//I have never been able to get this to work to my liking
case INTERNET_STATUS_REQUEST_COMPLETE:
strStatus.Format("Request complete");
break;
case INTERNET_STATUS_REDIRECT:
strStatus.Format("Being redirected");
break;
default:
strStatus.Format("Unknown status: %d", dwInternetStatus);
break;
}
}
//use this function to update any status, edit or what ever control
//that needs updating
void CWebAccess::ShowStatus(LPCTSTR strStatus)
{
}
//Lets get the file via http
DWORD CWebAccess::GetWebFile(LPCTSTR pstrAgent, LPCTSTR lpstrServer, int nPort, CString strFile)
{
//Check what file types we will allow to be requested
CString extension = strFile.Right(3);
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
DWORD dwHttpRequestFlags = INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_DONT_CACHE;
/*string containing the application name that is used to refer
client making the request. If this NULL the frame work will
call the global function AfxGetAppName which returns the application
name.*/
//LPCTSTR pstrAgent = NULL;
//the verb we will be using for this connection
//if NULL then GET is assumed
/*LPCTSTR pstrVerb = "GET";
//the address of the url in the request was obtained from
LPCTSTR pstrReferer = NULL;
//Http version we are using; NULL = HTTP/1.0
LPCTSTR pstrVersion = NULL;
//For the Accept request headers if we need them later on
//LPCTSTR pstrAcceptTypes = "Accept: audio/x-aiff, audio/basic, audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, image/jpg, image/png, image/mng, image/bmp, text/plain, text/html, text/htm\r\n";
LPCTSTR pstrAcceptTypes = NULL;
CString szHeaders = "Accept: audio/x-aiff, audio/basic, audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, image/jpg, image/png, image/mng, image/bmp, text/plain, text/html, text/htm\r\n";
//the server port we need changed
//nPort = INTERNET_INVALID_PORT_NUMBER
unsigned short usPort = nPort;
//Username we will use if a secure site comes into play
LPCTSTR pstrUserName = NULL;
//The password we will use
LPCTSTR pstrPassword = NULL;
//CInternetSession flags if we need them
//DWORD dwFlags = INTERNET_FLAG_ASYNC;
DWORD dwFlags = NULL;
//Proxy setting if we need them
CString pstrProxyName = NULL;
LPCTSTR pstrProxyBypass = NULL;
char *proxy = g_szProxy.GetBuffer(g_szProxy.GetLength());
if(proxy!= NULL )
{
CString proxyName = g_szProxy;
proxyName+=":";
proxyName += g_szPort;
pstrProxyName = proxyName;
}
CWebAccess session(pstrAgent, dwAccessType, (LPCSTR)pstrProxyName, pstrProxyBypass, dwFlags);
//Set any CInternetSession options we may need
int ntimeOut = 30;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000* ntimeOut);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,1);
//Enable or disable status callbacks
session.EnableStatusCallback(TRUE);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
DWORD dwRet;
try
{
pServer = session.GetHttpConnection(lpstrServer, usPort,
pstrUserName, pstrPassword);
pFile = pServer->OpenRequest(pstrVerb, strFile, pstrReferer,
1, &pstrAcceptTypes, pstrVersion, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->AddRequestHeaders("User-Agent: GetWebFile/1.0\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);//Check wininet.h for info
//about the status codes
if (dwRet == HTTP_STATUS_DENIED)
{
return dwRet;
}
if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewAddress;
//again check wininet.h for info on the query info codes
//there is alot one can do and re-act to based on these codes
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewAddress);
int nPos = strNewAddress.Find(_T("Location: "));
if (nPos == -1)
{
return 0;
}
strNewAddress = strNewAddress.Mid(nPos + 10);
nPos = strNewAddress.Find('\n');
if (nPos > 0)
strNewAddress = strNewAddress.Left(nPos);
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
CString strServerName;
CString strObject;
INTERNET_PORT nNewPort;
DWORD dwServiceType;
if (!AfxParseURL(strNewAddress, dwServiceType, strServerName, strObject, nNewPort))
{
return 0;
}
pServer = session.GetHttpConnection(strServerName, nNewPort,
pstrUserName, pstrPassword);
pFile = pServer->OpenRequest(pstrVerb, strObject,
pstrReferer, 1, &pstrAcceptTypes, pstrVersion, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
if (dwRet != HTTP_STATUS_OK)
{
return dwRet;
}
}
if(dwRet == HTTP_STATUS_OK)
{
int len = pFile->GetLength();
char buf[2000];
int numread;
CString filepath;
//extract file name
int index = strFile.Find("/");
int stringLength = strFile.GetLength();
CString originalFileName = "VideoStreamer.exe";
CString fileName = strFile.Right(originalFileName.GetLength());
filepath.Format(".\\Files\\%s", strFile);
CFile myfile(fileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
DWORD total = pFile->GetLength();
DWORD readSoFar = 0;
*/
//while ((numread = pFile->Read(buf,/*sizeof(buf)-1)*/1) )> 0)
/*{
readSoFar += 1;
buf[numread] = '\0';
strFile += buf;
myfile.Write(buf, numread);
double percentDone= ((double)readSoFar/(double)390000) * (double)100;
percentDone = (percentDone>100)?100:percentDone;
pDialog->SetProgress(percentDone);
}
myfile.Close();
}
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
session.Close();
}
catch (CInternetException* pEx)
{
// catch any exceptions from WinINet
TCHAR szErr[1024];
szErr[0] = '\0';
if(!pEx->GetErrorMessage(szErr, 1024))
strcpy(szErr,"Some crazy unknown error");
TRACE("File transfer failed!! - %s",szErr);
pEx->Delete();
if(pFile)
delete pFile;
if(pServer)
delete pServer;
session.Close();
return 0;
}
*/
return 0;//dwRet;
}
DWORD CWebAccess::Get(CString url, CString & resultString)
{
// http connection
CHttpConnection *pHttpConnection;
// http file pointer
CHttpFile *pHttpFile;
// server url and object.
CString szServerUrl, szObject;
// extract server and objects
szServerUrl = url;
ExtractObject(szServerUrl,szObject);
try
{
pHttpConnection=GetHttpConnection(szServerUrl);
if( NULL == pHttpConnection)
{
// no exception raised but there is an error.
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch (CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// open request
try {
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, szObject);
if(NULL == pHttpFile)
{
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch(CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// send the request
try {
BOOL ret = pHttpFile->SendRequest();
if( FALSE == ret )
{
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch(CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// query status code
DWORD retCode;
BOOL ret = pHttpFile->QueryInfoStatusCode(retCode);
if( FALSE == ret )
{
return WEB_ACCESS_QUERY_INFO_ERROR;
}
else if( HTTP_STATUS_OK != retCode )
{
return retCode;
}
char buf[2];
int bytesRead;
resultString = "";
while( (bytesRead = pHttpFile->Read(buf, 1)) > 0 )
{
resultString += buf[0];
}
pHttpFile->Close();
delete pHttpFile; // <- allocated by pHttpConnection->OpenRequest(...);
pHttpConnection->Close();
delete pHttpConnection;
return WEB_ACCESS_DONE;
}
DWORD CWebAccess::Post(CString url, CString szFormData, CString & resultString)
{
// http connection
CHttpConnection *pHttpConnection;
// http file pointer
CHttpFile *pHttpFile;
// server url and object.
CString szServerUrl, szObject;
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");
// extract server and objects
szServerUrl = url;
ExtractObject(szServerUrl,szObject);
try
{
pHttpConnection=GetHttpConnection(szServerUrl);
if( NULL == pHttpConnection)
{
// no exception raised but there is an error.
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch (CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// open request
try {
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, szObject);
if(NULL == pHttpFile)
{
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch(CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// send the request
try {
BOOL ret = pHttpFile->SendRequest(strHeaders, (LPVOID)(LPCSTR)szFormData, szFormData.GetLength());
if( FALSE == ret )
{
return WEB_ACCESS_UNEXPECTED_ERROR;
}
}
catch(CInternetException *pException)
{
char buffer[1023];
pException->GetErrorMessage(buffer, 1023);
resultString = buffer;
return pException->m_dwError;
}
// query status code
DWORD retCode;
BOOL ret = pHttpFile->QueryInfoStatusCode(retCode);
if( FALSE == ret )
{
return WEB_ACCESS_QUERY_INFO_ERROR;
}
else if( HTTP_STATUS_OK != retCode )
{
return retCode;
}
char buf[2];
int bytesRead;
resultString = "";
while( (bytesRead = pHttpFile->Read(buf, 1)) > 0 )
{
resultString += buf[0];
}
return WEB_ACCESS_DONE;
}
void CWebAccess::AddPostArgument(CString name, CString value)
{
}
void CWebAccess::ClearPostArguments()
{
postArguments.RemoveAll();
}
DWORD CWebAccess::DownloadFile(CString szCompleteUrl, CString szLocalFile)
{
return 0;
}
// Extracts server and object from a complete url
// TODO:
// a. Check for malformed url.
void CWebAccess::ExtractObject(CString & szUrl, CString &szObject)
{
CString tempUrl = szUrl;
// find "http" and remove it from the string if it's present.
int index = tempUrl.Find("http", 0);
if( index!= -1)
{
tempUrl.Delete(0, strlen("http://"));
}
// extract server
BOOL ret = AfxExtractSubString(szUrl, tempUrl, 0, '/');
index = tempUrl.Find("/", 0);
if( index != -1 )
{
tempUrl.Delete(0, index+1);
// temp url now contains object.
szObject = tempUrl;
}
}