forked from openvetsim/WinVetSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericHTTPClient.cpp
More file actions
896 lines (749 loc) · 23.9 KB
/
Copy pathGenericHTTPClient.cpp
File metadata and controls
896 lines (749 loc) · 23.9 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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
// GenericHTTPClient.cpp: implementation of the GenericHTTPClient class.
//
//////////////////////////////////////////////////////////////////////
#include "GenericHTTPClient.h"
#include <winreg.h>
GenericHTTPClient::GenericHTTPClient() {
_hHTTPOpen = _hHTTPConnection = _hHTTPRequest = NULL;
}
GenericHTTPClient::~GenericHTTPClient() {
_hHTTPOpen = _hHTTPConnection = _hHTTPRequest = NULL;
}
GenericHTTPClient::RequestMethod GenericHTTPClient::GetMethod(int nMethod) {
return nMethod <= 0 ? GenericHTTPClient::RequestGetMethod : static_cast<GenericHTTPClient::RequestMethod>(nMethod);
}
GenericHTTPClient::TypePostArgument GenericHTTPClient::GetPostArgumentType(int nType) {
return nType <= 0 ? GenericHTTPClient::TypeNormal : static_cast<GenericHTTPClient::TypePostArgument>(nType);
}
BOOL GenericHTTPClient::Connect(LPCTSTR szAddress, LPCTSTR szAgent, unsigned short nPort, LPCTSTR szUserAccount, LPCTSTR szPassword) {
_hHTTPOpen = ::InternetOpen(szAgent, // agent name
INTERNET_OPEN_TYPE_PRECONFIG, // proxy option
"", // proxy
"", // proxy bypass
0); // flags
if (!_hHTTPOpen) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
_hHTTPConnection = ::InternetConnect(_hHTTPOpen, // internet opened handle
szAddress, // server name
nPort, // ports
szUserAccount, // user name
szPassword, // password
INTERNET_SERVICE_HTTP, // service type
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, // service option
0); // context call-back option
if (!_hHTTPConnection) {
_dwError = ::GetLastError();
::CloseHandle(_hHTTPOpen);
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
if (::InternetAttemptConnect(NULL) != ERROR_SUCCESS) {
_dwError = ::GetLastError();
::CloseHandle(_hHTTPConnection);
::CloseHandle(_hHTTPOpen);
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
return TRUE;
}
BOOL GenericHTTPClient::Close() {
if (_hHTTPRequest)
::InternetCloseHandle(_hHTTPRequest);
if (_hHTTPConnection)
::InternetCloseHandle(_hHTTPConnection);
if (_hHTTPOpen)
::InternetCloseHandle(_hHTTPOpen);
return TRUE;
}
BOOL GenericHTTPClient::Request(LPCTSTR szURL, int nMethod, LPCTSTR szAgent) {
BOOL bReturn = TRUE;
DWORD dwPort = 0;
TCHAR szProtocol[__SIZE_BUFFER] = "";
TCHAR szAddress[__SIZE_BUFFER] = "";
TCHAR szURI[__SIZE_BUFFER] = "";
DWORD dwSize = 0;
ParseURL(szURL, szProtocol, szAddress, dwPort, szURI);
if (Connect(szAddress, szAgent, dwPort)) {
if (!RequestOfURI(szURI, nMethod)) {
bReturn = FALSE;
}
else {
if (!Response((PBYTE)_szHTTPResponseHeader, __SIZE_HTTP_BUFFER, (PBYTE)_szHTTPResponseHTML, __SIZE_HTTP_BUFFER, dwSize)) {
bReturn = FALSE;
}
}
Close();
}
else {
bReturn = FALSE;
}
return bReturn;
}
BOOL GenericHTTPClient::RequestOfURI(LPCTSTR szURI, int nMethod) {
BOOL bReturn = TRUE;
try {
switch (nMethod) {
case GenericHTTPClient::RequestGetMethod:
default:
bReturn = RequestGet(szURI);
break;
case GenericHTTPClient::RequestPostMethod:
bReturn = RequestPost(szURI);
break;
case GenericHTTPClient::RequestPostMethodMultiPartsFormData:
bReturn = RequestPostMultiPartsFormData(szURI);
break;
}
}
catch (CException* e) {
#ifdef _DEBUG
TRACE("\nEXCEPTION\n");
TCHAR szERROR[1024];
e->GetErrorMessage(szERROR, 1024);
TRACE(szERROR);
#endif
}
return bReturn;
}
BOOL GenericHTTPClient::RequestGet(LPCTSTR szURI) {
CONST TCHAR* szAcceptType = __HTTP_ACCEPT_TYPE;
_hHTTPRequest = ::HttpOpenRequest(_hHTTPConnection,
__HTTP_VERB_GET, // HTTP Verb
szURI, // Object Name
HTTP_VERSION, // Version
"", // Reference
&szAcceptType, // Accept Type
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE,
0); // context call-back point
if (!_hHTTPRequest) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
// REPLACE HEADER
if (!::HttpAddRequestHeaders(_hHTTPRequest, __HTTP_ACCEPT, _tcslen(__HTTP_ACCEPT), HTTP_ADDREQ_FLAG_REPLACE)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
// SEND REQUEST
if (!::HttpSendRequest(_hHTTPRequest, // handle by returned HttpOpenRequest
NULL, // additional HTTP header
0, // additional HTTP header length
NULL, // additional data in HTTP Post or HTTP Put
0) // additional data length
) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
return TRUE;
}
BOOL GenericHTTPClient::RequestPost(LPCTSTR szURI) {
CONST TCHAR* szAcceptType = __HTTP_ACCEPT_TYPE;
TCHAR szPostArguments[__SIZE_BUFFER] = "";
LPCTSTR szContentType = TEXT("Content-Type: application/x-www-form-urlencoded\r\n");
DWORD dwPostArgumentsLegnth = GetPostArguments(szPostArguments, __SIZE_BUFFER);
_hHTTPRequest = ::HttpOpenRequest(_hHTTPConnection,
__HTTP_VERB_POST, // HTTP Verb
szURI, // Object Name
HTTP_VERSION, // Version
"", // Reference
&szAcceptType, // Accept Type
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_FORMS_SUBMIT,
0); // context call-back point
if (!_hHTTPRequest) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE("\n%d : %s\n", _dwError, reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
// REPLACE HEADER
if (!::HttpAddRequestHeaders(_hHTTPRequest, __HTTP_ACCEPT, _tcslen(__HTTP_ACCEPT), HTTP_ADDREQ_FLAG_REPLACE)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE("\n%d : %s\n", _dwError, reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
// SEND REQUEST
if (!::HttpSendRequest(_hHTTPRequest, // handle by returned HttpOpenRequest
szContentType, // additional HTTP header
_tcslen(szContentType), // additional HTTP header length
(LPVOID)szPostArguments, // additional data in HTTP Post or HTTP Put
_tcslen(szPostArguments)) // additional data length
) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE("\n%d : %s\n", _dwError, reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
return TRUE;
}
BOOL GenericHTTPClient::RequestPostMultiPartsFormData(LPCTSTR szURI) {
CONST TCHAR* szAcceptType = __HTTP_ACCEPT_TYPE;
LPCTSTR szContentType = TEXT("Content-Type: multipart/form-data; boundary=--MULTI-PARTS-FORM-DATA-BOUNDARY\r\n");
// ALLOCATE POST MULTI-PARTS FORM DATA ARGUMENTS
PBYTE pPostBuffer = NULL;
DWORD dwPostBufferLength = AllocMultiPartsFormData(pPostBuffer, "--MULTI-PARTS-FORM-DATA-BOUNDARY");
_hHTTPRequest = ::HttpOpenRequest(_hHTTPConnection,
__HTTP_VERB_POST, // HTTP Verb
szURI, // Object Name
HTTP_VERSION, // Version
"", // Reference
&szAcceptType, // Accept Type
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_FORMS_SUBMIT, // flags
0); // context call-back point
if (!_hHTTPRequest) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
// REPLACE HEADER
if (!::HttpAddRequestHeaders(_hHTTPRequest, __HTTP_ACCEPT, _tcslen(__HTTP_ACCEPT), HTTP_ADDREQ_FLAG_REPLACE)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
if (!::HttpAddRequestHeaders(_hHTTPRequest, szContentType, _tcslen(szContentType), HTTP_ADDREQ_FLAG_ADD_IF_NEW)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
TCHAR szContentLength[__SIZE_BUFFER] = "";
::ZeroMemory(szContentLength, __SIZE_BUFFER);
_stprintf(szContentLength, "Content-Length: %d\r\n", dwPostBufferLength);
if (!::HttpAddRequestHeaders(_hHTTPRequest, szContentLength, _tcslen(szContentLength), HTTP_ADDREQ_FLAG_ADD_IF_NEW)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
#ifdef _DEBUG
TCHAR szHTTPRequest[__SIZE_HTTP_BUFFER] = "";
DWORD dwHTTPRequestLength = __SIZE_HTTP_BUFFER;
::ZeroMemory(szHTTPRequest, __SIZE_HTTP_BUFFER);
if (!::HttpQueryInfo(_hHTTPRequest, HTTP_QUERY_RAW_HEADERS_CRLF, szHTTPRequest, &dwHTTPRequestLength, NULL)) {
_dwError = ::GetLastError();
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE("\n%d : %s\n", ::GetLastError(), reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
//return FALSE;
}
TRACE(szHTTPRequest);
#endif
// SEND REQUEST WITH HttpSendRequestEx and InternetWriteFile
INTERNET_BUFFERS InternetBufferIn = { 0 };
InternetBufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
InternetBufferIn.Next = NULL;
if (!::HttpSendRequestEx(_hHTTPRequest, &InternetBufferIn, NULL, HSR_INITIATE, 0)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
TRACE(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
DWORD dwOutPostBufferLength = 0;
if (!::InternetWriteFile(_hHTTPRequest, pPostBuffer, dwPostBufferLength, &dwOutPostBufferLength)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
if (!::HttpEndRequest(_hHTTPRequest, NULL, HSR_INITIATE, 0)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
// FREE POST MULTI-PARTS FORM DATA ARGUMENTS
FreeMultiPartsFormData(pPostBuffer);
return TRUE;
}
DWORD GenericHTTPClient::ResponseOfBytes(PBYTE pBuffer, DWORD dwSize) {
static DWORD dwBytes = 0;
if (!_hHTTPRequest) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return 0;
}
::ZeroMemory(pBuffer, dwSize);
if (!::InternetReadFile(_hHTTPRequest,
pBuffer,
dwSize,
&dwBytes)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return 0;
}
return dwBytes;
}
BOOL GenericHTTPClient::Response(PBYTE pHeaderBuffer, DWORD dwHeaderBufferLength, PBYTE pBuffer, DWORD dwBufferLength, DWORD& dwResultSize) {
BYTE pResponseBuffer[__SIZE_BUFFER] = "";
DWORD dwNumOfBytesToRead = 0;
if (!_hHTTPRequest) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
::ZeroMemory(pBuffer, dwBufferLength);
dwResultSize = 0;
while ((dwNumOfBytesToRead = ResponseOfBytes(pResponseBuffer, __SIZE_BUFFER)) != NULL && dwResultSize < dwBufferLength) {
::CopyMemory((pBuffer + dwResultSize), pResponseBuffer, dwNumOfBytesToRead);
dwResultSize += dwNumOfBytesToRead;
}
::ZeroMemory(pHeaderBuffer, dwHeaderBufferLength);
if (!::HttpQueryInfo(_hHTTPRequest, HTTP_QUERY_RAW_HEADERS_CRLF, pHeaderBuffer, &dwHeaderBufferLength, NULL)) {
_dwError = ::GetLastError();
#ifdef _DEBUG
LPVOID lpMsgBuffer;
DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
#endif
return FALSE;
}
return (dwResultSize ? TRUE : FALSE);
}
VOID GenericHTTPClient::AddPostArguments(LPCTSTR szName, LPCTSTR szValue, BOOL bBinary) {
GenericHTTPArgument arg;
::ZeroMemory(&arg, sizeof(arg));
_tcscpy(arg.szName, szName);
_tcscpy(arg.szValue, szValue);
if (!bBinary)
arg.dwType = GenericHTTPClient::TypeNormal;
else
arg.dwType = GenericHTTPClient::TypeBinary;
_vArguments.push_back(arg);
return;
}
VOID GenericHTTPClient::AddPostArguments(LPCTSTR szName, DWORD nValue) {
GenericHTTPArgument arg;
::FillMemory(&arg, sizeof(arg), 0x00);
_tcscpy(arg.szName, szName);
_stprintf(arg.szValue, "%d", nValue);
arg.dwType = GenericHTTPClient::TypeNormal;
_vArguments.push_back(arg);
return;
}
DWORD GenericHTTPClient::GetPostArguments(LPTSTR szArguments, DWORD dwLength) {
std::vector<GenericHTTPArgument>::iterator itArg;
::ZeroMemory(szArguments, dwLength);
for (itArg = _vArguments.begin(); itArg < _vArguments.end(); ) {
_tcscat(szArguments, itArg->szName);
_tcscat(szArguments, "=");
_tcscat(szArguments, itArg->szValue);
if ((++itArg) < _vArguments.end()) {
_tcscat(szArguments, "&");
}
}
*(szArguments + dwLength) = '\0';
return _tcslen(szArguments);
}
VOID GenericHTTPClient::InitilizePostArguments() {
_vArguments.clear();
return;
}
VOID GenericHTTPClient::FreeMultiPartsFormData(PBYTE& pBuffer) {
if (pBuffer) {
::LocalFree(pBuffer);
pBuffer = NULL;
}
return;
}
DWORD GenericHTTPClient::AllocMultiPartsFormData(PBYTE& pInBuffer, LPCTSTR szBoundary) {
if (pInBuffer) {
::LocalFree(pInBuffer);
pInBuffer = NULL;
}
pInBuffer = (PBYTE)::LocalAlloc(LPTR, GetMultiPartsFormDataLength());
std::vector<GenericHTTPArgument>::iterator itArgv;
DWORD dwPosition = 0;
DWORD dwBufferSize = 0;
for (itArgv = _vArguments.begin(); itArgv < _vArguments.end(); ++itArgv) {
PBYTE pBuffer = NULL;
// SET MULTI_PRATS FORM DATA BUFFER
switch (itArgv->dwType) {
case GenericHTTPClient::TypeNormal:
pBuffer = (PBYTE)::LocalAlloc(LPTR, __SIZE_HTTP_HEAD_LINE * 4);
_stprintf((TCHAR*)pBuffer,
"--%s\r\n"
"Content-Disposition: form-data; name=\"%s\"\r\n"
"\r\n"
"%s\r\n",
szBoundary,
itArgv->szName,
itArgv->szValue);
dwBufferSize = _tcslen((TCHAR*)pBuffer);
break;
case GenericHTTPClient::TypeBinary:
DWORD dwNumOfBytesToRead = 0;
DWORD dwTotalBytes = 0;
HANDLE hFile = ::CreateFile(itArgv->szValue,
GENERIC_READ, // desired access
FILE_SHARE_READ, // share mode
NULL, // security attribute
OPEN_EXISTING, // create disposition
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, // flag and attributes
NULL); // template file handle
DWORD dwSize = ::GetFileSize(hFile, NULL);
pBuffer = (PBYTE)::LocalAlloc(LPTR, __SIZE_HTTP_HEAD_LINE * 3 + dwSize + 1);
BYTE pBytes[__SIZE_BUFFER + 1] = "";
_stprintf((TCHAR*)pBuffer,
"--%s\r\n"
"Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n"
"Content-Type: %s\r\n"
"\r\n",
szBoundary,
itArgv->szName, itArgv->szValue,
GetContentType(itArgv->szValue)
);
DWORD dwBufPosition = _tcslen((TCHAR*)pBuffer);
while (::ReadFile(hFile, pBytes, __SIZE_BUFFER, &dwNumOfBytesToRead, NULL) && dwNumOfBytesToRead > 0 && dwTotalBytes <= dwSize) {
::CopyMemory((pBuffer + dwBufPosition + dwTotalBytes), pBytes, dwNumOfBytesToRead);
::ZeroMemory(pBytes, __SIZE_BUFFER + 1);
dwTotalBytes += dwNumOfBytesToRead;
}
::CopyMemory((pBuffer + dwBufPosition + dwTotalBytes), "\r\n", _tcslen("\r\n"));
::CloseHandle(hFile);
dwBufferSize = dwBufPosition + dwTotalBytes + _tcslen("\r\n");
break;
}
::CopyMemory((pInBuffer + dwPosition), pBuffer, dwBufferSize);
dwPosition += dwBufferSize;
if (pBuffer) {
::LocalFree(pBuffer);
pBuffer = NULL;
}
}
::CopyMemory((pInBuffer + dwPosition), "--", 2);
::CopyMemory((pInBuffer + dwPosition + 2), szBoundary, _tcslen(szBoundary));
::CopyMemory((pInBuffer + dwPosition + 2 + _tcslen(szBoundary)), "--\r\n", 3);
return dwPosition + 5 + _tcslen(szBoundary);
}
DWORD GenericHTTPClient::GetMultiPartsFormDataLength() {
std::vector<GenericHTTPArgument>::iterator itArgv;
DWORD dwLength = 0;
for (itArgv = _vArguments.begin(); itArgv < _vArguments.end(); ++itArgv) {
switch (itArgv->dwType) {
case GenericHTTPClient::TypeNormal:
dwLength += __SIZE_HTTP_HEAD_LINE * 4;
break;
case GenericHTTPClient::TypeBinary:
HANDLE hFile = ::CreateFile(itArgv->szValue,
GENERIC_READ, // desired access
FILE_SHARE_READ, // share mode
NULL, // security attribute
OPEN_EXISTING, // create disposition
FILE_ATTRIBUTE_NORMAL, // flag and attributes
NULL); // template file handle
dwLength += __SIZE_HTTP_HEAD_LINE * 3 + ::GetFileSize(hFile, NULL);
::CloseHandle(hFile);
break;
}
}
return dwLength;
}
LPCTSTR GenericHTTPClient::GetContentType(LPCTSTR szName) {
static TCHAR szReturn[1024] = "";
LONG dwLen = 1024;
DWORD dwDot = 0;
for (dwDot = _tcslen(szName); dwDot > 0; dwDot--) {
if (!_tcsncmp((szName + dwDot), ".", 1))
break;
}
HKEY hKEY;
LPTSTR szWord = (char*)(szName + dwDot);
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szWord, 0, KEY_QUERY_VALUE, &hKEY) == ERROR_SUCCESS) {
if (RegQueryValueEx(hKEY, TEXT("Content Type"), NULL, NULL, (LPBYTE)szReturn, (unsigned long*)&dwLen) != ERROR_SUCCESS)
_tcsncpy(szReturn, "application/octet-stream", _tcslen("application/octet-stream"));
RegCloseKey(hKEY);
}
else {
_tcsncpy(szReturn, "application/octet-stream", _tcslen("application/octet-stream"));
}
return szReturn;
}
DWORD GenericHTTPClient::GetLastError() {
return _dwError;
}
VOID GenericHTTPClient::ParseURL(LPCTSTR szURL, LPTSTR szProtocol, LPTSTR szAddress, DWORD& dwPort, LPTSTR szURI) {
TCHAR szPort[__SIZE_BUFFER] = "";
DWORD dwPosition = 0;
BOOL bFlag = FALSE;
while (_tcslen(szURL) > 0 && dwPosition < _tcslen(szURL) && _tcsncmp((szURL + dwPosition), ":", 1))
++dwPosition;
if (!_tcsncmp((szURL + dwPosition + 1), "/", 1)) { // is PROTOCOL
if (szProtocol) {
_tcsncpy(szProtocol, szURL, dwPosition);
szProtocol[dwPosition] = 0;
}
bFlag = TRUE;
}
else { // is HOST
if (szProtocol) {
_tcsncpy(szProtocol, "http", 4);
szProtocol[5] = 0;
}
}
DWORD dwStartPosition = 0;
if (bFlag) {
dwStartPosition = dwPosition += 3;
}
else {
dwStartPosition = dwPosition = 0;
}
bFlag = FALSE;
while (_tcslen(szURL) > 0 && dwPosition < _tcslen(szURL) && _tcsncmp((szURL + dwPosition), "/", 1))
++dwPosition;
DWORD dwFind = dwStartPosition;
for (; dwFind <= dwPosition; dwFind++) {
if (!_tcsncmp((szURL + dwFind), ":", 1)) { // find PORT
bFlag = TRUE;
break;
}
}
if (bFlag) {
TCHAR sztmp[__SIZE_BUFFER] = "";
_tcsncpy(sztmp, (szURL + dwFind + 1), dwPosition - dwFind);
dwPort = _ttol(sztmp);
_tcsncpy(szAddress, (szURL + dwStartPosition), dwFind - dwStartPosition);
}
else if (!_tcscmp(szProtocol, "https")) {
dwPort = INTERNET_DEFAULT_HTTPS_PORT;
_tcsncpy(szAddress, (szURL + dwStartPosition), dwPosition - dwStartPosition);
}
else {
dwPort = INTERNET_DEFAULT_HTTP_PORT;
_tcsncpy(szAddress, (szURL + dwStartPosition), dwPosition - dwStartPosition);
}
if (dwPosition < _tcslen(szURL)) { // find URI
_tcsncpy(szURI, (szURL + dwPosition), _tcslen(szURL) - dwPosition);
}
else {
szURI[0] = 0;
}
return;
}
LPCTSTR GenericHTTPClient::QueryHTTPResponseHeader() {
return _szHTTPResponseHeader;
}
LPCTSTR GenericHTTPClient::QueryHTTPResponse() {
return _szHTTPResponseHTML;
}