pxcookie.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:9k
源码类别:

Symbian

开发平台:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: pxcookie.cpp,v 1.2.24.1 2004/07/09 01:54:51 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. // include
  50. #include "hxtypes.h"
  51. #include "hxcom.h"
  52. #include "hxcomm.h"
  53. #include "ihxpckts.h"
  54. // hxmisc
  55. #include "chxpckts.h"
  56. #include "hxurl.h"
  57. // pxcomlib
  58. #include "pxcookie.h"
  59. // hxdebug
  60. #include "hxheap.h"
  61. #ifdef _DEBUG
  62. #undef HX_THIS_FILE
  63. static char HX_THIS_FILE[] = __FILE__;
  64. #endif
  65. char* reverse_string(const char* pszStr)
  66. {
  67.     char* pszRev = NULL;
  68.     if (pszStr)
  69.     {
  70.         INT32 lLen = strlen(pszStr);
  71.         pszRev = new char [lLen + 1];
  72.         if (pszRev)
  73.         {
  74.             const char* pszStrCopy = pszStr + lLen - 1;
  75.             char*       pszRevCopy = pszRev;
  76.             while (pszStrCopy >= pszStr)
  77.             {
  78.                 *pszRevCopy++ = *pszStrCopy--;
  79.             }
  80.             *pszRevCopy = '';
  81.         }
  82.     }
  83.     return pszRev;
  84. }
  85. HX_RESULT MakeDomainFromHost(IHXBuffer*             pHostStr,
  86.                              IHXCommonClassFactory* pFactory,
  87.                              REF(IHXBuffer*)        rpDomainStr)
  88. {
  89.     HX_RESULT retVal = HXR_OK;
  90.     if (pHostStr && pFactory)
  91.     {
  92.         // We will attempt to construct a domain from the host
  93.         //
  94.         const char* pszHost   = (const char*) pHostStr->GetBuffer();
  95.         INT32       lHostLen  = strlen(pszHost);
  96.         char*       pszDomain = NULL;
  97.         // First, create a string which is a reversed copy of the pHostStr
  98.         char* pszHostRev = reverse_string(pszHost);
  99.         if (pszHostRev)
  100.         {
  101.             // Now search the reversed string for two dots
  102.             char* pszRightMostDot = strchr(pszHostRev, '.');
  103.             if (pszRightMostDot)
  104.             {
  105.                 char* pszSecondRightMostDot = strchr(pszRightMostDot + 1, '.');
  106.                 if (pszSecondRightMostDot)
  107.                 {
  108.                     // We found two dots from the right in the host,
  109.                     // so we will use that as the domain. End the reversed
  110.                     // string right after the second dot.
  111.                     *(pszSecondRightMostDot + 1) = '';
  112.                     // Re-reverse the string
  113.                     pszDomain = reverse_string((const char*) pszHostRev);
  114.                 }
  115.                 else
  116.                 {
  117.                     // We only found one dot from the right in the host,
  118.                     // so we prepend a dot
  119.                     pszDomain = new char [lHostLen + 2];
  120.                     if (pszDomain)
  121.                     {
  122.                         strcpy(pszDomain, "."); /* Flawfinder: ignore */
  123.                         strcat(pszDomain, pszHost); /* Flawfinder: ignore */
  124.                     }
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 // There are no dots in the host at all,
  130.                 // so we will pass the host straight through
  131.                 pszDomain = new char [lHostLen + 1];
  132.                 if (pszDomain)
  133.                 {
  134.                     strcpy(pszDomain, pszHost); /* Flawfinder: ignore */
  135.                 }
  136.             }
  137.         }
  138.         HX_VECTOR_DELETE(pszHostRev);
  139.         if (pszDomain)
  140.         {
  141.             // Create an IHXBuffer
  142.             IHXBuffer* pBuffer = NULL;
  143.             retVal              = pFactory->CreateInstance(CLSID_IHXBuffer,
  144.                                                            (void**) &pBuffer);
  145.             if (SUCCEEDED(retVal))
  146.             {
  147.                 // Copy the domain to the IHXBuffer
  148.                 retVal = pBuffer->Set((const unsigned char*) pszDomain, strlen(pszDomain) + 1);
  149.                 if (SUCCEEDED(retVal))
  150.                 {
  151.                     // Copy the out parameter
  152.                     HX_RELEASE(rpDomainStr);
  153.                     rpDomainStr = pBuffer;
  154.                     rpDomainStr->AddRef();
  155.                 }
  156.             }
  157.             HX_RELEASE(pBuffer);
  158.         }
  159.         else
  160.         {
  161.             retVal = HXR_FAIL;
  162.         }
  163.         HX_VECTOR_DELETE(pszDomain);
  164.     }
  165.     else
  166.     {
  167.         retVal = HXR_INVALID_PARAMETER;
  168.     }
  169.     return retVal;
  170. }
  171. HX_RESULT GetHostAndPathFromURL(const char*             pszURL,
  172.                                 IHXCommonClassFactory* pFactory,
  173.                                 REF(IHXBuffer*)        rpHostStr,
  174.                                 REF(IHXBuffer*)        rpPathStr)
  175. {
  176.     HX_RESULT retVal = HXR_OK;
  177.     if (pszURL && pFactory)
  178.     {
  179.         // Create a CHXURL from the url
  180.         CHXURL* pURL = new CHXURL(pszURL);
  181.         if (pURL)
  182.         {
  183.             retVal = pURL->GetLastError();
  184.             if (SUCCEEDED(retVal))
  185.             {
  186.                 // Get the properties from the CHXURL
  187.                 IHXValues* pHeader = pURL->GetProperties();
  188.                 if (pHeader)
  189.                 {
  190.                     // Get the host from CHXURL
  191.                     IHXBuffer* pHostStr = NULL;
  192.                     retVal               = pHeader->GetPropertyBuffer(PROPERTY_HOST, pHostStr);
  193.                     if (SUCCEEDED(retVal))
  194.                     {
  195.                         // Make a domain from the host
  196.                         IHXBuffer* pDomainStr = NULL;
  197.                         retVal                 = MakeDomainFromHost(pHostStr, pFactory, pDomainStr);
  198.                         if (SUCCEEDED(retVal))
  199.                         {
  200.                             // Always use "/" for the path
  201.                             IHXBuffer* pPathStr = NULL;
  202.                             retVal               = pFactory->CreateInstance(CLSID_IHXBuffer,
  203.                                                                             (void**) &pPathStr);
  204.                             if (SUCCEEDED(retVal))
  205.                             {
  206.                                 const char* pszPath = "/";
  207.                                 retVal = pPathStr->Set((const unsigned char*) pszPath, strlen(pszPath) + 1);
  208.                                 if (SUCCEEDED(retVal))
  209.                                 {
  210.                                     HX_RELEASE(rpHostStr);
  211.                                     rpHostStr = pDomainStr;
  212.                                     rpHostStr->AddRef();
  213.                                     HX_RELEASE(rpPathStr);
  214.                                     rpPathStr = pPathStr;
  215.                                     rpPathStr->AddRef();
  216.                                 }
  217.                             }
  218.                             HX_RELEASE(pPathStr);
  219.                         }
  220.                         HX_RELEASE(pDomainStr);
  221.                     }
  222.                     HX_RELEASE(pHostStr);
  223.                 }
  224.                 else
  225.                 {
  226.                     retVal = HXR_FAIL;
  227.                 }
  228.                 HX_RELEASE(pHeader);
  229.             }
  230.         }
  231.         else
  232.         {
  233.             retVal = HXR_OUTOFMEMORY;
  234.         }
  235.         HX_DELETE(pURL);
  236.     }
  237.     else
  238.     {
  239.         retVal = HXR_INVALID_PARAMETER;
  240.     }
  241.     return retVal;
  242. }