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.

340 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. thh.c
  5. Abstract:
  6. Test program for handle hierarchy
  7. Author:
  8. Richard L Firth (rfirth) 12-Jan-1996
  9. Revision History:
  10. 12-Jan-1996 rfirth
  11. Created
  12. --*/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <windows.h>
  16. #include <wininet.h>
  17. #include <catlib.h>
  18. #ifndef _CRTAPI1
  19. #define _CRTAPI1
  20. #endif
  21. #define OPEN_CONTEXT_VALUE 0x11
  22. #define CONNECT_CONTEXT_VALUE 0x22
  23. #define IS_ARG(c) (((c) == '-') || ((c) == '/'))
  24. void _CRTAPI1 main(int, char**);
  25. void usage(void);
  26. void my_callback(HINTERNET, DWORD, DWORD, LPVOID, DWORD);
  27. BOOL Verbose = FALSE;
  28. DWORD AsyncResult;
  29. DWORD AsyncError;
  30. void _CRTAPI1 main(int argc, char** argv) {
  31. HINTERNET hInternet;
  32. HINTERNET hConnect;
  33. HINTERNET hRequest;
  34. BOOL callbacks = FALSE;
  35. INTERNET_STATUS_CALLBACK cbres;
  36. for (--argc, ++argv; argc; --argc, ++argv) {
  37. if (IS_ARG(**argv)) {
  38. switch (*++*argv) {
  39. case 'c':
  40. callbacks = TRUE;
  41. break;
  42. case 'v':
  43. Verbose = TRUE;
  44. break;
  45. default:
  46. printf("error: unrecognized command line flag: '%c'\n", **argv);
  47. usage();
  48. break;
  49. }
  50. } else {
  51. printf("error: unrecognized command line argument: \"%s\"\n", *argv);
  52. usage();
  53. }
  54. }
  55. hInternet = InternetOpen("Handle Hierarchy Test Program (thh)",
  56. LOCAL_INTERNET_ACCESS,
  57. NULL,
  58. 0,
  59. 0
  60. );
  61. if (!hInternet) {
  62. print_error("thh()", "InternetOpen()");
  63. exit(1);
  64. }
  65. if (Verbose) {
  66. printf("InternetOpen() returns handle %x\n", hInternet);
  67. }
  68. if (callbacks) {
  69. if (Verbose) {
  70. printf("installing callback\n");
  71. }
  72. cbres = InternetSetStatusCallback(hInternet, my_callback);
  73. if (cbres == INTERNET_INVALID_STATUS_CALLBACK) {
  74. print_error("thh()", "InternetSetStatusCallback()");
  75. exit(1);
  76. } else if (Verbose) {
  77. printf("previous callback = %x\n", cbres);
  78. }
  79. }
  80. if (Verbose) {
  81. printf("calling InternetConnect()...\n");
  82. }
  83. hConnect = InternetConnect(hInternet,
  84. "foo.bar.com",
  85. 0,
  86. "albert einstein",
  87. "e=mc2",
  88. INTERNET_SERVICE_HTTP,
  89. 0,
  90. callbacks ? CONNECT_CONTEXT_VALUE : 0
  91. );
  92. if (!hInternet) {
  93. print_error("thh()", "InternetConnect()");
  94. exit(1);
  95. } else if (Verbose) {
  96. printf("InternetConnect() returns handle %x\n", hConnect);
  97. }
  98. if (Verbose) {
  99. printf("calling HttpOpenRequest()...\n");
  100. }
  101. hRequest = HttpOpenRequest(hConnect,
  102. NULL, // verb
  103. NULL, // object
  104. NULL, // version
  105. NULL, // referrer
  106. NULL, // accept types
  107. 0, // flags
  108. callbacks ? OPEN_CONTEXT_VALUE : 0
  109. );
  110. if (!hRequest) {
  111. print_error("thh()", "HttpOpenRequest()");
  112. exit(1);
  113. } else if (Verbose) {
  114. printf("HttpOpenRequest() returns handle %x\n", hRequest);
  115. }
  116. if (Verbose) {
  117. printf("closing InternetOpen() handle %x\n", hInternet);
  118. }
  119. if (InternetCloseHandle(hInternet)) {
  120. if (Verbose) {
  121. printf("closed Internet handle %x OK\n", hInternet);
  122. }
  123. } else {
  124. print_error("thh()", "InternetCloseHandle()");
  125. exit(1);
  126. }
  127. printf("Done.\n");
  128. exit(0);
  129. }
  130. void usage() {
  131. printf("usage: thh [-c] [-v]\n"
  132. "where: -c = use callbacks\n"
  133. " -v = verbose mode\n"
  134. );
  135. exit(1);
  136. }
  137. VOID
  138. my_callback(
  139. HINTERNET Handle,
  140. DWORD Context,
  141. DWORD Status,
  142. LPVOID Info,
  143. DWORD Length
  144. )
  145. {
  146. char* type$;
  147. switch (Status) {
  148. case INTERNET_STATUS_RESOLVING_NAME:
  149. type$ = "RESOLVING NAME";
  150. break;
  151. case INTERNET_STATUS_NAME_RESOLVED:
  152. type$ = "NAME RESOLVED";
  153. break;
  154. case INTERNET_STATUS_CONNECTING_TO_SERVER:
  155. type$ = "CONNECTING TO SERVER";
  156. break;
  157. case INTERNET_STATUS_CONNECTED_TO_SERVER:
  158. type$ = "CONNECTED TO SERVER";
  159. break;
  160. case INTERNET_STATUS_SENDING_REQUEST:
  161. type$ = "SENDING REQUEST";
  162. break;
  163. case INTERNET_STATUS_REQUEST_SENT:
  164. type$ = "REQUEST SENT";
  165. break;
  166. case INTERNET_STATUS_RECEIVING_RESPONSE:
  167. type$ = "RECEIVING RESPONSE";
  168. break;
  169. case INTERNET_STATUS_RESPONSE_RECEIVED:
  170. type$ = "RESPONSE RECEIVED";
  171. break;
  172. case INTERNET_STATUS_CLOSING_CONNECTION:
  173. type$ = "CLOSING CONNECTION";
  174. break;
  175. case INTERNET_STATUS_CONNECTION_CLOSED:
  176. type$ = "CONNECTION CLOSED";
  177. break;
  178. case INTERNET_STATUS_HANDLE_CREATED:
  179. type$ = "HANDLE CREATED";
  180. break;
  181. case INTERNET_STATUS_HANDLE_CLOSING:
  182. type$ = "HANDLE CLOSING";
  183. break;
  184. case INTERNET_STATUS_REQUEST_COMPLETE:
  185. type$ = "REQUEST COMPLETE";
  186. AsyncResult = ((LPINTERNET_ASYNC_RESULT)Info)->dwResult;
  187. AsyncError = ((LPINTERNET_ASYNC_RESULT)Info)->dwError;
  188. break;
  189. default:
  190. type$ = "???";
  191. break;
  192. }
  193. if (Verbose) {
  194. printf("callback: H=%x [C=%x [%s]] %s ",
  195. Handle,
  196. Context,
  197. (Context == CONNECT_CONTEXT_VALUE)
  198. ? "Connect"
  199. : (Context == OPEN_CONTEXT_VALUE)
  200. ? "Open "
  201. : "???",
  202. type$
  203. );
  204. if (Info) {
  205. if ((Status == INTERNET_STATUS_HANDLE_CREATED)
  206. || (Status == INTERNET_STATUS_HANDLE_CLOSING)) {
  207. DWORD handleType;
  208. DWORD handleTypeSize = sizeof(handleType);
  209. if (InternetQueryOption(*(LPHINTERNET)Info,
  210. INTERNET_OPTION_HANDLE_TYPE,
  211. (LPVOID)&handleType,
  212. &handleTypeSize
  213. )) {
  214. switch (handleType) {
  215. case INTERNET_HANDLE_TYPE_INTERNET:
  216. type$ = "Internet";
  217. break;
  218. case INTERNET_HANDLE_TYPE_CONNECT_FTP:
  219. type$ = "FTP Connect";
  220. break;
  221. case INTERNET_HANDLE_TYPE_CONNECT_GOPHER:
  222. type$ = "Gopher Connect";
  223. break;
  224. case INTERNET_HANDLE_TYPE_CONNECT_HTTP:
  225. type$ = "HTTP Connect";
  226. break;
  227. case INTERNET_HANDLE_TYPE_FTP_FIND:
  228. type$ = "FTP Find";
  229. break;
  230. case INTERNET_HANDLE_TYPE_FTP_FIND_HTML:
  231. type$ = "FTP Find HTML";
  232. break;
  233. case INTERNET_HANDLE_TYPE_FTP_FILE:
  234. type$ = "FTP File";
  235. break;
  236. case INTERNET_HANDLE_TYPE_FTP_FILE_HTML:
  237. type$ = "FTP File HTML";
  238. break;
  239. case INTERNET_HANDLE_TYPE_GOPHER_FIND:
  240. type$ = "Gopher Find";
  241. break;
  242. case INTERNET_HANDLE_TYPE_GOPHER_FIND_HTML:
  243. type$ = "Gopher Find HTML";
  244. break;
  245. case INTERNET_HANDLE_TYPE_GOPHER_FILE:
  246. type$ = "Gopher File";
  247. break;
  248. case INTERNET_HANDLE_TYPE_GOPHER_FILE_HTML:
  249. type$ = "Gopher File HTML";
  250. break;
  251. case INTERNET_HANDLE_TYPE_HTTP_REQUEST:
  252. type$ = "HTTP Request";
  253. break;
  254. default:
  255. type$ = "???";
  256. break;
  257. }
  258. } else {
  259. type$ = "<error>";
  260. }
  261. printf("%x [%s]", *(LPHINTERNET)Info, type$);
  262. } else if (Status == INTERNET_STATUS_REQUEST_COMPLETE) {
  263. //
  264. // nothing
  265. //
  266. } else if (Length == sizeof(DWORD)) {
  267. printf("%d", *(LPDWORD)Info);
  268. } else {
  269. printf(Info);
  270. }
  271. }
  272. putchar('\n');
  273. }
  274. }