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.

286 lines
7.3 KiB

  1. // --------------------------------------------------------------------------------
  2. // WebDocs.cpp
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // --------------------------------------------------------------------------------
  5. #include "pch.hxx"
  6. #include "webdocs.h"
  7. #ifdef MAC
  8. #include "urlhlink.h"
  9. #else // !MAC
  10. #include "urlmon.h"
  11. #endif // !MAC
  12. #include "stmlock.h"
  13. #ifndef MAC
  14. #include "wininet.h"
  15. #include "winineti.h"
  16. #endif // !MAC
  17. #include <demand.h>
  18. #ifndef MAC
  19. HRESULT HrOpenUrlViaCache(LPCSTR pszUrl, LPSTREAM *ppstm);
  20. #endif // !MAC
  21. // --------------------------------------------------------------------------------
  22. // CMimeWebDocument::CMimeWebDocument
  23. // --------------------------------------------------------------------------------
  24. CMimeWebDocument::CMimeWebDocument(void)
  25. {
  26. m_cRef = 1;
  27. m_pszBase = NULL;
  28. m_pszURL = NULL;
  29. InitializeCriticalSection(&m_cs);
  30. }
  31. // --------------------------------------------------------------------------------
  32. // CMimeWebDocument::CMimeWebDocument
  33. // --------------------------------------------------------------------------------
  34. CMimeWebDocument::~CMimeWebDocument(void)
  35. {
  36. EnterCriticalSection(&m_cs);
  37. SafeMemFree(m_pszBase);
  38. SafeMemFree(m_pszURL);
  39. LeaveCriticalSection(&m_cs);
  40. DeleteCriticalSection(&m_cs);
  41. }
  42. // --------------------------------------------------------------------------------
  43. // CMimeWebDocument::QueryInterface
  44. // --------------------------------------------------------------------------------
  45. STDMETHODIMP CMimeWebDocument::QueryInterface(REFIID riid, LPVOID *ppv)
  46. {
  47. // Locals
  48. HRESULT hr=S_OK;
  49. // check params
  50. if (ppv == NULL)
  51. return TrapError(E_INVALIDARG);
  52. // Find IID
  53. if (IID_IUnknown == riid)
  54. *ppv = (IUnknown *)this;
  55. else if (IID_IMimeWebDocument == riid)
  56. *ppv = (IMimeWebDocument *)this;
  57. else
  58. {
  59. *ppv = NULL;
  60. hr = TrapError(E_NOINTERFACE);
  61. goto exit;
  62. }
  63. // AddRef It
  64. ((IUnknown *)*ppv)->AddRef();
  65. exit:
  66. // Done
  67. return hr;
  68. }
  69. // --------------------------------------------------------------------------------
  70. // CMimeWebDocument::AddRef
  71. // --------------------------------------------------------------------------------
  72. STDMETHODIMP_(ULONG) CMimeWebDocument::AddRef(void)
  73. {
  74. return (ULONG)InterlockedIncrement(&m_cRef);
  75. }
  76. // --------------------------------------------------------------------------------
  77. // CMimeWebDocument::Release
  78. // --------------------------------------------------------------------------------
  79. STDMETHODIMP_(ULONG) CMimeWebDocument::Release(void)
  80. {
  81. LONG cRef = InterlockedDecrement(&m_cRef);
  82. if (0 == cRef)
  83. delete this;
  84. return (ULONG)cRef;
  85. }
  86. // --------------------------------------------------------------------------------
  87. // CMimeWebDocument::GetURL
  88. // --------------------------------------------------------------------------------
  89. STDMETHODIMP CMimeWebDocument::GetURL(LPSTR *ppszURL)
  90. {
  91. // Locals
  92. HRESULT hr=S_OK;
  93. // Invalid Arg
  94. if (NULL == ppszURL)
  95. return TrapError(E_INVALIDARG);
  96. // Thread Safety
  97. EnterCriticalSection(&m_cs);
  98. // No Data
  99. if (NULL == m_pszURL)
  100. {
  101. hr = TrapError(MIME_E_NO_DATA);
  102. goto exit;
  103. }
  104. // Combine the URL
  105. if (m_pszBase)
  106. {
  107. // Combine
  108. CHECKHR(hr = MimeOleCombineURL(m_pszBase, lstrlen(m_pszBase), m_pszURL, lstrlen(m_pszURL), FALSE, ppszURL));
  109. }
  110. // Otherwise, just dup m_pszURL
  111. else
  112. {
  113. // Dup It
  114. CHECKALLOC(*ppszURL = PszDupA(m_pszURL));
  115. }
  116. exit:
  117. // Thread Safety
  118. LeaveCriticalSection(&m_cs);
  119. // Done
  120. return hr;
  121. }
  122. // --------------------------------------------------------------------------------
  123. // CMimeWebDocument::BindToStorage
  124. // --------------------------------------------------------------------------------
  125. STDMETHODIMP CMimeWebDocument::BindToStorage(REFIID riid, LPVOID *ppvObject)
  126. {
  127. // Locals
  128. HRESULT hr=S_OK;
  129. LPSTR pszURL=NULL;
  130. IStream *pStream=NULL;
  131. ILockBytes *pLockBytes=NULL;
  132. // Invalid Arg
  133. if (NULL == ppvObject || (IID_IStream != riid && IID_ILockBytes != riid))
  134. return TrapError(E_INVALIDARG);
  135. // Init
  136. *ppvObject = NULL;
  137. // Thread Safety
  138. EnterCriticalSection(&m_cs);
  139. // No Data
  140. if (NULL == m_pszURL)
  141. {
  142. hr = TrapError(MIME_E_NO_DATA);
  143. goto exit;
  144. }
  145. #ifndef MAC
  146. // Combine the URL
  147. if (m_pszBase)
  148. {
  149. // Combine
  150. CHECKHR(hr = MimeOleCombineURL(m_pszBase, lstrlen(m_pszBase), m_pszURL, lstrlen(m_pszURL), FALSE, &pszURL));
  151. // Get the Stream
  152. CHECKHR(hr = HrOpenUrlViaCache(pszURL, &pStream));
  153. }
  154. // Otherwise, just dup m_pszURL
  155. else
  156. {
  157. // Get the Stream
  158. CHECKHR(hr = HrOpenUrlViaCache(m_pszURL, &pStream));
  159. }
  160. #endif // !MAC
  161. // User wants ILockBytes
  162. if (IID_ILockBytes == riid)
  163. {
  164. // Create CStreamLockBytes
  165. CHECKALLOC(pLockBytes = new CStreamLockBytes(pStream));
  166. // Add Ref It
  167. pLockBytes->AddRef();
  168. // Return It
  169. *ppvObject = pLockBytes;
  170. }
  171. // IID_IStream
  172. else
  173. {
  174. // Add Ref It
  175. pStream->AddRef();
  176. // Return It
  177. *ppvObject = pStream;
  178. }
  179. exit:
  180. // Cleanup
  181. SafeMemFree(pszURL);
  182. SafeRelease(pStream);
  183. SafeRelease(pLockBytes);
  184. // Thread Safety
  185. LeaveCriticalSection(&m_cs);
  186. // Done
  187. return hr;
  188. }
  189. // --------------------------------------------------------------------------------
  190. // CMimeWebDocument::HrInitialize
  191. // --------------------------------------------------------------------------------
  192. HRESULT CMimeWebDocument::HrInitialize(LPCSTR pszBase, LPCSTR pszURL)
  193. {
  194. // Locals
  195. HRESULT hr=S_OK;
  196. // Invalid Arg
  197. if (NULL == pszURL)
  198. return TrapError(E_INVALIDARG);
  199. // Thread Safety
  200. EnterCriticalSection(&m_cs);
  201. // Free Current Data
  202. SafeMemFree(m_pszBase);
  203. SafeMemFree(m_pszURL);
  204. // pszURL
  205. CHECKALLOC(m_pszURL = PszDupA(pszURL));
  206. // pszBase
  207. if (pszBase)
  208. {
  209. // Dup the Base
  210. CHECKALLOC(m_pszBase = PszDupA(pszBase));
  211. }
  212. exit:
  213. // Thread Safety
  214. LeaveCriticalSection(&m_cs);
  215. // Done
  216. return hr;
  217. }
  218. #ifndef MAC
  219. HRESULT HrOpenUrlViaCache(LPCSTR pszUrl, LPSTREAM *ppstm)
  220. {
  221. BYTE buf[MAX_CACHE_ENTRY_INFO_SIZE];
  222. INTERNET_CACHE_ENTRY_INFO *pCacheInfo = (INTERNET_CACHE_ENTRY_INFO *) buf;
  223. DWORD cInfo = sizeof(buf);
  224. HRESULT hr;
  225. pCacheInfo->dwStructSize = sizeof(INTERNET_CACHE_ENTRY_INFO);
  226. // try to get from the cache
  227. if (RetrieveUrlCacheEntryFileA(pszUrl, pCacheInfo, &cInfo, 0))
  228. {
  229. UnlockUrlCacheEntryFile(pszUrl, 0);
  230. if (OpenFileStream(pCacheInfo->lpszLocalFileName, OPEN_EXISTING, GENERIC_READ, ppstm)==S_OK)
  231. return S_OK;
  232. }
  233. // if failed to get a cache, get from the net
  234. hr = URLOpenBlockingStreamA(NULL, pszUrl, ppstm, 0, NULL);
  235. return hr == S_OK ? S_OK : MIME_E_URL_NOTFOUND;
  236. };
  237. #endif // !MAC