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.

520 lines
13 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. tproxy.c
  5. Abstract:
  6. Test program for Wininet proxy settings
  7. Contents:
  8. Author:
  9. Richard L Firth (rfirth) 23-Jul-1996
  10. Revision History:
  11. 23-Jul-1996 rfirth
  12. Created
  13. --*/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <windows.h>
  17. #include <wininet.h>
  18. #include <catlib.h>
  19. #include <malloc.h>
  20. #include <memory.h>
  21. #ifndef _CRTAPI1
  22. #define _CRTAPI1
  23. #endif
  24. #define IS_ARG(c) (((c) == '-') || ((c) == '/'))
  25. #define NEW_USER_AGENT "and now for a completely different user-agent"
  26. void _CRTAPI1 main(int, char**);
  27. void usage(void);
  28. void get_proxy_info(HINTERNET);
  29. void set_proxy_info(HINTERNET, LPINTERNET_PROXY_INFO);
  30. void dump_proxy_info(HINTERNET, LPINTERNET_PROXY_INFO);
  31. void get_user_agent(HINTERNET, char*, LPDWORD);
  32. void set_user_agent(HINTERNET, char*);
  33. void refresh_handle(HINTERNET);
  34. BOOL Verbose = FALSE;
  35. DWORD Failures = 0;
  36. void _CRTAPI1 main(int argc, char** argv) {
  37. HINTERNET hInternet1;
  38. HINTERNET hInternet2;
  39. HINTERNET hInternet3;
  40. HINTERNET hInternet4;
  41. INTERNET_PROXY_INFO proxyInfo;
  42. char proxyBuffer[512];
  43. DWORD length;
  44. BOOL ok;
  45. char uaBuf1[128];
  46. char uaBuf2[128];
  47. DWORD uaLen1;
  48. DWORD uaLen2;
  49. for (--argc, ++argv; argc; --argc, ++argv) {
  50. if (IS_ARG(**argv)) {
  51. switch (*++*argv) {
  52. case 'v':
  53. Verbose = TRUE;
  54. break;
  55. default:
  56. printf("error: unrecognized command line flag: '%c'\n", **argv);
  57. usage();
  58. break;
  59. }
  60. } else {
  61. printf("error: unrecognized command line argument: \"%s\"\n", *argv);
  62. usage();
  63. }
  64. }
  65. //
  66. // get global proxy info
  67. //
  68. get_proxy_info(NULL);
  69. //
  70. // get & remember it
  71. //
  72. length = sizeof(proxyBuffer);
  73. ok = InternetQueryOption(NULL, INTERNET_OPTION_PROXY, (LPVOID)proxyBuffer, &length);
  74. if (!ok) {
  75. print_error("tproxy()", "InternetQueryOption()");
  76. ++Failures;
  77. }
  78. //
  79. // create handles
  80. //
  81. //
  82. // 1. preconfig
  83. //
  84. hInternet1 = InternetOpen("tproxy", INTERNET_OPEN_TYPE_PRECONFIG, "foo", "bar", 0);
  85. if (hInternet1 == NULL) {
  86. print_error("tproxy()", "InternetOpen(PRECONFIG)");
  87. ++Failures;
  88. } else if (Verbose) {
  89. printf("InternetOpen(PRECONFIG) returns %#x\n", hInternet1);
  90. }
  91. get_proxy_info(hInternet1);
  92. //
  93. // 2. direct
  94. //
  95. hInternet2 = InternetOpen("tproxy", INTERNET_OPEN_TYPE_DIRECT, "foo", "bar", 0);
  96. if (hInternet1 == NULL) {
  97. print_error("tproxy()", "InternetOpen(DIRECT)");
  98. ++Failures;
  99. } else if (Verbose) {
  100. printf("InternetOpen(DIRECT) returns %#x\n", hInternet2);
  101. }
  102. get_proxy_info(hInternet2);
  103. //
  104. // 3. private proxy
  105. //
  106. hInternet3 = InternetOpen("tproxy", INTERNET_OPEN_TYPE_PROXY, "foo", "bar", 0);
  107. if (hInternet1 == NULL) {
  108. print_error("tproxy()", "InternetOpen(PROXY)");
  109. ++Failures;
  110. } else if (Verbose) {
  111. printf("InternetOpen(PROXY) returns %#x\n", hInternet3);
  112. }
  113. get_proxy_info(hInternet3);
  114. //
  115. // 4. another preconfig
  116. //
  117. hInternet4 = InternetOpen("tproxy", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  118. if (hInternet1 == NULL) {
  119. print_error("tproxy()", "InternetOpen(PRECONFIG)");
  120. ++Failures;
  121. } else if (Verbose) {
  122. printf("InternetOpen(PRECONFIG #2) returns %#x\n", hInternet4);
  123. }
  124. get_proxy_info(hInternet4);
  125. //
  126. // change global proxy
  127. //
  128. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
  129. proxyInfo.lpszProxy = "modified.global.proxy";
  130. proxyInfo.lpszProxyBypass = "modified.global.proxy.bypass.list, *";
  131. set_proxy_info(NULL, &proxyInfo);
  132. //
  133. // make sure global, hInternet1 and hInternet4 all reference same proxy info
  134. //
  135. get_proxy_info(NULL);
  136. get_proxy_info(hInternet1);
  137. get_proxy_info(hInternet4);
  138. //
  139. // reload global proxy info from registry
  140. //
  141. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG;
  142. proxyInfo.lpszProxy = "modified.global.proxy";
  143. proxyInfo.lpszProxyBypass = "modified.global.proxy.bypass.list, *";
  144. set_proxy_info(NULL, &proxyInfo);
  145. //
  146. // set hInternet2 to use private proxy
  147. //
  148. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
  149. proxyInfo.lpszProxy = "my.test.proxy";
  150. proxyInfo.lpszProxyBypass = "www.foo.com www.bar.com";
  151. set_proxy_info(hInternet2, &proxyInfo);
  152. get_proxy_info(hInternet2);
  153. //
  154. // set hInternet3 to use direct
  155. //
  156. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
  157. proxyInfo.lpszProxy = "this.is.a.bogus.proxy";
  158. proxyInfo.lpszProxyBypass = "this.is.a.bogus.bypass.entry";
  159. set_proxy_info(hInternet3, &proxyInfo);
  160. get_proxy_info(hInternet3);
  161. //
  162. //
  163. //
  164. //
  165. // get the user-agent
  166. //
  167. uaLen1 = sizeof(uaBuf1);
  168. get_user_agent(hInternet1, uaBuf1, &uaLen1);
  169. //
  170. // set the user-agent
  171. //
  172. set_user_agent(hInternet1, NEW_USER_AGENT);
  173. //
  174. // get it again to make sure its the correct value
  175. //
  176. uaLen2 = sizeof(uaBuf2);
  177. get_user_agent(hInternet1, uaBuf2, &uaLen2);
  178. //
  179. // compare 'em
  180. //
  181. if (strcmp(uaBuf2, NEW_USER_AGENT)) {
  182. printf("error: tproxy(): set_user_agent() failed\n");
  183. }
  184. //
  185. // reset global proxy info
  186. //
  187. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG;
  188. proxyInfo.lpszProxy = NULL;
  189. proxyInfo.lpszProxyBypass = NULL;
  190. set_proxy_info(NULL, &proxyInfo);
  191. //
  192. // make sure global, hInternet1 and hInternet4 all reference same proxy info
  193. //
  194. get_proxy_info(NULL);
  195. get_proxy_info(hInternet1);
  196. get_proxy_info(hInternet4);
  197. //
  198. // close all handles
  199. //
  200. InternetCloseHandle(hInternet1);
  201. InternetCloseHandle(hInternet2);
  202. InternetCloseHandle(hInternet3);
  203. InternetCloseHandle(hInternet4);
  204. //
  205. // do the AOL test
  206. //
  207. if (Verbose) {
  208. printf("\nThe AOL Test\n\n");
  209. }
  210. if (Verbose) {
  211. printf("Opening PRECONFIG Internet handle #1\n");
  212. }
  213. hInternet1 = InternetOpen("tproxy", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  214. if (hInternet1 == NULL) {
  215. print_error("tproxy()", "InternetOpen(PRECONFIG)");
  216. ++Failures;
  217. } else if (Verbose) {
  218. printf("InternetOpen(PRECONFIG) returns %#x\n", hInternet1);
  219. }
  220. get_proxy_info(hInternet1);
  221. if (Verbose) {
  222. printf("Changing global proxy info\n");
  223. }
  224. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
  225. proxyInfo.lpszProxy = "my.test.proxy";
  226. proxyInfo.lpszProxyBypass = "www.foo.com www.bar.com";
  227. set_proxy_info(NULL, &proxyInfo);
  228. get_proxy_info(NULL);
  229. get_proxy_info(hInternet1);
  230. if (Verbose) {
  231. printf("Opening PRECONFIG Internet handle #2\n");
  232. }
  233. hInternet2 = InternetOpen("tproxy", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  234. if (hInternet2 == NULL) {
  235. print_error("tproxy()", "InternetOpen(PRECONFIG)");
  236. ++Failures;
  237. } else if (Verbose) {
  238. printf("InternetOpen(PRECONFIG) returns %#x\n", hInternet2);
  239. }
  240. get_proxy_info(hInternet2);
  241. get_proxy_info(NULL);
  242. get_proxy_info(hInternet1);
  243. get_proxy_info(hInternet2);
  244. if (Verbose) {
  245. printf("Changing proxy info on Internet handle #2\n");
  246. }
  247. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
  248. proxyInfo.lpszProxy = "THIS.IS.A.BOGUS.SERVER.LIST";
  249. proxyInfo.lpszProxyBypass = "THIS.IS.A.BOGUS.BYPASS.LIST";
  250. set_proxy_info(hInternet2, &proxyInfo);
  251. get_proxy_info(NULL);
  252. get_proxy_info(hInternet1);
  253. get_proxy_info(hInternet2);
  254. //
  255. // refresh handle 2 - shouldn't change
  256. //
  257. if (Verbose) {
  258. printf("Refreshing Internet handle #2\n");
  259. }
  260. refresh_handle(hInternet2);
  261. //
  262. // refresh the global handle - should refresh global handle & handle 1
  263. //
  264. if (Verbose) {
  265. printf("Refreshing global handle\n");
  266. }
  267. refresh_handle(NULL);
  268. get_proxy_info(NULL);
  269. get_proxy_info(hInternet1);
  270. get_proxy_info(hInternet2);
  271. //
  272. // change global proxy info back to registry. Global handle & handle 1
  273. // proxy info should change
  274. //
  275. if (Verbose) {
  276. printf("Changing global proxy info back to preconfig\n");
  277. }
  278. proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG;
  279. proxyInfo.lpszProxy = NULL;
  280. proxyInfo.lpszProxyBypass = NULL;
  281. set_proxy_info(NULL, &proxyInfo);
  282. get_proxy_info(NULL);
  283. get_proxy_info(hInternet1);
  284. get_proxy_info(hInternet2);
  285. //
  286. // refresh the global handle - should refresh global handle & handle 1
  287. //
  288. if (Verbose) {
  289. printf("Refreshing global handle\n");
  290. }
  291. refresh_handle(NULL);
  292. get_proxy_info(NULL);
  293. get_proxy_info(hInternet1);
  294. get_proxy_info(hInternet2);
  295. //
  296. // close all handles
  297. //
  298. InternetCloseHandle(hInternet1);
  299. InternetCloseHandle(hInternet2);
  300. //
  301. // pass or fail?
  302. //
  303. if (Verbose) {
  304. printf("\nDone.\n");
  305. if (Failures) {
  306. printf("Test failed\n");
  307. } else {
  308. printf("Test passed\n");
  309. }
  310. }
  311. exit(Failures);
  312. }
  313. void usage() {
  314. printf("usage: tproxy [-v]\n"
  315. "where: -v = Verbose mode\n"
  316. );
  317. exit(1);
  318. }
  319. void get_proxy_info(HINTERNET hInternet) {
  320. DWORD length;
  321. BOOL ok;
  322. length = 0;
  323. ok = InternetQueryOption(hInternet, INTERNET_OPTION_PROXY, NULL, &length);
  324. if (ok) {
  325. printf("error: get_proxy_info(%#x): InternetQueryOption() w/ no buffer succeeds\n", hInternet);
  326. ++Failures;
  327. } else {
  328. LPVOID buf = malloc(length);
  329. if (Verbose) {
  330. printf("get_proxy_info(%#x): %d bytes required for proxy info buffer\n", hInternet, length);
  331. }
  332. memset(buf, 0x99, length);
  333. ok = InternetQueryOption(hInternet, INTERNET_OPTION_PROXY, buf, &length);
  334. if (!ok) {
  335. print_error("get_proxy_info()", "InternetQueryOption()");
  336. ++Failures;
  337. } else if (Verbose) {
  338. dump_proxy_info(hInternet, (LPINTERNET_PROXY_INFO)buf);
  339. }
  340. free(buf);
  341. }
  342. }
  343. void set_proxy_info(HINTERNET hInternet, LPINTERNET_PROXY_INFO ProxyInfo) {
  344. BOOL ok;
  345. ok = InternetSetOption(hInternet, INTERNET_OPTION_PROXY, (LPVOID)ProxyInfo, sizeof(*ProxyInfo));
  346. if (!ok) {
  347. print_error("set_proxy_info()", "InternetSetOption()");
  348. ++Failures;
  349. }
  350. }
  351. void dump_proxy_info(HINTERNET hInternet, LPINTERNET_PROXY_INFO ProxyInfo) {
  352. printf("INTERNET_PROXY_INFO for handle %#x:\n"
  353. "\tAccess Type : %s\n"
  354. "\tProxy Server : \"%s\"\n"
  355. "\tProxy Bypass : \"%s\"\n"
  356. "\n",
  357. hInternet,
  358. (ProxyInfo->dwAccessType == INTERNET_OPEN_TYPE_PRECONFIG)
  359. ? "PRECONFIG"
  360. : (ProxyInfo->dwAccessType == INTERNET_OPEN_TYPE_DIRECT)
  361. ? "DIRECT"
  362. : (ProxyInfo->dwAccessType == INTERNET_OPEN_TYPE_PROXY)
  363. ? "PROXY"
  364. : "?",
  365. (ProxyInfo->lpszProxy != NULL) ? ProxyInfo->lpszProxy : "",
  366. (ProxyInfo->lpszProxyBypass != NULL) ? ProxyInfo->lpszProxyBypass : ""
  367. );
  368. }
  369. void get_user_agent(HINTERNET hInternet, char* Buffer, LPDWORD lpdwLen) {
  370. DWORD length;
  371. BOOL ok;
  372. length = 0;
  373. ok = InternetQueryOption(hInternet, INTERNET_OPTION_USER_AGENT, NULL, &length);
  374. if (ok) {
  375. printf("error: get_user_agent(%#x): InternetQueryOption() w/ no buffer succeeds\n", hInternet);
  376. ++Failures;
  377. } else if (length <= *lpdwLen) {
  378. if (Verbose) {
  379. printf("get_user_agent(%#x): %d bytes required for proxy info buffer\n", hInternet, length);
  380. }
  381. memset(Buffer, 0x99, *lpdwLen);
  382. ok = InternetQueryOption(hInternet, INTERNET_OPTION_USER_AGENT, Buffer, &length);
  383. if (!ok) {
  384. print_error("get_proxy_info()", "InternetQueryOption()");
  385. ++Failures;
  386. } else {
  387. if (Buffer[length] != '\0') {
  388. printf("error: InternetQueryOption(USER_AGENT) returns incorrectly terminated string\n");
  389. ++Failures;
  390. }
  391. *lpdwLen = length;
  392. if (Verbose) {
  393. printf("User-Agent for %#x = \"%s\" (%d)\n", hInternet, Buffer, length);
  394. }
  395. }
  396. } else {
  397. printf("error: get_user_agent(%#x): not enough buffer (%d)\n", hInternet, *lpdwLen);
  398. ++Failures;
  399. }
  400. }
  401. void set_user_agent(HINTERNET hInternet, char* String) {
  402. BOOL ok;
  403. ok = InternetSetOption(hInternet, INTERNET_OPTION_USER_AGENT, (LPVOID)String, strlen(String) + 1);
  404. if (!ok) {
  405. print_error("set_user_agent()", "InternetSetOption()");
  406. ++Failures;
  407. }
  408. }
  409. void refresh_handle(HINTERNET hInternet) {
  410. BOOL ok;
  411. DWORD zero;
  412. zero = 0;
  413. ok = InternetSetOption(hInternet, INTERNET_OPTION_REFRESH, &zero, sizeof(zero));
  414. if (!ok) {
  415. print_error("refresh_handle()", "InternetSetOption()");
  416. ++Failures;
  417. }
  418. }