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.

1531 lines
37 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1996 Microsoft Corporation. All Rights Reserved.
  5. Component: Request, Response objects
  6. File: cookies.cpp
  7. Owner: DGottner
  8. This file contains the code for the implementation of the
  9. Request.Cookies and Response.Cookies collections.
  10. ===================================================================*/
  11. #include "denpre.h"
  12. #pragma hdrstop
  13. #include "objbase.h"
  14. #include "cookies.h"
  15. #include "memchk.h"
  16. #pragma warning (disable: 4355) // ignore: "'this' used in base member init
  17. /*------------------------------------------------------------------
  18. * C C o o k i e S u p p o r t E r r
  19. */
  20. /*===================================================================
  21. CCookieSupportErr::CCookieSupportErr
  22. constructor
  23. ===================================================================*/
  24. CCookieSupportErr::CCookieSupportErr(CCookie *pCookie)
  25. {
  26. m_pCookie = pCookie;
  27. }
  28. /*===================================================================
  29. CCookieSupportErr::QueryInterface
  30. CCookieSupportErr::AddRef
  31. CCookieSupportErr::Release
  32. Delegating IUnknown members for CCookieSupportErr object.
  33. ===================================================================*/
  34. STDMETHODIMP CCookieSupportErr::QueryInterface(const IID &idInterface, void **ppvObj)
  35. {
  36. return m_pCookie->QueryInterface(idInterface, ppvObj);
  37. }
  38. STDMETHODIMP_(ULONG) CCookieSupportErr::AddRef()
  39. {
  40. return m_pCookie->AddRef();
  41. }
  42. STDMETHODIMP_(ULONG) CCookieSupportErr::Release()
  43. {
  44. return m_pCookie->Release();
  45. }
  46. /*===================================================================
  47. CCookieSupportErr::InterfaceSupportsErrorInfo
  48. Report back to OA about which interfaces we support that return
  49. error information
  50. ===================================================================*/
  51. STDMETHODIMP CCookieSupportErr::InterfaceSupportsErrorInfo(const GUID &idInterface)
  52. {
  53. if (idInterface == IID_IDispatch || idInterface == IID_IWriteCookie || idInterface == IID_IReadCookie)
  54. return S_OK;
  55. return S_FALSE;
  56. }
  57. /*------------------------------------------------------------------
  58. * C W r i t e C o o k i e
  59. */
  60. /*===================================================================
  61. CWriteCookie::CWriteCookie
  62. constructor
  63. ===================================================================*/
  64. CWriteCookie::CWriteCookie(CCookie *pCookie)
  65. {
  66. m_pCookie = pCookie;
  67. CDispatch::Init(IID_IWriteCookie);
  68. }
  69. /*===================================================================
  70. CWriteCookie::QueryInterface
  71. CWriteCookie::AddRef
  72. CWriteCookie::Release
  73. Delegating IUnknown members for CWriteCookie object.
  74. ===================================================================*/
  75. STDMETHODIMP CWriteCookie::QueryInterface(const IID &idInterface, void **ppvObj)
  76. {
  77. // Bug 85953 Trap IDispatch before it gets to the core object
  78. if (idInterface == IID_IUnknown ||
  79. idInterface == IID_IWriteCookie ||
  80. idInterface == IID_IDispatch)
  81. {
  82. *ppvObj = this;
  83. static_cast<IUnknown *>(*ppvObj)->AddRef();
  84. return S_OK;
  85. }
  86. else
  87. return m_pCookie->QueryInterface(idInterface, ppvObj);
  88. }
  89. STDMETHODIMP_(ULONG) CWriteCookie::AddRef()
  90. {
  91. return m_pCookie->AddRef();
  92. }
  93. STDMETHODIMP_(ULONG) CWriteCookie::Release()
  94. {
  95. return m_pCookie->Release();
  96. }
  97. /*===================================================================
  98. CWriteCookie::put_Item
  99. Set the primary value for a cookie.
  100. ===================================================================*/
  101. STDMETHODIMP CWriteCookie::put_Item(VARIANT varKey, BSTR bstrValue)
  102. {
  103. char *szKey; // ascii value of the key
  104. CWCharToMBCS convValue;
  105. CWCharToMBCS convKey;
  106. // Bug 122589: Don't crash when "bstrValue" is NULL
  107. if (bstrValue == NULL)
  108. return E_FAIL;
  109. // Initialize things
  110. //
  111. VARIANT *pvarKey = &varKey;
  112. HRESULT hrReturn = S_OK;
  113. // BUG 937: VBScript passes VT_VARIANT|VT_BYREF when passing obect
  114. // produced by IEnumVariant
  115. //
  116. // Use VariantResolveDispatch which will:
  117. //
  118. // * Copy BYREF variants for us using VariantCopyInd
  119. // * handle E_OUTOFMEMORY for us
  120. // * get the default value from an IDispatch, which seems
  121. // like an appropriate conversion.
  122. //
  123. VARIANT varKeyCopy;
  124. VariantInit(&varKeyCopy);
  125. if (V_VT(pvarKey) != VT_BSTR)
  126. {
  127. if (FAILED(VariantResolveDispatch(&varKeyCopy, &varKey, IID_IRequestDictionary, IDE_REQUEST)))
  128. goto LExit;
  129. pvarKey = &varKeyCopy;
  130. }
  131. switch (V_VT(pvarKey))
  132. {
  133. case VT_BSTR:
  134. break;
  135. case VT_ERROR:
  136. if (V_ERROR(pvarKey) == DISP_E_PARAMNOTFOUND)
  137. {
  138. if (m_pCookie->m_szValue == NULL) // current value is a dictionary
  139. {
  140. CCookiePair *pNukePair = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.Head());
  141. while (pNukePair != NULL)
  142. {
  143. CCookiePair *pNext = static_cast<CCookiePair *>(pNukePair->m_pNext);
  144. delete pNukePair;
  145. pNukePair = pNext;
  146. }
  147. m_pCookie->m_mpszValues.ReInit();
  148. }
  149. else // no dictionary value
  150. if (m_pCookie->m_fDuplicate)
  151. free(m_pCookie->m_szValue);
  152. if (FAILED(hrReturn = convValue.Init(bstrValue,m_pCookie->m_lCodePage))) {
  153. goto LExit;
  154. }
  155. m_pCookie->m_szValue = NULL;
  156. m_pCookie->AddValue(convValue.GetString(), TRUE);
  157. m_pCookie->m_fDirty = TRUE;
  158. goto LExit;
  159. }
  160. // Other error, FALL THROUGH to wrong type case
  161. default:
  162. ExceptionId(IID_IWriteCookie, IDE_COOKIE, IDE_EXPECTING_STR);
  163. hrReturn = E_FAIL;
  164. goto LExit;
  165. }
  166. // don't allow empty keys in the cookie
  167. //
  168. if (V_BSTR(pvarKey)) {
  169. if (FAILED(hrReturn = convKey.Init(V_BSTR(pvarKey),m_pCookie->m_lCodePage))) {
  170. goto LExit;
  171. }
  172. else {
  173. szKey = convKey.GetString();
  174. }
  175. }
  176. else {
  177. szKey = "";
  178. }
  179. if (*szKey == '\0')
  180. {
  181. ExceptionId(IID_IWriteCookie, IDE_COOKIE, IDE_COOKIE_EMPTY_DICT);
  182. hrReturn = E_FAIL;
  183. goto LExit;
  184. }
  185. // we're changing a dictionary value, so first trash the primary value
  186. //
  187. if (m_pCookie->m_fDuplicate)
  188. free(m_pCookie->m_szValue);
  189. if (FAILED(hrReturn = convValue.Init(bstrValue,m_pCookie->m_lCodePage))) {
  190. goto LExit;
  191. }
  192. m_pCookie->m_szValue = NULL;
  193. m_pCookie->AddKeyAndValue(szKey, convValue.GetString(), TRUE);
  194. m_pCookie->m_fDirty = TRUE;
  195. LExit:
  196. VariantClear(&varKeyCopy);
  197. return hrReturn;
  198. }
  199. /*===================================================================
  200. CWriteCookie::put_Expires
  201. Set the expires attribute for a cookie.
  202. ===================================================================*/
  203. STDMETHODIMP CWriteCookie::put_Expires(DATE dtExpires)
  204. {
  205. if (FAILED(VariantDateToCTime(dtExpires, &m_pCookie->m_tExpires)))
  206. {
  207. ExceptionId(IID_IWriteCookie, IDE_COOKIE, IDE_COOKIE_BAD_EXPIRATION);
  208. return E_FAIL;
  209. }
  210. m_pCookie->m_fDirty = TRUE;
  211. return S_OK;
  212. }
  213. /*===================================================================
  214. CWriteCookie::put_Domain
  215. Set the domain attribute for a cookie.
  216. ===================================================================*/
  217. STDMETHODIMP CWriteCookie::put_Domain(BSTR bstrDomain)
  218. {
  219. CWCharToMBCS convDomain;
  220. HRESULT hr = S_OK;
  221. if (FAILED(hr = convDomain.Init(bstrDomain,m_pCookie->m_lCodePage)));
  222. else {
  223. if (m_pCookie->m_szDomain)
  224. free(m_pCookie->m_szDomain);
  225. m_pCookie->m_szDomain = convDomain.GetString(TRUE);
  226. m_pCookie->m_fDirty = TRUE;
  227. }
  228. return hr;
  229. }
  230. /*===================================================================
  231. CWriteCookie::put_Path
  232. Set the path attribute for a cookie.
  233. ===================================================================*/
  234. STDMETHODIMP CWriteCookie::put_Path(BSTR bstrPath)
  235. {
  236. HRESULT hr = S_OK;
  237. CWCharToMBCS convPath;
  238. if (FAILED(hr = convPath.Init(bstrPath,m_pCookie->m_lCodePage)));
  239. else {
  240. if (m_pCookie->m_szPath)
  241. free(m_pCookie->m_szPath);
  242. m_pCookie->m_szPath = convPath.GetString(TRUE);
  243. if (m_pCookie->m_szPath == NULL)
  244. hr = E_OUTOFMEMORY;
  245. }
  246. if (SUCCEEDED(hr))
  247. m_pCookie->m_fDirty = TRUE;
  248. return hr;
  249. }
  250. /*===================================================================
  251. CWriteCookie::put_Secure
  252. Set the secure attribute for a cookie.
  253. ===================================================================*/
  254. STDMETHODIMP CWriteCookie::put_Secure(VARIANT_BOOL fSecure)
  255. {
  256. m_pCookie->m_fSecure = fSecure;
  257. m_pCookie->m_fDirty = TRUE;
  258. return S_OK;
  259. }
  260. /*===================================================================
  261. CWriteCookie::get_HasKeys
  262. Return True if the cookie contains keys, False if it is a simple
  263. value
  264. ===================================================================*/
  265. STDMETHODIMP CWriteCookie::get_HasKeys(VARIANT_BOOL *pfHasKeys)
  266. {
  267. *pfHasKeys = ( m_pCookie->m_mpszValues.Count() > 0 ? VARIANT_TRUE : VARIANT_FALSE);
  268. return S_OK;
  269. }
  270. /*===================================================================
  271. CWriteCookie::get__NewEnum
  272. Return an enumerator object.
  273. ReadCookie and WriteCookie use the same iterator object.
  274. To reduce useless redundancy, deletage to IReadCookie.
  275. The IReadCookie enumerator will likely be used much more
  276. frequently than the IWriteCookie iterator, so we pay the
  277. overhead of delegation in this function.
  278. ===================================================================*/
  279. STDMETHODIMP CWriteCookie::get__NewEnum(IUnknown **ppEnumReturn)
  280. {
  281. IReadCookie *pReadCookie;
  282. if (FAILED(QueryInterface(IID_IReadCookie, reinterpret_cast<void **>(&pReadCookie))))
  283. {
  284. Assert (FALSE); // expect success!
  285. return E_FAIL;
  286. }
  287. HRESULT hrNewEnum = pReadCookie->get__NewEnum(ppEnumReturn);
  288. pReadCookie->Release();
  289. return hrNewEnum;
  290. }
  291. /*------------------------------------------------------------------
  292. * C R e a d C o o k i e
  293. */
  294. /*===================================================================
  295. CReadCookie::CReadCookie
  296. constructor
  297. ===================================================================*/
  298. CReadCookie::CReadCookie(CCookie *pCookie)
  299. {
  300. m_pCookie = pCookie;
  301. CDispatch::Init(IID_IReadCookie);
  302. }
  303. /*===================================================================
  304. CReadCookie::QueryInterface
  305. CReadCookie::AddRef
  306. CReadCookie::Release
  307. Delegating IUnknown members for CReadCookie object.
  308. ===================================================================*/
  309. STDMETHODIMP CReadCookie::QueryInterface(const IID &idInterface, void **ppvObj)
  310. {
  311. // Bug 85953 Trap IDispatch before it gets to the core object
  312. if (idInterface == IID_IUnknown ||
  313. idInterface == IID_IReadCookie ||
  314. idInterface == IID_IDispatch)
  315. {
  316. *ppvObj = this;
  317. static_cast<IUnknown *>(*ppvObj)->AddRef();
  318. return S_OK;
  319. }
  320. else
  321. return m_pCookie->QueryInterface(idInterface, ppvObj);
  322. }
  323. STDMETHODIMP_(ULONG) CReadCookie::AddRef()
  324. {
  325. return m_pCookie->AddRef();
  326. }
  327. STDMETHODIMP_(ULONG) CReadCookie::Release()
  328. {
  329. return m_pCookie->Release();
  330. }
  331. /*===================================================================
  332. CReadCookie::get_Item
  333. Retrieve a value in the cookie dictionary.
  334. ===================================================================*/
  335. STDMETHODIMP CReadCookie::get_Item(VARIANT varKey, VARIANT *pvarReturn)
  336. {
  337. char *szKey; // ascii version of the key
  338. CCookiePair *pPair = NULL; // name and value of cookie in the dictionary
  339. CWCharToMBCS convKey;
  340. STACK_BUFFER( tempCookie, 128 );
  341. // Initialize things
  342. //
  343. VariantInit(pvarReturn);
  344. VARIANT *pvarKey = &varKey;
  345. HRESULT hrReturn = S_OK;
  346. // BUG 937: VBScript passes VT_VARIANT|VT_BYREF when passing obect
  347. // produced by IEnumVariant
  348. //
  349. // Use VariantResolveDispatch which will:
  350. //
  351. // * Copy BYREF variants for us using VariantCopyInd
  352. // * handle E_OUTOFMEMORY for us
  353. // * get the default value from an IDispatch, which seems
  354. // like an appropriate conversion.
  355. //
  356. VARIANT varKeyCopy;
  357. VariantInit(&varKeyCopy);
  358. DWORD vt = V_VT(pvarKey);
  359. if ((V_VT(pvarKey) != VT_BSTR) && (vt != VT_I2) && (vt != VT_I4))
  360. {
  361. if (FAILED(VariantResolveDispatch(&varKeyCopy, &varKey, IID_IRequestDictionary, IDE_REQUEST)))
  362. goto LExit;
  363. pvarKey = &varKeyCopy;
  364. }
  365. vt = V_VT(pvarKey);
  366. switch (vt)
  367. {
  368. // Bug 95201 support all numberic sub-types
  369. case VT_I1: case VT_I2: case VT_I8:
  370. case VT_UI1: case VT_UI2: case VT_UI4: case VT_UI8:
  371. case VT_R4: case VT_R8:
  372. // Coerce all integral types to VT_I4
  373. if (FAILED(hrReturn = VariantChangeType(pvarKey, pvarKey, 0, VT_I4)))
  374. goto LExit;
  375. // fallthru to VT_I4
  376. case VT_I4:
  377. case VT_BSTR:
  378. break;
  379. case VT_ERROR:
  380. if (V_ERROR(pvarKey) == DISP_E_PARAMNOTFOUND)
  381. {
  382. V_VT(pvarReturn) = VT_BSTR;
  383. // simple value, URLEncoding NOT a good idea in this case
  384. if (m_pCookie->m_szValue)
  385. {
  386. BSTR bstrT;
  387. if (FAILED(SysAllocStringFromSz(m_pCookie->m_szValue, 0, &bstrT,m_pCookie->m_lCodePage)))
  388. {
  389. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  390. hrReturn = E_FAIL;
  391. goto LExit;
  392. }
  393. V_BSTR(pvarReturn) = bstrT;
  394. }
  395. // dictionary value, must URLEncode to prevent '&', '=' from being misinterpreted
  396. else
  397. {
  398. int cbHTTPCookie = m_pCookie->GetHTTPCookieSize();
  399. if (cbHTTPCookie > REQUEST_ALLOC_MAX)
  400. {
  401. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_STACK_OVERFLOW);
  402. hrReturn = E_FAIL;
  403. goto LExit;
  404. }
  405. if (tempCookie.Resize(cbHTTPCookie) == FALSE) {
  406. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  407. hrReturn = E_OUTOFMEMORY;
  408. goto LExit;
  409. }
  410. char *szHTTPCookie = static_cast<char *>(tempCookie.QueryPtr());
  411. m_pCookie->GetHTTPCookie(szHTTPCookie);
  412. BSTR bstrT;
  413. if (FAILED(SysAllocStringFromSz(szHTTPCookie, 0, &bstrT,m_pCookie->m_lCodePage)))
  414. {
  415. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  416. hrReturn = E_FAIL;
  417. goto LExit;
  418. }
  419. V_BSTR(pvarReturn) = bstrT;
  420. }
  421. goto LExit;
  422. }
  423. default:
  424. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_EXPECTING_STR);
  425. hrReturn = E_FAIL;
  426. goto LExit;
  427. }
  428. if (vt == VT_BSTR)
  429. {
  430. // convert the key to ANSI
  431. if (V_BSTR(pvarKey)) {
  432. if (FAILED(hrReturn = convKey.Init(V_BSTR(pvarKey),m_pCookie->m_lCodePage))) {
  433. goto LExit;
  434. }
  435. else {
  436. szKey = convKey.GetString();
  437. }
  438. }
  439. else {
  440. szKey = "";
  441. }
  442. // Look up the key in the Cookie.
  443. pPair = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.FindElem(szKey, strlen(szKey)));
  444. }
  445. else
  446. {
  447. // Look up item by index
  448. int iCount;
  449. iCount = V_I4(pvarKey);
  450. if ((iCount < 1) ||
  451. (m_pCookie->m_mpszValues.Count() == 0) ||
  452. (iCount > (int) m_pCookie->m_mpszValues.Count() ))
  453. {
  454. hrReturn = E_FAIL;
  455. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_BAD_ARRAY_INDEX);
  456. goto LExit;
  457. }
  458. pPair = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.Head());
  459. while((iCount > 1) && (pPair != NULL))
  460. {
  461. pPair = static_cast<CCookiePair *>(pPair->m_pNext);
  462. iCount--;
  463. }
  464. }
  465. if (pPair)
  466. {
  467. BSTR bstrT;
  468. if (FAILED(SysAllocStringFromSz(pPair->m_szValue, 0, &bstrT,m_pCookie->m_lCodePage)))
  469. {
  470. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  471. hrReturn = E_FAIL;
  472. goto LExit;
  473. }
  474. V_VT(pvarReturn) = VT_BSTR;
  475. V_BSTR(pvarReturn) = bstrT;
  476. }
  477. LExit:
  478. VariantClear(&varKeyCopy);
  479. return hrReturn;
  480. }
  481. /*===================================================================
  482. CReadCookie::get_HasKeys
  483. Return True if the cookie contains keys, False if it is a simple
  484. value
  485. ===================================================================*/
  486. STDMETHODIMP CReadCookie::get_HasKeys(VARIANT_BOOL *pfHasKeys)
  487. {
  488. *pfHasKeys = (m_pCookie->m_mpszValues.Count() > 0 ? VARIANT_TRUE : VARIANT_FALSE);
  489. return S_OK;
  490. }
  491. /*===================================================================
  492. CReadCookie::get__NewEnum
  493. Return an enumerator object.
  494. ===================================================================*/
  495. STDMETHODIMP CReadCookie::get__NewEnum(IUnknown **ppEnumReturn)
  496. {
  497. *ppEnumReturn = NULL;
  498. CCookieIterator *pIterator = new CCookieIterator(m_pCookie);
  499. if (pIterator == NULL)
  500. {
  501. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  502. return E_OUTOFMEMORY;
  503. }
  504. *ppEnumReturn = pIterator;
  505. return S_OK;
  506. }
  507. /*===================================================================
  508. CReadCookie::get_Count
  509. Parameters:
  510. pcValues - count is stored in *pcValues. Set to 0 if this
  511. cookie is not multi-valued.
  512. ===================================================================*/
  513. STDMETHODIMP CReadCookie::get_Count(int *pcValues)
  514. {
  515. *pcValues = m_pCookie->m_mpszValues.Count();
  516. return S_OK;
  517. }
  518. /*===================================================================
  519. CReadCookie::get_Key
  520. Function called from DispInvoke to get keys from a multi-valued
  521. Cookie collection.
  522. Parameters:
  523. vKey VARIANT [in], which parameter to get the key of
  524. pvarReturn VARIANT *, [out] value of the requested parameter
  525. Returns:
  526. S_OK on success, E_FAIL on failure.
  527. ===================================================================*/
  528. STDMETHODIMP CReadCookie::get_Key(VARIANT varKey, VARIANT *pvarReturn)
  529. {
  530. char *szKey; // ascii version of the key
  531. CCookiePair *pPair = NULL; // name and value of cookie in the dictionary
  532. CWCharToMBCS convKey;
  533. STACK_BUFFER( tempCookie, 128);
  534. // Initialize things
  535. //
  536. VariantInit(pvarReturn);
  537. VARIANT *pvarKey = &varKey;
  538. HRESULT hrReturn = S_OK;
  539. // BUG 937: VBScript passes VT_VARIANT|VT_BYREF when passing obect
  540. // produced by IEnumVariant
  541. //
  542. // Use VariantResolveDispatch which will:
  543. //
  544. // * Copy BYREF variants for us using VariantCopyInd
  545. // * handle E_OUTOFMEMORY for us
  546. // * get the default value from an IDispatch, which seems
  547. // like an appropriate conversion.
  548. //
  549. VARIANT varKeyCopy;
  550. VariantInit(&varKeyCopy);
  551. DWORD vt = V_VT(pvarKey);
  552. if ((V_VT(pvarKey) != VT_BSTR) && (vt != VT_I2) && (vt != VT_I4))
  553. {
  554. if (FAILED(VariantResolveDispatch(&varKeyCopy, &varKey, IID_IRequestDictionary, IDE_REQUEST)))
  555. goto LExit;
  556. pvarKey = &varKeyCopy;
  557. }
  558. vt = V_VT(pvarKey);
  559. switch (vt)
  560. {
  561. // Bug 95201 support all numberic sub-types
  562. case VT_I1: case VT_I2: case VT_I8:
  563. case VT_UI1: case VT_UI2: case VT_UI4: case VT_UI8:
  564. case VT_R4: case VT_R8:
  565. // Coerce all integral types to VT_I4
  566. if (FAILED(hrReturn = VariantChangeType(pvarKey, pvarKey, 0, VT_I4)))
  567. goto LExit;
  568. // fallthru to VT_I4
  569. case VT_I4:
  570. case VT_BSTR:
  571. break;
  572. case VT_ERROR:
  573. if (V_ERROR(pvarKey) == DISP_E_PARAMNOTFOUND)
  574. {
  575. V_VT(pvarReturn) = VT_BSTR;
  576. // simple value, URLEncoding NOT a good idea in this case
  577. if (m_pCookie->m_szValue)
  578. {
  579. BSTR bstrT;
  580. if (FAILED(SysAllocStringFromSz(m_pCookie->m_szValue, 0, &bstrT,m_pCookie->m_lCodePage)))
  581. {
  582. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  583. hrReturn = E_FAIL;
  584. goto LExit;
  585. }
  586. V_BSTR(pvarReturn) = bstrT;
  587. }
  588. // dictionary value, must URLEncode to prevent '&', '=' from being misinterpreted
  589. else
  590. {
  591. int cbHTTPCookie = m_pCookie->GetHTTPCookieSize();
  592. if (cbHTTPCookie > REQUEST_ALLOC_MAX)
  593. {
  594. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_STACK_OVERFLOW);
  595. hrReturn = E_FAIL;
  596. goto LExit;
  597. }
  598. if (tempCookie.Resize(cbHTTPCookie) == FALSE) {
  599. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  600. hrReturn = E_OUTOFMEMORY;
  601. goto LExit;
  602. }
  603. char *szHTTPCookie = static_cast<char *>(tempCookie.QueryPtr());
  604. m_pCookie->GetHTTPCookie(szHTTPCookie);
  605. BSTR bstrT;
  606. if (FAILED(SysAllocStringFromSz(szHTTPCookie, 0, &bstrT, m_pCookie->m_lCodePage)))
  607. {
  608. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_OOM);
  609. hrReturn = E_FAIL;
  610. goto LExit;
  611. }
  612. V_BSTR(pvarReturn) = bstrT;
  613. }
  614. goto LExit;
  615. }
  616. default:
  617. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_EXPECTING_STR);
  618. hrReturn = E_FAIL;
  619. goto LExit;
  620. }
  621. if (vt == VT_BSTR)
  622. {
  623. // convert the key to ANSI
  624. if (V_BSTR(pvarKey)) {
  625. if (FAILED(hrReturn = convKey.Init(V_BSTR(pvarKey),m_pCookie->m_lCodePage))) {
  626. goto LExit;
  627. }
  628. else {
  629. szKey = convKey.GetString();
  630. }
  631. }
  632. else {
  633. szKey = "";
  634. }
  635. // Look up the key in the Cookie.
  636. pPair = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.FindElem(szKey, strlen(szKey)));
  637. }
  638. else
  639. {
  640. // Look up item by index
  641. int iCount;
  642. iCount = V_I4(pvarKey);
  643. if ((iCount < 1) ||
  644. (m_pCookie->m_mpszValues.Count() == 0) ||
  645. (iCount > (int) m_pCookie->m_mpszValues.Count() ))
  646. {
  647. hrReturn = E_FAIL;
  648. ExceptionId(IID_IReadCookie, IDE_COOKIE, IDE_BAD_ARRAY_INDEX);
  649. goto LExit;
  650. }
  651. pPair = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.Head());
  652. while((iCount > 1) && (pPair != NULL))
  653. {
  654. pPair = static_cast<CCookiePair *>(pPair->m_pNext);
  655. iCount--;
  656. }
  657. }
  658. if (pPair)
  659. {
  660. // Create a BSTR containing the key for this variant
  661. BSTR bstrT;
  662. SysAllocStringFromSz((CHAR *)pPair->m_pKey, 0, &bstrT, m_pCookie->m_lCodePage);
  663. if (!bstrT)
  664. return E_OUTOFMEMORY;
  665. V_VT(pvarReturn) = VT_BSTR;
  666. V_BSTR(pvarReturn) = bstrT;
  667. }
  668. LExit:
  669. VariantClear(&varKeyCopy);
  670. return hrReturn;
  671. }
  672. /*------------------------------------------------------------------
  673. * C C o o k i e
  674. */
  675. /*===================================================================
  676. CCookie::CCookie
  677. constructor
  678. ===================================================================*/
  679. CCookie::CCookie(CIsapiReqInfo *pIReq, UINT lCodePage, IUnknown *pUnkOuter, PFNDESTROYED pfnDestroy)
  680. : m_WriteCookieInterface(this),
  681. m_ReadCookieInterface(this),
  682. m_CookieSupportErrorInfo(this)
  683. {
  684. m_szValue = NULL;
  685. m_tExpires = -1;
  686. m_szDomain = NULL;
  687. m_szPath = NULL;
  688. m_fSecure = FALSE;
  689. m_fDirty = FALSE;
  690. m_fDuplicate = FALSE;
  691. m_pfnDestroy = pfnDestroy;
  692. m_pIReq = pIReq;
  693. m_lCodePage = lCodePage;
  694. m_cRefs = 1;
  695. }
  696. /*===================================================================
  697. CCookie::~CCookie
  698. Destructor
  699. ===================================================================*/
  700. CCookie::~CCookie()
  701. {
  702. CCookiePair *pNukePair = static_cast<CCookiePair *>(m_mpszValues.Head());
  703. while (pNukePair != NULL)
  704. {
  705. CCookiePair *pNext = static_cast<CCookiePair *>(pNukePair->m_pNext);
  706. delete pNukePair;
  707. pNukePair = pNext;
  708. }
  709. m_mpszValues.UnInit();
  710. if (m_fDuplicate)
  711. free(m_szValue);
  712. if (m_szDomain) free(m_szDomain);
  713. if (m_szPath) free(m_szPath);
  714. }
  715. /*===================================================================
  716. CCookie::Init
  717. initialize the cookie. This initializes the cookie's value hashing
  718. table
  719. ===================================================================*/
  720. HRESULT CCookie::Init()
  721. {
  722. HRESULT hr = S_OK;
  723. TCHAR pathInfo[MAX_PATH];
  724. #if UNICODE
  725. CWCharToMBCS convStr;
  726. #endif
  727. if (FAILED(hr = m_mpszValues.Init(7)));
  728. // it would be nice if we could use the application path from the metabase,
  729. // but because of case sensitivity issues, we can't. The safest bet is
  730. // to use the request's path info up to the length of the application's
  731. // pathinfo.
  732. else if (FAILED(hr=FindApplicationPath(m_pIReq, pathInfo, sizeof(pathInfo))));
  733. #if UNICODE
  734. else if (FAILED(hr = convStr.Init(m_pIReq->QueryPszPathInfo(), m_lCodePage, _tcslen(pathInfo))));
  735. else {
  736. m_szPath = convStr.GetString(TRUE);
  737. }
  738. #else
  739. else {
  740. TCHAR *reqPath = m_pIReq->QueryPszPathInfo();
  741. if (reqPath == NULL)
  742. {
  743. reqPath=pathInfo;
  744. }
  745. DWORD cchPathInfo = _tcslen(pathInfo);
  746. if (cchPathInfo > _tcslen(reqPath))
  747. { // unlikely
  748. cchPathInfo = _tcslen(reqPath);
  749. }
  750. if (!(m_szPath = (char *)malloc(cchPathInfo+1))) {
  751. hr = E_OUTOFMEMORY;
  752. }
  753. else {
  754. memcpy(m_szPath, reqPath, cchPathInfo);
  755. m_szPath[ cchPathInfo ] = 0;
  756. }
  757. }
  758. #endif
  759. return hr;
  760. }
  761. /*===================================================================
  762. CCookie::QueryInterface
  763. CCookie::AddRef
  764. CCookie::Release
  765. IUnknown members for CCookie object.
  766. Note on CCookie::QueryInterface: The Query for IDispatch is
  767. ambiguous because it can either refer to IReadCookie or
  768. IWriteCookie. To resolve this, we resolve requests for IDispatch
  769. to IReadCookie. The rationale for this is that the code in
  770. request.cpp calls QueryInterface for a generic IDispatch pointer
  771. (because the collection is heterogenous) The Response.Cookies
  772. collection is homogeneous and so only calls QueryInterface for
  773. IWriteCookie.
  774. ===================================================================*/
  775. STDMETHODIMP CCookie::QueryInterface(const IID &idInterface, void **ppvObj)
  776. {
  777. if (idInterface == IID_IUnknown)
  778. *ppvObj = this;
  779. else if (idInterface == IID_IReadCookie || idInterface == IID_IDispatch)
  780. *ppvObj = &m_ReadCookieInterface;
  781. else if (idInterface == IID_IWriteCookie)
  782. *ppvObj = &m_WriteCookieInterface;
  783. else if (idInterface == IID_ISupportErrorInfo)
  784. *ppvObj = &m_CookieSupportErrorInfo;
  785. else
  786. *ppvObj = NULL;
  787. if (*ppvObj != NULL)
  788. {
  789. static_cast<IUnknown *>(*ppvObj)->AddRef();
  790. return S_OK;
  791. }
  792. return ResultFromScode(E_NOINTERFACE);
  793. }
  794. STDMETHODIMP_(ULONG) CCookie::AddRef()
  795. {
  796. return ++m_cRefs;
  797. }
  798. STDMETHODIMP_(ULONG) CCookie::Release(void)
  799. {
  800. if (--m_cRefs != 0)
  801. return m_cRefs;
  802. if (m_pfnDestroy != NULL)
  803. (*m_pfnDestroy)();
  804. delete this;
  805. return 0;
  806. }
  807. /*===================================================================
  808. CCookie::AddValue
  809. Set the cookie's primary value. One you set the primary value,
  810. you can't reset it.
  811. ===================================================================*/
  812. HRESULT CCookie::AddValue(char *szValue, BOOL fDuplicate)
  813. {
  814. if (m_szValue != NULL) // cookie already is marked as single-valued
  815. return E_FAIL;
  816. if (m_mpszValues.Count() != 0) // cookie already has a value
  817. return E_FAIL;
  818. if (fDuplicate)
  819. {
  820. char *szNew = (char *)malloc(strlen(szValue) + 1);
  821. if (szNew == NULL)
  822. return E_OUTOFMEMORY;
  823. m_szValue = strcpy(szNew, szValue);
  824. }
  825. else
  826. m_szValue = szValue;
  827. m_fDuplicate = fDuplicate;
  828. return S_OK;
  829. }
  830. /*===================================================================
  831. CCookie::AddKeyAndValue
  832. Add a key and value pair to the Cookie's dictionary. It fails
  833. if the cookie has a primary value already set. It will overwrite
  834. the value if the key already exists.
  835. ===================================================================*/
  836. HRESULT CCookie::AddKeyAndValue(char *szKey, char *szValue, BOOL fDuplicate)
  837. {
  838. if (m_szValue != NULL)
  839. return E_FAIL;
  840. delete static_cast<CCookiePair *>(m_mpszValues.DeleteElem(szKey, strlen(szKey)));
  841. CCookiePair *pCookiePair = new CCookiePair;
  842. if (pCookiePair == NULL)
  843. return E_OUTOFMEMORY;
  844. if (FAILED(pCookiePair->Init(szKey, szValue, fDuplicate)))
  845. return E_FAIL;
  846. m_mpszValues.AddElem(pCookiePair);
  847. return S_OK;
  848. }
  849. /*===================================================================
  850. CCookie::GetHTTPCookieSize
  851. Return the number of bytes required for the expansion of the HTTP_COOKIE variable
  852. ===================================================================*/
  853. size_t CCookie::GetHTTPCookieSize()
  854. {
  855. if (m_szValue)
  856. return URLEncodeLen(m_szValue);
  857. else
  858. {
  859. int cbValue = 1;
  860. CCookiePair *pPair = static_cast<CCookiePair *>(m_mpszValues.Head());
  861. while (pPair)
  862. {
  863. // Add size of the URL Encoded key, a character for the '=', and a
  864. // character for the '&' or the NUL terminator. URLEncodeLen
  865. // returns the size + 1, so the two calls to URLEncodeLen() add the
  866. // two characters we need.
  867. //
  868. cbValue += URLEncodeLen(reinterpret_cast<char *>(pPair->m_pKey)) + URLEncodeLen(pPair->m_szValue);
  869. pPair = static_cast<CCookiePair *>(pPair->m_pNext);
  870. }
  871. return cbValue;
  872. }
  873. }
  874. /*===================================================================
  875. CCookie::GetHTTPCookie
  876. Return the URL Encoded value a single cookie
  877. Parameters:
  878. szBuffer - pointer to the destination buffer to store the
  879. URL encoded value
  880. Returns:
  881. Returns a pointer to the terminating NUL character.
  882. ===================================================================*/
  883. char *CCookie::GetHTTPCookie(char *szBuffer)
  884. {
  885. if (m_szValue)
  886. return URLEncode(szBuffer, m_szValue);
  887. else
  888. {
  889. char *szDest = szBuffer;
  890. *szDest = '\0';
  891. CCookiePair *pPair = static_cast<CCookiePair *>(m_mpszValues.Head());
  892. while (pPair)
  893. {
  894. // Write <name>=<value> string
  895. szDest = URLEncode(szDest, reinterpret_cast<char *>(pPair->m_pKey));
  896. *szDest++ = '=';
  897. szDest = URLEncode(szDest, pPair->m_szValue);
  898. // Advance
  899. pPair = static_cast<CCookiePair *>(pPair->m_pNext);
  900. // Append '&' if there's another one following
  901. if (pPair)
  902. *szDest++ = '&';
  903. }
  904. Assert (*szDest == '\0'); // make sure we are nul-terminated
  905. return szDest;
  906. }
  907. }
  908. /*===================================================================
  909. CCookie::GetCookieHeaderSize
  910. Return the number of bytes required to allocate for the "Set-Cookie" header.
  911. Parameters:
  912. szName - the name of the cookie (the size of the name is added to the value)
  913. Returns:
  914. Returns 0 if *this does not contain a cookie value.
  915. ===================================================================*/
  916. size_t CCookie::GetCookieHeaderSize(const char *szName)
  917. {
  918. int cbCookie = sizeof "Set-Cookie: "; // initialize and add NUL terminator now
  919. // Add size of the URL Encoded name, a character for the '=', and the size
  920. // of the URL Encoded cookie value. URLEncodeLen, and GetHttpCookieSize
  921. // compensate for the NUL terminator, so we actually SUBTRACT 1. (-2 for
  922. // these two function calls, +1 for the '=' sign
  923. //
  924. cbCookie += URLEncodeLen(szName) + GetHTTPCookieSize() - 1;
  925. if (m_tExpires != -1)
  926. cbCookie += (sizeof "; expires=") + DATE_STRING_SIZE - 1;
  927. // BUG 250 - DBCS External
  928. // ASP does not URLEncode the domain and path attributes, which was noticed
  929. // during localizaiton.
  930. //
  931. // NOTE: URLEncodeLen and sizeof both add a space for the nul terminator,
  932. // so we subtract 2 to compensate.
  933. //
  934. if (m_szDomain)
  935. cbCookie += (sizeof "; domain=") + DBCSEncodeLen(m_szDomain) - 2;
  936. cbCookie += (sizeof "; path=") + DBCSEncodeLen(m_szPath) - 2;
  937. if (m_fSecure)
  938. cbCookie += (sizeof "; secure") - 1;
  939. return cbCookie;
  940. }
  941. /*===================================================================
  942. CCookie::GetCookieHeader
  943. Construct the appropriate "Set-Cookie" header for a cookie.
  944. Parameters:
  945. szName - the name of the cookie (the size of the name is added to the value)
  946. Returns:
  947. Returns 0 if *this does not contain a cookie value.
  948. ===================================================================*/
  949. char *CCookie::GetCookieHeader(const char *szName, char *szBuffer)
  950. {
  951. // write out the cookie name and value
  952. //
  953. char *szDest = strcpyExA(szBuffer, "Set-Cookie: ");
  954. szDest = URLEncode(szDest, szName);
  955. szDest = strcpyExA(szDest, "=");
  956. szDest = GetHTTPCookie(szDest);
  957. if (m_tExpires != -1) {
  958. char szExpires[DATE_STRING_SIZE];
  959. CTimeToStringGMT(&m_tExpires, szExpires, TRUE);
  960. szDest = strcpyExA(szDest, "; expires=");
  961. szDest = strcpyExA(szDest, szExpires);
  962. }
  963. if (m_szDomain) {
  964. szDest = strcpyExA(szDest, "; domain=");
  965. szDest = DBCSEncode(szDest, m_szDomain);
  966. }
  967. szDest = strcpyExA(szDest, "; path=");
  968. szDest = DBCSEncode(szDest, m_szPath);
  969. if (m_fSecure)
  970. szDest = strcpyExA(szDest, "; secure");
  971. return szDest;
  972. }
  973. /*------------------------------------------------------------------
  974. * C C o o k i e P a i r
  975. */
  976. /*===================================================================
  977. CCookiePair::CCookiePair
  978. constructor
  979. ===================================================================*/
  980. CCookiePair::CCookiePair()
  981. {
  982. m_fDuplicate = FALSE;
  983. m_szValue = NULL;
  984. }
  985. /*===================================================================
  986. CCookiePair::Init
  987. Initialize the cookie pair with a key and a value. Optionally,
  988. it will copy the strings as well.
  989. ===================================================================*/
  990. HRESULT CCookiePair::Init(const char *szKey, const char *szValue, BOOL fDuplicate)
  991. {
  992. m_fDuplicate = fDuplicate;
  993. if (fDuplicate)
  994. {
  995. char *szNewKey = (char *)malloc(strlen(szKey) + 1);
  996. if (szNewKey == NULL)
  997. return E_OUTOFMEMORY;
  998. char *szNewValue = (char *)malloc(strlen(szValue) + 1);
  999. if (szNewValue == NULL)
  1000. {
  1001. free(szNewKey);
  1002. return E_OUTOFMEMORY;
  1003. }
  1004. if (FAILED(CLinkElem::Init(strcpy(szNewKey, szKey), strlen(szKey))))
  1005. {
  1006. free(szNewKey);
  1007. free(szNewValue);
  1008. return E_FAIL;
  1009. }
  1010. m_szValue = strcpy(szNewValue, szValue);
  1011. }
  1012. else
  1013. {
  1014. if (FAILED(CLinkElem::Init(const_cast<char *>(szKey), strlen(szKey))))
  1015. return E_FAIL;
  1016. m_szValue = const_cast<char *>(szValue);
  1017. }
  1018. return S_OK;
  1019. }
  1020. /*===================================================================
  1021. CCookiePair::~CCookiePair
  1022. destructor
  1023. ===================================================================*/
  1024. CCookiePair::~CCookiePair()
  1025. {
  1026. if (m_fDuplicate)
  1027. {
  1028. if (m_pKey) free(m_pKey);
  1029. if (m_szValue) free(m_szValue);
  1030. }
  1031. }
  1032. /*------------------------------------------------------------------
  1033. * C C o o k i e I t e r a t o r
  1034. */
  1035. /*===================================================================
  1036. CCookieIterator::CCookieIterator
  1037. Constructor
  1038. ===================================================================*/
  1039. CCookieIterator::CCookieIterator(CCookie *pCookie)
  1040. {
  1041. m_pCookie = pCookie;
  1042. m_pCurrent = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.Head());
  1043. m_cRefs = 1;
  1044. m_pCookie->AddRef();
  1045. }
  1046. /*===================================================================
  1047. CCookieIterator::CCookieIterator
  1048. Destructor
  1049. ===================================================================*/
  1050. CCookieIterator::~CCookieIterator()
  1051. {
  1052. m_pCookie->Release();
  1053. }
  1054. /*===================================================================
  1055. CCookieIterator::QueryInterface
  1056. CCookieIterator::AddRef
  1057. CCookieIterator::Release
  1058. IUnknown members for CServVarsIterator object.
  1059. ===================================================================*/
  1060. STDMETHODIMP CCookieIterator::QueryInterface(REFIID iid, void **ppvObj)
  1061. {
  1062. if (iid == IID_IUnknown || iid == IID_IEnumVARIANT)
  1063. {
  1064. AddRef();
  1065. *ppvObj = this;
  1066. return S_OK;
  1067. }
  1068. *ppvObj = NULL;
  1069. return E_NOINTERFACE;
  1070. }
  1071. STDMETHODIMP_(ULONG) CCookieIterator::AddRef()
  1072. {
  1073. return ++m_cRefs;
  1074. }
  1075. STDMETHODIMP_(ULONG) CCookieIterator::Release()
  1076. {
  1077. if (--m_cRefs > 0)
  1078. return m_cRefs;
  1079. delete this;
  1080. return 0;
  1081. }
  1082. /*===================================================================
  1083. CCookieIterator::Clone
  1084. Clone this iterator (standard method)
  1085. ===================================================================*/
  1086. STDMETHODIMP CCookieIterator::Clone(IEnumVARIANT **ppEnumReturn)
  1087. {
  1088. CCookieIterator *pNewIterator = new CCookieIterator(m_pCookie);
  1089. if (pNewIterator == NULL)
  1090. return E_OUTOFMEMORY;
  1091. // new iterator should point to same location as this.
  1092. pNewIterator->m_pCurrent = m_pCurrent;
  1093. *ppEnumReturn = pNewIterator;
  1094. return S_OK;
  1095. }
  1096. /*===================================================================
  1097. CCookieIterator::Next
  1098. Get next value (standard method)
  1099. To rehash standard OLE semantics:
  1100. We get the next "cElements" from the collection and store them
  1101. in "rgVariant" which holds at least "cElements" items. On
  1102. return "*pcElementsFetched" contains the actual number of elements
  1103. stored. Returns S_FALSE if less than "cElements" were stored, S_OK
  1104. otherwise.
  1105. ===================================================================*/
  1106. STDMETHODIMP CCookieIterator::Next(unsigned long cElementsRequested, VARIANT *rgVariant, unsigned long *pcElementsFetched)
  1107. {
  1108. // give a valid pointer value to 'pcElementsFetched'
  1109. //
  1110. unsigned long cElementsFetched;
  1111. if (pcElementsFetched == NULL)
  1112. pcElementsFetched = &cElementsFetched;
  1113. // Loop through the collection until either we reach the end or
  1114. // cElements becomes zero
  1115. //
  1116. unsigned long cElements = cElementsRequested;
  1117. *pcElementsFetched = 0;
  1118. while (cElements > 0 && m_pCurrent != NULL)
  1119. {
  1120. BSTR bstrT;
  1121. if (FAILED(SysAllocStringFromSz(reinterpret_cast<char *>(m_pCurrent->m_pKey), 0, &bstrT, m_pCookie->m_lCodePage)))
  1122. return E_OUTOFMEMORY;
  1123. V_VT(rgVariant) = VT_BSTR;
  1124. V_BSTR(rgVariant) = bstrT;
  1125. ++rgVariant;
  1126. --cElements;
  1127. ++*pcElementsFetched;
  1128. m_pCurrent = static_cast<CCookiePair *>(m_pCurrent->m_pNext);
  1129. }
  1130. // initialize the remaining variants
  1131. //
  1132. while (cElements-- > 0)
  1133. VariantInit(rgVariant++);
  1134. return (*pcElementsFetched == cElementsRequested)? S_OK : S_FALSE;
  1135. }
  1136. /*===================================================================
  1137. CCookieIterator::Skip
  1138. Skip items (standard method)
  1139. To rehash standard OLE semantics:
  1140. We skip over the next "cElements" from the collection.
  1141. Returns S_FALSE if less than "cElements" were skipped, S_OK
  1142. otherwise.
  1143. ===================================================================*/
  1144. STDMETHODIMP CCookieIterator::Skip(unsigned long cElements)
  1145. {
  1146. /* Loop through the collection until either we reach the end or
  1147. * cElements becomes zero
  1148. */
  1149. while (cElements > 0 && m_pCurrent != NULL)
  1150. {
  1151. --cElements;
  1152. m_pCurrent = static_cast<CCookiePair *>(m_pCurrent->m_pNext);
  1153. }
  1154. return (cElements == 0)? S_OK : S_FALSE;
  1155. }
  1156. /*===================================================================
  1157. CCookieIterator::Reset
  1158. Reset the iterator (standard method)
  1159. ===================================================================*/
  1160. STDMETHODIMP CCookieIterator::Reset()
  1161. {
  1162. m_pCurrent = static_cast<CCookiePair *>(m_pCookie->m_mpszValues.Head());
  1163. return S_OK;
  1164. }