buffutil.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. // system
  36. #include "hlxclib/string.h"
  37. #include "safestring.h"
  38. // include
  39. #include "hxtypes.h"
  40. #include "hxcom.h"
  41. #include "ihxpckts.h"
  42. #include "hxcomm.h"
  43. #include "hxfiles.h"
  44. // pncont
  45. #include "hxstring.h"
  46. #include "chxpckts.h"
  47. // pnmisc
  48. #include "hxurl.h"
  49. // pxcomlib
  50. #include "buffutil.h"
  51. HX_RESULT PXUtilities::CreateSizedBuffer(UINT32 ulSize, IUnknown* pContext,
  52.                                          REF(IHXBuffer*) rpBuffer)
  53. {
  54.     HX_RESULT retVal = HXR_FAIL;
  55.     if (pContext && ulSize)
  56.     {
  57.         IHXCommonClassFactory* pFactory = NULL;
  58.         retVal                           = pContext->QueryInterface(IID_IHXCommonClassFactory,
  59.                                                                     (void**) &pFactory);
  60.         if (SUCCEEDED(retVal))
  61.         {
  62.             IHXBuffer* pBuffer = NULL;
  63.             retVal              = pFactory->CreateInstance(CLSID_IHXBuffer,
  64.                                                            (void**) &pBuffer);
  65.             if (SUCCEEDED(retVal))
  66.             {
  67.                 retVal = pBuffer->SetSize(ulSize);
  68.                 if (SUCCEEDED(retVal))
  69.                 {
  70.                     HX_RELEASE(rpBuffer);
  71.                     rpBuffer = pBuffer;
  72.                     rpBuffer->AddRef();
  73.                 }
  74.             }
  75.             HX_RELEASE(pBuffer);
  76.         }
  77.         HX_RELEASE(pFactory);
  78.     }
  79.     return retVal;
  80. }
  81. HX_RESULT PXUtilities::CreateStringBuffer(const char*      pszStr,
  82.                                           IUnknown*        pContext,
  83.                                           REF(IHXBuffer*) rpBuffer)
  84. {
  85.     HX_RESULT retVal = HXR_FAIL;
  86.     if (pszStr && pContext)
  87.     {
  88.         IHXCommonClassFactory* pFactory = NULL;
  89.         retVal                           = pContext->QueryInterface(IID_IHXCommonClassFactory,
  90.                                                                     (void**) &pFactory);
  91.         if (SUCCEEDED(retVal))
  92.         {
  93.             IHXBuffer* pBuffer = NULL;
  94.             retVal              = pFactory->CreateInstance(CLSID_IHXBuffer,
  95.                                                            (void**) &pBuffer);
  96.             if (SUCCEEDED(retVal))
  97.             {
  98.                 retVal = pBuffer->Set((const UCHAR*) pszStr,
  99.                                       strlen(pszStr) + 1);
  100.                 if (SUCCEEDED(retVal))
  101.                 {
  102.                     HX_RELEASE(rpBuffer);
  103.                     rpBuffer = pBuffer;
  104.                     rpBuffer->AddRef();
  105.                 }
  106.             }
  107.             HX_RELEASE(pBuffer);
  108.         }
  109.         HX_RELEASE(pFactory);
  110.     }
  111.     return retVal;
  112. }
  113. HX_RESULT PXUtilities::GetURLParam(IHXRequest*     pRequest,
  114.                                    BOOL             bAddBase,
  115.                                    IUnknown*        pContext,
  116.                                    const char*      pszParamName,
  117.                                    REF(IHXBuffer*) rpParamValue)
  118. {
  119.     HX_RESULT retVal = HXR_FAIL;
  120.     if (pRequest && pContext && pszParamName)
  121.     {
  122.         // First try and get the parameter
  123.         // as an URL-encoded parameter
  124.         const char* pszURL = NULL;
  125.         retVal = pRequest->GetURL(pszURL);
  126.         if (SUCCEEDED(retVal))
  127.         {
  128.             // Create an URL string
  129.             CHXString cDummy;
  130.             if (bAddBase)
  131.             {
  132.                 cDummy = "rtsp://chxurl-sucks.com/";
  133.             }
  134.             cDummy += pszURL;
  135.             // Set up a CHXURL object
  136.             CHXURL cURL(cDummy);
  137.             retVal = cURL.GetLastError();
  138.             if (SUCCEEDED(retVal))
  139.             {
  140.                 // Get the URL-encoded parameters from the url object
  141.                 IHXValues* pOptions = NULL;
  142.                 pOptions = cURL.GetOptions();
  143.                 if (pOptions)
  144.                 {
  145.                     // If the param is a string, it will be a "buffer" property
  146.                     // coming from CHXURL.
  147.                     HX_RELEASE(rpParamValue);
  148.                     pOptions->GetPropertyBuffer(pszParamName, rpParamValue);
  149.                     if (!rpParamValue)
  150.                     {
  151.                         // If the param is purely digits, then CHXURL will
  152.                         // pass it as a "ULONG32" property
  153.                         UINT32 ulValue = 0;
  154.                         retVal = pOptions->GetPropertyULONG32(pszParamName, ulValue);
  155.                         if (SUCCEEDED(retVal))
  156.                         {
  157.                             // It WAS a ULONG32 property, so we need to
  158.                             // convert it back to a buffer
  159.                             //
  160.                             // UINT32 can't be any more than 10 characters, but
  161.                             // we'll be safe
  162.                             char szTmp[16]; /* Flawfinder: ignore */
  163.                             SafeSprintf(szTmp, sizeof(szTmp), "%lu", ulValue);
  164.                             // Now convert to string
  165.                             retVal = PXUtilities::CreateStringBuffer((const char*) szTmp,
  166.                                                                      pContext,
  167.                                                                      rpParamValue);
  168.                         }
  169.                     }
  170.                 }
  171.                 else
  172.                 {
  173.                     retVal = HXR_FAIL;
  174.                 }
  175.                 HX_RELEASE(pOptions);
  176.             }
  177.         }
  178.     }
  179.     return retVal;
  180. }
  181. HX_RESULT PXUtilities::GetRequestParam(IHXRequest*     pRequest,
  182.                                        IUnknown*        pContext,
  183.                                        const char*      pszParamName,
  184.                                        REF(IHXBuffer*) rpParamValue)
  185. {
  186.     HX_RESULT retVal = HXR_FAIL;
  187.     if (pRequest && pContext && pszParamName)
  188.     {
  189.         // Obtain the request headers from the IHXRequest
  190.         IHXValues* pHeaders = NULL;
  191.         pRequest->GetRequestHeaders(pHeaders);
  192.         if (pHeaders)
  193.         {
  194.             // All properties coming out of the request
  195.             // headers will be CString properties
  196.             HX_RELEASE(rpParamValue);
  197.             retVal = pHeaders->GetPropertyCString(pszParamName, rpParamValue);
  198.         }
  199.         HX_RELEASE(pHeaders);
  200.     }
  201.     return retVal;
  202. }
  203. HX_RESULT PXUtilities::GetURLOrRequestParam(IHXRequest*     pRequest,
  204.                                             BOOL             bAddBase,
  205.                                             IUnknown*        pContext,
  206.                                             const char*      pszParamName,
  207.                                             REF(IHXBuffer*) rpParamValue)
  208. {
  209.     HX_RESULT retVal = HXR_FAIL;
  210.     if (pRequest && pContext && pszParamName)
  211.     {
  212.         // Try first to get it as a URL-encoded parameter
  213.         retVal = PXUtilities::GetURLParam(pRequest, bAddBase, pContext,
  214.                                           pszParamName, rpParamValue);
  215.         if (FAILED(retVal))
  216.         {
  217.             // Didn't get it as a URL-encoded parameter, so
  218.             // try getting it from the request headers
  219.             retVal = PXUtilities::GetRequestParam(pRequest, pContext,
  220.                                                   pszParamName, rpParamValue);
  221.         }
  222.     }
  223.     return retVal;
  224. }