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.

324 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. ftpszcls.cxx
  5. Abstract:
  6. Tests FTP abort during FtpGetFileSize(). Ensure we overcome IE5 bug #71219
  7. Author:
  8. Richard L Firth (rfirth) 09-Feb-1999
  9. Revision History:
  10. 09-Feb-1999 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 IS_ARG(c) (((c) == '-') || ((c) == '/'))
  22. #define SESSION_CONTEXT 0x01010101
  23. #define FILE_CONTEXT 0x02020202
  24. void _CRTAPI1 main(int, char**);
  25. void usage(void);
  26. VOID
  27. my_callback(
  28. HINTERNET Handle,
  29. DWORD Context,
  30. DWORD Status,
  31. LPVOID Info,
  32. DWORD Length
  33. );
  34. BOOL Verbose = FALSE;
  35. void _CRTAPI1 main(int argc, char** argv) {
  36. HINTERNET hInternet;
  37. HINTERNET hFtpSession;
  38. HINTERNET hFile;
  39. char server[INTERNET_MAX_HOST_NAME_LENGTH + 1];
  40. char username[INTERNET_MAX_USER_NAME_LENGTH];
  41. char password[INTERNET_MAX_PASSWORD_LENGTH];
  42. LPSTR filename = NULL;
  43. DWORD accessMethod = INTERNET_OPEN_TYPE_PRECONFIG;
  44. BOOL expectingProxyServer = FALSE;
  45. LPSTR proxyServer = NULL;
  46. LPSTR pszUrl = NULL;
  47. for (--argc, ++argv; argc; --argc, ++argv) {
  48. if (IS_ARG(**argv)) {
  49. switch (*++*argv) {
  50. case '?':
  51. usage();
  52. case 'a':
  53. ++*argv;
  54. if (**argv == 'p') {
  55. accessMethod = INTERNET_OPEN_TYPE_PROXY;
  56. } else if (**argv == 'd') {
  57. accessMethod = INTERNET_OPEN_TYPE_DIRECT;
  58. } else {
  59. if (**argv) {
  60. printf("error: unrecognised access type: '%c'\n", **argv);
  61. } else {
  62. printf("error: missing access type\n");
  63. }
  64. usage();
  65. }
  66. break;
  67. case 'v':
  68. Verbose = TRUE;
  69. break;
  70. default:
  71. printf("error: unrecognized command line flag: '%c'\n", **argv);
  72. usage();
  73. }
  74. } else if (expectingProxyServer) {
  75. proxyServer = *argv;
  76. expectingProxyServer = FALSE;
  77. } else if (!pszUrl) {
  78. pszUrl = *argv;
  79. }
  80. }
  81. if (!pszUrl) {
  82. printf("error: must supply file URL\n");
  83. usage();
  84. }
  85. URL_COMPONENTS urlbits;
  86. memset(&urlbits, 0, sizeof(urlbits));
  87. urlbits.dwStructSize = sizeof(urlbits);
  88. urlbits.lpszHostName = (LPSTR)server;
  89. urlbits.dwHostNameLength = (DWORD)-1;
  90. urlbits.lpszUserName = (LPSTR)username;
  91. urlbits.dwUserNameLength = (DWORD)-1;
  92. urlbits.lpszPassword = (LPSTR)password;
  93. urlbits.dwPasswordLength = (DWORD)-1;
  94. urlbits.dwUrlPathLength = (DWORD)-1;
  95. BOOL ok;
  96. ok = InternetCrackUrl(pszUrl, (DWORD)-1, 0, &urlbits);
  97. if (!ok) {
  98. print_error("ftpszcls()", "InternetCrackUrl()");
  99. exit(1);
  100. }
  101. hInternet = InternetOpen("ftpszcls",
  102. accessMethod,
  103. proxyServer,
  104. NULL,
  105. 0);
  106. if (!hInternet) {
  107. print_error("ftpszcls()", "InternetOpen()");
  108. exit(1);
  109. } else if (Verbose) {
  110. printf("opened Internet handle %#x\n", hInternet);
  111. }
  112. hFtpSession = InternetConnect(hInternet,
  113. server,
  114. INTERNET_INVALID_PORT_NUMBER,
  115. username,
  116. password,
  117. INTERNET_SERVICE_FTP,
  118. 0,
  119. SESSION_CONTEXT
  120. );
  121. if (!hFtpSession) {
  122. print_error("ftpszcls()", "InternetConnect()");
  123. InternetCloseHandle(hInternet);
  124. exit(1);
  125. } else if (Verbose) {
  126. printf("opened FTP connect handle %#x\n", hFtpSession);
  127. }
  128. filename = urlbits.lpszUrlPath;
  129. hFile = FtpOpenFile(hFtpSession,
  130. filename,
  131. GENERIC_READ,
  132. FTP_TRANSFER_TYPE_BINARY,
  133. FILE_CONTEXT
  134. );
  135. if (!hFile) {
  136. print_error("ftpszcls()", "FtpOpenFile(%s)", filename);
  137. InternetCloseHandle(hFtpSession);
  138. InternetCloseHandle(hInternet);
  139. exit(1);
  140. } else if (Verbose) {
  141. printf("opened FTP File handle %#x\n", hFile);
  142. }
  143. InternetSetStatusCallback(hInternet, my_callback);
  144. DWORD dwFileSizeLow, dwFileSizeHigh;
  145. //
  146. // force TLS handle value to be not FTP file handle object
  147. //
  148. HINTERNET hInternet2;
  149. hInternet2 = InternetOpen("ftpszcls",
  150. INTERNET_OPEN_TYPE_PRECONFIG,
  151. NULL,
  152. NULL,
  153. 0
  154. );
  155. InternetCloseHandle(hInternet2);
  156. //
  157. // get the size of the FTP file. Buggy wininet now has TLS handle and
  158. // mapped handle values set to hInternet2
  159. //
  160. dwFileSizeLow = FtpGetFileSize(hFile, &dwFileSizeHigh);
  161. if (dwFileSizeLow == (DWORD)-1) {
  162. print_error("ftpszcls()", "FtpGetFileSize()");
  163. exit(1);
  164. } else if (Verbose) {
  165. printf("size of \"%s\" is %d\n", pszUrl, dwFileSizeLow);
  166. }
  167. InternetCloseHandle(hFile);
  168. InternetCloseHandle(hFtpSession);
  169. InternetCloseHandle(hInternet);
  170. printf("Done.\n");
  171. exit(0);
  172. }
  173. void usage() {
  174. printf("\n"
  175. "usage: ftpszcls [-a{d|p}] [-v] fileURL\n"
  176. "\n"
  177. "where: -a = Access mode: d = direct; p = proxy. Default is pre-config\n"
  178. " -v = Verbose mode\n"
  179. );
  180. exit(1);
  181. }
  182. VOID
  183. my_callback(
  184. HINTERNET Handle,
  185. DWORD Context,
  186. DWORD Status,
  187. LPVOID Info,
  188. DWORD Length
  189. )
  190. {
  191. char* type$;
  192. switch (Status) {
  193. case INTERNET_STATUS_RESOLVING_NAME:
  194. type$ = "RESOLVING NAME";
  195. break;
  196. case INTERNET_STATUS_NAME_RESOLVED:
  197. type$ = "NAME RESOLVED";
  198. break;
  199. case INTERNET_STATUS_CONNECTING_TO_SERVER:
  200. type$ = "CONNECTING TO SERVER";
  201. break;
  202. case INTERNET_STATUS_CONNECTED_TO_SERVER:
  203. type$ = "CONNECTED TO SERVER";
  204. break;
  205. case INTERNET_STATUS_SENDING_REQUEST:
  206. type$ = "SENDING REQUEST";
  207. break;
  208. case INTERNET_STATUS_REQUEST_SENT:
  209. type$ = "REQUEST SENT";
  210. break;
  211. case INTERNET_STATUS_RECEIVING_RESPONSE:
  212. type$ = "RECEIVING RESPONSE";
  213. break;
  214. case INTERNET_STATUS_RESPONSE_RECEIVED:
  215. type$ = "RESPONSE RECEIVED";
  216. break;
  217. case INTERNET_STATUS_CLOSING_CONNECTION:
  218. type$ = "CLOSING CONNECTION";
  219. break;
  220. case INTERNET_STATUS_CONNECTION_CLOSED:
  221. type$ = "CONNECTION CLOSED";
  222. break;
  223. case INTERNET_STATUS_HANDLE_CREATED:
  224. type$ = "HANDLE CREATED";
  225. //hCancel = *(LPHINTERNET)Info;
  226. break;
  227. case INTERNET_STATUS_HANDLE_CLOSING:
  228. type$ = "HANDLE CLOSING";
  229. break;
  230. case INTERNET_STATUS_REQUEST_COMPLETE:
  231. type$ = "REQUEST COMPLETE";
  232. //AsyncResult = ((LPINTERNET_ASYNC_RESULT)Info)->dwResult;
  233. //AsyncError = ((LPINTERNET_ASYNC_RESULT)Info)->dwError;
  234. break;
  235. default:
  236. type$ = "???";
  237. break;
  238. }
  239. if (Verbose) {
  240. printf("callback: handle %x [context %x [%s]] %s ",
  241. Handle,
  242. Context,
  243. (Context == SESSION_CONTEXT) ? "Session"
  244. : (Context == FILE_CONTEXT) ? "File"
  245. : "???",
  246. type$
  247. );
  248. if (Info) {
  249. if ((Status == INTERNET_STATUS_HANDLE_CREATED)
  250. || (Status == INTERNET_STATUS_HANDLE_CLOSING)) {
  251. printf("%x", *(LPHINTERNET)Info);
  252. } else if (Length == sizeof(DWORD)) {
  253. printf("%d", *(LPDWORD)Info);
  254. } else {
  255. printf((LPSTR)Info);
  256. }
  257. }
  258. putchar('\n');
  259. }
  260. //if (Status == INTERNET_STATUS_REQUEST_COMPLETE) {
  261. // get_response(Handle);
  262. // if (AsyncMode) {
  263. // SetEvent(AsyncEvent);
  264. // } else {
  265. // printf("error: INTERNET_STATUS_REQUEST_COMPLETE returned. Not async\n");
  266. // }
  267. //}
  268. }