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.

1254 lines
31 KiB

  1. /*******************************************************************
  2. *
  3. * DESCRIPTION:
  4. * Upload.cpp : Implements Class UploadFile to upload dumps to servers
  5. * AUTHOR:
  6. *
  7. * DATE:8/22/2002
  8. *
  9. *******************************************************************/
  10. #include <windows.h>
  11. #include <malloc.h>
  12. #include <strsafe.h>
  13. #include <unknwn.h>
  14. #include "ocaupld.h"
  15. BOOL
  16. OcaUpldCreate(POCA_UPLOADFILE* pUpload)
  17. {
  18. *pUpload = (POCA_UPLOADFILE) new UploadFile();
  19. return TRUE;
  20. }
  21. UploadFile::UploadFile()
  22. {
  23. m_szFile = NULL;
  24. m_Sent = NULL;
  25. m_Size = NULL;
  26. m_fInitialized = FALSE;
  27. m_hFile = NULL;
  28. m_hRequest = NULL;
  29. m_hSession = NULL;
  30. m_hConnect = NULL;
  31. m_hCancelEvent = NULL;
  32. m_Refs = 0;
  33. m_fLastUploadStatus = UploadNotStarted;
  34. CreateCancelEventObject();
  35. }
  36. UploadFile::~UploadFile()
  37. {
  38. UnInitialize();
  39. if (m_hCancelEvent)
  40. {
  41. CloseHandle(m_hCancelEvent);
  42. }
  43. }
  44. STDMETHODIMP
  45. UploadFile::QueryInterface(
  46. THIS_
  47. IN REFIID InterfaceId,
  48. OUT PVOID* Interface
  49. )
  50. {
  51. HRESULT Status;
  52. *Interface = NULL;
  53. Status = S_OK;
  54. if (IsEqualIID(InterfaceId, IID_IUnknown) ||
  55. IsEqualIID(InterfaceId, __uuidof(IOcaUploadFile)))
  56. {
  57. *Interface = (IOcaUploadFile *)this;
  58. AddRef();
  59. }
  60. else
  61. {
  62. Status = E_NOINTERFACE;
  63. }
  64. return Status;
  65. }
  66. STDMETHODIMP_(ULONG)
  67. UploadFile::AddRef(
  68. THIS
  69. )
  70. {
  71. return InterlockedIncrement((PLONG)&m_Refs);
  72. }
  73. STDMETHODIMP_(ULONG)
  74. UploadFile::Release(
  75. THIS
  76. )
  77. {
  78. LONG Refs = InterlockedDecrement((PLONG)&m_Refs);
  79. if (Refs == 0)
  80. {
  81. delete this;
  82. }
  83. return Refs;
  84. }
  85. HRESULT
  86. UploadFile::SetFileToSend(
  87. LPWSTR wszFileName
  88. )
  89. {
  90. HRESULT Hr;
  91. ULONG dwSizeHigh=0;
  92. if (m_hFile || m_fInitialized)
  93. {
  94. // Already in middle of upload
  95. return E_FAIL;
  96. }
  97. m_szFile = wszFileName;
  98. m_NumTries = 0;
  99. if (m_hCancelEvent == NULL)
  100. {
  101. CreateCancelEventObject();
  102. }
  103. m_hFile = CreateFile(m_szFile,
  104. GENERIC_READ,
  105. FILE_SHARE_READ,
  106. NULL,
  107. OPEN_EXISTING,
  108. FILE_ATTRIBUTE_NORMAL,
  109. NULL);
  110. if (m_hFile == NULL || m_hFile == INVALID_HANDLE_VALUE)
  111. {
  112. m_szFile = NULL;
  113. return E_FAIL;
  114. }
  115. m_Size = GetFileSize(m_hFile, &dwSizeHigh);
  116. m_Size += ((ULONG64) dwSizeHigh << 32);
  117. m_fLastUploadStatus = UploadNotStarted;
  118. m_wszLastUploadResult[0] = 0;
  119. m_fInitialized = TRUE;
  120. return S_OK;
  121. }
  122. HRESULT
  123. UploadFile::UnInitialize()
  124. {
  125. if (m_hFile)
  126. {
  127. CloseHandle(m_hFile);
  128. m_hFile = NULL;
  129. }
  130. CloseSession();
  131. m_fInitialized = FALSE;
  132. return S_OK;
  133. }
  134. //
  135. // Lookup registry entry to get default proxy settings from Internet Settings key
  136. //
  137. HRESULT
  138. UploadFile::GetProxySettings(
  139. LPWSTR wszServerName,
  140. ULONG ServerNameSIze,
  141. LPWSTR wszByPass,
  142. ULONG ByPassSize
  143. )
  144. {
  145. HKEY hkey, hkeySettings;
  146. const WCHAR c_wszInetSettings[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
  147. BOOL bProxyEnabled = FALSE;
  148. DWORD cb, err;
  149. if (RegOpenKeyExW(HKEY_CURRENT_USER, c_wszInetSettings, 0,
  150. KEY_READ | KEY_WOW64_64KEY, &hkey) == ERROR_SUCCESS)
  151. {
  152. cb = sizeof(bProxyEnabled);
  153. if ((err = RegQueryValueEx(hkey, L"ProxyEnable", NULL, NULL, (PBYTE)&bProxyEnabled,
  154. &cb)) != ERROR_SUCCESS)
  155. {
  156. RegCloseKey(hkey);
  157. return err;
  158. }
  159. if (bProxyEnabled)
  160. {
  161. cb = ServerNameSIze;
  162. if ((err = RegQueryValueEx(hkey, L"ProxyServer", NULL, NULL, (PBYTE)wszServerName,
  163. &cb)) != ERROR_SUCCESS)
  164. {
  165. wszServerName[0] = 0;
  166. }
  167. cb = ByPassSize;
  168. if ((err = RegQueryValueEx(hkey, L"ProxyOverride", NULL, NULL, (PBYTE)wszByPass,
  169. &cb)) != ERROR_SUCCESS)
  170. {
  171. wszByPass[0] = 0;
  172. }
  173. } else
  174. {
  175. wszServerName[0] = 0;
  176. wszByPass[0] = 0;
  177. }
  178. RegCloseKey(hkey);
  179. }
  180. return S_OK;
  181. }
  182. HRESULT
  183. UploadFile::GetSessionRedirUrl(
  184. LPWSTR SessionUrl,
  185. LPWSTR wszRedirUrl,
  186. ULONG SizeofRedirUrl
  187. )
  188. {
  189. HRESULT Hr;
  190. Hr = OpenSession();
  191. if (FAILED(Hr))
  192. {
  193. return Hr;
  194. }
  195. if (!GetRedirServerName(NULL, SessionUrl,
  196. NULL, 0,
  197. wszRedirUrl, SizeofRedirUrl))
  198. {
  199. CloseSession();
  200. Hr = GetLastError();
  201. return Hr;
  202. }
  203. return Hr;
  204. }
  205. //
  206. // Gets the data on the web page wszUrl. This should be used with pages with small data size only.
  207. //
  208. HRESULT
  209. UploadFile::GetUrlPageData(
  210. LPWSTR wszUrl,
  211. LPWSTR wszUrlPage,
  212. ULONG cbUrlPage
  213. )
  214. {
  215. HRESULT Hr;
  216. BOOL bRet;
  217. wchar_t ConnectString [255];
  218. HINTERNET hInet = NULL;
  219. HINTERNET hRedirUrl = NULL;
  220. HINTERNET hConnect = NULL;
  221. DWORD dwDownloaded , dwSize = 0;
  222. PCHAR Buffer;
  223. Hr = OpenSession();
  224. if (FAILED(Hr))
  225. {
  226. return Hr;
  227. }
  228. hConnect = WinHttpConnect(m_hSession, m_wszServerName, INTERNET_DEFAULT_HTTP_PORT, 0);
  229. if (hConnect == NULL)
  230. {
  231. goto exitPageData;
  232. }
  233. hRedirUrl = WinHttpOpenRequest(hConnect, L"GET", wszUrl, NULL, WINHTTP_NO_REFERER,
  234. WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
  235. if(!hRedirUrl)
  236. {
  237. goto exitPageData;
  238. }
  239. bRet = WinHttpSendRequest( hRedirUrl,
  240. WINHTTP_NO_ADDITIONAL_HEADERS, 0,
  241. WINHTTP_NO_REQUEST_DATA, 0,
  242. 0, 0);
  243. if (bRet)
  244. {
  245. bRet = WinHttpReceiveResponse(hRedirUrl, NULL);
  246. }
  247. if (!bRet)
  248. {
  249. goto exitPageData;
  250. }
  251. if (!WinHttpQueryDataAvailable( hRedirUrl, &dwSize))
  252. {
  253. goto exitPageData;
  254. }
  255. // Allocate space for the buffer.
  256. Buffer = (PCHAR) malloc(dwSize);
  257. if (!Buffer)
  258. {
  259. goto exitPageData;
  260. }
  261. if (dwSize >= cbUrlPage/sizeof(WCHAR))
  262. {
  263. // ::MessageBoxW(NULL,L"Failed pUploadurl memory allocation",NULL,MB_OK);
  264. SetLastError(STG_E_INSUFFICIENTMEMORY);
  265. free(Buffer);
  266. goto exitPageData;
  267. }
  268. else
  269. {
  270. ZeroMemory(Buffer, dwSize);
  271. // Read the Data.
  272. if (!WinHttpReadData( hRedirUrl, (LPVOID)Buffer,
  273. dwSize, &dwDownloaded))
  274. {
  275. free(Buffer);
  276. goto exitPageData;
  277. }
  278. ZeroMemory(wszUrlPage, cbUrlPage);
  279. if (!MultiByteToWideChar(CP_ACP, 0, Buffer, dwSize, wszUrlPage, cbUrlPage/sizeof(WCHAR)))
  280. {
  281. }
  282. free (Buffer);
  283. }
  284. CloseSession();
  285. exitPageData:
  286. Hr = GetLastError();
  287. return Hr;
  288. }
  289. HRESULT
  290. UploadFile::InitializeSession(
  291. LPWSTR OptionCode,
  292. LPWSTR wszFileToSend
  293. )
  294. {
  295. HRESULT Hr;
  296. Hr = SetFileToSend(wszFileToSend);
  297. if (FAILED(Hr))
  298. {
  299. return Hr;
  300. }
  301. m_fLastUploadStatus = UploadConnecting;
  302. m_dwConnectPercentage = 10;
  303. Hr = OpenSession();
  304. if (FAILED(Hr))
  305. {
  306. return Hr;
  307. }
  308. if (!GetRedirServerName(OptionCode, NULL,
  309. m_wszServerName, sizeof(m_wszServerName),
  310. NULL, 0))
  311. {
  312. CloseSession();
  313. Hr = GetLastError();
  314. return Hr;
  315. }
  316. return Hr;
  317. }
  318. HRESULT
  319. UploadFile::SendFile(
  320. LPWSTR wszRemoteFileName,
  321. BOOL bSecureMode
  322. )
  323. {
  324. HRESULT Hr;
  325. Hr = OpenConnection(wszRemoteFileName, bSecureMode);
  326. if (SUCCEEDED(Hr))
  327. {
  328. m_fLastUploadStatus = UploadTransferInProgress;
  329. Hr = StartSend();
  330. if (SUCCEEDED(Hr))
  331. {
  332. Hr = CompleteSend();
  333. }
  334. CloseConnection();
  335. }
  336. return Hr;
  337. }
  338. HRESULT
  339. UploadFile::OpenSession(
  340. void
  341. )
  342. {
  343. ULONG ErrorCode;
  344. WCHAR wszProxyServer[100], wszByPass[100];
  345. if (m_hSession != NULL)
  346. {
  347. return E_UNEXPECTED;
  348. }
  349. #ifdef _USE_WINHTTP
  350. wszByPass[0] = wszProxyServer[0] = 0;
  351. GetProxySettings(wszProxyServer, sizeof(wszProxyServer),
  352. wszByPass, sizeof(wszByPass));
  353. if (wszProxyServer[0])
  354. {
  355. m_hSession = WinHttpOpen(L"OCARPT Control",
  356. WINHTTP_ACCESS_TYPE_NAMED_PROXY,
  357. wszProxyServer, wszByPass,
  358. //L"itgproxy:80", L"<local>",
  359. 0);
  360. } else
  361. {
  362. m_hSession = WinHttpOpen(L"OCARPT Control",
  363. WINHTTP_ACCESS_TYPE_NO_PROXY,
  364. WINHTTP_NO_PROXY_NAME,
  365. WINHTTP_NO_PROXY_BYPASS,
  366. // L"itgproxy:80", L"<local>",
  367. 0);
  368. }
  369. #else
  370. m_hSession = InternetOpenW(L"OCARPT Control", INTERNET_OPEN_TYPE_PRECONFIG,
  371. NULL, NULL, 0);
  372. InetCheckTimeouts(m_hSession);
  373. if (InternetSetStatusCallback(hSession, (INTERNET_STATUS_CALLBACK) InetCallback)
  374. == INTERNET_INVALID_STATUS_CALLBACK)
  375. {
  376. // Not significant if this fails
  377. }
  378. #endif
  379. if (!m_hSession)
  380. {
  381. ErrorCode = GetLastError();
  382. return ErrorCode;
  383. }
  384. return S_OK;
  385. }
  386. HRESULT
  387. UploadFile::OpenConnection(
  388. LPWSTR wszRemoteFileName,
  389. BOOL bSecureMode
  390. )
  391. {
  392. #ifndef _USE_WINHTTP
  393. INTERNET_BUFFERSW BufferInW = {0};
  394. #endif
  395. static const wchar_t *pszAccept[] = {L"*.*", 0};
  396. if (m_hSession == NULL)
  397. {
  398. return E_UNEXPECTED;
  399. }
  400. if (m_hFile == NULL || m_hFile == INVALID_HANDLE_VALUE)
  401. {
  402. return E_UNEXPECTED;
  403. }
  404. if (m_NumTries > 0)
  405. {
  406. // m_Size /= 2;
  407. }
  408. // m_NumTries++;
  409. if (!m_hConnect)
  410. {
  411. #ifdef _USE_WINHTTP
  412. m_hConnect = WinHttpConnect(m_hSession,
  413. m_wszServerName,
  414. (bSecureMode ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT),
  415. 0);
  416. #else
  417. m_hConnect = InternetConnectW(m_hSession,
  418. (wchar_t *)m_wszServerName,
  419. (bSecureMode ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT),
  420. L"",
  421. L"",
  422. INTERNET_SERVICE_HTTP,
  423. 0,
  424. NULL);
  425. #endif // _USE_WINHTTP
  426. if (!m_hConnect)
  427. {
  428. return GetLastError();
  429. }
  430. }
  431. if (!m_hRequest)
  432. {
  433. #ifdef _USE_WINHTTP
  434. m_hRequest = WinHttpOpenRequest(m_hConnect,
  435. L"PUT",
  436. wszRemoteFileName,
  437. NULL,
  438. WINHTTP_NO_REFERER,
  439. pszAccept, //WINHTTP_DEFAULT_ACCEPT_TYPES,
  440. (bSecureMode ? WINHTTP_FLAG_SECURE : 0) | WINHTTP_FLAG_REFRESH);
  441. #else
  442. m_hRequest = HttpOpenRequestW(m_hConnect,
  443. L"PUT",
  444. wszRemoteFileName,
  445. NULL,
  446. NULL,
  447. pszAccept,
  448. INTERNET_FLAG_NEED_FILE|
  449. INTERNET_FLAG_NO_CACHE_WRITE |
  450. (bSecureMode ? INTERNET_FLAG_SECURE : 0),
  451. 0);
  452. #endif // _USE_WINHTTP
  453. if (!m_hRequest)
  454. {
  455. // ::MessageBoxW(NULL,L"Put request failed ",NULL,MB_OK);
  456. return GetLastError();
  457. }
  458. }
  459. // Clear the buffer
  460. #ifdef _USE_WINHTTP
  461. if (!WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
  462. WINHTTP_NO_REQUEST_DATA, 0,
  463. (ULONG) m_Size, 0))
  464. #else
  465. InetCheckTimeouts(m_hRequest);
  466. BufferInW.dwStructSize = sizeof( INTERNET_BUFFERSW );
  467. BufferInW.Next = NULL;
  468. BufferInW.lpcszHeader = NULL;
  469. BufferInW.dwHeadersLength = 0;
  470. BufferInW.dwHeadersTotal = 0;
  471. BufferInW.lpvBuffer = NULL;
  472. BufferInW.dwBufferLength = 0;
  473. BufferInW.dwOffsetLow = 0;
  474. BufferInW.dwOffsetHigh = 0;
  475. BufferInW.dwBufferTotal = (ULONG) m_Size;
  476. if(!HttpSendRequestExW( m_hRequest, &BufferInW, NULL, 0, 0))
  477. #endif
  478. {
  479. // ::MessageBoxW(NULL,L"Failed to send request ",NULL,MB_OK);
  480. return GetLastError();
  481. }
  482. return S_OK;
  483. }
  484. HRESULT
  485. UploadFile::CompleteSend()
  486. {
  487. BOOL bRet;
  488. ULONG ResLength;
  489. ULONG ResponseCode;
  490. WCHAR Text[500];
  491. ULONG index;
  492. ULONG ErrorCode = 0;
  493. #ifdef _USE_WINHTTP
  494. bRet = WinHttpReceiveResponse(m_hRequest, NULL);
  495. #else
  496. bRet = HttpEndRequest(m_hRequest, NULL, 0, 0);
  497. #endif
  498. if (!bRet)
  499. {
  500. // ::MessageBoxW(NULL,L"End RequestFailed ",NULL,MB_OK);
  501. ErrorCode = GetLastError();
  502. }
  503. else
  504. {
  505. ResLength = sizeof(ResponseCode);
  506. ResponseCode = 0;
  507. #ifdef _USE_WINHTTP
  508. WinHttpQueryHeaders(m_hRequest,
  509. WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
  510. WINHTTP_HEADER_NAME_BY_INDEX,
  511. &ResponseCode,
  512. &ResLength,
  513. WINHTTP_NO_HEADER_INDEX);
  514. ResLength = sizeof(Text);
  515. WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_STATUS_TEXT,
  516. WINHTTP_HEADER_NAME_BY_INDEX,
  517. Text, &ResLength, &index);
  518. if ( ResponseCode >= 400 )
  519. #else
  520. HttpQueryInfo(m_hRequest,
  521. HTTP_QUERY_STATUS_CODE |HTTP_QUERY_FLAG_NUMBER,
  522. &ResponseCode,
  523. &ResLength,
  524. &index);
  525. ResLength = sizeof(Text);
  526. HttpQueryInfo(m_hRequest, HTTP_QUERY_STATUS_TEXT,
  527. Text, &ResLength, &index);
  528. if ( (ResponseCode != HTTP_STATUS_CREATED) &&
  529. (ResponseCode != HTTP_STATUS_OK))
  530. #endif
  531. {
  532. ::MessageBoxW(NULL,Text,NULL,MB_OK);
  533. ErrorCode= ResponseCode;
  534. // Cleanup for retry
  535. // InternetCloseHandle(hRequest); hRequest = NULL;
  536. // InternetCloseHandle(hConnect); hConnect = NULL;
  537. }
  538. else
  539. {
  540. ErrorCode = 0;
  541. }
  542. }
  543. return ErrorCode;
  544. }
  545. HRESULT
  546. UploadFile::CloseConnection()
  547. {
  548. #ifdef _USE_WINHTTP
  549. if (m_hConnect) WinHttpCloseHandle(m_hConnect);
  550. if (m_hRequest) WinHttpCloseHandle(m_hRequest);
  551. #else
  552. if (m_hConnect) InternetCloseHandle(m_hConnect);
  553. if (m_hRequest) InternetCloseHandle(m_hRequest);
  554. #endif
  555. m_hRequest = NULL;
  556. m_hConnect = NULL;
  557. return S_OK;
  558. }
  559. HRESULT
  560. UploadFile::CloseSession()
  561. {
  562. if (m_hConnect || m_hRequest)
  563. {
  564. CloseConnection();
  565. }
  566. #ifdef _USE_WINHTTP
  567. if (m_hSession)
  568. WinHttpCloseHandle(m_hSession);
  569. #else
  570. if (m_hSession)
  571. InternetCloseHandle(m_hSession);
  572. #endif // _USE_WINHTTP
  573. m_hSession = NULL;
  574. return S_OK;
  575. }
  576. BOOL
  577. UploadFile::GetUploadResult(
  578. LPTSTR Result,
  579. ULONG cbResult
  580. )
  581. {
  582. if (Result)
  583. {
  584. if (m_fLastUploadStatus == UploadTransferInProgress)
  585. {
  586. StringCbPrintfW(Result, cbResult, L"Transferring to server (%ld KB) ...",
  587. m_Size / 1024);
  588. return TRUE;
  589. } else if (m_fLastUploadStatus == UploadConnecting)
  590. {
  591. StringCbPrintfW(Result, cbResult, L"Connecting to server ...");
  592. return TRUE;
  593. }
  594. StringCbCopyW(Result, cbResult, m_wszLastUploadResult);
  595. }
  596. return m_wszLastUploadResult[0] != 0;
  597. }
  598. HRESULT
  599. UploadFile::SetUploadResult(
  600. EnumUploadStatus Success,
  601. LPCTSTR Text
  602. )
  603. {
  604. m_fLastUploadStatus = Success;
  605. return StringCbCopyW(m_wszLastUploadResult, sizeof(m_wszLastUploadResult),
  606. Text);
  607. }
  608. HRESULT
  609. UploadFile::StartSend()
  610. {
  611. HRESULT Hr = S_OK;
  612. BYTE *pBuffer;
  613. BOOL bRet;
  614. DWORD dwBytesWritten, dwBytesRead = 0;
  615. #define MAX_SEND_SIZE 50000
  616. if (m_hFile == NULL || m_hRequest == NULL)
  617. {
  618. return E_UNEXPECTED;
  619. }
  620. if (m_hCancelEvent)
  621. {
  622. ResetEvent(m_hCancelEvent);
  623. }
  624. // Get the buffer memory from the heap
  625. if ( (pBuffer = (BYTE *)malloc (MAX_SEND_SIZE)) == NULL)
  626. {
  627. // ::MessageBoxW(NULL,L"Failed Memory allocation",NULL,MB_OK);
  628. return E_OUTOFMEMORY;
  629. }
  630. SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN);
  631. m_fLastUploadStatus = UploadTransferInProgress;
  632. m_Sent = 0;
  633. do
  634. {
  635. bRet = ReadFile(m_hFile, pBuffer, MAX_SEND_SIZE, &dwBytesRead, NULL);
  636. if (bRet != 0)
  637. {
  638. if (dwBytesRead > (ULONG)(m_Size - m_Sent))
  639. {
  640. dwBytesRead = (ULONG)(m_Size - m_Sent);
  641. }
  642. #ifdef _USE_WINHTTP
  643. bRet = WinHttpWriteData(m_hRequest,
  644. pBuffer,
  645. dwBytesRead,
  646. &dwBytesWritten);
  647. #else
  648. bRet = InternetWriteFile(m_hRequest,
  649. pBuffer,
  650. dwBytesRead,
  651. &dwBytesWritten);
  652. #endif
  653. if ( (!bRet) || (dwBytesWritten==0) )
  654. {
  655. // ::MessageBoxW(NULL,L"Failed write ",NULL,MB_OK);
  656. Hr = HRESULT_FROM_NT (GetLastError());
  657. break;
  658. }
  659. m_Sent += dwBytesWritten;
  660. if (CheckCancelRequest())
  661. {
  662. // Send was aborted
  663. Hr = E_ABORT;
  664. break;
  665. }
  666. } else
  667. {
  668. Hr = E_FAIL;
  669. break;
  670. }
  671. } while (dwBytesRead == MAX_SEND_SIZE && m_Sent < m_Size);
  672. if (Hr == S_OK)
  673. {
  674. m_fLastUploadStatus = UploadSucceded;
  675. }
  676. free (pBuffer);
  677. return Hr;
  678. }
  679. HRESULT
  680. UploadFile::Cancel()
  681. {
  682. FireCancelEvent();
  683. m_fLastUploadStatus = UploadFailure;
  684. return S_OK;
  685. }
  686. HRESULT
  687. UploadFile::CreateCancelEventObject()
  688. {
  689. m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE,
  690. L"OCA_CancelUpload");
  691. if (m_hCancelEvent == NULL)
  692. {
  693. return E_FAIL;
  694. }
  695. return S_OK;
  696. }
  697. BOOL
  698. UploadFile::FireCancelEvent()
  699. {
  700. if (m_hCancelEvent)
  701. {
  702. return SetEvent(m_hCancelEvent);
  703. }
  704. return FALSE;
  705. }
  706. ULONG
  707. UploadFile::GetPercentComplete()
  708. {
  709. if (m_fLastUploadStatus == UploadSucceded)
  710. {
  711. return 101;
  712. }
  713. else if (m_fLastUploadStatus == UploadFailure)
  714. {
  715. return -1;
  716. } else if (m_fLastUploadStatus == UploadNotStarted)
  717. {
  718. return 0;
  719. } else if (m_fLastUploadStatus == UploadConnecting)
  720. {
  721. return m_dwConnectPercentage;
  722. } else if (m_fLastUploadStatus == UploadTransferInProgress)
  723. {
  724. return (ULONG) ((m_Sent * 100) / m_Size);
  725. }
  726. return 0;
  727. }
  728. BOOL
  729. UploadFile::IsUploadInProgress()
  730. {
  731. if (m_fLastUploadStatus == UploadNotStarted ||
  732. m_fLastUploadStatus == UploadSucceded ||
  733. m_fLastUploadStatus == UploadFailure)
  734. {
  735. return FALSE;
  736. }
  737. return m_fInitialized;
  738. }
  739. BOOL
  740. UploadFile::CheckCancelRequest()
  741. {
  742. DWORD dwWait;
  743. if (m_hCancelEvent)
  744. {
  745. if ((dwWait = WaitForSingleObject(m_hCancelEvent, 1)) == WAIT_TIMEOUT)
  746. {
  747. return FALSE;
  748. } else if (dwWait == WAIT_OBJECT_0)
  749. {
  750. return TRUE;
  751. }
  752. }
  753. return FALSE;
  754. }
  755. void
  756. InetCheckTimeouts(
  757. HINTERNET hInet
  758. )
  759. {
  760. ULONG dwConnectTimeout;
  761. ULONG dwSendTimeOut;
  762. ULONG dwSize;
  763. dwSize = sizeof(ULONG);
  764. #ifndef _USE_WINHTTP
  765. if (!InternetQueryOption(hInet,
  766. INTERNET_OPTION_DATA_SEND_TIMEOUT,
  767. &dwSendTimeOut,
  768. &dwSize))
  769. {
  770. dwSendTimeOut = 0;
  771. }
  772. if (!InternetQueryOption(hInet,
  773. INTERNET_OPTION_CONNECT_TIMEOUT,
  774. &dwConnectTimeout,
  775. &dwSize))
  776. {
  777. dwConnectTimeout = 0;
  778. }
  779. if (dwSendTimeOut != 0)
  780. {
  781. dwSendTimeOut *= 4;
  782. InternetSetOption(hInet, INTERNET_OPTION_DATA_SEND_TIMEOUT,
  783. &dwSendTimeOut, dwSize);
  784. }
  785. if (dwConnectTimeout != 0)
  786. {
  787. dwConnectTimeout *= 4;
  788. InternetSetOption(hInet, INTERNET_OPTION_CONNECT_TIMEOUT,
  789. &dwConnectTimeout, dwSize);
  790. }
  791. #endif // _USE_WINHTTP
  792. }
  793. /**********************************************************************************
  794. *
  795. * Callback for WinInet APIs
  796. *
  797. **********************************************************************************/
  798. void
  799. __stdcall InetCallback(
  800. HINTERNET hInet,
  801. DWORD dwContext,
  802. DWORD dwInetStatus,
  803. LPVOID lpvStatusInfo,
  804. DWORD dwStatusInfoLength
  805. )
  806. {
  807. #ifndef _USE_WINHTTP
  808. switch (dwInetStatus)
  809. {
  810. case 0:
  811. dwInetStatus = 2;
  812. break;
  813. default:
  814. break;
  815. }
  816. #endif
  817. return;
  818. }
  819. BOOL
  820. LocalCrackUrlServer(
  821. LPWSTR lpwszUrlName,
  822. DWORD dwUrlLength,
  823. DWORD Flags,
  824. LPWSTR *lpServerName
  825. )
  826. {
  827. URL_COMPONENTSW urlComponents;
  828. BOOL bRet = FALSE;
  829. DWORD dwLastError;
  830. ZeroMemory(&urlComponents, sizeof(URL_COMPONENTSW));
  831. urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
  832. urlComponents.lpszHostName = NULL;
  833. urlComponents.dwHostNameLength = 512;
  834. urlComponents.lpszHostName = (wchar_t*)malloc(urlComponents.dwHostNameLength );
  835. if(!urlComponents.lpszHostName)
  836. {
  837. return FALSE;
  838. }
  839. do
  840. {
  841. ZeroMemory(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
  842. #ifdef _USE_WINHTTP
  843. bRet = WinHttpCrackUrl(lpwszUrlName, dwUrlLength, 0, &urlComponents);
  844. #else
  845. bRet = InternetCrackUrlW(lpwszUrlName, dwUrlLength, 0, &urlComponents);
  846. #endif
  847. if(!bRet)
  848. {
  849. dwLastError = GetLastError();
  850. // If last error was due to insufficient buffer size, create a new one the correct size.
  851. if(dwLastError == ERROR_INSUFFICIENT_BUFFER)
  852. {
  853. free(urlComponents.lpszHostName);
  854. urlComponents.lpszHostName = (wchar_t*)malloc(urlComponents.dwHostNameLength);
  855. if(!urlComponents.lpszHostName)
  856. {
  857. return FALSE;
  858. }
  859. }
  860. else
  861. {
  862. return FALSE;
  863. }
  864. }
  865. }while(!bRet);
  866. *lpServerName = urlComponents.lpszHostName;
  867. return TRUE;
  868. }
  869. LPWSTR
  870. UploadFile::GetServerName()
  871. {
  872. return m_wszServerName;
  873. }
  874. BOOL
  875. UploadFile::GetRedirServerName(
  876. LPWSTR OptionCode,
  877. LPWSTR ConnectUrl,
  878. LPWSTR lpwszServerName,
  879. ULONG dwServerNameLength,
  880. LPWSTR lpwszUrl,
  881. ULONG dwUrlLength
  882. )
  883. {
  884. wchar_t ConnectString [255];
  885. HINTERNET hInet = NULL;
  886. HINTERNET hRedirUrl = NULL;
  887. HINTERNET hConnect = NULL;
  888. wchar_t* pUploadUrl = NULL;
  889. ULONG dwUploadUrlLength;
  890. URL_COMPONENTSW urlComponents;
  891. BOOL bRet;
  892. DWORD dwLastError;
  893. LPWSTR lpszHostName = NULL;
  894. #ifdef _USE_WINHTTP
  895. if (ConnectUrl == NULL)
  896. {
  897. if (StringCbPrintfW(ConnectString,
  898. sizeof (ConnectString),
  899. L"/fwlink/?linkid=%s",
  900. OptionCode
  901. // L"908" // LIVE SITE
  902. ) != S_OK)
  903. {
  904. goto exitServerName;
  905. }
  906. hConnect = WinHttpConnect(m_hSession, L"go.microsoft.com", INTERNET_DEFAULT_HTTP_PORT, 0);
  907. if (hConnect == NULL)
  908. {
  909. goto exitServerName;
  910. }
  911. } else
  912. {
  913. hConnect = WinHttpConnect(m_hSession, m_wszServerName, INTERNET_DEFAULT_HTTP_PORT, 0);
  914. if (hConnect == NULL)
  915. {
  916. goto exitServerName;
  917. }
  918. if (StringCbCopyW(ConnectString, sizeof(ConnectString), ConnectUrl) != S_OK)
  919. {
  920. goto exitServerName;
  921. }
  922. }
  923. hRedirUrl = WinHttpOpenRequest(hConnect, L"GET", ConnectString, NULL, WINHTTP_NO_REFERER,
  924. WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
  925. #else
  926. if (ConnectUrl == NULL)
  927. {
  928. if (StringCbPrintfW(ConnectString,
  929. sizeof (ConnectString),
  930. L"http://go.microsoft.com/fwlink/?linkid=%s",
  931. OptionCode
  932. // L"908" // LIVE SITE
  933. ) != S_OK)
  934. {
  935. goto exitServerName;
  936. }
  937. } else
  938. {
  939. if (StringCbCopyW(ConnectString, sizeof(ConnectString), ConnectUrl) != S_OK)
  940. {
  941. goto exitServerName;
  942. }
  943. }
  944. hRedirUrl = InternetOpenUrlW(m_hSession, ConnectString, NULL, 0, 0, 0);
  945. #endif
  946. if(!hRedirUrl)
  947. {
  948. goto exitServerName;
  949. }
  950. #ifdef _USE_WINHTTP
  951. bRet = WinHttpSendRequest( hRedirUrl,
  952. WINHTTP_NO_ADDITIONAL_HEADERS, 0,
  953. WINHTTP_NO_REQUEST_DATA, 0,
  954. 0, 0);
  955. if (bRet)
  956. {
  957. bRet = WinHttpReceiveResponse(hRedirUrl, NULL);
  958. }
  959. if (!bRet)
  960. {
  961. goto exitServerName;
  962. }
  963. #endif
  964. // Get the URL returned from the MS Corporate IIS redir.dll isapi URL redirector
  965. dwUploadUrlLength = 512;
  966. pUploadUrl = (wchar_t*)malloc(dwUploadUrlLength);
  967. if(!pUploadUrl)
  968. {
  969. // ::MessageBoxW(NULL,L"Failed pUploadurl memory allocation",NULL,MB_OK);
  970. goto exitServerName;
  971. return FALSE;
  972. }
  973. do
  974. {
  975. ZeroMemory(pUploadUrl, dwUploadUrlLength);
  976. #ifdef _USE_WINHTTP
  977. bRet = WinHttpQueryOption(hRedirUrl, WINHTTP_OPTION_URL, pUploadUrl, &dwUploadUrlLength);
  978. #else
  979. bRet = InternetQueryOptionW(hRedirUrl, INTERNET_OPTION_URL, pUploadUrl, &dwUploadUrlLength);
  980. #endif
  981. if(!bRet)
  982. {
  983. dwLastError = GetLastError();
  984. // If last error was due to insufficient buffer size, create a new one the correct size.
  985. if(dwLastError == ERROR_INSUFFICIENT_BUFFER)
  986. {
  987. if (pUploadUrl)
  988. {
  989. free(pUploadUrl);
  990. pUploadUrl = NULL;
  991. }
  992. pUploadUrl = (wchar_t*)malloc(dwUploadUrlLength);
  993. if(!pUploadUrl)
  994. {
  995. goto exitServerName;
  996. }
  997. }
  998. else
  999. {
  1000. goto exitServerName;
  1001. }
  1002. }
  1003. m_dwConnectPercentage+=10;
  1004. }while(!bRet);
  1005. // Strip out the host name from the URL
  1006. ZeroMemory(&urlComponents, sizeof(URL_COMPONENTSW));
  1007. urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
  1008. urlComponents.lpszHostName = NULL;
  1009. urlComponents.dwHostNameLength = 512;
  1010. urlComponents.lpszHostName = (wchar_t*)malloc(urlComponents.dwHostNameLength );
  1011. if(!urlComponents.lpszHostName)
  1012. {
  1013. goto exitServerName;;
  1014. }
  1015. do
  1016. {
  1017. ZeroMemory(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
  1018. #ifdef _USE_WINHTTP
  1019. bRet = WinHttpCrackUrl(pUploadUrl, dwUploadUrlLength, 0, &urlComponents);
  1020. #else
  1021. bRet = InternetCrackUrlW(pUploadUrl, dwUploadUrlLength, 0, &urlComponents);
  1022. #endif
  1023. if(!bRet)
  1024. {
  1025. dwLastError = GetLastError();
  1026. // If last error was due to insufficient buffer size, create a new one the correct size.
  1027. if(dwLastError == ERROR_INSUFFICIENT_BUFFER)
  1028. {
  1029. free(urlComponents.lpszHostName);
  1030. urlComponents.lpszHostName = (wchar_t*)malloc(urlComponents.dwHostNameLength);
  1031. if(!urlComponents.lpszHostName)
  1032. {
  1033. goto exitServerName;
  1034. }
  1035. }
  1036. else
  1037. {
  1038. goto exitServerName;
  1039. }
  1040. }
  1041. }while(!bRet);
  1042. bRet = TRUE;
  1043. if (lpwszServerName)
  1044. {
  1045. if (!_wcsicmp(OptionCode, L"908"))
  1046. {
  1047. if (StringCbCopyW(lpwszServerName, dwServerNameLength,
  1048. L"redbgitwb10"
  1049. ) != S_OK)
  1050. {
  1051. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  1052. bRet = FALSE;
  1053. }
  1054. } else
  1055. if (StringCbCopyW(lpwszServerName, dwServerNameLength,
  1056. //L"tkbgitwb15"
  1057. //L"tkbgitwb16"
  1058. L"redbgitwb10"
  1059. //L"redbgitwb11"
  1060. //L"tkbgitwb18"
  1061. //L"oca.microsoft.com"
  1062. //urlComponents.lpszHostName
  1063. ) != S_OK)
  1064. {
  1065. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  1066. bRet = FALSE;
  1067. }
  1068. }
  1069. if (lpwszUrl)
  1070. {
  1071. if (StringCbCopyW(lpwszUrl, dwUrlLength, pUploadUrl) != S_OK)
  1072. {
  1073. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  1074. bRet = FALSE;
  1075. }
  1076. }
  1077. exitServerName:
  1078. if (pUploadUrl)
  1079. {
  1080. free (pUploadUrl);
  1081. }
  1082. if (urlComponents.lpszHostName)
  1083. {
  1084. free (urlComponents.lpszHostName);
  1085. }
  1086. if (hRedirUrl)
  1087. {
  1088. #ifdef _USE_WINHTTP
  1089. if (hConnect)
  1090. WinHttpCloseHandle(hConnect);
  1091. WinHttpCloseHandle(hRedirUrl);
  1092. #else
  1093. InternetCloseHandle(hRedirUrl);
  1094. #endif
  1095. }
  1096. return bRet;
  1097. }