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.

201 lines
5.7 KiB

  1. #define UNICODE
  2. #include "windows.h"
  3. #include "winhttp.h"
  4. #include "stdio.h"
  5. #define INLINE_SET
  6. bool
  7. ApplyCredentials(
  8. HINTERNET hRequest
  9. );
  10. bool
  11. ApplyServerCredentials(
  12. HINTERNET hRequest
  13. );
  14. void wmain (int argc, wchar_t *argv[])
  15. {
  16. DWORD dwFlags = 0;
  17. HINTERNET hInternet = 0;
  18. HINTERNET hConnect = 0;
  19. HINTERNET hRequest = 0;
  20. if (argc < 5 || (0 != wcsicmp(argv[1], L"HEAD") && 0 != wcsicmp(argv[1], L"GET")))
  21. {
  22. printf("usage: \n"
  23. " https {GET | HEAD} <proxy> <proxy-username> <proxy-password>\n"
  24. );
  25. return;
  26. }
  27. printf("verb: %S, proxy %S, username %S, password %S\n",
  28. argv[1], argv[2], argv[3], argv[4] );
  29. const WCHAR * const C_BITS_USER_AGENT = L"Microsoft BITS/6.5";
  30. hInternet = WinHttpOpen( C_BITS_USER_AGENT,
  31. WINHTTP_ACCESS_TYPE_NO_PROXY,
  32. NULL,
  33. NULL,
  34. 0 );
  35. if (! hInternet )
  36. {
  37. printf("internet %d\n", GetLastError());
  38. }
  39. if (! (hConnect = WinHttpConnect( hInternet,
  40. L"bitsnet",
  41. INTERNET_DEFAULT_HTTP_PORT,
  42. 0))) //context
  43. {
  44. printf("connect %d\n", GetLastError());
  45. }
  46. LPCWSTR AcceptTypes[] = {L"*/*", NULL};
  47. if (! (hRequest = WinHttpOpenRequest(
  48. hConnect,
  49. argv[1],
  50. L"/dload/security/basic/500k.zip",
  51. L"HTTP/1.1",
  52. NULL, //referer
  53. AcceptTypes,
  54. dwFlags)))
  55. {
  56. printf("open %d\n", GetLastError());
  57. }
  58. // security-callback calls go here
  59. //
  60. WINHTTP_PROXY_INFO ProxyInfo;
  61. ProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
  62. ProxyInfo.lpszProxy = argv[2]; // L"172.26.242.86:8080"; // formerly bitsisa:8080
  63. ProxyInfo.lpszProxyBypass = NULL;
  64. if (!WinHttpSetOption( hRequest,
  65. WINHTTP_OPTION_PROXY,
  66. &ProxyInfo,
  67. sizeof(ProxyInfo)
  68. ))
  69. {
  70. DWORD err = GetLastError();
  71. printf( "can't set proxy option: %d", err );
  72. }
  73. //
  74. bool done = false;
  75. do
  76. {
  77. BOOL b;
  78. printf("sending...");
  79. b = WinHttpSendRequest( hRequest,
  80. 0,
  81. 0,
  82. 0,
  83. 0,
  84. 0,
  85. 0
  86. );
  87. if (!b)
  88. {
  89. printf("send %d\n", GetLastError());
  90. }
  91. printf("\n");
  92. b = WinHttpReceiveResponse( hRequest, 0 );
  93. if (!b)
  94. {
  95. printf("receive %d\n", GetLastError());
  96. }
  97. // check status
  98. DWORD dwStatus;
  99. DWORD dwLength = sizeof(dwStatus);
  100. if (! WinHttpQueryHeaders( hRequest,
  101. WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
  102. LPCWSTR(&dwStatus),
  103. &dwStatus,
  104. &dwLength,
  105. NULL))
  106. {
  107. printf("can't query status %d\n", GetLastError());
  108. }
  109. printf("dwStatus %d\n", dwStatus );
  110. switch (dwStatus)
  111. {
  112. case 200: done = true; break;
  113. case 407:
  114. {
  115. printf("setting proxy creds: target 1, scheme 8...");
  116. if (!WinHttpSetCredentials( hRequest,
  117. 1,
  118. 0x8,
  119. argv[3], // L"dbitsusr", // formerly bitsisa\\butsusr
  120. argv[4], //L"Bits1Usr1",
  121. NULL
  122. ))
  123. {
  124. printf("set-cred failed %d\n", GetLastError());
  125. }
  126. break;
  127. }
  128. case 401:
  129. {
  130. printf("setting server creds: target 0, scheme 1...\n");
  131. if (!WinHttpSetCredentials( hRequest,
  132. 0,
  133. 0x1,
  134. argv[3],
  135. argv[4],
  136. NULL
  137. ))
  138. {
  139. printf("set-cred failed %d\n", GetLastError());
  140. }
  141. #if 0
  142. printf("setting proxy creds: target 1, scheme 8...\n");
  143. if (!WinHttpSetCredentials( hRequest,
  144. 1,
  145. 0x8,
  146. argv[3], // L"dbitsusr", // formerly bitsisa\\butsusr
  147. argv[4], //L"Bits1Usr1",
  148. NULL
  149. ))
  150. {
  151. printf("set-cred failed %d\n", GetLastError());
  152. }
  153. #endif
  154. break;
  155. }
  156. default:
  157. {
  158. done = true;
  159. }
  160. }
  161. }
  162. while ( !done );
  163. }