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.

790 lines
20 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. openurl.c
  5. Abstract:
  6. Tests InternetOpenUrl()/InternetReadFile()
  7. Author:
  8. Richard L Firth (rfirth) 29-May-1995
  9. Revision History:
  10. 29-May-1995 rfirth
  11. Created
  12. --*/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <malloc.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include <windows.h>
  21. #include <wininet.h>
  22. #include <winsock.h>
  23. #ifndef _CRTAPI1
  24. #define _CRTAPI1
  25. #endif
  26. #define IS_ARG(c) (((c) == '-') || ((c) == '/'))
  27. //
  28. // prototypes
  29. //
  30. void _CRTAPI1 main(int, char**);
  31. void usage(void);
  32. void _CRTAPI1 my_cleanup(void);
  33. void my_callback(DWORD, DWORD, LPVOID, DWORD);
  34. void open_urls(LPSTR*, int);
  35. void get_url_data(HINTERNET);
  36. void ftp_find(HINTERNET);
  37. void gopher_find(HINTERNET);
  38. void read_data(HINTERNET);
  39. void print_error(char*, char*, ...);
  40. char* map_error(DWORD);
  41. void get_last_internet_error(void);
  42. //
  43. // data
  44. //
  45. BOOL Verbose = FALSE;
  46. HINTERNET InternetHandle = NULL;
  47. INTERNET_STATUS_CALLBACK PreviousCallback;
  48. //
  49. // functions
  50. //
  51. void _CRTAPI1 main(int argc, char** argv) {
  52. BOOL ok;
  53. LPSTR urls[64];
  54. int numberOfUrls = 0;
  55. BOOL fCallback = FALSE;
  56. for (--argc, ++argv; argc; --argc, ++argv) {
  57. if (IS_ARG(**argv)) {
  58. switch (tolower(*++*argv)) {
  59. case 'c':
  60. fCallback = TRUE;
  61. break;
  62. case 'v':
  63. Verbose = TRUE;
  64. break;
  65. default:
  66. printf("unknown command line flag: '%c'\n", **argv);
  67. usage();
  68. }
  69. } else {
  70. if (numberOfUrls == sizeof(urls)/sizeof(urls[0]) - 1) {
  71. break;
  72. }
  73. urls[numberOfUrls++] = *argv;
  74. }
  75. }
  76. //
  77. // exit function
  78. //
  79. atexit(my_cleanup);
  80. //
  81. // let's have a status callback
  82. //
  83. if (fCallback) {
  84. PreviousCallback = InternetSetStatusCallback(my_callback);
  85. if (Verbose) {
  86. printf("previous Internet callback = %x\n", PreviousCallback);
  87. }
  88. }
  89. //
  90. // open gateway
  91. //
  92. InternetHandle = InternetOpen("ou",
  93. PRE_CONFIG_INTERNET_ACCESS,
  94. NULL,
  95. 0,
  96. 0
  97. );
  98. if (InternetHandle == NULL) {
  99. printf("error: openurl: InternetOpen() returns %d\n", GetLastError());
  100. exit(1);
  101. }
  102. if (numberOfUrls == 0) {
  103. printf("error: you must supply an URL\n");
  104. usage();
  105. } else {
  106. open_urls(urls, numberOfUrls);
  107. }
  108. ok = InternetCloseHandle(InternetHandle);
  109. if (!ok) {
  110. printf("error: openurl: InternetClose(InternetHandle) returns %d\n", GetLastError());
  111. exit(1);
  112. }
  113. printf("Done.\n");
  114. exit(0);
  115. }
  116. void usage() {
  117. printf("usage: ou [-c] [-v] [url]*\n"
  118. "where: -c = enable status call-backs\n"
  119. " -v = Verbose mode\n"
  120. " url = one or more URLs to open\n"
  121. );
  122. exit(1);
  123. }
  124. void _CRTAPI1 my_cleanup() {
  125. if (InternetHandle != NULL) {
  126. printf("closing Internet handle %x\n", InternetHandle);
  127. if (!InternetCloseHandle(InternetHandle)) {
  128. print_error("my_cleanup", "InternetCloseHandle()");
  129. }
  130. }
  131. }
  132. VOID
  133. my_callback(
  134. DWORD Context,
  135. DWORD Status,
  136. LPVOID Info,
  137. DWORD Length
  138. )
  139. {
  140. char* type$;
  141. switch (Status) {
  142. case INTERNET_STATUS_RESOLVING_NAME:
  143. type$ = "RESOLVING NAME";
  144. break;
  145. case INTERNET_STATUS_NAME_RESOLVED:
  146. type$ = "NAME RESOLVED";
  147. break;
  148. case INTERNET_STATUS_CONNECTING_TO_SERVER:
  149. type$ = "CONNECTING TO SERVER";
  150. break;
  151. case INTERNET_STATUS_CONNECTED_TO_SERVER:
  152. type$ = "CONNECTED TO SERVER";
  153. break;
  154. case INTERNET_STATUS_SENDING_REQUEST:
  155. type$ = "SENDING REQUEST";
  156. break;
  157. case INTERNET_STATUS_REQUEST_SENT:
  158. type$ = "REQUEST SENT";
  159. break;
  160. case INTERNET_STATUS_RECEIVING_RESPONSE:
  161. type$ = "RECEIVING RESPONSE";
  162. break;
  163. case INTERNET_STATUS_RESPONSE_RECEIVED:
  164. type$ = "RESPONSE RECEIVED";
  165. break;
  166. case INTERNET_STATUS_CLOSING_CONNECTION:
  167. type$ = "CLOSING CONNECTION";
  168. break;
  169. case INTERNET_STATUS_CONNECTION_CLOSED:
  170. type$ = "CONNECTION CLOSED";
  171. break;
  172. default:
  173. type$ = "???";
  174. break;
  175. }
  176. printf("callback: %s ", type$);
  177. if (Info) {
  178. printf(Info);
  179. }
  180. putchar('\n');
  181. }
  182. void open_urls(LPSTR* purls, int nurls) {
  183. HINTERNET handle;
  184. while (nurls--) {
  185. printf("\nopening URL \"%s\"\n\n", *purls);
  186. handle = InternetOpenUrl(InternetHandle,
  187. *purls,
  188. NULL,
  189. 0,
  190. 0,
  191. 0
  192. );
  193. if (handle == NULL) {
  194. print_error("open_urls", "InternetOpenUrl(%s)", *purls);
  195. } else {
  196. get_url_data(handle);
  197. }
  198. ++purls;
  199. }
  200. }
  201. void get_url_data(HINTERNET handle) {
  202. DWORD handleType;
  203. DWORD handleTypeLen;
  204. handleTypeLen = sizeof(handleType);
  205. if (InternetQueryOption(handle,
  206. INTERNET_OPTION_HANDLE_TYPE,
  207. (LPVOID)&handleType,
  208. &handleTypeLen
  209. )) {
  210. switch (handleType) {
  211. case INTERNET_HANDLE_TYPE_INTERNET:
  212. printf("error: get_url_data: HANDLE_TYPE_INTERNET?\n");
  213. break;
  214. case INTERNET_HANDLE_TYPE_CONNECT_FTP:
  215. printf("error: get_url_data: INTERNET_HANDLE_TYPE_CONNECT_FTP?\n");
  216. break;
  217. case INTERNET_HANDLE_TYPE_CONNECT_GOPHER:
  218. printf("error: get_url_data: INTERNET_HANDLE_TYPE_CONNECT_GOPHER?\n");
  219. break;
  220. case INTERNET_HANDLE_TYPE_CONNECT_HTTP:
  221. printf("error: get_url_data: INTERNET_HANDLE_TYPE_CONNECT_HTTP?\n");
  222. break;
  223. case INTERNET_HANDLE_TYPE_FTP_FIND:
  224. ftp_find(handle);
  225. break;
  226. case INTERNET_HANDLE_TYPE_FTP_FILE:
  227. read_data(handle);
  228. break;
  229. case INTERNET_HANDLE_TYPE_GOPHER_FIND:
  230. gopher_find(handle);
  231. break;
  232. case INTERNET_HANDLE_TYPE_GOPHER_FILE:
  233. read_data(handle);
  234. break;
  235. case INTERNET_HANDLE_TYPE_HTTP_REQUEST:
  236. read_data(handle);
  237. break;
  238. default:
  239. printf("error: get_url_data: handleType == %d?\n", handleType);
  240. break;
  241. }
  242. if (!InternetCloseHandle(handle)) {
  243. print_error("get_url_data", "InternetCloseHandle()");
  244. }
  245. } else {
  246. print_error("get_url_data", "InternetQueryOption()");
  247. }
  248. }
  249. void ftp_find(HINTERNET handle) {
  250. WIN32_FIND_DATA ffd;
  251. DWORD nRead;
  252. while (InternetReadFile(handle, (LPVOID)&ffd, sizeof(ffd), &nRead)) {
  253. SYSTEMTIME stDbg;
  254. if (!FileTimeToSystemTime(&ffd.ftLastWriteTime, &stDbg)) {
  255. printf("| ftLastWriteTime = ERROR\n");
  256. }
  257. printf("%2d-%02d-%04d %2d:%02d:%02d %15d bytes %-s%-s%-s %s\n",
  258. stDbg.wMonth, stDbg.wDay, stDbg.wYear,
  259. stDbg.wHour, stDbg.wMinute, stDbg.wSecond,
  260. ffd.nFileSizeLow,
  261. (ffd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) ? "Normal " : "",
  262. (ffd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? "ReadOnly " : "",
  263. (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? "Directory " : "",
  264. ffd.cFileName
  265. );
  266. }
  267. if (GetLastError() != ERROR_NO_MORE_FILES) {
  268. print_error("ftp_find", "InternetReadFile()");
  269. }
  270. }
  271. void gopher_find(HINTERNET handle) {
  272. GOPHER_FIND_DATA data;
  273. int i;
  274. DWORD nRead;
  275. i = 0;
  276. while (InternetReadFile(handle, (LPVOID)&data, sizeof(data), &nRead)) {
  277. LPGOPHER_FIND_DATA p;
  278. SYSTEMTIME systemTime;
  279. char timeBuf[9];
  280. char sizeBuf[32];
  281. p = (LPGOPHER_FIND_DATA)&data;
  282. if ((p->LastModificationTime.dwLowDateTime != 0)
  283. && (p->LastModificationTime.dwHighDateTime != 0)) {
  284. FileTimeToSystemTime(&p->LastModificationTime, &systemTime);
  285. sprintf(timeBuf,
  286. "%02d-%02d-%02d",
  287. systemTime.wMonth,
  288. systemTime.wDay,
  289. systemTime.wYear % 100
  290. );
  291. sprintf(sizeBuf, "%d", p->SizeLow);
  292. } else {
  293. timeBuf[0] = '\0';
  294. sizeBuf[0] = '\0';
  295. }
  296. printf("%5d %c %7s %10s %8s %s\n",
  297. i,
  298. (p->GopherType & GOPHER_TYPE_GOPHER_PLUS) ? '+' : ' ',
  299. (p->GopherType & GOPHER_TYPE_TEXT_FILE) ? "Text"
  300. : (p->GopherType & GOPHER_TYPE_DIRECTORY) ? "Dir"
  301. : (p->GopherType & GOPHER_TYPE_CSO) ? "Phone"
  302. : (p->GopherType & GOPHER_TYPE_ERROR) ? "Error"
  303. : (p->GopherType & GOPHER_TYPE_MAC_BINHEX) ? "MAC"
  304. : (p->GopherType & GOPHER_TYPE_DOS_ARCHIVE) ? "Archive"
  305. : (p->GopherType & GOPHER_TYPE_UNIX_UUENCODED) ? "UNIX"
  306. : (p->GopherType & GOPHER_TYPE_INDEX_SERVER) ? "Index"
  307. : (p->GopherType & GOPHER_TYPE_TELNET) ? "Telnet"
  308. : (p->GopherType & GOPHER_TYPE_BINARY) ? "Binary"
  309. : (p->GopherType & GOPHER_TYPE_REDUNDANT) ? "Backup"
  310. : (p->GopherType & GOPHER_TYPE_TN3270) ? "TN3270"
  311. : (p->GopherType & GOPHER_TYPE_GIF) ? "GIF"
  312. : (p->GopherType & GOPHER_TYPE_IMAGE) ? "Image"
  313. : (p->GopherType & GOPHER_TYPE_BITMAP) ? "Bitmap"
  314. : (p->GopherType & GOPHER_TYPE_MOVIE) ? "Movie"
  315. : (p->GopherType & GOPHER_TYPE_SOUND) ? "Sound"
  316. : (p->GopherType & GOPHER_TYPE_HTML) ? "HTML"
  317. : (p->GopherType & GOPHER_TYPE_PDF) ? "PDF"
  318. : (p->GopherType & GOPHER_TYPE_CALENDAR) ? "Cal"
  319. : (p->GopherType & GOPHER_TYPE_INLINE) ? "Inline"
  320. : (p->GopherType & GOPHER_TYPE_UNKNOWN) ? "Unknown"
  321. : "\a????",
  322. sizeBuf,
  323. timeBuf,
  324. p->DisplayString
  325. );
  326. ++i;
  327. }
  328. if (GetLastError() != ERROR_NO_MORE_FILES) {
  329. print_error("gopher_find", "InternetReadFile()");
  330. }
  331. }
  332. void read_data(HINTERNET handle) {
  333. char buf[1021]; // odd number for fun!
  334. DWORD nread;
  335. while (InternetReadFile(handle, buf, sizeof(buf), &nread)) {
  336. if (!nread) {
  337. printf("=== end of file ===\n");
  338. break;
  339. } else {
  340. setmode(1, _O_BINARY);
  341. write(1, buf, nread);
  342. }
  343. }
  344. if (GetLastError() != ERROR_SUCCESS) {
  345. print_error("read_file", "InternetReadFile()");
  346. }
  347. }
  348. void print_error(char* func, char* format, ...) {
  349. va_list argptr;
  350. char buf[256];
  351. DWORD error;
  352. error = GetLastError();
  353. va_start(argptr, format);
  354. vsprintf(buf, format, argptr);
  355. printf("error: %s: %s returns %d [%s]\n", func, buf, error, map_error(error));
  356. va_end(argptr);
  357. if (error == ERROR_INTERNET_EXTENDED_ERROR) {
  358. get_last_internet_error();
  359. }
  360. }
  361. char* map_error(DWORD error) {
  362. switch (error) {
  363. case ERROR_FILE_NOT_FOUND:
  364. return "ERROR_FILE_NOT_FOUND";
  365. case ERROR_PATH_NOT_FOUND:
  366. return "ERROR_PATH_NOT_FOUND";
  367. case ERROR_ACCESS_DENIED:
  368. return "ERROR_ACCESS_DENIED";
  369. case ERROR_INVALID_HANDLE:
  370. return "ERROR_INVALID_HANDLE";
  371. case ERROR_NOT_ENOUGH_MEMORY:
  372. return "ERROR_NOT_ENOUGH_MEMORY";
  373. case ERROR_NO_MORE_FILES:
  374. return "ERROR_NO_MORE_FILES";
  375. case ERROR_INVALID_PASSWORD:
  376. return "ERROR_INVALID_PASSWORD";
  377. case ERROR_INVALID_PARAMETER:
  378. return "ERROR_INVALID_PARAMETER";
  379. case ERROR_BUFFER_OVERFLOW:
  380. return "ERROR_BUFFER_OVERFLOW";
  381. case ERROR_NO_MORE_SEARCH_HANDLES:
  382. return "ERROR_NO_MORE_SEARCH_HANDLES";
  383. case ERROR_INVALID_TARGET_HANDLE:
  384. return "ERROR_INVALID_TARGET_HANDLE";
  385. case ERROR_CALL_NOT_IMPLEMENTED:
  386. return "ERROR_CALL_NOT_IMPLEMENTED";
  387. case ERROR_INSUFFICIENT_BUFFER:
  388. return "ERROR_INSUFFICIENT_BUFFER";
  389. case ERROR_INVALID_NAME:
  390. return "ERROR_INVALID_NAME";
  391. case ERROR_INVALID_LEVEL:
  392. return "ERROR_INVALID_LEVEL";
  393. case ERROR_BAD_PATHNAME:
  394. return "ERROR_BAD_PATHNAME";
  395. case ERROR_BUSY:
  396. return "ERROR_BUSY";
  397. case ERROR_ALREADY_EXISTS:
  398. return "ERROR_ALREADY_EXISTS";
  399. case ERROR_FILENAME_EXCED_RANGE:
  400. return "ERROR_FILENAME_EXCED_RANGE";
  401. case ERROR_MORE_DATA:
  402. return "ERROR_MORE_DATA";
  403. case ERROR_NO_MORE_ITEMS:
  404. return "ERROR_NO_MORE_ITEMS";
  405. case ERROR_INVALID_ADDRESS:
  406. return "ERROR_INVALID_ADDRESS";
  407. case ERROR_OPERATION_ABORTED:
  408. return "ERROR_OPERATION_ABORTED";
  409. case ERROR_INTERNET_OUT_OF_HANDLES:
  410. return "ERROR_INTERNET_OUT_OF_HANDLES";
  411. case ERROR_INTERNET_TIMEOUT:
  412. return "ERROR_INTERNET_TIMEOUT";
  413. case ERROR_INTERNET_EXTENDED_ERROR:
  414. return "ERROR_INTERNET_EXTENDED_ERROR";
  415. case ERROR_INTERNET_INTERNAL_ERROR:
  416. return "ERROR_INTERNET_INTERNAL_ERROR";
  417. case ERROR_INTERNET_INVALID_URL:
  418. return "ERROR_INTERNET_INVALID_URL";
  419. case ERROR_INTERNET_UNRECOGNIZED_SCHEME:
  420. return "ERROR_INTERNET_UNRECOGNIZED_SCHEME";
  421. case ERROR_INTERNET_NAME_NOT_RESOLVED:
  422. return "ERROR_INTERNET_NAME_NOT_RESOLVED";
  423. case ERROR_INTERNET_PROTOCOL_NOT_FOUND:
  424. return "ERROR_INTERNET_PROTOCOL_NOT_FOUND";
  425. case ERROR_INTERNET_INVALID_OPTION:
  426. return "ERROR_INTERNET_INVALID_OPTION";
  427. case ERROR_FTP_TRANSFER_IN_PROGRESS:
  428. return "ERROR_FTP_TRANSFER_IN_PROGRESS";
  429. case ERROR_FTP_CONNECTED:
  430. return "ERROR_FTP_CONNECTED";
  431. case ERROR_FTP_DROPPED:
  432. return "ERROR_FTP_DROPPED";
  433. case ERROR_GOPHER_PROTOCOL_ERROR:
  434. return "ERROR_GOPHER_PROTOCOL_ERROR";
  435. case ERROR_GOPHER_NOT_FILE:
  436. return "ERROR_GOPHER_NOT_FILE";
  437. case ERROR_GOPHER_DATA_ERROR:
  438. return "ERROR_GOPHER_DATA_ERROR";
  439. case ERROR_GOPHER_END_OF_DATA:
  440. return "ERROR_GOPHER_END_OF_DATA";
  441. case ERROR_GOPHER_INVALID_LOCATOR:
  442. return "ERROR_GOPHER_INVALID_LOCATOR";
  443. case ERROR_GOPHER_INCORRECT_LOCATOR_TYPE:
  444. return "ERROR_GOPHER_INCORRECT_LOCATOR_TYPE";
  445. case ERROR_GOPHER_NOT_GOPHER_PLUS:
  446. return "ERROR_GOPHER_NOT_GOPHER_PLUS";
  447. case ERROR_GOPHER_ATTRIBUTE_NOT_FOUND:
  448. return "ERROR_GOPHER_ATTRIBUTE_NOT_FOUND";
  449. case ERROR_GOPHER_UNKNOWN_LOCATOR:
  450. return "ERROR_GOPHER_UNKNOWN_LOCATOR";
  451. case ERROR_HTTP_HEADER_NOT_FOUND:
  452. return "ERROR_HTTP_HEADER_NOT_FOUND";
  453. case ERROR_HTTP_DOWNLEVEL_SERVER:
  454. return "ERROR_HTTP_DOWNLEVEL_SERVER";
  455. case ERROR_HTTP_INVALID_SERVER_RESPONSE:
  456. return "ERROR_HTTP_INVALID_SERVER_RESPONSE";
  457. case WSAEINTR:
  458. return "WSAEINTR";
  459. case WSAEBADF:
  460. return "WSAEBADF";
  461. case WSAEACCES:
  462. return "WSAEACCES";
  463. case WSAEFAULT:
  464. return "WSAEFAULT";
  465. case WSAEINVAL:
  466. return "WSAEINVAL";
  467. case WSAEMFILE:
  468. return "WSAEMFILE";
  469. case WSAEWOULDBLOCK:
  470. return "WSAEWOULDBLOCK";
  471. case WSAEINPROGRESS:
  472. return "WSAEINPROGRESS";
  473. case WSAEALREADY:
  474. return "WSAEALREADY";
  475. case WSAENOTSOCK:
  476. return "WSAENOTSOCK";
  477. case WSAEDESTADDRREQ:
  478. return "WSAEDESTADDRREQ";
  479. case WSAEMSGSIZE:
  480. return "WSAEMSGSIZE";
  481. case WSAEPROTOTYPE:
  482. return "WSAEPROTOTYPE";
  483. case WSAENOPROTOOPT:
  484. return "WSAENOPROTOOPT";
  485. case WSAEPROTONOSUPPORT:
  486. return "WSAEPROTONOSUPPORT";
  487. case WSAESOCKTNOSUPPORT:
  488. return "WSAESOCKTNOSUPPORT";
  489. case WSAEOPNOTSUPP:
  490. return "WSAEOPNOTSUPP";
  491. case WSAEPFNOSUPPORT:
  492. return "WSAEPFNOSUPPORT";
  493. case WSAEAFNOSUPPORT:
  494. return "WSAEAFNOSUPPORT";
  495. case WSAEADDRINUSE:
  496. return "WSAEADDRINUSE";
  497. case WSAEADDRNOTAVAIL:
  498. return "WSAEADDRNOTAVAIL";
  499. case WSAENETDOWN:
  500. return "WSAENETDOWN";
  501. case WSAENETUNREACH:
  502. return "WSAENETUNREACH";
  503. case WSAENETRESET:
  504. return "WSAENETRESET";
  505. case WSAECONNABORTED:
  506. return "WSAECONNABORTED";
  507. case WSAECONNRESET:
  508. return "WSAECONNRESET";
  509. case WSAENOBUFS:
  510. return "WSAENOBUFS";
  511. case WSAEISCONN:
  512. return "WSAEISCONN";
  513. case WSAENOTCONN:
  514. return "WSAENOTCONN";
  515. case WSAESHUTDOWN:
  516. return "WSAESHUTDOWN";
  517. case WSAETOOMANYREFS:
  518. return "WSAETOOMANYREFS";
  519. case WSAETIMEDOUT:
  520. return "WSAETIMEDOUT";
  521. case WSAECONNREFUSED:
  522. return "WSAECONNREFUSED";
  523. case WSAELOOP:
  524. return "WSAELOOP";
  525. case WSAENAMETOOLONG:
  526. return "WSAENAMETOOLONG";
  527. case WSAEHOSTDOWN:
  528. return "WSAEHOSTDOWN";
  529. case WSAEHOSTUNREACH:
  530. return "WSAEHOSTUNREACH";
  531. case WSAENOTEMPTY:
  532. return "WSAENOTEMPTY";
  533. case WSAEPROCLIM:
  534. return "WSAEPROCLIM";
  535. case WSAEUSERS:
  536. return "WSAEUSERS";
  537. case WSAEDQUOT:
  538. return "WSAEDQUOT";
  539. case WSAESTALE:
  540. return "WSAESTALE";
  541. case WSAEREMOTE:
  542. return "WSAEREMOTE";
  543. case WSAEDISCON:
  544. return "WSAEDISCON";
  545. case WSASYSNOTREADY:
  546. return "WSASYSNOTREADY";
  547. case WSAVERNOTSUPPORTED:
  548. return "WSAVERNOTSUPPORTED";
  549. case WSANOTINITIALISED:
  550. return "WSANOTINITIALISED";
  551. case WSAHOST_NOT_FOUND:
  552. return "WSAHOST_NOT_FOUND";
  553. case WSATRY_AGAIN:
  554. return "WSATRY_AGAIN";
  555. case WSANO_RECOVERY:
  556. return "WSANO_RECOVERY";
  557. case WSANO_DATA:
  558. return "WSANO_DATA";
  559. default:
  560. return "???";
  561. }
  562. }
  563. void get_last_internet_error() {
  564. DWORD bufLength;
  565. char buffer[256];
  566. DWORD category;
  567. bufLength = sizeof(buffer);
  568. if (InternetGetLastResponseInfo(&category, buffer, &bufLength)) {
  569. printf("InternetGetLastResponseInfo() returns %d bytes\n", bufLength);
  570. if (bufLength != 0) {
  571. printf("Text = \"%s\"\n", buffer);
  572. }
  573. if (strlen(buffer) != bufLength) {
  574. printf("\aerror: get_last_internet_error: InternetGetLastResponseInfo() returns %d bytes; strlen(buffer) = %d\n",
  575. bufLength,
  576. strlen(buffer)
  577. );
  578. }
  579. } else {
  580. LPSTR errbuf;
  581. printf("InternetGetLastResponseInfo() returns error %d (bufLength = %d)\n",
  582. GetLastError(),
  583. bufLength
  584. );
  585. if ((errbuf = malloc(bufLength)) == NULL) {
  586. printf("error: get_last_internet_error: malloc(%d) failed\n", bufLength);
  587. return;
  588. }
  589. if (InternetGetLastResponseInfo(&category, errbuf, &bufLength)) {
  590. printf("InternetGetLastResponseInfo() returns %d bytes\n", bufLength);
  591. if (bufLength != 0) {
  592. printf("Text = \"%s\"\n", errbuf);
  593. }
  594. if (strlen(buffer) != bufLength) {
  595. printf("\aerror: get_last_internet_error: InternetGetLastResponseInfo() returns %d bytes; strlen(buffer) = %d\n",
  596. bufLength,
  597. strlen(buffer)
  598. );
  599. }
  600. } else {
  601. printf("error: get_last_internet_error: InternetGetLastResponseInfo() returns error %d (bufLength = %d)\n",
  602. GetLastError(),
  603. bufLength
  604. );
  605. }
  606. free(errbuf);
  607. }
  608. }