Source code of Windows XP (NT5)
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.

146 lines
3.7 KiB

  1. /*****************************************************************************\
  2. FILE: inetutil.cpp
  3. DESCRIPTION:
  4. These are wininet wrappers that fix the error values and wrap functionality.
  5. BryanSt 10/12/2000
  6. Copyright (C) Microsoft Corp 2000-2000. All rights reserved.
  7. \*****************************************************************************/
  8. #include "stock.h"
  9. #pragma hdrstop
  10. #include <wininet.h>
  11. ////////////////////////////////
  12. // Wininet/URL Helpers
  13. ////////////////////////////////
  14. STDAPI InternetReadFileWrap(HINTERNET hFile, LPVOID pvBuffer, DWORD dwNumberOfBytesToRead, LPDWORD pdwNumberOfBytesRead)
  15. {
  16. HRESULT hr = S_OK;
  17. DWORD dwError = 0;
  18. if (!InternetReadFile(hFile, pvBuffer, dwNumberOfBytesToRead, pdwNumberOfBytesRead))
  19. {
  20. dwError = GetLastError();
  21. hr = HRESULT_FROM_WIN32(dwError);
  22. }
  23. return hr;
  24. }
  25. #define SIZE_COPY_BUFFER (32 * 1024) // 32k
  26. STDAPI InternetReadIntoBSTR(HINTERNET hInternetRead, OUT BSTR * pbstrXML)
  27. {
  28. BYTE byteBuffer[SIZE_COPY_BUFFER];
  29. DWORD cbRead = SIZE_COPY_BUFFER;
  30. DWORD cchSize = 0;
  31. HRESULT hr = S_OK;
  32. *pbstrXML = NULL;
  33. while (SUCCEEDED(hr) && cbRead)
  34. {
  35. hr = InternetReadFileWrap(hInternetRead, byteBuffer, sizeof(byteBuffer), &cbRead);
  36. if (SUCCEEDED(hr) && cbRead)
  37. {
  38. BSTR bstrOld = *pbstrXML;
  39. BSTR bstrEnd;
  40. // The string may not be terminated.
  41. byteBuffer[cbRead] = 0;
  42. cchSize += ARRAYSIZE(byteBuffer);
  43. *pbstrXML = SysAllocStringLen(NULL, cchSize);
  44. if (*pbstrXML)
  45. {
  46. if (bstrOld)
  47. {
  48. StrCpy(*pbstrXML, bstrOld);
  49. }
  50. else
  51. {
  52. (*pbstrXML)[0] = 0;
  53. }
  54. bstrEnd = *pbstrXML + lstrlenW(*pbstrXML);
  55. SHAnsiToUnicode((LPCSTR) byteBuffer, bstrEnd, ARRAYSIZE(byteBuffer));
  56. }
  57. else
  58. {
  59. hr = E_OUTOFMEMORY;
  60. }
  61. SysFreeString(bstrOld);
  62. }
  63. }
  64. return hr;
  65. }
  66. STDAPI InternetOpenWrap(LPCTSTR pszAgent, DWORD dwAccessType, LPCTSTR pszProxy, LPCTSTR pszProxyBypass, DWORD dwFlags, HINTERNET * phFileHandle)
  67. {
  68. HRESULT hr = S_OK;
  69. DWORD dwError = 0;
  70. *phFileHandle = InternetOpen(pszAgent, dwAccessType, pszProxy, pszProxyBypass, dwFlags);
  71. if (!*phFileHandle)
  72. {
  73. dwError = GetLastError();
  74. hr = HRESULT_FROM_WIN32(dwError);
  75. }
  76. return hr;
  77. }
  78. STDAPI InternetOpenUrlWrap(HINTERNET hInternet, LPCTSTR pszUrl, LPCTSTR pszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext, HINTERNET * phFileHandle)
  79. {
  80. HRESULT hr = S_OK;
  81. DWORD dwError = 0;
  82. *phFileHandle = InternetOpenUrl(hInternet, pszUrl, pszHeaders, dwHeadersLength, dwFlags, dwContext);
  83. if (!*phFileHandle)
  84. {
  85. dwError = GetLastError();
  86. hr = HRESULT_FROM_WIN32(dwError);
  87. }
  88. return hr;
  89. }
  90. #define SZ_WININET_AGENT_FILEASSOC TEXT("Microsoft File Association Lookup")
  91. STDAPI DownloadUrl(LPCTSTR pszUrl, BSTR * pbstrXML)
  92. {
  93. HRESULT hr = E_OUTOFMEMORY;
  94. HINTERNET hInternetSession;
  95. hr = InternetOpenWrap(SZ_WININET_AGENT_FILEASSOC, PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, 0, &hInternetSession);
  96. if (SUCCEEDED(hr))
  97. {
  98. HINTERNET hOpenUrlSession;
  99. hr = InternetOpenUrlWrap(hInternetSession, pszUrl, NULL, 0, INTERNET_FLAG_NO_UI, NULL, &hOpenUrlSession);
  100. if (SUCCEEDED(hr))
  101. {
  102. hr = InternetReadIntoBSTR(hOpenUrlSession, pbstrXML);
  103. InternetCloseHandle(hOpenUrlSession);
  104. }
  105. InternetCloseHandle(hInternetSession);
  106. }
  107. return hr;
  108. }