Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

749 lines
24 KiB

  1. // ===========================================================================
  2. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5. // PARTICULAR PURPOSE.
  6. //
  7. // Copyright 1996 Microsoft Corporation. All Rights Reserved.
  8. // ===========================================================================
  9. //
  10. // Test driver for the WinHTTP-UrlCache interaction layer
  11. //
  12. #include <windows.h>
  13. #include <winhttp.h>
  14. #include <internal.h>
  15. #include <conio.h>
  16. #include <stdio.h>
  17. #include <tchar.h>
  18. #include <fstream.h>
  19. #include <stdlib.h>
  20. //////////////////////////////////////////////////////////////////////////////////////////////////
  21. // Globals
  22. wchar_t g_wszHost[50];
  23. wchar_t g_wszPath[50];
  24. DWORD g_dwTotalRequests;
  25. DWORD g_dwNumConnections;
  26. // Hard-coded #define
  27. //#define HOSTNAME L"t-eddieng"
  28. //#define HOSTNAME "www.w3.org"
  29. //#define HOSTNAME "msw"
  30. LPCWSTR szObjectName[] = { L"/?action=a",
  31. L"default.asp",
  32. L"/",
  33. L"/default.asp",
  34. L"/?action=a",
  35. L"/?action=b",
  36. L"/default.asp?action=a",
  37. L"/?action=a"
  38. };
  39. //////////////////////////////////////////////////////////////////////////////////////////////////
  40. //
  41. //
  42. #if 0
  43. void TestCase1() {
  44. HINTERNET hSession;
  45. HINTERNET hConnect;
  46. HINTERNET hRequest;
  47. DWORD dwSize = 0;
  48. DWORD dwDownloaded = 0;
  49. LPSTR pszOutBuffer;
  50. // Initiate a HTTP session
  51. hSession = WinHttpCacheOpen(L"WinHTTP Cache Extension", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, CACHE_FLAG_DEFAULT_SETTING);
  52. if (hSession == NULL) {
  53. printf("Error %u in WinHttpCacheOpen.\n", GetLastError());
  54. goto done;
  55. }
  56. // Connect to a server
  57. hConnect = WinHttpConnect(hSession, HOSTNAME, INTERNET_DEFAULT_HTTP_PORT, 0);
  58. if (hConnect == NULL) {
  59. printf("Error %u in WinHttpCacheConnect.\n", GetLastError());
  60. goto done;
  61. }
  62. for (int i=0; i<1; i++)
  63. {
  64. // Create a HTTP request handle
  65. hRequest = WinHttpCacheOpenRequest( hConnect, L"GET", szObjectName[i], NULL, NULL, NULL, 0);
  66. if (hRequest == NULL) {
  67. printf("Error %u in WinHttpCacheOpenRequest.\n", GetLastError());
  68. goto done;
  69. }
  70. // Send a Request
  71. if(!WinHttpCacheSendRequest( hRequest, NULL, 0, NULL, 0, 0, 0))
  72. {
  73. printf("Error %u in WinHttpCacheSendRequest.\n", GetLastError());
  74. goto done;
  75. }
  76. // End the request
  77. if(!WinHttpCacheReceiveResponse( hRequest, NULL))
  78. printf("Error %u in WinHttpCacheReceiveResponse.\n", GetLastError());
  79. // intentional fall through...
  80. // Keep checking for data until there is nothing left.
  81. do
  82. {
  83. // Check for available data.
  84. dwSize = 0;
  85. if (!WinHttpCacheQueryDataAvailable( hRequest, &dwSize))
  86. printf("Error %u in WinHttpCacheQueryDataAvailable.\n", GetLastError());
  87. // Allocate space for the buffer.
  88. pszOutBuffer = new char[dwSize+1];
  89. ZeroMemory(pszOutBuffer, dwSize+1);
  90. // Read the Data.
  91. if (!WinHttpCacheReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
  92. printf("Error %u in WinHttpCacheReadData.\n", GetLastError());
  93. else
  94. printf("%s", pszOutBuffer);
  95. // Free the memory allocated to the buffer.
  96. delete [] (LPVOID)pszOutBuffer;
  97. } while (dwSize>0);
  98. // There may be problems querying or looking up headers if the resource is from the cache.
  99. // Test that out here
  100. WCHAR wszOptionData[2048];
  101. DWORD dwSize = 2048;
  102. if (WinHttpQueryOption( hRequest, WINHTTP_OPTION_URL, (LPVOID)wszOptionData, &dwSize))
  103. {
  104. printf("WinHttpQueryOption returns dwSize = %d, URL = %S\n", dwSize, wszOptionData);
  105. }
  106. else
  107. {
  108. printf("Error %u in WinHttpQueryOption.\n", GetLastError());
  109. }
  110. }
  111. done:
  112. if (hRequest != NULL) WinHttpCacheCloseHandle(hRequest);
  113. if (hConnect != NULL) WinHttpCacheCloseHandle(hConnect);
  114. if (hSession != NULL) WinHttpCacheCloseHandle(hSession);
  115. }
  116. //////////////////////////////////////////////////////////////////////////////////////////////////
  117. //
  118. // TestCase2 - weird case (call QueryDataAvailable before ReceiveResponse)
  119. //
  120. // Get from network all the time
  121. //
  122. void TestCase2() {
  123. HINTERNET hSession;
  124. HINTERNET hConnect;
  125. HINTERNET hRequest;
  126. DWORD dwSize = 0;
  127. DWORD dwDownloaded = 0;
  128. LPSTR pszOutBuffer;
  129. // Initiate a HTTP session
  130. hSession = WinHttpCacheOpen(L"WinHTTP Cache Extension", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, CACHE_FLAG_DEFAULT_SETTING);
  131. if (hSession == NULL) {
  132. printf("Error %u in WinHttpCacheOpen.\n", GetLastError());
  133. goto done;
  134. }
  135. // Connect to a server
  136. hConnect = WinHttpConnect(hSession, HOSTNAME, INTERNET_DEFAULT_HTTP_PORT, 0);
  137. if (hConnect == NULL) {
  138. printf("Error %u in WinHttpCacheConnect.\n", GetLastError());
  139. goto done;
  140. }
  141. for (int i=0; i<4; i++)
  142. {
  143. // Create a HTTP request handle
  144. hRequest = WinHttpCacheOpenRequest( hConnect, L"GET", szObjectName[0], NULL, NULL, NULL, 0);
  145. if (hRequest == NULL) {
  146. printf("Error %u in WinHttpCacheOpenRequest.\n", GetLastError());
  147. goto done;
  148. }
  149. // Send a Request
  150. if(!WinHttpCacheSendRequest( hRequest, NULL, 0, NULL, 0, 0, 0))
  151. {
  152. printf("Error %u in WinHttpCacheSendRequest.\n", GetLastError());
  153. goto done;
  154. }
  155. // End the request
  156. if(!WinHttpCacheReceiveResponse( hRequest, NULL))
  157. printf("Error %u in WinHttpCacheReceiveResponse.\n", GetLastError());
  158. // intentional fall through...
  159. // There may be problems querying or looking up headers if the resource is from the cache.
  160. // Test that out here
  161. // TEST CASE: Calling WinHttpQueryOption and WinHttpQueryHeaders before WinHttpReceiveResponse
  162. WCHAR wszOptionData[2048];
  163. DWORD dwSize = 2048;
  164. /*
  165. if (WinHttpQueryOption(hRequest, WINHTTP_OPTION_URL, (LPVOID)wszOptionData, &dwSize))
  166. {
  167. printf("WinHttpQueryOption returns dwSize = %d, URL = %S\n", dwSize, wszOptionData);
  168. }
  169. else
  170. {
  171. printf("Error %u in WinHttpQueryOption.\n", GetLastError());
  172. }
  173. dwSize = 2048;
  174. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LAST_MODIFIED,
  175. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  176. printf ("WinHttpQueryHeaders returns last_modified = %S\n", wszOptionData);
  177. else
  178. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  179. dwSize = 2048;
  180. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH,
  181. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  182. printf ("WinHttpQueryHeaders returns content length = %S\n", wszOptionData);
  183. else
  184. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  185. dwSize = 2048;
  186. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
  187. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  188. printf ("WinHttpQueryHeaders returns all headers = \n%S\n\n", wszOptionData);
  189. else
  190. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  191. */
  192. // Keep checking for data until there is nothing left.
  193. do
  194. {
  195. // Check for available data.
  196. dwSize = 0;
  197. if (!WinHttpCacheQueryDataAvailable( hRequest, &dwSize))
  198. {
  199. printf("Error %u in WinHttpCacheQueryDataAvailable.\n", GetLastError());
  200. goto done;
  201. }
  202. // Allocate space for the buffer.
  203. pszOutBuffer = new char[dwSize+1];
  204. ZeroMemory(pszOutBuffer, dwSize+1);
  205. // Read the Data.
  206. if (!WinHttpCacheReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
  207. {
  208. printf("Error %u in WinHttpCacheReadData.\n", GetLastError());
  209. goto done;
  210. }
  211. else
  212. printf("%s", pszOutBuffer);
  213. // Free the memory allocated to the buffer.
  214. delete [] (LPVOID)pszOutBuffer;
  215. } while (dwSize>0);
  216. }
  217. done:
  218. if (hRequest != NULL) WinHttpCacheCloseHandle(hRequest);
  219. if (hConnect != NULL) WinHttpCacheCloseHandle(hConnect);
  220. if (hSession != NULL) WinHttpCacheCloseHandle(hSession);
  221. }
  222. //////////////////////////////////////////////////////////////////////////////////////////////////
  223. //
  224. //
  225. void TestCase3() {
  226. HINTERNET hSession;
  227. HINTERNET hConnect;
  228. HINTERNET hRequest;
  229. DWORD dwSize = 0;
  230. DWORD dwDownloaded = 0;
  231. LPSTR pszOutBuffer;
  232. // Initiate a HTTP session
  233. hSession = WinHttpCacheOpen(L"WinHTTP Cache Extension", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, CACHE_FLAG_DEFAULT_SETTING);
  234. if (hSession == NULL) {
  235. printf("Error %u in WinHttpCacheOpen.\n", GetLastError());
  236. goto done;
  237. }
  238. // Connect to a server
  239. hConnect = WinHttpConnect(hSession, HOSTNAME, INTERNET_DEFAULT_HTTP_PORT, 0);
  240. if (hConnect == NULL) {
  241. printf("Error %u in WinHttpCacheConnect.\n", GetLastError());
  242. goto done;
  243. }
  244. for (int i=0; i<1; i++)
  245. {
  246. // Create a HTTP request handle
  247. hRequest = WinHttpCacheOpenRequest( hConnect, L"GET", szObjectName[i], NULL, NULL, NULL, 0);
  248. if (hRequest == NULL) {
  249. printf("Error %u in WinHttpCacheOpenRequest.\n", GetLastError());
  250. goto done;
  251. }
  252. // Send a Request
  253. if(!WinHttpCacheSendRequest( hRequest, NULL, 0, NULL, 0, 0, 0))
  254. {
  255. printf("Error %u in WinHttpCacheSendRequest.\n", GetLastError());
  256. goto done;
  257. }
  258. // End the request
  259. if(!WinHttpCacheReceiveResponse( hRequest, NULL))
  260. printf("Error %u in WinHttpCacheReceiveResponse.\n", GetLastError());
  261. // intentional fall through...
  262. // There may be problems querying or looking up headers if the resource is from the cache.
  263. // Test that out here
  264. // TEST CASE: Calling WinHttpQueryOption and WinHttpQueryHeaders before WinHttpReceiveResponse
  265. WCHAR wszOptionData[2048];
  266. DWORD dwSize = 2048;
  267. if (WinHttpQueryOption(hRequest, WINHTTP_OPTION_URL, (LPVOID)wszOptionData, &dwSize))
  268. {
  269. printf("WinHttpQueryOption returns URL - dwSize = %d, URL = %S\n", dwSize, wszOptionData);
  270. }
  271. else
  272. {
  273. printf("Error %u in WinHttpQueryOption.\n", GetLastError());
  274. }
  275. dwSize = 2048;
  276. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_EXPIRES,
  277. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  278. printf ("WinHttpQueryHeaders returns expires = %S\n", wszOptionData);
  279. else
  280. printf ("Error %u in expires.\n", GetLastError());
  281. dwSize = 2048;
  282. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LAST_MODIFIED,
  283. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  284. printf ("WinHttpQueryHeaders returns last_modified = %S\n", wszOptionData);
  285. else
  286. printf ("Error %u in last-modified.\n", GetLastError());
  287. dwSize = 2048;
  288. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_IF_MATCH,
  289. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  290. printf ("WinHttpQueryHeaders returns if-match = %S\n", wszOptionData);
  291. else
  292. printf ("Error %u in If-match.\n", GetLastError());
  293. dwSize = 2048;
  294. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH,
  295. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  296. printf ("WinHttpQueryHeaders returns content length = %S\n", wszOptionData);
  297. else
  298. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  299. dwSize = 2048;
  300. if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
  301. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  302. printf ("WinHttpQueryHeaders returns all headers = \n%S\n\n", wszOptionData);
  303. else
  304. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  305. // Keep checking for data until there is nothing left.
  306. do
  307. {
  308. // Check for available data.
  309. dwSize = 0;
  310. if (!WinHttpCacheQueryDataAvailable( hRequest, &dwSize))
  311. {
  312. printf("Error %u in WinHttpCacheQueryDataAvailable.\n", GetLastError());
  313. goto done;
  314. }
  315. // Allocate space for the buffer.
  316. pszOutBuffer = new char[dwSize+1];
  317. ZeroMemory(pszOutBuffer, dwSize+1);
  318. // Read the Data.
  319. if (!WinHttpCacheReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
  320. {
  321. printf("Error %u in WinHttpCacheReadData.\n", GetLastError());
  322. goto done;
  323. }
  324. else
  325. printf("%s", pszOutBuffer);
  326. // Free the memory allocated to the buffer.
  327. delete [] (LPVOID)pszOutBuffer;
  328. } while (dwSize>0);
  329. }
  330. done:
  331. if (hRequest != NULL) WinHttpCacheCloseHandle(hRequest);
  332. if (hConnect != NULL) WinHttpCacheCloseHandle(hConnect);
  333. if (hSession != NULL) WinHttpCacheCloseHandle(hSession);
  334. }
  335. //////////////////////////////////////////////////////////////////////////////////////////////////
  336. //
  337. // Test multiple open connect handles and open request handles synchronously
  338. //
  339. void TestCase4(DWORD dwNumOfConnect, DWORD dwNumOfRequest) {
  340. HINTERNET hSession;
  341. HINTERNET * hConnect;
  342. HINTERNET * hRequest;
  343. hConnect = new HINTERNET[dwNumOfConnect];
  344. hRequest = new HINTERNET[dwNumOfRequest];
  345. DWORD dwSize = 0;
  346. DWORD dwDownloaded = 0;
  347. LPSTR pszOutBuffer;
  348. // Initiate a HTTP session
  349. hSession = WinHttpCacheOpen(L"WinHTTP Cache", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, CACHE_FLAG_DEFAULT_SETTING);
  350. if (hSession == NULL) {
  351. printf("Error %u in WinHttpCacheOpen.\n", GetLastError());
  352. goto done;
  353. }
  354. // Connect to a server
  355. for (DWORD i=0; i<dwNumOfConnect; i++)
  356. {
  357. hConnect[i] = WinHttpConnect(hSession, HOSTNAME, INTERNET_DEFAULT_HTTP_PORT, 0);
  358. if (hConnect[i] == NULL)
  359. {
  360. printf("Error %u in WinHttpCacheConnect.\n", GetLastError());
  361. goto done;
  362. }
  363. }
  364. // Create a HTTP request handle
  365. for (DWORD i=0; i<dwNumOfRequest; i++)
  366. {
  367. hRequest[i] = WinHttpCacheOpenRequest( hConnect[0], L"GET", szObjectName[0], NULL, NULL, NULL, 0);
  368. if (hRequest[i] == NULL)
  369. {
  370. printf("Error %u in WinHttpCacheOpenRequest.\n", GetLastError());
  371. goto done;
  372. }
  373. // Send a Request
  374. if(!WinHttpCacheSendRequest( hRequest[i], NULL, 0, NULL, 0, 0, 0))
  375. {
  376. printf("Error %u in WinHttpCacheSendRequest.\n", GetLastError());
  377. goto done;
  378. }
  379. // End the request
  380. if(!WinHttpCacheReceiveResponse( hRequest[i], NULL))
  381. printf("Error %u in WinHttpCacheReceiveResponse.\n", GetLastError());
  382. }
  383. done:
  384. for (DWORD i=0; i<dwNumOfRequest; i++)
  385. if (hRequest[i] != NULL) WinHttpCacheCloseHandle(hRequest[i]);
  386. delete [] hRequest;
  387. for (DWORD i=0; i<dwNumOfConnect; i++)
  388. if (hConnect[i] != NULL) WinHttpCacheCloseHandle(hConnect[i]);
  389. delete [] hConnect;
  390. if (hSession != NULL) WinHttpCacheCloseHandle(hSession);
  391. }
  392. #endif
  393. //////////////////////////////////////////////////////////////////////////////////////////////////
  394. //
  395. //
  396. void TestFullRead() {
  397. HINTERNET hSession;
  398. HINTERNET * hConnect;
  399. HINTERNET * hRequest;
  400. hConnect = new HINTERNET[g_dwNumConnections];
  401. hRequest = new HINTERNET[g_dwTotalRequests];
  402. DWORD dwSize = 0;
  403. DWORD dwDownloaded = 0;
  404. LPSTR pszOutBuffer;
  405. // Initiate a HTTP session
  406. hSession = WinHttpCacheOpen(L"WinHTTP Agent", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, CACHE_FLAG_DEFAULT_SETTING);
  407. if (hSession == NULL) {
  408. printf("Error %u in WinHttpCacheOpen.\n", GetLastError());
  409. goto done;
  410. }
  411. // Connect to a server
  412. for (DWORD i=0; i<g_dwNumConnections; i++)
  413. {
  414. hConnect[i] = WinHttpConnect(hSession, g_wszHost, INTERNET_DEFAULT_HTTP_PORT, 0);
  415. if (hConnect[i] == NULL)
  416. {
  417. printf("Error %u in WinHttpCacheConnect.\n", GetLastError());
  418. goto done;
  419. }
  420. }
  421. // Create a HTTP request handle
  422. for (DWORD i=0; i<g_dwTotalRequests; i++)
  423. {
  424. printf ("Interation %d in request loop.\n\n", i);
  425. hRequest[i] = WinHttpCacheOpenRequest( hConnect[0], L"GET", g_wszPath, NULL, NULL, NULL, 0);
  426. if (hRequest[i] == NULL)
  427. {
  428. printf("Error %u in WinHttpCacheOpenRequest.\n", GetLastError());
  429. goto done;
  430. }
  431. // Send a Request
  432. if(!WinHttpCacheSendRequest( hRequest[i], NULL, 0, NULL, 0, 0, 0))
  433. {
  434. printf("Error %u in WinHttpCacheSendRequest.\n", GetLastError());
  435. goto done;
  436. }
  437. // End the request
  438. if(!WinHttpCacheReceiveResponse( hRequest[i], NULL))
  439. printf("Error %u in WinHttpCacheReceiveResponse.\n", GetLastError());
  440. WCHAR wszOptionData[2048];
  441. dwSize = 2048;
  442. if (WinHttpQueryHeaders(hRequest[i], WINHTTP_QUERY_RAW_HEADERS_CRLF,
  443. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  444. printf ("WinHttpQueryHeaders returns all headers = \n%S\n\n", wszOptionData);
  445. else
  446. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  447. // Keep checking for data until there is nothing left.
  448. do
  449. {
  450. // Check for available data.
  451. dwSize = 0;
  452. if (!WinHttpCacheQueryDataAvailable( hRequest[i], &dwSize))
  453. {
  454. printf("Error %u in WinHttpCacheQueryDataAvailable.\n", GetLastError());
  455. goto done;
  456. }
  457. // Allocate space for the buffer.
  458. pszOutBuffer = new char[dwSize+1];
  459. ZeroMemory(pszOutBuffer, dwSize+1);
  460. // Read the Data.
  461. if (!WinHttpCacheReadData( hRequest[i], (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
  462. {
  463. printf("Error %u in WinHttpCacheReadData.\n", GetLastError());
  464. goto done;
  465. }
  466. else
  467. printf("%s", pszOutBuffer);
  468. // Free the memory allocated to the buffer.
  469. delete [] (LPVOID)pszOutBuffer;
  470. } while (dwSize>0);
  471. }
  472. done:
  473. for (DWORD i=0; i<g_dwTotalRequests; i++)
  474. if (hRequest[i] != NULL) WinHttpCacheCloseHandle(hRequest[i]);
  475. delete [] hRequest;
  476. for (DWORD i=0; i<g_dwNumConnections; i++)
  477. if (hConnect[i] != NULL) WinHttpCacheCloseHandle(hConnect[i]);
  478. delete [] hConnect;
  479. if (hSession != NULL) WinHttpCacheCloseHandle(hSession);
  480. }
  481. //////////////////////////////////////////////////////////////////////////////////////////////////
  482. //
  483. //
  484. void TestPartialRead() {
  485. HINTERNET hSession;
  486. HINTERNET * hConnect;
  487. HINTERNET * hRequest;
  488. hConnect = new HINTERNET[g_dwNumConnections];
  489. hRequest = new HINTERNET[g_dwTotalRequests];
  490. DWORD dwSize = 0;
  491. DWORD dwDownloaded = 0;
  492. LPSTR pszOutBuffer;
  493. // Initiate a HTTP session
  494. hSession = WinHttpCacheOpen(L"WinHTTP Agent", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, CACHE_FLAG_DEFAULT_SETTING);
  495. if (hSession == NULL) {
  496. printf("Error %u in WinHttpCacheOpen.\n", GetLastError());
  497. goto done;
  498. }
  499. // Connect to a server
  500. for (DWORD i=0; i<g_dwNumConnections; i++)
  501. {
  502. hConnect[i] = WinHttpConnect(hSession, g_wszHost, INTERNET_DEFAULT_HTTP_PORT, 0);
  503. if (hConnect[i] == NULL)
  504. {
  505. printf("Error %u in WinHttpCacheConnect.\n", GetLastError());
  506. goto done;
  507. }
  508. }
  509. // Create a HTTP request handle
  510. for (DWORD i=0; i<g_dwTotalRequests; i++)
  511. {
  512. printf ("Interation %d in request loop.\n\n", i);
  513. hRequest[i] = WinHttpCacheOpenRequest( hConnect[0], L"GET", g_wszPath, NULL, NULL, NULL, 0);
  514. if (hRequest[i] == NULL)
  515. {
  516. printf("Error %u in WinHttpCacheOpenRequest.\n", GetLastError());
  517. goto done;
  518. }
  519. // Send a Request
  520. if(!WinHttpCacheSendRequest( hRequest[i], NULL, 0, NULL, 0, 0, 0))
  521. {
  522. printf("Error %u in WinHttpCacheSendRequest.\n", GetLastError());
  523. goto done;
  524. }
  525. // End the request
  526. if(!WinHttpCacheReceiveResponse( hRequest[i], NULL))
  527. printf("Error %u in WinHttpCacheReceiveResponse.\n", GetLastError());
  528. WCHAR wszOptionData[2048];
  529. dwSize = 2048;
  530. if (WinHttpQueryHeaders(hRequest[i], WINHTTP_QUERY_RAW_HEADERS_CRLF,
  531. NULL, (LPVOID)wszOptionData, &dwSize, NULL))
  532. printf ("WinHttpQueryHeaders returns all headers = \n%S\n\n", wszOptionData);
  533. else
  534. printf ("Error %u in WinHttpQueryHeaders.\n", GetLastError());
  535. // DON'T Keep checking for data until there is nothing left.
  536. // Check for available data.
  537. dwSize = 0;
  538. if (!WinHttpCacheQueryDataAvailable( hRequest[i], &dwSize))
  539. {
  540. printf("Error %u in WinHttpCacheQueryDataAvailable.\n", GetLastError());
  541. goto done;
  542. }
  543. // Allocate space for the buffer.
  544. pszOutBuffer = new char[dwSize+1];
  545. ZeroMemory(pszOutBuffer, dwSize+1);
  546. // Read the Data.
  547. if (!WinHttpCacheReadData( hRequest[i], (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
  548. {
  549. printf("Error %u in WinHttpCacheReadData.\n", GetLastError());
  550. goto done;
  551. }
  552. else
  553. printf("%s", pszOutBuffer);
  554. // Free the memory allocated to the buffer.
  555. delete [] (LPVOID)pszOutBuffer;
  556. }
  557. done:
  558. for (DWORD i=0; i<g_dwTotalRequests; i++)
  559. if (hRequest[i] != NULL) WinHttpCacheCloseHandle(hRequest[i]);
  560. delete [] hRequest;
  561. for (DWORD i=0; i<g_dwNumConnections; i++)
  562. if (hConnect[i] != NULL) WinHttpCacheCloseHandle(hConnect[i]);
  563. delete [] hConnect;
  564. if (hSession != NULL) WinHttpCacheCloseHandle(hSession);
  565. }
  566. void __cdecl main(
  567. int argc,
  568. CHAR * argv[]
  569. )
  570. {
  571. BOOL fPartial = FALSE;
  572. int iArgStart = 2;
  573. if (argc == 1 || (argc >= 2 && strcmp(argv[1], "/?") == 0))
  574. goto syntax;
  575. if (strcmp(argv[1], "/P") == 0 || strcmp(argv[1], "/p") == 0)
  576. fPartial = TRUE;
  577. else if (strcmp(argv[1], "/F") == 0 || strcmp(argv[1], "/f") == 0)
  578. fPartial = FALSE;
  579. else
  580. {
  581. fPartial = FALSE;
  582. iArgStart = 1;
  583. }
  584. if (argc != iArgStart + 4)
  585. {
  586. printf ("Error: Invalid number of parameters\n");
  587. printf ("For help type %s /?\n", argv[0]);
  588. return;
  589. }
  590. g_dwTotalRequests = atoi(argv[iArgStart + 2]);
  591. g_dwNumConnections = atoi(argv[iArgStart + 3]);
  592. if (g_dwTotalRequests <= 0 || g_dwNumConnections <= 0)
  593. {
  594. printf ("Error: Number of requests and number of connections must be greater than 0\n");
  595. printf ("For help type %s /?\n", argv[0]);
  596. return;
  597. }
  598. MultiByteToWideChar(CP_ACP, 0, argv[iArgStart + 0], -1, g_wszHost, 50);
  599. MultiByteToWideChar(CP_ACP, 0, argv[iArgStart + 1], -1, g_wszPath, 50);
  600. if (fPartial == TRUE)
  601. TestPartialRead();
  602. else
  603. TestFullRead();
  604. return;
  605. syntax:
  606. printf ("Test driver for the WinHTTP caching layer.\n\n");
  607. printf ("%s [/F | /P] ServerName ObjectName NumRequests NumConn\n\n", argv[0]);
  608. printf (" /F Full GET request (default setting)\n");
  609. printf (" /P Partial GET request (i.e. interrupt before the entire file is downloaded)\n");
  610. printf (" ServerName\n");
  611. printf (" ObjectName\n");
  612. printf (" NumReqPerConn\n");
  613. printf (" NumConn URL you want to connect to, and how many loops do you want.\n");
  614. }