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.

518 lines
13 KiB

  1. #include "IsapiStress.h"
  2. // Globals
  3. HANDLE hLogFile = INVALID_HANDLE_VALUE;
  4. void Usage()
  5. {
  6. _tprintf(_T("Usage:\n\r"));
  7. _tprintf(_T("IsapiStress /s:<IISServer> /v:<VirtualDirectory> /d:<Directory> /f:<FileName> /l:<LogFileName> /? <Usage>\r\n"));
  8. _tprintf(_T("Where:\r\n"));
  9. _tprintf(_T("\t<IISServer> - Website to upload file to (ie. ocadeviis) \r\n"));
  10. _tprintf(_T("\t<VirualDirectory> - Location on Web server to upload the file(s) to.\r\n"));
  11. _tprintf(_T("\t<Directory> - Path to a directory of .cab files to be uploaded\r\n"));
  12. _tprintf(_T("\t<FileName> - Qualified path of a file to be uploaded.\r\n"));
  13. _tprintf(_T("\t<LogFileName> - Name of file to log OCA response url or errors.\r\n\r\n"));
  14. _tprintf(_T("\tNote: Default iisserver is ocatest. \r\n\t Default VirtualDirectory is OCA"));
  15. }
  16. void
  17. LogMessage(TCHAR *pFormat,...)
  18. {
  19. // Routine to Log Fatal Errors to NT Event Log
  20. TCHAR chMsg[256];
  21. DWORD dwBytesWritten;
  22. va_list pArg;
  23. va_start(pArg, pFormat);
  24. StringCbVPrintf(chMsg, sizeof chMsg,pFormat, pArg);
  25. va_end(pArg);
  26. // add a cr\lf combination for file fomrating.
  27. StringCbCat(chMsg, sizeof chMsg,_T("\r\n"));
  28. if (hLogFile != INVALID_HANDLE_VALUE)
  29. {
  30. /* Write to event log. */
  31. WriteFile(hLogFile,
  32. chMsg,
  33. _tcslen(chMsg) * (DWORD)sizeof TCHAR,
  34. &dwBytesWritten,
  35. NULL);
  36. }
  37. else // write it to the console.
  38. _tprintf(chMsg);
  39. }
  40. DWORD Upload(TCHAR *SourceFileName, TCHAR *VirtualDir, TCHAR *HostName, TCHAR *RemoteFileName)
  41. {
  42. static const TCHAR *pszAccept[] = {_T("*.*"), 0};
  43. //TCHAR RemoteFileName[MAX_PATH]; // Host/Virtualdirectory/filename
  44. TCHAR *pUploadUrl = NULL;
  45. BOOL bRet = FALSE;
  46. BOOL UploadSuccess = FALSE;
  47. DWORD dwBytesRead;
  48. DWORD dwBytesWritten;
  49. DWORD ResponseCode = 0;
  50. DWORD ResLength = 255;
  51. DWORD index = 0;
  52. DWORD ErrorCode = 0;
  53. HINTERNET hSession = NULL;
  54. HINTERNET hConnect = NULL;
  55. HINTERNET hRequest = NULL;
  56. INTERNET_BUFFERS BufferIn = {0};
  57. INTERNET_BUFFERS BufferOut = {0};
  58. HANDLE hFile = INVALID_HANDLE_VALUE;
  59. BYTE *pBuffer;
  60. BOOL bOnce = FALSE;
  61. GUID guidNewGuid;
  62. TCHAR *szGuidRaw = NULL;
  63. HRESULT hResult = S_OK;
  64. wchar_t *wszGuidRaw = NULL;
  65. CoInitialize(NULL);
  66. hResult = CoCreateGuid(&guidNewGuid);
  67. if (FAILED(hResult))
  68. {
  69. //-------------What do we send here....
  70. //goto ERRORS;
  71. ;
  72. }
  73. else
  74. {
  75. if (UuidToStringW(&guidNewGuid, &wszGuidRaw) == RPC_S_OK)
  76. {
  77. if ( (szGuidRaw = (TCHAR *) malloc ( wcslen(wszGuidRaw)*2 )) != NULL)
  78. {
  79. // clear the memory
  80. ZeroMemory(szGuidRaw, wcslen(wszGuidRaw) * 2);
  81. wcstombs( szGuidRaw, wszGuidRaw, wcslen(wszGuidRaw));
  82. }
  83. else
  84. {
  85. LogMessage(_T("Memory allocation failed: ErrorCode:%d"),GetLastError());
  86. if (wszGuidRaw)
  87. {
  88. RpcStringFreeW(&wszGuidRaw);
  89. }
  90. CoUninitialize();
  91. return GetLastError();
  92. }
  93. }
  94. }
  95. if (wszGuidRaw)
  96. {
  97. RpcStringFreeW(&wszGuidRaw);
  98. }
  99. StringCbPrintf(RemoteFileName, MAX_PATH * sizeof TCHAR, _T("\\%s\\%s%s"),VirtualDir,szGuidRaw + 19, PathFindFileName(SourceFileName));
  100. if (szGuidRaw)
  101. free (szGuidRaw);
  102. hSession = InternetOpen( _T("IsapiStress"),
  103. INTERNET_OPEN_TYPE_PRECONFIG,
  104. NULL,
  105. NULL,
  106. 0);
  107. if (!hSession)
  108. {
  109. LogMessage(_T("Failed to create an internet session."));
  110. CoUninitialize();
  111. return GetLastError();
  112. }
  113. hConnect = InternetConnect(hSession,
  114. HostName,
  115. INTERNET_DEFAULT_HTTP_PORT,
  116. NULL,
  117. NULL,
  118. INTERNET_SERVICE_HTTP,
  119. 0,
  120. NULL);
  121. if (hConnect == INVALID_HANDLE_VALUE)
  122. {
  123. InternetCloseHandle(hSession);
  124. LogMessage(_T("Failed to create an internet connection."));
  125. CoUninitialize();
  126. return GetLastError();
  127. }
  128. LogMessage(_T("Connecting to: %s"),HostName);
  129. LogMessage(_T("Uploading file: %s"),RemoteFileName);
  130. hRequest = HttpOpenRequest( hConnect,
  131. _T("PUT"),
  132. RemoteFileName,
  133. NULL,
  134. NULL,
  135. pszAccept,
  136. INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_NO_CACHE_WRITE ,
  137. 0);
  138. if (hRequest != INVALID_HANDLE_VALUE)
  139. {
  140. hFile = CreateFile( SourceFileName,
  141. GENERIC_READ,
  142. FILE_SHARE_READ,
  143. NULL,
  144. OPEN_EXISTING,
  145. FILE_ATTRIBUTE_NORMAL,
  146. NULL);
  147. if (hFile != INVALID_HANDLE_VALUE)
  148. {
  149. // Clear the buffer
  150. if ( (pBuffer = (BYTE *)malloc (70000)) != NULL)
  151. {
  152. BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
  153. BufferIn.Next = NULL;
  154. BufferIn.lpcszHeader = NULL;
  155. BufferIn.dwHeadersLength = 0;
  156. BufferIn.dwHeadersTotal = 0;
  157. BufferIn.lpvBuffer = NULL;
  158. BufferIn.dwBufferLength = 0;
  159. BufferIn.dwOffsetLow = 0;
  160. BufferIn.dwOffsetHigh = 0;
  161. BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
  162. FillMemory(pBuffer, 70000,'/0'); // Fill buffer with data
  163. DWORD dwFlags;
  164. DWORD dwBuffLen = sizeof(dwFlags);
  165. if(!HttpSendRequestEx( hRequest,
  166. &BufferIn,
  167. NULL,
  168. HSR_INITIATE,
  169. 0))
  170. {
  171. LogMessage(_T("HttpSendRequestEx Failed."));
  172. }
  173. else
  174. {
  175. do
  176. {
  177. dwBytesRead = 0;
  178. bRet = ReadFile(hFile,
  179. pBuffer,
  180. 70000,
  181. &dwBytesRead,
  182. NULL);
  183. if (bRet != 0)
  184. {
  185. bRet = InternetWriteFile(hRequest,
  186. pBuffer,
  187. dwBytesRead,
  188. &dwBytesWritten);
  189. if ( (!bRet) || (dwBytesWritten==0) )
  190. {
  191. LogMessage(_T("Error Writting File: %d"),ErrorCode = GetLastError());
  192. }
  193. }
  194. } while (dwBytesRead == sizeof (pBuffer));
  195. CloseHandle(hFile);
  196. hFile = INVALID_HANDLE_VALUE;
  197. bRet = HttpEndRequest( hRequest,
  198. NULL,
  199. 0,
  200. 0);
  201. if (bRet)
  202. {
  203. ResponseCode = 0;
  204. HttpQueryInfo(hRequest,
  205. HTTP_QUERY_STATUS_CODE |HTTP_QUERY_FLAG_NUMBER,
  206. &ResponseCode,
  207. &ResLength,
  208. &index);
  209. if ( (ResponseCode != 200) && (ResponseCode != 201))
  210. {
  211. ErrorCode=ResponseCode;
  212. LogMessage(_T("IIS Response Code = %d"),ResponseCode);
  213. // Cleanup for retry
  214. }
  215. else
  216. {
  217. ErrorCode = 0;
  218. LogMessage(_T("IIS Response Code = %d"),ResponseCode);
  219. UploadSuccess = TRUE;
  220. }
  221. }
  222. else
  223. {
  224. LogMessage(_T("HttpEndrequest Returned an Error"));
  225. ErrorCode = GetLastError();
  226. }
  227. }
  228. }
  229. else
  230. {
  231. LogMessage(_T("Failed to allocate buffer memory"));
  232. ErrorCode = GetLastError();
  233. }
  234. }
  235. else
  236. {
  237. LogMessage(_T("Failed to Open Source File"));
  238. ErrorCode = GetLastError();
  239. }
  240. }
  241. else
  242. {
  243. LogMessage(_T("Failed to Create Put Request"));
  244. ErrorCode = GetLastError();
  245. }
  246. // Clean up
  247. if (hFile!= INVALID_HANDLE_VALUE)
  248. CloseHandle (hFile);
  249. if (hRequest)
  250. if (!InternetCloseHandle(hRequest))
  251. {
  252. LogMessage(_T("Failed to close the hRequest handle: error: %d"), GetLastError);
  253. }
  254. if (hConnect)
  255. if (!InternetCloseHandle(hConnect))
  256. {
  257. LogMessage(_T("Failed to close the hRequest handle: error: %d"), GetLastError);
  258. }
  259. if (hSession)
  260. if (!InternetCloseHandle(hSession))
  261. {
  262. LogMessage(_T("Failed to close the hRequest handle: error: %d"), GetLastError);
  263. }
  264. if (pUploadUrl)
  265. free (pUploadUrl);
  266. CoUninitialize();
  267. return ErrorCode;
  268. }
  269. DWORD GetResponseUrl(TCHAR * HostName, TCHAR *RemoteFileName,TCHAR *ResponseURL)
  270. {
  271. TCHAR IsapiUrl[255];
  272. TCHAR *pUploadUrl = NULL;
  273. TCHAR *temp = NULL;
  274. BOOL bRet = FALSE;
  275. DWORD dwUrlLength = 0;
  276. DWORD ErrorCode = 0;
  277. DWORD dwLastError = 0;
  278. TCHAR NewState;
  279. HINTERNET hSession;
  280. HINTERNET hRedirUrl;
  281. BOOL bOnce = FALSE;
  282. DWORD dwBuffLen = 0;
  283. DWORD dwFlags = 0;
  284. TCHAR LocalUrl[255];
  285. ZeroMemory (IsapiUrl, sizeof IsapiUrl);
  286. ZeroMemory (LocalUrl, sizeof LocalUrl);
  287. HINTERNET hUrlFile = NULL;
  288. StringCbPrintf (IsapiUrl,sizeof IsapiUrl, _T("http://%s/isapi/oca_extension.dll?id=%s&Type=5"),HostName, RemoteFileName);
  289. LogMessage(_T("Connecting to url: %s"),IsapiUrl);
  290. //StringCbPrintf (IsapiUrl,sizeof IsapiUrl, _T("http://www.microsoft.com"));
  291. hSession = InternetOpen(_T("Isapi Stress"),
  292. INTERNET_OPEN_TYPE_PRECONFIG,
  293. NULL,
  294. NULL,
  295. 0);
  296. if (hSession)
  297. {
  298. // Open the url we want to connect to.
  299. hUrlFile = InternetOpenUrl(hSession,
  300. IsapiUrl,
  301. NULL,
  302. 0,
  303. 0,
  304. 0);
  305. // Read the page returned by the isapi dll.
  306. TCHAR buffer[255] ;
  307. ZeroMemory (buffer, sizeof buffer);
  308. DWORD dwBytesRead = 0;
  309. BOOL bRead = InternetReadFile(hUrlFile,
  310. buffer,
  311. sizeof(buffer),
  312. &dwBytesRead);
  313. //buffer[sizeof (buffer) -1] = _T('\0');
  314. StringCbCopy (ResponseURL, sizeof buffer, buffer);
  315. }
  316. InternetCloseHandle(hUrlFile);
  317. InternetCloseHandle(hSession);
  318. return 0;
  319. }
  320. int __cdecl _tmain( int argc, TCHAR *argv[])
  321. {
  322. TCHAR SourceFileName[MAX_PATH];
  323. TCHAR SourcePath[MAX_PATH];
  324. TCHAR HostName[MAX_PATH];
  325. TCHAR VirtualDir[MAX_PATH];
  326. TCHAR LogPath[MAX_PATH];
  327. TCHAR temp;
  328. TCHAR RemoteFileName[MAX_PATH];
  329. DWORD ErrorCode = 0;
  330. TCHAR ResponseUrl[255];
  331. int i = 1;
  332. ZeroMemory (SourceFileName, sizeof SourceFileName);
  333. ZeroMemory (SourcePath, sizeof SourcePath);
  334. ZeroMemory (HostName, sizeof HostName);
  335. ZeroMemory (VirtualDir, sizeof VirtualDir);
  336. ZeroMemory (LogPath, sizeof LogPath);
  337. ZeroMemory (ResponseUrl,sizeof ResponseUrl);
  338. StringCbCopy (HostName, sizeof HostName, _T("Ocatest"));
  339. StringCbCopy (VirtualDir, sizeof VirtualDir, _T("OCA"));
  340. if ( (argc < 2) || (argc > 6))
  341. {
  342. Usage();
  343. return (1);
  344. }
  345. for (i = 1; i < argc; i++)
  346. {
  347. switch (*argv[i])
  348. {
  349. case _T('/'):
  350. case _T('-'):
  351. {
  352. temp = _toupper( *(argv[i]+1));
  353. switch (temp)
  354. {
  355. case _T('S'): // IIS Sever- Override Default
  356. if(*(argv[i]+2) == _T(':'))
  357. {
  358. StringCbCopy(HostName, sizeof HostName, argv[i]+3);
  359. }
  360. break;
  361. case _T('V'): // Virtual Directory override default
  362. if(*(argv[i]+2) == _T(':'))
  363. {
  364. StringCbCopy(VirtualDir, sizeof VirtualDir, argv[i]+3);
  365. }
  366. break;
  367. case _T('?'):
  368. Usage();
  369. break;
  370. case _T('D'):
  371. if(*(argv[i]+2) == _T(':'))
  372. {
  373. StringCbCopy(SourcePath, sizeof SourcePath, argv[i]+3);
  374. }
  375. break;
  376. case _T('F'): // User specified an offset in days...
  377. if(*(argv[i]+2) == _T(':'))
  378. {
  379. StringCbCopy(SourceFileName, sizeof SourceFileName, argv[i]+3);
  380. }
  381. break;
  382. case _T('L'):
  383. if(*(argv[i]+2) == _T(':'))
  384. {
  385. StringCbCopy(LogPath, sizeof LogPath, argv[i]+3);
  386. }
  387. break;
  388. default:
  389. LogMessage(_T("Unknown option: %s"),argv[i]);
  390. Usage();
  391. return (1);
  392. } // end switch
  393. } // end case
  394. break;
  395. default:
  396. LogMessage(_T("Unknown option: %s\n"),argv[i]);
  397. Usage();
  398. return (1);
  399. break;
  400. }// end switch
  401. }// end for
  402. // Ok now the fun part.
  403. // Create the log file if one was specified.
  404. if (LogPath[0] != _T('\0'))
  405. {
  406. hLogFile = CreateFile(LogPath,
  407. GENERIC_WRITE,
  408. FILE_SHARE_READ,
  409. NULL,
  410. OPEN_ALWAYS,
  411. FILE_ATTRIBUTE_NORMAL,
  412. NULL);
  413. if (hLogFile == INVALID_HANDLE_VALUE)
  414. {
  415. LogMessage(_T("Failed to open log file: %s, Logging disabled."), LogPath);
  416. }
  417. }
  418. // if we have a file name we just want to upload it.
  419. if (SourceFileName[0] != _T('\0'))
  420. {
  421. LogMessage(_T("Uploading file: %s"), SourceFileName);
  422. ErrorCode = Upload(SourceFileName, VirtualDir, HostName,RemoteFileName);
  423. if(ErrorCode != 0)
  424. {
  425. LogMessage(_T("Upload Failed: ErrorCode = %d"),ErrorCode);
  426. }
  427. else
  428. {
  429. LogMessage(_T("Upload Succeeded. "));
  430. }
  431. ErrorCode = GetResponseUrl(HostName,PathFindFileName(RemoteFileName),ResponseUrl);
  432. if (ErrorCode != 0)
  433. {
  434. LogMessage(_T("GetResponseUrl Failed ErrorCode=%d"),ErrorCode);
  435. }
  436. LogMessage(_T("ResponseUrl = %s"),ResponseUrl);
  437. }
  438. else
  439. {
  440. if (SourcePath[0] != _T('\0'))
  441. {
  442. // if we have a directory we want to walk the directory and submit all the cabs.
  443. ;
  444. }
  445. else
  446. {
  447. // If we don't have either we just exit with usage
  448. Usage();
  449. if (hLogFile != INVALID_HANDLE_VALUE)
  450. CloseHandle (hLogFile);
  451. return (1);
  452. }
  453. }
  454. if (hLogFile != INVALID_HANDLE_VALUE)
  455. CloseHandle (hLogFile);
  456. }