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.

300 lines
5.3 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1997
  5. //
  6. // File: cmime.cxx
  7. //
  8. // Contents: MimeType object
  9. //
  10. // History: 04-1-97 krishnag Created.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "iis.hxx"
  14. #pragma hdrstop
  15. // Class CMimeType
  16. DEFINE_Simple_IDispatch_Implementation(CMimeType)
  17. CMimeType::CMimeType():
  18. _pDispMgr(NULL),
  19. _lpMimeType(NULL),
  20. _lpExtension(NULL)
  21. {
  22. ENLIST_TRACKING(CMimeType);
  23. }
  24. HRESULT
  25. CMimeType::CreateMimeType(
  26. REFIID riid,
  27. void **ppvObj
  28. )
  29. {
  30. CMimeType FAR * pMimeType = NULL;
  31. HRESULT hr = S_OK;
  32. hr = AllocateMimeTypeObject(&pMimeType);
  33. BAIL_ON_FAILURE(hr);
  34. hr = pMimeType->QueryInterface(riid, ppvObj);
  35. BAIL_ON_FAILURE(hr);
  36. pMimeType->Release();
  37. RRETURN(hr);
  38. error:
  39. delete pMimeType;
  40. RRETURN(hr);
  41. }
  42. CMimeType::~CMimeType( )
  43. {
  44. if (_lpMimeType) {
  45. FreeADsStr(_lpMimeType);
  46. }
  47. if (_lpExtension) {
  48. FreeADsStr(_lpExtension);
  49. }
  50. delete _pDispMgr;
  51. }
  52. STDMETHODIMP
  53. CMimeType::QueryInterface(
  54. REFIID iid,
  55. LPVOID FAR* ppv
  56. )
  57. {
  58. if (IsEqualIID(iid, IID_IUnknown))
  59. {
  60. *ppv = (IISMimeType FAR *) this;
  61. }
  62. else if (IsEqualIID(iid, IID_IISMimeType))
  63. {
  64. *ppv = (IISMimeType FAR *) this;
  65. }
  66. else if (IsEqualIID(iid, IID_IDispatch))
  67. {
  68. *ppv = (IISMimeType FAR *) this;
  69. }
  70. else
  71. {
  72. *ppv = NULL;
  73. return E_NOINTERFACE;
  74. }
  75. AddRef();
  76. return NOERROR;
  77. }
  78. HRESULT
  79. CMimeType::InitFromIISString(
  80. LPWSTR pszStr
  81. )
  82. {
  83. LPWSTR pszToken = NULL;
  84. DWORD dwLen = 0;
  85. if (!pszStr) {
  86. return S_FALSE;
  87. }
  88. //
  89. // get length of pszStr; do not count ',' ; + 1 for null pointer and -1
  90. // for ',' so dwLen is = wcslen
  91. //
  92. dwLen = (DWORD)wcslen(pszStr);
  93. //
  94. // first token is extension
  95. //
  96. pszToken = wcstok(pszStr, L",");
  97. if (pszToken) {
  98. _lpExtension = AllocADsStr(pszToken);
  99. //
  100. // second token is mimetype
  101. //
  102. if (wcslen(pszStr) + 1 < dwLen) {
  103. pszToken = pszStr + wcslen(pszStr) + 1;
  104. _lpMimeType = AllocADsStr(pszToken);
  105. }
  106. }
  107. return S_OK;
  108. }
  109. HRESULT
  110. CMimeType::CopyMimeType(
  111. LPWSTR *ppszMimeType
  112. )
  113. {
  114. HRESULT hr = S_OK;
  115. LPWSTR pszMimeType = NULL;
  116. DWORD dwLen = 0;
  117. if (!ppszMimeType) {
  118. return S_FALSE;
  119. }
  120. if (_lpExtension) {
  121. dwLen = (DWORD)wcslen(_lpExtension);
  122. }
  123. if (_lpMimeType) {
  124. dwLen += (DWORD)wcslen(_lpMimeType);
  125. }
  126. //
  127. // dwLen +2 to include comma and null terminator
  128. //
  129. pszMimeType = (LPWSTR)AllocADsMem((dwLen+2) * sizeof(WCHAR));
  130. if (!pszMimeType) {
  131. hr = E_OUTOFMEMORY;
  132. BAIL_ON_FAILURE(hr);
  133. }
  134. //
  135. // empty contents
  136. //
  137. wcscpy(pszMimeType, L"");
  138. if (_lpExtension) {
  139. wcscpy(pszMimeType, _lpExtension);
  140. }
  141. wcscat(pszMimeType, L",");
  142. if (_lpMimeType) {
  143. wcscat(pszMimeType, _lpMimeType);
  144. }
  145. pszMimeType[wcslen(pszMimeType)] = L'\0';
  146. *ppszMimeType = pszMimeType;
  147. error:
  148. RRETURN(hr);
  149. }
  150. HRESULT
  151. CMimeType::AllocateMimeTypeObject(
  152. CMimeType ** ppMimeType
  153. )
  154. {
  155. CMimeType FAR * pMimeType = NULL;
  156. CAggregatorDispMgr FAR * pDispMgr = NULL;
  157. HRESULT hr = S_OK;
  158. pMimeType = new CMimeType();
  159. if (pMimeType == NULL) {
  160. hr = E_OUTOFMEMORY;
  161. }
  162. BAIL_ON_FAILURE(hr);
  163. pDispMgr = new CAggregatorDispMgr;
  164. if (pDispMgr == NULL) {
  165. hr = E_OUTOFMEMORY;
  166. }
  167. if (FAILED(hr))
  168. {
  169. delete pMimeType;
  170. }
  171. BAIL_ON_FAILURE(hr);
  172. hr = pDispMgr->LoadTypeInfoEntry(
  173. LIBID_IISOle,
  174. IID_IISMimeType,
  175. (IISMimeType *)pMimeType,
  176. DISPID_REGULAR
  177. );
  178. if (FAILED(hr))
  179. {
  180. delete pMimeType;
  181. }
  182. BAIL_ON_FAILURE(hr);
  183. pMimeType->_pDispMgr = pDispMgr;
  184. *ppMimeType = pMimeType;
  185. RRETURN(hr);
  186. error:
  187. delete pDispMgr;
  188. RRETURN(hr);
  189. }
  190. STDMETHODIMP
  191. CMimeType::get_MimeType(THIS_ BSTR FAR * retval)
  192. {
  193. HRESULT hr = S_OK;
  194. hr = ADsAllocString(_lpMimeType, retval);
  195. RRETURN(hr);
  196. }
  197. STDMETHODIMP
  198. CMimeType::put_MimeType(THIS_ BSTR bstrMimeType)
  199. {
  200. if (!bstrMimeType) {
  201. RRETURN(E_FAIL);
  202. }
  203. if (_lpMimeType) {
  204. FreeADsStr(_lpMimeType);
  205. }
  206. _lpMimeType = AllocADsStr(bstrMimeType);
  207. if (!_lpMimeType) {
  208. RRETURN(E_OUTOFMEMORY);
  209. }
  210. RRETURN(S_OK);
  211. }
  212. STDMETHODIMP
  213. CMimeType::get_Extension(THIS_ BSTR FAR * retval)
  214. {
  215. HRESULT hr = S_OK;
  216. hr = ADsAllocString(_lpExtension, retval);
  217. RRETURN(hr);
  218. }
  219. STDMETHODIMP
  220. CMimeType::put_Extension(THIS_ BSTR bstrExtension)
  221. {
  222. if (!bstrExtension) {
  223. RRETURN(E_FAIL);
  224. }
  225. if (_lpExtension) {
  226. FreeADsStr(_lpExtension);
  227. }
  228. _lpExtension = AllocADsStr(bstrExtension);
  229. if (!_lpExtension) {
  230. RRETURN(E_OUTOFMEMORY);
  231. }
  232. RRETURN(S_OK);
  233. }