pnpmobileservices/pnpms/PnP/NHwrParser/NHeadWrapperParser.cpp
changeset 0 3ce708148e4d
equal deleted inserted replaced
-1:000000000000 0:3ce708148e4d
       
     1 /*
       
     2 * Copyright (c) 2004-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Implementation of PnPMS components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32svr.h>
       
    21 #include "NHeadWrapperParser.h"
       
    22 #include "NHwrParserLogger.h"
       
    23 #include "HttpProvHeaders.h"
       
    24 #include "HttpProvContent.h"
       
    25 #include "HttpProvContentType.h"
       
    26 #include "SupportedContentTypes.h"
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CNHeadWrapperParser::CNHeadWrapperParser
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CNHeadWrapperParser::CNHeadWrapperParser()
       
    37     {
       
    38     }
       
    39     
       
    40 // Destructor
       
    41 EXPORT_C CNHeadWrapperParser::~CNHeadWrapperParser()
       
    42     {
       
    43     LOGSTRING("~CNHeadWrapperParser");
       
    44     delete iHttpProvHeaders;
       
    45     delete iHttpProvContentType;
       
    46     LOGSTRING("~CNHeadWrapperParser - done");
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CNHeadWrapperParser::NewL
       
    51 // Two-phased constructor.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 EXPORT_C CNHeadWrapperParser* CNHeadWrapperParser::NewL(const TPtrC8& aWrapperData)
       
    55     {
       
    56     CNHeadWrapperParser* self = new (ELeave) CNHeadWrapperParser;
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL( aWrapperData );
       
    59     CleanupStack::Pop( self );
       
    60     return self;
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CNHeadWrapperParser::ConstructL
       
    65 // Symbian 2nd phase constructor can leave.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CNHeadWrapperParser::ConstructL( const TPtrC8& aWrapperData )
       
    69     {
       
    70     iWrapperData.Set( aWrapperData );
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CNHeadWrapperParser::ParseL
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C THttpProvStates::TProvisioningStatus CNHeadWrapperParser::Parse()
       
    78     {
       
    79     LOGSTRING("CNHeadWrapperParser::Parse - begin");
       
    80     TRAPD( err, DoParseL() );
       
    81     if( err != KErrNone )
       
    82         {
       
    83         LOGSTRING("CNHeadWrapperParser::Parse ret: EStatusWrapperParsingFailed");
       
    84         return THttpProvStates::EStatusWrapperParsingFailed; // Do not continue
       
    85         }
       
    86         
       
    87     TRAP( err, CheckHeadWrapperL() );
       
    88 
       
    89     // CheckHeadWrapperL might leave with one of TProvisioningStatus codes
       
    90     // or with standard error codes
       
    91     if( err < 0 )    // standar error codes
       
    92         {
       
    93         LOGSTRING("CNHeadWrapperParser::Parse ret: EStatusWrapperParsingFailed");
       
    94         return THttpProvStates::EStatusWrapperParsingFailed;
       
    95         }
       
    96     else            // one of TProvisioningStatus
       
    97         {
       
    98         LOGSTRING2("CNHeadWrapperParser::Parse ret: %d", err);
       
    99         return (THttpProvStates::TProvisioningStatus) err;
       
   100         }
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CNHeadWrapperParser::DoParseL
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 void CNHeadWrapperParser::DoParseL()
       
   108     {
       
   109     LOGSTRING( "CNHeadWrapperParser::DoParseL - begin" );
       
   110     LOGSTRING( "Check wrapper length " );
       
   111     // Check wrapper length min 6 bytes
       
   112     if( iWrapperData.Length() < 6 )
       
   113         {
       
   114         User::Leave( KErrCorrupt );
       
   115         }
       
   116 
       
   117     LOGSTRING( "Check signature" );
       
   118     // Check signature
       
   119     _LIT8( KHeadWrapperSignature, "NHWR" );
       
   120     if( iWrapperData.Left(4).Compare( KHeadWrapperSignature ) != 0 )
       
   121         {
       
   122         User::Leave( KErrCorrupt );
       
   123         }
       
   124 
       
   125     // Parse version
       
   126     TUint8 version = (iWrapperData)[4];
       
   127     if( version != 1 )
       
   128         {
       
   129         User::Leave( KErrNotSupported );
       
   130         }
       
   131         
       
   132     LOGSTRING( "Parse content type" );
       
   133     // Parse content type
       
   134     TUint8 contentTypeLen = (iWrapperData)[5];
       
   135     LOGSTRING2( "content type len: %i", contentTypeLen );
       
   136 
       
   137     if( iWrapperData.Length() < 6 + contentTypeLen )
       
   138         {   
       
   139         User::Leave( KErrCorrupt );
       
   140         }
       
   141 
       
   142     if( contentTypeLen < 1 )
       
   143         {
       
   144         User::Leave( KErrCorrupt );
       
   145         }
       
   146 
       
   147     iContentTypeField.Set( iWrapperData.Mid( 6, contentTypeLen ) );
       
   148 
       
   149     // Parse Headers and Content Length
       
   150     LOGSTRING( "Parse headers and data length" );
       
   151     if( iWrapperData.Length() < 6+contentTypeLen+2 ) //  +2 min for length datas
       
   152         {
       
   153         User::Leave( KErrCorrupt );
       
   154         }
       
   155     TUint32    headersLen = 0; //(*iWrapperData)[6+contentTypeLen+1];
       
   156     TUint32    contentLen = 0; //(*iWrapperData)[6+contentTypeLen+2];
       
   157     TUint headersVarLen = 0;
       
   158     TUint contentVarLen = 0;
       
   159 
       
   160     if( ParseUintVar( iWrapperData.Mid( 6+contentTypeLen ),
       
   161                       headersLen, headersVarLen ) != KErrNone )
       
   162         {
       
   163         User::Leave( KErrCorrupt );
       
   164         }
       
   165     LOGSTRING2( "headersLen %i", headersLen );
       
   166     LOGSTRING2( "headersVarLen %i", headersLen );
       
   167 
       
   168     if( ParseUintVar( iWrapperData.Mid( 6+contentTypeLen+headersVarLen ),
       
   169                       contentLen, contentVarLen ) != KErrNone )
       
   170         {
       
   171         LOGSTRING( "content length failed" );
       
   172         User::Leave( KErrCorrupt );
       
   173         }
       
   174     LOGSTRING2( "contentLen %i", contentLen );
       
   175     LOGSTRING2( "contentVarLen %i", contentVarLen );
       
   176 
       
   177     // Final check for sizes
       
   178     if( iWrapperData.Length() != (TInt)( 6+contentTypeLen+headersVarLen
       
   179         +contentVarLen+headersLen+contentLen ) )
       
   180         {
       
   181         LOGSTRING( "Lengths do not match" );
       
   182         LOGSTRING2( "Expected length %i", (TInt)( 6+contentTypeLen+headersVarLen
       
   183                                             +contentVarLen+headersLen+contentLen ) );
       
   184         LOGSTRING2( "Actual data length %i", iWrapperData.Length() );
       
   185         User::Leave( KErrCorrupt );
       
   186         }
       
   187 
       
   188     if( headersLen > 0 )
       
   189         {
       
   190         iHeaders.Set( iWrapperData.Mid( 6+contentTypeLen+headersVarLen
       
   191                                          +contentVarLen, headersLen ) ); 
       
   192         }
       
   193 
       
   194     LOGSTRING( "Parse content" );
       
   195     iContent.Set( iWrapperData.Mid( 6+contentTypeLen+headersVarLen+contentVarLen+headersLen,contentLen ) );
       
   196 
       
   197     // Log
       
   198 #ifdef _DEBUG
       
   199     TInt i(0);
       
   200     LOGSTRING( "Content type:" );
       
   201     for( i = 0; i < iContentTypeField.Length(); i += 128 )
       
   202         {
       
   203         TPtrC8 logText = iContentTypeField.Right( iContentTypeField.Length() - i );
       
   204         LOGTEXT( logText );
       
   205         }
       
   206 
       
   207     if( iHeaders.Length() )
       
   208         {
       
   209         LOGSTRING( "Headers:" );
       
   210         for( i = 0; i < iHeaders.Length(); i += 128 )
       
   211             {
       
   212             TPtrC8 logText = iHeaders.Right( iHeaders.Length() - i );
       
   213             LOGTEXT( logText );
       
   214             }
       
   215         }
       
   216 
       
   217     LOGSTRING( "Content:" );
       
   218     for( i = 0; i < iContent.Length(); i += 128 )
       
   219         {
       
   220         TPtrC8 logText = iContent.Right( iContent.Length() - i );
       
   221         LOGTEXT( logText );
       
   222         }
       
   223 #endif
       
   224     LOGSTRING( "CNHeadWrapperParser::DoParseL - end" );
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CNHeadWrapperParser::CheckHeadWrapperL
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CNHeadWrapperParser::CheckHeadWrapperL()
       
   232     {
       
   233     LOGSTRING( "CNHeadWrapperParser::CheckHeadWrapperL()" );
       
   234     LOGSTRING( "Get content type" );
       
   235     if( iContentTypeField.Length() < 1 )
       
   236         {
       
   237         User::Leave( THttpProvStates::EStatusWrapperParsingFailed );
       
   238         }
       
   239 
       
   240     LOGSTRING( "Create CHttpProvContentType" );
       
   241     if( iHttpProvContentType )
       
   242         {
       
   243         delete iHttpProvContentType;
       
   244         iHttpProvContentType = 0;
       
   245         }
       
   246     TRAPD( err, iHttpProvContentType = CHttpProvContentType::NewL( iContentTypeField ) );
       
   247     LOGSTRING( "Create CHttpProvContentType 2" );
       
   248     if( err != KErrNone )
       
   249         {
       
   250         LOGSTRING2( "Content type, create failed: %i", err );
       
   251         User::Leave( THttpProvStates::EStatusWrapperParsingFailed );
       
   252         }
       
   253 
       
   254     LOGSTRING( "Get headers" );
       
   255     if( iHeaders.Length() < 1 )
       
   256         {
       
   257         LOGSTRING( "no headers" );
       
   258         // Plug and Play Mobile Services Specification.doc:
       
   259         // "The Headers field MAY contain headers defining additional
       
   260         // Meta data about the content"
       
   261         // if there is no headers in the wrapper we will just leave,
       
   262         // we do not want to open any unauthorized content:
       
   263         User::Leave( THttpProvStates::EStatusWrapperParsingFailed );
       
   264         }
       
   265 
       
   266     delete iHttpProvHeaders;
       
   267     iHttpProvHeaders = 0;
       
   268     iHttpProvHeaders = CHttpProvHeaders::NewL( iHeaders );
       
   269     
       
   270     LOGSTRING( "Get wrapper content" );
       
   271     if( iContent.Length() < 1 )
       
   272         {
       
   273         User::Leave( THttpProvStates::EStatusWrapperParsingFailed );
       
   274         }
       
   275 
       
   276     CHttpProvContent* c = CHttpProvContent::NewLC( iContent );
       
   277 
       
   278     TInt res( KErrNone );
       
   279     TRAP( err, res = c->AuthenticateL( *iHttpProvHeaders, *iHttpProvContentType ) );
       
   280     if( res == KHttpProvAuthResultTokenExpired )
       
   281         {
       
   282         User::Leave( THttpProvStates::EStatusTokenExpired );
       
   283         }
       
   284     else if( res!=KHttpProvAuthResultAuthenticated || err != KErrNone )
       
   285         {
       
   286         LOGSTRING2("AuthenticationFailed, res: %i", res );
       
   287         LOGSTRING2("err: %i", err );
       
   288         User::Leave( THttpProvStates::EStatusSignatureFailed );
       
   289         }
       
   290 
       
   291     CleanupStack::PopAndDestroy(c);
       
   292     LOGSTRING( "CNHeadWrapperParser::CheckHeadWrapperL() - done" );
       
   293     }
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CNHeadWrapperParser::ParseUintVar
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 TInt CNHeadWrapperParser::ParseUintVar(const TDesC8& aVarDes, TUint32& aVar, TUint &aVarLength)
       
   300     {
       
   301     TUint32 finalValue = 0;
       
   302     TInt i;
       
   303     for(i=0; i<5; i++)
       
   304         {
       
   305         if(aVarDes.Length()<=i)
       
   306             {
       
   307             return KErrCorrupt;
       
   308             }
       
   309 
       
   310         TUint8 val = aVarDes[i];
       
   311         finalValue <<= 7;
       
   312         finalValue |= (((TUint32)val)&0x7F);
       
   313         if( !(val&0x80 ) )
       
   314             {
       
   315             break;
       
   316             }
       
   317         }
       
   318 
       
   319     if(i==5)
       
   320         {
       
   321         return KErrCorrupt;
       
   322         }
       
   323     else
       
   324         {
       
   325         aVar = finalValue;
       
   326         aVarLength = i+1;
       
   327         return KErrNone;
       
   328         }
       
   329     }
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CNHeadWrapperParser::GetHeaders
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 EXPORT_C const TDesC8& CNHeadWrapperParser::GetHeadersL() const
       
   336     {
       
   337     if( iHeaders.Length() < 1 )
       
   338         {
       
   339         User::Leave( KErrNotFound );
       
   340         }
       
   341     return iHeaders;
       
   342     }
       
   343 
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CNHeadWrapperParser::GetContentType
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 EXPORT_C const TDesC8& CNHeadWrapperParser::GetContentTypeL() const
       
   350     {
       
   351     if( !iHttpProvContentType )
       
   352         {
       
   353         User::Leave( KErrNotFound );
       
   354         }
       
   355     return iHttpProvContentType->ContentTypeL();
       
   356     }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CNHeadWrapperParser::GetContent
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 EXPORT_C const TDesC8& CNHeadWrapperParser::GetContentL() const
       
   363     {
       
   364     if( iContent.Length() < 1 )
       
   365         {
       
   366         User::Leave( KErrNotFound );
       
   367         }
       
   368     return iContent;
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CNHeadWrapperParser::GetReportUrlL
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 EXPORT_C const TDesC8& CNHeadWrapperParser::GetReportUrlL() const
       
   376     {
       
   377     if( !iHttpProvHeaders )
       
   378         {
       
   379         User::Leave( KErrNotFound );
       
   380         }
       
   381     return iHttpProvHeaders->GetParamValL( KReportUrl );
       
   382     }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CNHeadWrapperParser::GetProvisioningActivation
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 EXPORT_C TProvisioningActivation CNHeadWrapperParser::GetProvisioningActivation() const
       
   389     {
       
   390     // The default value is "User selects"
       
   391     TProvisioningActivation value( EProvisioningActivationUser );
       
   392 
       
   393     if( !iHttpProvHeaders )
       
   394         {
       
   395         return value;
       
   396         }
       
   397 
       
   398     TInt err( KErrNone );
       
   399     TPtrC8 descValue( KNullDesC8 );
       
   400     TRAP( err, descValue.Set( iHttpProvHeaders->GetParamValL( KProvisioningActivation ) ) );
       
   401     if( err != KErrNone )
       
   402         {
       
   403         return value;
       
   404         }
       
   405 
       
   406     TInt intValue(0);
       
   407     TLex8 parser( descValue );
       
   408     err = parser.Val( intValue );
       
   409 
       
   410     // ignore illegal values
       
   411     if( err != KErrNone )
       
   412         {
       
   413         return value;
       
   414         }
       
   415 
       
   416     if( intValue < EProvisioningActivationUser || intValue > EProvisioningActivationOff )
       
   417         {
       
   418         return value;
       
   419         }
       
   420 
       
   421     // cast
       
   422     value = (TProvisioningActivation) intValue;
       
   423     return value;
       
   424     }
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CNHeadWrapperParser::GetUserInteraction
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 EXPORT_C TBool CNHeadWrapperParser::GetUserInteraction() const
       
   431     {
       
   432     // The default value is EFalse
       
   433     TBool value( EFalse );
       
   434 
       
   435     if( !iHttpProvHeaders )
       
   436         {
       
   437         return value;
       
   438         }
       
   439 
       
   440     TInt err( KErrNone );
       
   441     TRAP( err, value = GetBoolParamValueL( KUserInteraction ) );
       
   442     return value;
       
   443     }
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // CNHeadWrapperParser::GetBoolParamValueL
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 TBool CNHeadWrapperParser::GetBoolParamValueL( const TDesC8& aParam ) const
       
   450     {
       
   451     LOGSTRING("CNHeadWrapperParser::GetBoolParamValueL");
       
   452     LOGTEXT( aParam );
       
   453     const TDesC8& paramVal = iHttpProvHeaders->GetParamValL( aParam );
       
   454     LOGTEXT( paramVal );
       
   455     if( paramVal.Compare( KStringValueTrue ) == 0 )
       
   456         {
       
   457         return ETrue;
       
   458         }
       
   459     return EFalse;        
       
   460     }
       
   461 
       
   462 // End of File