upnp/upnpstack/dlnawebserver/src/upnphttpchunkparser.cpp
changeset 0 f5a58ecadc66
child 9 e84a4b44b2e8
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-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:  Declares chunk parsing class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnphttpchunkparser.h"
       
    21 #include "upnpcons.h"
       
    22 
       
    23 
       
    24 // CONSTANTS
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CUpnpHttpChunkParser::NewL
       
    30 // Two-phased constructor
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CUpnpHttpChunkParser* CUpnpHttpChunkParser::NewL()
       
    34 	{
       
    35 
       
    36 	CUpnpHttpChunkParser* self = new (ELeave) CUpnpHttpChunkParser();
       
    37 	CleanupStack::PushL( self );
       
    38 	self->ConstructL();
       
    39 	CleanupStack::Pop( self );
       
    40 	
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CUpnpHttpChunkParser::~CUpnpHttpChunkParser
       
    46 // C++ default destructor
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CUpnpHttpChunkParser::~CUpnpHttpChunkParser()
       
    50 	{
       
    51 	}
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CUpnpHttpChunkParser::CUpnpHttpChunkParser
       
    55 // C++ default constructor
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CUpnpHttpChunkParser::CUpnpHttpChunkParser()
       
    59 :iContext(EUnknown),
       
    60  iChunkSize(KErrNotFound),
       
    61  iError (KErrNone)
       
    62 	{
       
    63 	}
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CUpnpHttpChunkParser::ConstructL
       
    67 // Two-phased constructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 void CUpnpHttpChunkParser::ConstructL()
       
    71 	{
       
    72 	}
       
    73    
       
    74 // -----------------------------------------------------------------------------
       
    75 // CUpnpHttpChunkParser::Reset
       
    76 // Checks if decoding ended
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void CUpnpHttpChunkParser::Reset()
       
    80     {
       
    81     iContext = EUnknown;
       
    82     iError = KErrNone;
       
    83     iChunkSize = KErrNotFound;	
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CUpnpHttpChunkParser::SetErrorState
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CUpnpHttpChunkParser::SetErrorState()
       
    91     {
       
    92     iError = EHttpRequestTimeout;
       
    93     iContext = EError;
       
    94     }				    
       
    95 	
       
    96 // -----------------------------------------------------------------------------
       
    97 // CUpnpHttpChunkParser::Parse
       
    98 // Decoding the chunked-encoded buffer
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 TInt CUpnpHttpChunkParser::Parse(TDes8& aBuffer, TInt& aPos)
       
   102     {		
       
   103     TBool interrupt = EFalse;
       
   104     while(!interrupt)
       
   105         {
       
   106         switch(iContext)
       
   107             {
       
   108             case EUnknown:
       
   109                 iContext = EHeader;
       
   110             case EHeader:
       
   111                 interrupt = ParseHeader(aBuffer,aPos);
       
   112                 break;
       
   113             case ELastChunk:
       
   114                 interrupt = ParseLastChunk(aBuffer,aPos);
       
   115                 break;	
       
   116             case EBody:
       
   117                 interrupt = ParseBody(aBuffer,aPos);
       
   118                 break;
       
   119             case EEndChunkBody:
       
   120                 iContext = EHeader;
       
   121                 break;
       
   122             case EExtension:
       
   123                 interrupt = ParseExtension(aBuffer,aPos);
       
   124                 break;
       
   125             case ETrailer:
       
   126                 interrupt = ParseTrailer(aBuffer,aPos);
       
   127                 break;
       
   128             case EError:			
       
   129                 return iError;
       
   130             case EFinish:
       
   131                 return KErrNone;
       
   132             default:
       
   133                 return KErrUnknown;
       
   134             }
       
   135         }
       
   136     return KErrNone;
       
   137     }
       
   138 // -----------------------------------------------------------------------------
       
   139 // CUpnpHttpChunkParser::ParseL
       
   140 // Decoding the chunked-encoded buffer
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 TBool CUpnpHttpChunkParser::ParseBody( TDes8& aBuffer, TInt& aPos )
       
   144 	{	
       
   145     if ( IsEmpty( aBuffer, aPos ) )
       
   146         {	    
       
   147         return ETrue;
       
   148         }
       
   149 
       
   150     TPtrC8 pointer( aBuffer.Right( aBuffer.Length() - aPos ) );	
       
   151 
       
   152     if ( iBytesAppended + pointer.Length() >= iChunkSize )
       
   153         {	
       
   154         aPos += ( iChunkSize - iBytesAppended );		
       
   155         pointer.Set( aBuffer.Right( aBuffer.Length() - aPos ) );
       
   156         TInt lineFeed = pointer.FindF(UpnpString::KLineFeed());		
       
   157         //linefeed found
       
   158         if ( !lineFeed )
       
   159             {			
       
   160             aBuffer.Delete( aPos, UpnpString::KLineFeed().Length() );
       
   161             iContext = EEndChunkBody;	
       
   162             return ( aPos == aBuffer.Length() );
       
   163             }
       
   164         //linefeed not found, end of buffer	
       
   165         else if ( lineFeed == KErrNotFound && 
       
   166                 ( ( aPos + UpnpString::KLineFeed().Length() ) > aBuffer.Length() )
       
   167                 )
       
   168             {			
       
   169             iBytesAppended = iChunkSize; 				
       
   170             return ETrue;
       
   171             }
       
   172         //size doesn't comply with the header value
       
   173         iError = KErrGeneral;	
       
   174         iContext = EError;	
       
   175         return EFalse;	
       
   176         }
       
   177     else
       
   178         {
       
   179         iBytesAppended += pointer.Length();
       
   180         aPos = aBuffer.Length();
       
   181         return ETrue;
       
   182         }		
       
   183     }
       
   184 // -----------------------------------------------------------------------------
       
   185 // CUpnpHttpChunkParser::ParseL
       
   186 // Decoding the chunked-encoded buffer
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 TBool CUpnpHttpChunkParser::ParseHeader( TDes8& aBuffer, TInt& aPos )
       
   190 	{
       
   191     if ( IsEmpty( aBuffer, aPos ) )
       
   192         {	    
       
   193         return ETrue;
       
   194         }
       
   195     iChunkSize = KErrNotFound;
       
   196     iBytesAppended = 0;
       
   197     TPtrC8 pointer(NULL,0);
       
   198     //if '\r\n' exists
       
   199     TInt lineFeed = aBuffer.Right( aBuffer.Length() - aPos ).FindF( UpnpString::KLineFeed );
       
   200     //if ';' exists
       
   201     TInt semiColon = aBuffer.Right( aBuffer.Length() - aPos ).FindF( UpnpString::KSemiColon );
       
   202     //semicolon ignored if occurs after the linefeed
       
   203     if ( semiColon !=KErrNotFound && lineFeed !=KErrNotFound && lineFeed<semiColon )
       
   204         {
       
   205         semiColon = KErrNotFound;
       
   206         }
       
   207     if ( semiColon !=KErrNotFound )
       
   208         {		
       
   209         pointer.Set(aBuffer.Right( aBuffer.Length() - aPos ).Mid( 0,semiColon ) );		
       
   210         }
       
   211     else if ( lineFeed !=KErrNotFound )
       
   212         {
       
   213         pointer.Set( aBuffer.Right( aBuffer.Length() - aPos ).Mid( 0,lineFeed ) );		
       
   214         }
       
   215     else
       
   216         {
       
   217         pointer.Set( aBuffer.Right( aBuffer.Length() - aPos ) );		
       
   218         }
       
   219 
       
   220     TLex8 lex( pointer );	
       
   221     //skip white spaces	
       
   222     lex.SkipSpace();
       
   223     TUint size;
       
   224     //neither semicolon nor linefeed found
       
   225     if ( lineFeed == KErrNotFound && semiColon == KErrNotFound )
       
   226         {
       
   227         //remember the num of cut spaces
       
   228         TInt len = lex.Offset();
       
   229         if ( !lex.Eos() )
       
   230             {
       
   231             //check the chunk header size for the limit
       
   232             TInt error = lex.Val( size, EHex );			
       
   233             if ( error!= KErrNone || size > KMaxTInt )	
       
   234                 {
       
   235                 //size too big
       
   236                 iError = ( error ) ? error: EHttpInsufficientStorage;
       
   237                 iContext = EError;
       
   238                 return EFalse;
       
   239                 }			
       
   240             }
       
   241 
       
   242         aBuffer.Delete( aPos,len );
       
   243         return ETrue;
       
   244         }
       
   245 			
       
   246     //get size	
       
   247     TInt error = lex.Val( size, EHex );
       
   248     if ( error!= KErrNone || size > KMaxTInt )
       
   249         {
       
   250         //unexpected characters or size too big
       
   251         iError = ( error ) ? error: EHttpInsufficientStorage ;
       
   252         iContext = EError;
       
   253         return EFalse;
       
   254         }	
       
   255     iChunkSize = size;	
       
   256     //skip white spaces	
       
   257     lex.SkipSpace();	
       
   258     if ( !lex.Eos() )
       
   259         {
       
   260         //unexpected characters
       
   261         iError = KErrGeneral;
       
   262         iContext = EError;
       
   263         return EFalse;
       
   264         }
       
   265     if ( lineFeed != KErrNotFound )
       
   266         {
       
   267         //the whole chunk header is parsed
       
   268         aBuffer.Delete( aPos, lineFeed + UpnpString::KLineFeed().Length() );
       
   269         //chunk size == 0
       
   270         if ( !iChunkSize )
       
   271             {        
       
   272             iContext = ELastChunk;
       
   273             }
       
   274         else
       
   275             {        
       
   276             iContext = EBody;
       
   277             }
       
   278         return ( aPos == aBuffer.Length() );
       
   279         }
       
   280     else if ( semiColon != KErrNotFound )
       
   281         {
       
   282         //extension found, 
       
   283         //one character left - possible linefeed
       
   284         if ( aPos<aBuffer.Length() )
       
   285             {            
       
   286             aBuffer.Delete( aPos, aBuffer.Length() - aPos - 1 );		
       
   287             }
       
   288         iContext = EExtension;
       
   289         return ETrue;	
       
   290         }
       
   291         
       
   292     iError = KErrUnknown;	
       
   293     iContext = EError;	
       
   294     return EFalse;		    	
       
   295     }
       
   296 // -----------------------------------------------------------------------------
       
   297 // CUpnpHttpChunkParser::ParseL
       
   298 // Decoding the chunked-encoded buffer
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 TBool CUpnpHttpChunkParser::ParseExtension( TDes8& aBuffer, TInt& aPos )
       
   302     {	
       
   303     if ( IsEmpty( aBuffer, aPos ) )
       
   304         {	    
       
   305         return ETrue;	
       
   306         }
       
   307 
       
   308     //if '\r\n' exists
       
   309     TInt lineFeed = aBuffer.Right( aBuffer.Length() - aPos ).FindF( UpnpString::KLineFeed );
       
   310     if ( lineFeed != KErrNotFound )
       
   311         {
       
   312         aBuffer.Delete( aPos, lineFeed + UpnpString::KLineFeed().Length() );		
       
   313         if ( !iChunkSize )
       
   314             {
       
   315             iContext = ETrailer;
       
   316             return EFalse;		
       
   317             }
       
   318         else
       
   319             {
       
   320             iContext = EBody;
       
   321             return EFalse;
       
   322             }		
       
   323         }
       
   324     else
       
   325         {
       
   326         //one character left - possible linefeed
       
   327         if ( aPos + 1 < aBuffer.Length() )
       
   328             {            
       
   329             aBuffer.Delete( aPos, aBuffer.Length() - aPos - 1 );
       
   330             }
       
   331         return ETrue;
       
   332         }
       
   333     }
       
   334 // -----------------------------------------------------------------------------
       
   335 // CUpnpHttpChunkParser::ParseL
       
   336 // Decoding the chunked-encoded buffer
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 TBool CUpnpHttpChunkParser::ParseLastChunk( TDes8& aBuffer, TInt& aPos )
       
   340     {		
       
   341     //if empty or only one character present
       
   342     if ( (aBuffer.Length() - aPos) < UpnpString::KLineFeedLength )
       
   343         {	    
       
   344         return ETrue;
       
   345         }
       
   346     //if '\r\n' exists
       
   347     TInt lineFeed = aBuffer.Right( aBuffer.Length() - aPos ).FindF( UpnpString::KLineFeed );
       
   348     if ( !lineFeed )
       
   349         {
       
   350         aBuffer.Delete( aPos, lineFeed + UpnpString::KLineFeed().Length() );		
       
   351         iContext = EFinish;
       
   352         return ETrue;		
       
   353         }
       
   354     else
       
   355         {
       
   356         iContext = ETrailer;
       
   357         return EFalse;		
       
   358         }		
       
   359 
       
   360     }	
       
   361 // -----------------------------------------------------------------------------
       
   362 // CUpnpHttpChunkParser::ParseL
       
   363 // Decoding the chunked-encoded buffer
       
   364 // -----------------------------------------------------------------------------
       
   365 //
       
   366 TBool CUpnpHttpChunkParser::ParseTrailer( TDes8& aBuffer, TInt& aPos )
       
   367 	{		
       
   368     if ( IsEmpty( aBuffer, aPos ) )
       
   369         {	    
       
   370         return ETrue;
       
   371         }
       
   372     //if double '\r\n' exists
       
   373     TInt doublelineFeed = aBuffer.Right( aBuffer.Length() - aPos ).FindF( UpnpString::KDoubleLineFeed );
       
   374     if (doublelineFeed != KErrNotFound ) 
       
   375         {
       
   376         aBuffer.Delete( aPos, doublelineFeed + UpnpString::KDoubleLineFeed().Length() );		
       
   377         iContext = EFinish;
       
   378         return ETrue;	
       
   379         }
       
   380     else
       
   381         {
       
   382         //waiting,3 characters reserved for double linefeed
       
   383         if ( aPos + UpnpString::KCRLFCRLength < aBuffer.Length() )
       
   384             {            
       
   385             aBuffer.Delete( aPos, aBuffer.Length() - aPos - UpnpString::KCRLFCRLength );
       
   386             }
       
   387         return ETrue;	
       
   388         }
       
   389     }
       
   390 		
       
   391 // -----------------------------------------------------------------------------
       
   392 // CUpnpHttpChunkParser::IsEmpty
       
   393 // Decoding the chunked-encoded buffer
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 TBool CUpnpHttpChunkParser::IsEmpty( TDes8& aBuffer, TInt aPos )
       
   397     {
       
   398     return ( aPos >= aBuffer.Length() );
       
   399     }   
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CUpnpHttpChunkParser::IsFinished
       
   403 // Checks if decoding ended
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 TBool CUpnpHttpChunkParser::IsFinished()
       
   407     {
       
   408     return ( iContext==EFinish );
       
   409     }   
       
   410 
       
   411 
       
   412 // End Of File