pxcookie.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:9k
源码类别:

Symbian

开发平台:

C/C++

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