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

Symbian

开发平台:

Visual C++

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