httpfilters/deflatefilter/src/DeflateDataSupplier.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 /*
       
     2 * Copyright (c) 2003 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 #include <http/rhttptransaction.h>
       
    19 #include <barsc.h>
       
    20 #include "DeflateDataSupplier.h"
       
    21 #include "DecmpStream.h"
       
    22 //------------------------------------------------------------------------------
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CDeflateDataSupplier::NewL
       
    28 // Two-phase constructor
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CDeflateDataSupplier* CDeflateDataSupplier::NewL( TInt aTransId, MHTTPDataSupplier* iDataBody, TInt aSize, TInt format )
       
    32 {
       
    33 	CDeflateDataSupplier* self = new ( ELeave ) CDeflateDataSupplier( aTransId, iDataBody, format );
       
    34 	CleanupStack::PushL( self );
       
    35 	self->ConstructL( aSize );
       
    36 	CleanupStack::Pop();
       
    37 	return self;
       
    38 }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CDeflateDataSupplier::CDeflateDataSupplier
       
    42 // constructor
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CDeflateDataSupplier::CDeflateDataSupplier( TInt aTransId, MHTTPDataSupplier* iDataBody, TInt format )
       
    46 	: iTransId( aTransId ), iBuf(0), iBufPtr( 0, 0 ), iHeaderBuf(0), iHeaderBufPtr(0,0),
       
    47 		iPHData( iDataBody ), iLastChunk( EFalse ), 
       
    48 		iStream(0), iFormat( format )
       
    49 {
       
    50 }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CDeflateDataSupplier::ConstructL
       
    54 // Data allocation constructor, which leaves
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CDeflateDataSupplier::ConstructL( TInt aSize )
       
    58 {
       
    59 	iBuf = HBufC8::NewMaxL( aSize );
       
    60 	iBufPtr.Set( iBuf->Des() );
       
    61 	iBufPtr.SetLength( 0 );
       
    62 
       
    63 	iHeaderBuf = HBufC8::NewMaxL( 10 );
       
    64 	iHeaderBufPtr.Set( iHeaderBuf->Des() );
       
    65 	iHeaderBufPtr.SetLength( 0 );
       
    66 }
       
    67 
       
    68 // destructor
       
    69 //
       
    70 CDeflateDataSupplier::~CDeflateDataSupplier()
       
    71 {
       
    72 	delete iHeaderBuf;
       
    73 	iHeaderBuf = 0;
       
    74 
       
    75 	delete iBuf;
       
    76 	iBuf = 0;
       
    77 	iPHData = 0;
       
    78 	
       
    79 	delete iStream;  //lint !e1551 no exception to throw here
       
    80 	iStream = 0;
       
    81 }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CDeflateDataSupplier::AppendDataL
       
    85 // Append a bulk of data into the supplier's buffer
       
    86 // reallocation is needed when buffer is not big enough
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CDeflateDataSupplier::AppendDataL( const TDesC8& aDataPart )
       
    90 {
       
    91 	TInt curLen = iBufPtr.Length();
       
    92 	TInt reqLen = curLen + aDataPart.Length();
       
    93 
       
    94 	if( reqLen > iBufPtr.MaxLength() )
       
    95 	{
       
    96 		TRAPD( error, ( iBuf = iBuf->ReAllocL( reqLen + aDataPart.Length() ) ) );  // realloc a bit more data
       
    97 		if( error != KErrNone ) User::Leave( KErrNoMemory );
       
    98 		
       
    99 		iBufPtr.Set( iBuf->Des() );
       
   100 		iBufPtr.SetLength( curLen );
       
   101 	}
       
   102 	iBufPtr.Append( aDataPart );
       
   103 	curLen = iBufPtr.Length();
       
   104 }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CDeflateDataSupplier::ProcessDataPartL
       
   108 // process the content
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CDeflateDataSupplier::ProcessDataPartL()
       
   112 {
       
   113 	// get the data part
       
   114 	TPtrC8 aDataPart;
       
   115 	iLastChunk = iPHData->GetNextDataPart( aDataPart );
       
   116 
       
   117 	// decompressing
       
   118 	TRAPD( err, InflateBodyDataL( aDataPart ) );
       
   119 	
       
   120 	// error in decompression, pass the original data
       
   121 	if( err != KErrNone ) 
       
   122 	{
       
   123 		ReleaseData();
       
   124 		AppendDataL( aDataPart );
       
   125 	}
       
   126 
       
   127 	iPHData->ReleaseData();
       
   128 }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CDeflateDataSupplier::InflateBodyDataL
       
   132 // decompress the content
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CDeflateDataSupplier::InflateBodyDataL( const TDesC8& aData )
       
   136 {
       
   137 	if( !iStream ) 
       
   138 	{
       
   139 		if( iFormat == CDecmpStream::EGzip )
       
   140 		{
       
   141 			iHeaderBuf = iHeaderBuf->ReAlloc( aData.Length() + iHeaderBufPtr.Length() );
       
   142 			iHeaderBufPtr.Set( iHeaderBuf->Des() );
       
   143 			iHeaderBufPtr.Append( aData );
       
   144 
       
   145 			if( iHeaderBufPtr.Length() > 10 )		// for too short Gzip header
       
   146 				iStream = CDecmpStream::NewL( iHeaderBufPtr, iFormat );
       
   147 		}
       
   148 		else
       
   149 			iStream = CDecmpStream::NewL( aData, iFormat );
       
   150 	}
       
   151 	else 
       
   152 		iStream->ResetBuffer( aData );
       
   153 
       
   154 	if( iStream )
       
   155 	{
       
   156 		while( !iStream->NeedInput() && !iStream->Finalized() )
       
   157 		{
       
   158 			iStream->InflateL();
       
   159 			if( !iStream->Finalized() )					// clumsy but necessary
       
   160 				AppendDataL( iStream->GetOutput() );
       
   161 			if( iStream->NeedOutput() )
       
   162 				iStream->ResetOutput();
       
   163 		}
       
   164 	}
       
   165 }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CDeflateDataSupplier::GetNextDataPart
       
   169 // virtual methods from MHTTPDataSupplier
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 TBool CDeflateDataSupplier::GetNextDataPart(TPtrC8& aDataPart)
       
   173 {
       
   174 	aDataPart.Set( iBufPtr );
       
   175 	return iLastChunk;
       
   176 }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CDeflateDataSupplier::ReleaseData
       
   180 // virtual methods from MHTTPDataSupplier
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CDeflateDataSupplier::ReleaseData()
       
   184 {
       
   185 //	iBufPtr.Set( iBuf->Des() );
       
   186 	iBufPtr.SetLength( 0 );
       
   187 }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CDeflateDataSupplier::OverallDataSize
       
   191 // virtual methods from MHTTPDataSupplier
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 TInt CDeflateDataSupplier::OverallDataSize()
       
   195 {
       
   196 	return iPHData->OverallDataSize();
       
   197 }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CDeflateDataSupplier::Reset
       
   201 // virtual methods from MHTTPDataSupplier
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 TInt CDeflateDataSupplier::Reset()
       
   205 {
       
   206 	return KErrNone;
       
   207 }