webengine/osswebengine/cache/src/HttpCacheStreamHandler.cpp
changeset 1 7c90e6132015
parent 0 dd21522fd290
child 10 a359256acfc6
equal deleted inserted replaced
0:dd21522fd290 1:7c90e6132015
    28 
    28 
    29 // EXTERNAL FUNCTION PROTOTYPES
    29 // EXTERNAL FUNCTION PROTOTYPES
    30 
    30 
    31 // CONSTANTS
    31 // CONSTANTS
    32 const TInt KHttpCacheActiveCount = 20;
    32 const TInt KHttpCacheActiveCount = 20;
    33 const TInt KBufferSize = 32768;
       
    34 #if 0
    33 #if 0
    35 const TInt KHttpCacheChunkSize = 2048;
    34 const TInt KHttpCacheChunkSize = 2048;
    36 #endif // 0
    35 #endif // 0
    37 // MACROS
    36 // MACROS
    38 
    37 
    47 // ============================= LOCAL FUNCTIONS ===============================
    46 // ============================= LOCAL FUNCTIONS ===============================
    48 
    47 
    49 // ============================ MEMBER FUNCTIONS ===============================
    48 // ============================ MEMBER FUNCTIONS ===============================
    50 
    49 
    51 // -----------------------------------------------------------------------------
    50 // -----------------------------------------------------------------------------
    52 // CHttpCacheStreamEntry::CHttpCacheStreamEntry
    51 // CHttpCacheStreamHandler::CHttpCacheStreamHandler
    53 // C++ default constructor can NOT contain any code, that
    52 // C++ default constructor can NOT contain any code, that
    54 // might leave.
    53 // might leave.
    55 // -----------------------------------------------------------------------------
    54 // -----------------------------------------------------------------------------
    56 //
    55 //
    57 CHttpCacheStreamEntry::CHttpCacheStreamEntry(
    56 CHttpCacheStreamHandler::CHttpCacheStreamHandler()
    58     RFs& aRfs,
    57     {
    59     CHttpCacheEntry& aHttpCacheEntry,
    58     }
    60     TDriveUnit aDrive,
    59 
    61     TInt64 aCriticalLevel )
    60 // -----------------------------------------------------------------------------
    62     : iRfs( aRfs ), iHttpCacheEntry( &aHttpCacheEntry ), iDrive( aDrive ), iCriticalLevel( aCriticalLevel )
    61 // CHttpCacheStreamHandler::ConstructL
    63     {
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CHttpCacheStreamEntry::ConstructL
       
    68 // Symbian 2nd phase constructor can leave.
    62 // Symbian 2nd phase constructor can leave.
    69 // -----------------------------------------------------------------------------
    63 // -----------------------------------------------------------------------------
    70 //
    64 //
    71 void CHttpCacheStreamEntry::ConstructL()
    65 void CHttpCacheStreamHandler::ConstructL(
    72     {
    66     const TDesC& aDirectory,
    73     // consistency check on header/body files
    67     TInt aCriticalLevel )
    74     // open the file or create one
    68     {
    75     iFileOk = ( iHttpCacheEntry->State() == CHttpCacheEntry::ECacheUninitialized ? CreateNewFilesL() : OpenCacheFiles() );
    69     User::LeaveIfError( iRfs.Connect() );
    76     if( !iFileOk )
    70     // set path for the entries
    77         {
    71     iRfs.SetSessionPath( aDirectory );
    78         User::Leave( KErrCorrupt );
    72     iActiveEntries = new( ELeave )CArrayPtrFlat<CHttpCacheEntry>( KHttpCacheActiveCount );
    79         }
    73     // get drive letter for sysutil
    80     else if( iFileOk && iHttpCacheEntry->State() == CHttpCacheEntry::ECacheUninitialized )
    74     TParsePtrC pathParser( aDirectory );
    81         {
    75     iDrive = pathParser.Drive();
    82         iHttpCacheEntry->SetState( CHttpCacheEntry::ECacheInitialized );
    76     iCriticalLevel = aCriticalLevel;
    83         }
    77     }
    84     iCacheBuffer = HBufC8::NewL( KBufferSize );
    78 
    85     }
    79 // -----------------------------------------------------------------------------
    86 
    80 // CHttpCacheStreamHandler::NewL
    87 // -----------------------------------------------------------------------------
       
    88 // CHttpCacheStreamEntry::NewL
       
    89 // Two-phased constructor.
    81 // Two-phased constructor.
    90 // -----------------------------------------------------------------------------
    82 // -----------------------------------------------------------------------------
    91 //
    83 //
    92 CHttpCacheStreamEntry* CHttpCacheStreamEntry::NewL(
    84 CHttpCacheStreamHandler* CHttpCacheStreamHandler::NewL(
    93     RFs& aRfs,
    85     const TDesC& aDirectory ,
    94     CHttpCacheEntry& aHttpCacheEntry,
    86     TInt aCriticalLevel)
    95     TDriveUnit aDrive,
    87     {
    96     TInt64 aCriticalLevel )
    88     CHttpCacheStreamHandler* self = new( ELeave ) CHttpCacheStreamHandler();
    97     {
       
    98     CHttpCacheStreamEntry* self = new( ELeave ) CHttpCacheStreamEntry( aRfs,
       
    99         aHttpCacheEntry, aDrive, aCriticalLevel );
       
   100 
    89 
   101     CleanupStack::PushL( self );
    90     CleanupStack::PushL( self );
   102     self->ConstructL();
    91     self->ConstructL( aDirectory , aCriticalLevel);
   103     CleanupStack::Pop();
    92     CleanupStack::Pop();
   104 
    93 
   105     return self;
    94     return self;
   106     }
    95     }
   107 
    96 
       
    97 // -----------------------------------------------------------------------------
   108 // Destructor
    98 // Destructor
   109 CHttpCacheStreamEntry::~CHttpCacheStreamEntry()
    99 // -----------------------------------------------------------------------------
   110     {
   100 //
   111     // commit changes
   101 CHttpCacheStreamHandler::~CHttpCacheStreamHandler()
   112     if( iFileOk )
   102     {
   113         {
   103     if ( iActiveEntries )
   114         iHeaderFile.Close();
   104         {
   115         iBodyFile.Close();
   105         iActiveEntries->ResetAndDestroy();
   116         }
   106         }
   117     delete iCacheBuffer;
   107     delete iActiveEntries;
   118     }
   108 
   119 
   109     iRfs.Close();
   120 // -----------------------------------------------------------------------------
   110     }
   121 // CHttpCacheStreamEntry::Erase
   111 
   122 //
   112 // -----------------------------------------------------------------------------
   123 // -----------------------------------------------------------------------------
   113 // CHttpCacheStreamHandler::AttachL
   124 //
   114 //
   125 void CHttpCacheStreamEntry::Erase()
   115 // -----------------------------------------------------------------------------
   126     {
   116 //
   127     //
   117 TBool CHttpCacheStreamHandler::AttachL( CHttpCacheEntry& aCacheEntry )
   128     HttpCacheUtil::WriteUrlToLog( 0, _L( "erase files associated with" ), iHttpCacheEntry->Url() );
   118     {
   129     iHeaderFile.Close();
   119 #ifdef __CACHELOG__
   130     iBodyFile.Close();
   120     // check for duplicates
   131     // dont care about return vales
   121     for ( TInt i = 0; i < iActiveEntries->Count(); i++ )
   132     // as we cannot do much
   122         {
   133     TFileName bodyFileName;
   123         __ASSERT_DEBUG( iActiveEntries->At( i ) != &aCacheEntry,
   134     // get body filename
   124         User::Panic( _L("cacheStreamHandler Panic"), KErrCorrupt )  );
   135     BodyFileName( bodyFileName );
   125         }
   136 
   126 #endif // __CACHELOG__
       
   127    
       
   128     TBool cacheFilesOpened( EFalse );
       
   129 
       
   130     if ( aCacheEntry.CacheFilesOpened() )
       
   131         {
       
   132         // Cache files already opened, no need to reopen
       
   133         cacheFilesOpened = ETrue;
       
   134         }
       
   135     else if ( aCacheEntry.State() == CHttpCacheEntry::ECacheUninitialized )
       
   136         {
       
   137         // Create new cache files, they don't already exist
       
   138         cacheFilesOpened = CreateNewFilesL( aCacheEntry );
       
   139         }
       
   140     else
       
   141         {
       
   142         // Open existing cache files
       
   143         cacheFilesOpened = OpenCacheFiles( aCacheEntry );
       
   144         }
       
   145     
       
   146     if ( cacheFilesOpened )
       
   147         {
       
   148         // Add to our active array, if not already there
       
   149         TInt index( -1 );
       
   150         FindCacheEntryIndex( aCacheEntry, &index );
       
   151         if ( index == -1 )
       
   152             {
       
   153             iActiveEntries->AppendL( &aCacheEntry );
       
   154             }
       
   155         }
       
   156 
       
   157     aCacheEntry.SetCacheFilesOpened( cacheFilesOpened );
       
   158 
       
   159     // Return ETrue, if files opened and attached
       
   160     return cacheFilesOpened;
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CHttpCacheStreamHandler::Detach
       
   165 //
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CHttpCacheStreamHandler::Detach( CHttpCacheEntry& aCacheEntry )
       
   169     {
       
   170     // Close the files, this will commit changes
       
   171     if ( aCacheEntry.CacheFilesOpened() )
       
   172         {
       
   173         aCacheEntry.BodyFile().Close();
       
   174         aCacheEntry.HeaderFile().Close();
       
   175         aCacheEntry.SetCacheFilesOpened( EFalse );
       
   176         }
       
   177 
       
   178     // Delete from our active array
       
   179     TInt index( -1 );
       
   180     FindCacheEntryIndex( (const CHttpCacheEntry&)aCacheEntry, &index );
       
   181     if ( index >= 0 )
       
   182         {
       
   183         iActiveEntries->Delete( index );
       
   184         }
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CHttpCacheStreamHandler::EraseCacheFile
       
   189 //
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 void CHttpCacheStreamHandler::EraseCacheFile( CHttpCacheEntry& aCacheEntry )
       
   193     {
       
   194     HttpCacheUtil::WriteUrlToLog( 0, _L( "CHttpCacheStreamHandler::Erase - erase files associated with" ), aCacheEntry.Url() );
       
   195 
       
   196     aCacheEntry.HeaderFile().Close();
       
   197     aCacheEntry.BodyFile().Close();
       
   198     aCacheEntry.SetCacheFilesOpened( EFalse );
       
   199 
       
   200     // Get body filename
       
   201     TFileName bodyFileName = aCacheEntry.Filename();
       
   202 
       
   203     // Get header filename
   137     TFileName headerFileName;
   204     TFileName headerFileName;
   138     HttpCacheUtil::GetHeaderFileName( bodyFileName, headerFileName );
   205     HttpCacheUtil::GetHeaderFileName( bodyFileName, headerFileName );
   139     //
   206 
   140     TInt status;
   207     TInt statusBody( KErrNotFound );
   141     status = iRfs.Delete( bodyFileName );
   208     statusBody = iRfs.Delete( bodyFileName );
   142     HttpCacheUtil::WriteLog( 0, bodyFileName, status );
   209 
   143     //
   210     TInt statusHeader( KErrNotFound );
   144     status = iRfs.Delete( headerFileName );
   211     statusHeader = iRfs.Delete( headerFileName );
   145     HttpCacheUtil::WriteLog( 0, headerFileName, status );
   212 
   146     // do not close them twice
   213     // Adjust the size
   147     iFileOk = EFalse;
   214     iContentSize -= aCacheEntry.BodySize();
   148     }
   215     iContentSize -= aCacheEntry.HeaderSize();
   149 
   216 
   150 // -----------------------------------------------------------------------------
   217 #ifdef __CACHELOG__
   151 // CHttpCacheStreamEntry::HeadersL
   218     if ( statusBody != KErrNone ) {
   152 //
   219         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
   153 // -----------------------------------------------------------------------------
   220                                            _L("CCHttpCacheStreamEntry::Erase - ERROR bodyFile delete"),
   154 //
   221                                            aCacheEntry.Filename(),
   155 HBufC8* CHttpCacheStreamEntry::HeadersL()
   222                                            aCacheEntry.Url(),
   156     {
   223                                            statusBody,
   157     //
   224                                            ELogFileErrorCode );
       
   225         }
       
   226     else {
       
   227         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   228                                            _L("CCHttpCacheStreamEntry::Erase - SUCCESS bodyFile delete"),
       
   229                                            aCacheEntry.Filename(),
       
   230                                            aCacheEntry.Url(),
       
   231                                            aCacheEntry.BodySize(),
       
   232                                            ELogEntrySize );
       
   233         }
       
   234     if ( statusHeader != KErrNone ) {
       
   235         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   236                                            _L("CCHttpCacheStreamEntry::Erase - ERROR headerFile delete"),
       
   237                                            aCacheEntry.Filename(),
       
   238                                            aCacheEntry.Url(),
       
   239                                            statusHeader,
       
   240                                            ELogFileErrorCode );
       
   241         }
       
   242     else {
       
   243         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   244                                            _L("CCHttpCacheStreamEntry::Erase - SUCCESS headerFile delete"),
       
   245                                            aCacheEntry.Filename(),
       
   246                                            aCacheEntry.Url(),
       
   247                                            aCacheEntry.BodySize(),
       
   248                                            ELogEntrySize );
       
   249         }
       
   250 #endif //__CACHELOG__
       
   251 
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CHttpCacheStreamHandler::HeadersL
       
   256 //
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 HBufC8* CHttpCacheStreamHandler::HeadersL( CHttpCacheEntry& aCacheEntry )
       
   260     {
   158     HBufC8* headerStr = NULL;
   261     HBufC8* headerStr = NULL;
   159     TInt headerLen( 0 );
   262     TInt headerLen( 0 );
   160     TInt err( iHeaderFile.Size( headerLen ) );
   263     TInt err( KErrNone );
   161     //
   264 
   162     if( err == KErrNone && headerLen > 0 )
   265     if ( !aCacheEntry.CacheFilesOpened() )
       
   266         {
       
   267         OpenCacheFiles( aCacheEntry );
       
   268         }
       
   269 
       
   270     err = aCacheEntry.HeaderFile().Size( headerLen );
       
   271     if ( err == KErrNone && headerLen > 0 )
   163         {
   272         {
   164         headerStr = HBufC8::NewL( headerLen );
   273         headerStr = HBufC8::NewL( headerLen );
   165         TPtr8 ptr( headerStr->Des() );
   274         TPtr8 ptr( headerStr->Des() );
   166         // read headers
   275         // read headers
   167         iHeaderFile.Read( 0, ptr, headerLen );
   276         aCacheEntry.HeaderFile().Read( 0, ptr, headerLen );
   168         }
   277         }
       
   278 
   169     return headerStr;
   279     return headerStr;
   170     }
   280     }
   171 
   281 
   172 // -----------------------------------------------------------------------------
   282 // -----------------------------------------------------------------------------
   173 // CHttpCacheStreamEntry::NextChunkL
   283 // CHttpCacheStreamHandler::NextChunkL
   174 //
   284 //
   175 // -----------------------------------------------------------------------------
   285 // -----------------------------------------------------------------------------
   176 //
   286 //
   177 HBufC8* CHttpCacheStreamEntry::NextChunkL(
   287 HBufC8* CHttpCacheStreamHandler::NextChunkL(
       
   288     CHttpCacheEntry& aCacheEntry,
   178     TBool& aLastChunk )
   289     TBool& aLastChunk )
   179     {
   290     {
   180     HBufC8* bodyStr = NULL;
   291     HBufC8* bodyStr = NULL;
   181 #if 0
   292     // Read whole body
   182     // incremental chunk handling
   293     TInt size( 0 );
   183     TInt size;
   294     TInt sizeErr( KErrNone );
   184     TInt contentSize( iBodyFile.Size( size ) );
   295 
   185     size = Min( KHttpCacheChunkSize, contentSize );
   296     if ( !aCacheEntry.CacheFilesOpened() )
   186 
   297         {
   187     bodyStr = HBufC8::NewL( size );
   298         OpenCacheFiles( aCacheEntry );
   188     TPtr8 ptr( bodyStr->Des() );
   299         }
   189     //
   300 
   190     iBodyFile.Read( ptr, size );
   301     sizeErr = aCacheEntry.BodyFile().Size( size );
   191     // check if we are at the end of the file
   302     if ( sizeErr == KErrNone && size > 0 )
   192     aLastChunk = ( bodyStr->Length() != size );
       
   193 #else // 0
       
   194     // read body
       
   195     TInt size;
       
   196     TInt err( iBodyFile.Size( size ) );
       
   197     if( err == KErrNone && size > 0 )
       
   198         {
   303         {
   199         bodyStr = HBufC8::NewL( size );
   304         bodyStr = HBufC8::NewL( size );
   200         TPtr8 ptr( bodyStr->Des() );
   305         TPtr8 ptr( bodyStr->Des() );
   201         //
   306 
   202         iBodyFile.Read( ptr, size );
   307         TInt readErr( KErrNone );
       
   308         readErr = aCacheEntry.BodyFile().Read( ptr, size );
       
   309 
       
   310 #ifdef __CACHELOG__
       
   311         if ( readErr != KErrNone ) {
       
   312             HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   313                                                _L("CCHttpCacheStreamEntry::NextChunkL - bodyFile.read"),
       
   314                                                aCacheEntry.Filename(),
       
   315                                                aCacheEntry.Url(),
       
   316                                                readErr,
       
   317                                                ELogFileErrorCode );
       
   318             }
       
   319         else {
       
   320             HttpCacheUtil::WriteLogFilenameAndUrl( 0,
       
   321                                                _L("CCHttpCacheStreamEntry::NextChunkL - bodyFile.read"),
       
   322                                                aCacheEntry.Filename(),
       
   323                                                aCacheEntry.Url(),
       
   324                                                ptr.Length(),
       
   325                                                ELogEntrySize );
       
   326             }
       
   327 #endif  // __CACHELOG__
       
   328 
   203         }
   329         }
   204     aLastChunk = ETrue;
   330     aLastChunk = ETrue;
   205 #endif // 0
   331 
   206     return bodyStr;
   332     return bodyStr;
   207     }
   333     }
   208 
   334 
   209 // -----------------------------------------------------------------------------
   335 // -----------------------------------------------------------------------------
   210 // CHttpCacheStreamEntry::SaveHeaders
   336 // CHttpCacheStreamHandler::SaveHeaders
   211 //
   337 //
   212 // -----------------------------------------------------------------------------
   338 // -----------------------------------------------------------------------------
   213 //
   339 //
   214 TInt CHttpCacheStreamEntry::SaveHeaders(
   340 TBool CHttpCacheStreamHandler::SaveHeaders(
       
   341     CHttpCacheEntry& aCacheEntry,
   215     const TDesC8& aHeaderStr )
   342     const TDesC8& aHeaderStr )
   216     {
   343     {
   217     TInt save( KErrNone );
   344     TBool headerSaved( EFalse );
   218 
   345 
   219     if( aHeaderStr.Length() )
   346     if ( !aCacheEntry.CacheFilesOpened() )
   220         {
   347         {
   221         // below critical level
   348         OpenCacheFiles( aCacheEntry );
   222         TBool below( ETrue );
   349         }
   223 
   350 
   224         below = DiskSpaceBelowCriticalLevel( aHeaderStr.Length() );
   351     if ( aHeaderStr.Length() && IsDiskSpaceAvailable( aHeaderStr.Length() ) )
   225 
   352         {
   226         if( !below )
   353         // We have space on disk, save headers. Don't force a flush, as the
       
   354         // File Server takes care of write and read consistency.
       
   355         TInt writeErr = aCacheEntry.HeaderFile().Write( aHeaderStr );
       
   356 
       
   357         if ( writeErr == KErrNone )
   227             {
   358             {
   228             // save headers
   359             aCacheEntry.SetHeaderSize( aHeaderStr.Length() );
   229             // Don't force a flush, as the File Server takes care of write and read consistency.
   360             iContentSize += aHeaderStr.Length();
   230             iHttpCacheEntry->SetHeaderSize( aHeaderStr.Length() );
   361             headerSaved = ETrue;
   231             save = iHeaderFile.Write( aHeaderStr );
   362             }
   232             }
   363         }
   233         else
   364 
       
   365     return headerSaved;
       
   366     }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CHttpCacheStreamHandler::RemoveHeaders
       
   370 //
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CHttpCacheStreamHandler::RemoveHeaders( CHttpCacheEntry& aCacheEntry )
       
   374     {
       
   375     iContentSize -= aCacheEntry.HeaderSize();
       
   376 
       
   377     // Destroy data
       
   378     aCacheEntry.HeaderFile().SetSize( 0 );
       
   379     aCacheEntry.SetHeaderSize( 0 );
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CHttpCacheStreamHandler::SaveBodyData
       
   384 //
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 TBool CHttpCacheStreamHandler::SaveBodyData(
       
   388     CHttpCacheEntry& aCacheEntry,
       
   389     const TDesC8& aBodyStr )
       
   390     {
       
   391     TInt bodySaved( EFalse );
       
   392     TInt newBodyLength( aBodyStr.Length() );
       
   393     TPtr8 buffer( aCacheEntry.CacheBuffer() );
       
   394 
       
   395     if ( newBodyLength && buffer.MaxLength() )
       
   396         {
       
   397         // Calculate if we have enough space in the buffer for incoming body
       
   398         if ( buffer.Length() + newBodyLength > buffer.MaxLength() )
   234             {
   399             {
   235             save = KErrDiskFull;
   400             // Incoming data is too big for the buffer
   236             }
       
   237         }
       
   238     return save;
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CHttpCacheStreamEntry::RemoveHeaders
       
   243 //
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CHttpCacheStreamEntry::RemoveHeaders()
       
   247     {
       
   248     // destroy data
       
   249     iHeaderFile.SetSize( 0 );
       
   250     iHttpCacheEntry->SetHeaderSize( 0 );
       
   251     }
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // CHttpCacheStreamEntry::SaveBodyData
       
   255 //
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 TInt CHttpCacheStreamEntry::SaveBodyData(
       
   259     const TDesC8& aBodyStr )
       
   260     {
       
   261     TBool save( KErrNone );
       
   262     TInt bodyLength( aBodyStr.Length() );
       
   263 
       
   264     if( bodyLength )
       
   265         {
       
   266         TPtr8 buffer( iCacheBuffer->Des() );
       
   267         if( buffer.Length() + bodyLength > buffer.MaxLength() )
       
   268             {
       
   269             //
       
   270             HBufC8* overflowBuffer = NULL;
   401             HBufC8* overflowBuffer = NULL;
   271             TInt cut( -1 );
   402             TInt bufferSpaceLeft( -1 );
   272             // running out of space
       
   273             TPtrC8 writePtr;
   403             TPtrC8 writePtr;
   274             if( buffer.Length() == 0 )
   404 
   275                 {
   405             if ( buffer.Length() == 0 )
   276                 // buffer is empty and the body is bigger than the buffer
   406                 {
       
   407                 // Buffer is empty and the body is bigger than the buffer,
       
   408                 // just take all of the incoming data
   277                 writePtr.Set( aBodyStr );
   409                 writePtr.Set( aBodyStr );
   278                 }
   410                 }
   279             else
   411             else
   280                 {
   412                 {
   281                 cut =  buffer.MaxLength() - buffer.Length();
   413                 // We have some data in buffer, how much space do we have left
   282                 // enough space for the leftover?
   414                 bufferSpaceLeft = buffer.MaxLength() - buffer.Length();
   283                 if( bodyLength - cut > buffer.MaxLength() )
   415                 
       
   416                 if ( newBodyLength - bufferSpaceLeft > buffer.MaxLength() )
   284                     {
   417                     {
   285                     // not enough
   418                     // Not enough space, so lets put the buffer and the new
   286                     // put the buffer and the body together and
   419                     // body together and write it in one go.
   287                     // write it in one go.
   420                     overflowBuffer = HBufC8::New( buffer.Length() + newBodyLength );
   288                     overflowBuffer = HBufC8::New( buffer.Length() + bodyLength );
   421                     if ( !overflowBuffer )
   289                     if( !overflowBuffer )
       
   290                         {
   422                         {
   291                         return KErrNoMemory;
   423                         return EFalse;
   292                         }
   424                         }
       
   425 
   293                     TPtr8 overflowPtr( overflowBuffer->Des() );
   426                     TPtr8 overflowPtr( overflowBuffer->Des() );
   294                     overflowPtr.Copy( buffer );
   427                     overflowPtr.Copy( buffer );
   295                     overflowPtr.Append( aBodyStr );
   428                     overflowPtr.Append( aBodyStr );
   296                     writePtr.Set( overflowBuffer->Des() );
   429                     writePtr.Set( overflowBuffer->Des() );
       
   430 
   297                     // empty buffer
   431                     // empty buffer
   298                     buffer.Zero();
   432                     buffer.Zero();
   299                     // no leftover left
   433                     // no leftover left
   300                     cut = -1;
   434                     bufferSpaceLeft = -1;
   301                     }
   435                     }
   302                 else
   436                 else
   303                     {
   437                     {
   304                     // fill the 32k
   438                     // Copy what we have enough space for
   305                     buffer.Append( aBodyStr.Left( cut ) );
   439                     buffer.Append( aBodyStr.Left( bufferSpaceLeft ) );
   306                     writePtr.Set( buffer );
   440                     writePtr.Set( buffer );
   307                     }
   441                     }
   308                 }
   442                 }
   309 
   443 
   310             // write to the disk
   444             // Write to the disk, if we have disk space
   311             TBool below;
   445             TInt writeErr( KErrNone );
   312             below = DiskSpaceBelowCriticalLevel( writePtr.Length() );
   446             if ( IsDiskSpaceAvailable( writePtr.Length() ) )
   313 
   447                 {
   314             if( !below )
   448 
   315                 {
   449                 if ( !aCacheEntry.CacheFilesOpened() )
   316                 // write body
   450                     {
   317                 save = iBodyFile.Write( writePtr );
   451                     OpenCacheFiles( aCacheEntry );
       
   452                     }
       
   453 
       
   454                 // We have enough disk space, save body
       
   455                 TInt writeErr = aCacheEntry.BodyFile().Write( writePtr );
       
   456                 bodySaved = ETrue;
   318                 }
   457                 }
   319             else
   458             else
   320                 {
   459                 {
   321                 save = KErrDiskFull;
   460                 // We don't have enough disk space, clean up 
   322                 // reset buffers
   461                 bodySaved = EFalse;
   323                 buffer.Zero();
   462                 buffer.Zero();
   324                 }
   463                 }
   325             //
   464 
   326             if( save == KErrNone && cut >= 0 )
   465             if ( writeErr == KErrNone && bufferSpaceLeft >= 0 )
   327                 {
   466                 {
   328                 // copy the leftover in to the buffer
   467                 // Copy what we can of the leftover in to the buffer
   329                 buffer.Copy( aBodyStr.Mid( cut ) );
   468                 buffer.Copy( aBodyStr.Mid( bufferSpaceLeft ) );
   330                 }
   469                 }
   331             delete overflowBuffer;
   470             delete overflowBuffer;
   332             }
   471             }
   333         else
   472         else
   334             {
   473             {
       
   474             // We have enough space in buffer, add and wait for next body
       
   475             // before writing to file
   335             buffer.Append( aBodyStr );
   476             buffer.Append( aBodyStr );
   336             save = KErrNone;
   477             bodySaved = ETrue;
   337             }
   478             }
   338         // update size information
   479 
   339         iHttpCacheEntry->SetSize( iHttpCacheEntry->Size() + bodyLength );
   480         // Body saved, update state
   340         }
   481         aCacheEntry.SetBodySize( aCacheEntry.BodySize() + newBodyLength );
   341     return save;
   482         iContentSize += aBodyStr.Length();
   342     }
   483         }
   343 
   484 
   344 // -----------------------------------------------------------------------------
   485     return bodySaved;
   345 // CHttpCacheStreamEntry::RemoveBodyData
   486     }
   346 //
   487 
   347 // -----------------------------------------------------------------------------
   488 // -----------------------------------------------------------------------------
   348 //
   489 // CHttpCacheStreamHandler::RemoveBodyData
   349 void CHttpCacheStreamEntry::RemoveBodyData()
   490 //
   350     {
   491 // -----------------------------------------------------------------------------
   351     // destroy data
   492 //
   352     iCacheBuffer->Des().Zero();
   493 void CHttpCacheStreamHandler::RemoveBodyData( CHttpCacheEntry& aCacheEntry )
   353     iBodyFile.SetSize( 0 );
   494     {
   354     iHttpCacheEntry->SetSize( 0 );
   495 #ifdef __CACHELOG__
   355     }
   496      HttpCacheUtil::WriteLogFilenameAndUrl( 0,
   356 
   497                                            _L("CHttpCacheStreamHandler::RemoveBodyData - CLEAR iCacheBuffer, entrySize, and iBodyFile"),
   357 // -----------------------------------------------------------------------------
   498                                            aCacheEntry.Filename(),
   358 // CHttpCacheStreamEntry::Flush
   499                                            aCacheEntry.Url(),
   359 //
   500                                            aCacheEntry.BodySize(),
   360 // -----------------------------------------------------------------------------
   501                                            ELogEntrySize );
   361 //
   502 #endif
   362 TInt CHttpCacheStreamEntry::Flush()
   503 
   363     {
   504     // Remove data 
   364     TInt saveOk( KErrNone );
   505     iContentSize -= aCacheEntry.BodySize();
   365 
   506     aCacheEntry.SetBodySize( 0 );
   366     if( iCacheBuffer->Length() )
   507     aCacheEntry.CacheBuffer().Zero();
   367         {
   508     aCacheEntry.BodyFile().SetSize( 0 );
   368         TPtr8 bufferPtr( iCacheBuffer->Des() );
   509     }
   369 
   510 
   370         TBool below;
   511 // -----------------------------------------------------------------------------
   371         below = DiskSpaceBelowCriticalLevel( bufferPtr.Length() );
   512 // CHttpCacheStreamHandler::Flush
   372 
   513 //
   373         if( !below )
   514 // -----------------------------------------------------------------------------
       
   515 //
       
   516 TBool CHttpCacheStreamHandler::Flush( CHttpCacheEntry& aCacheEntry )
       
   517     {
       
   518     TBool saved( EFalse );
       
   519     TInt writeErr( KErrGeneral );
       
   520     TInt cacheBufferLen( aCacheEntry.CacheBuffer().Length() );
       
   521 
       
   522     if ( cacheBufferLen && IsDiskSpaceAvailable( cacheBufferLen ) )
       
   523         {
       
   524         // We have enough space, save cache buffer
       
   525         TPtr8 bufferPtr( aCacheEntry.CacheBuffer() );
       
   526         if ( bufferPtr.Length() )
   374             {
   527             {
   375             // append body
   528             if ( !aCacheEntry.CacheFilesOpened() )
   376             saveOk = iBodyFile.Write( bufferPtr );
   529                 {
   377             }
   530                 OpenCacheFiles( aCacheEntry );
   378         else
   531                 }
   379             {
   532 
   380             saveOk = KErrDiskFull;
   533             writeErr = aCacheEntry.BodyFile().Write( bufferPtr );
   381             }
   534             if ( writeErr == KErrNone )
       
   535                 {
       
   536                 saved = ETrue;
       
   537                 }
       
   538             }
       
   539 
       
   540         // Clear the buffer
   382         bufferPtr.Zero();
   541         bufferPtr.Zero();
   383         }
   542         }
   384     return saveOk;
   543 
   385     }
   544     return saved;
   386 
   545     }
   387 // -----------------------------------------------------------------------------
   546 
   388 // CHttpCacheStreamEntry::OpenCacheFiles
   547 // -----------------------------------------------------------------------------
   389 //
   548 // CHttpCacheStreamHandler::OpenCacheFiles
   390 // -----------------------------------------------------------------------------
   549 //
   391 //
   550 // -----------------------------------------------------------------------------
   392 TBool CHttpCacheStreamEntry::OpenCacheFiles()
   551 //
   393     {
   552 TBool CHttpCacheStreamHandler::OpenCacheFiles( CHttpCacheEntry& aCacheEntry )
   394     TInt statusHeader;
   553     {
   395     TInt statusBody;
   554     TInt statusHeader( KErrNotFound );
   396     //
   555     TInt statusBody( KErrNotFound );
   397     TFileName bodyFileName;
   556 
   398     // get body filename
   557     // get body filename
   399     BodyFileName( bodyFileName );
   558     TFileName bodyFileName = aCacheEntry.Filename();
       
   559 
   400     // header filename
   560     // header filename
   401     TFileName headerFileName;
   561     TFileName headerFileName;
   402     //
       
   403     HttpCacheUtil::GetHeaderFileName( bodyFileName, headerFileName );
   562     HttpCacheUtil::GetHeaderFileName( bodyFileName, headerFileName );
   404 
   563 
   405     statusHeader = iHeaderFile.Open( iRfs, headerFileName, EFileShareExclusive | EFileWrite );
   564     statusHeader = aCacheEntry.HeaderFile().Open( iRfs, headerFileName, EFileShareExclusive | EFileWrite );
   406     statusBody = iBodyFile.Open( iRfs, bodyFileName, EFileShareExclusive | EFileWrite );
   565     statusBody = aCacheEntry.BodyFile().Open( iRfs, bodyFileName, EFileShareExclusive | EFileWrite );
   407     return ( statusHeader == KErrNone && statusBody == KErrNone );
   566 
   408     }
   567     TBool fileOk( statusHeader == KErrNone && statusBody == KErrNone );
   409 
   568     if ( fileOk )
   410 // -----------------------------------------------------------------------------
   569         {
   411 // CHttpCacheStreamEntry::CreateNewFilesL
   570         aCacheEntry.SetCacheFilesOpened( ETrue );
   412 //
   571         }
   413 // -----------------------------------------------------------------------------
   572 
   414 //
   573     return fileOk;
   415 TBool CHttpCacheStreamEntry::CreateNewFilesL()
   574     }
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CHttpCacheStreamHandler::CreateNewFilesL
       
   578 //
       
   579 // -----------------------------------------------------------------------------
       
   580 //
       
   581 TBool CHttpCacheStreamHandler::CreateNewFilesL( CHttpCacheEntry& aCacheEntry )
   416     {
   582     {
   417     TInt statusHeader( KErrNotFound );
   583     TInt statusHeader( KErrNotFound );
   418     TInt statusBody( KErrNotFound );
   584     TInt statusBody( KErrNotFound );
   419     TPath sessionPath;
   585     TPath sessionPath;
   420     User::LeaveIfError( iRfs.SessionPath( sessionPath ) );
   586     User::LeaveIfError( iRfs.SessionPath( sessionPath ) );
   421 
   587     
   422     //Given the full URL, generates a fully qualified path for saving the HTTP response
   588     // Given the full URL, generates a fully qualified path for saving the HTTP response
   423     HBufC* bodyFileName = HttpCacheUtil::GenerateNameLC( iHttpCacheEntry->Url(), sessionPath );
   589     HBufC* bodyFileName = HttpCacheUtil::GenerateNameLC( aCacheEntry.Url(), sessionPath );
   424     TPtrC bodyFileNamePtr( *bodyFileName );
   590     TPtrC bodyFileNamePtr( *bodyFileName );
   425     // get header file name
   591  
       
   592     // Create header file name from body file name
   426     TFileName headerFileName;
   593     TFileName headerFileName;
   427     HttpCacheUtil::GetHeaderFileName( bodyFileNamePtr, headerFileName );
   594     HttpCacheUtil::GetHeaderFileName( bodyFileNamePtr, headerFileName );
   428 
   595 
   429     // create a file or replace if it exists. 
   596     // Create the body file or replace it, if it exists. 
   430     statusBody = iBodyFile.Replace( iRfs, bodyFileNamePtr, EFileShareExclusive | EFileWrite );
   597     statusBody = aCacheEntry.BodyFile().Replace( iRfs, bodyFileNamePtr, EFileShareExclusive | EFileWrite );
   431     if( statusBody == KErrNone )
   598     if ( statusBody == KErrNone )
   432         {
   599         {
   433         // header file should not fail
   600         // Header file should not fail
   434         statusHeader = iHeaderFile.Replace( iRfs, headerFileName, EFileShareExclusive | EFileWrite );
   601         statusHeader = aCacheEntry.HeaderFile().Replace( iRfs, headerFileName, EFileShareExclusive | EFileWrite );
   435         }
   602         }
   436     //
   603 
       
   604 #ifdef __CACHELOG__ 
       
   605     HttpCacheUtil::WriteUrlToLog( 0, bodyFileNamePtr, aCacheEntry.Url() );
       
   606 #endif 
       
   607 
   437     TBool fileOk( statusHeader == KErrNone && statusBody == KErrNone );
   608     TBool fileOk( statusHeader == KErrNone && statusBody == KErrNone );
   438 #ifdef __CACHELOG__ 
   609     if ( fileOk )
   439     HttpCacheUtil::WriteUrlToLog( 0, bodyFileNamePtr, iHttpCacheEntry->Url() );
   610         {
   440 #endif 
   611         // Both body and header files created correctly 
   441 
   612         aCacheEntry.SetFileNameL( bodyFileNamePtr );
   442     //
   613         aCacheEntry.SetState( CHttpCacheEntry::ECacheInitialized );
   443     if( fileOk )
       
   444         {
       
   445         iHttpCacheEntry->SetFileNameL( bodyFileNamePtr );
       
   446 #ifdef __CACHELOG__ 
       
   447         HttpCacheUtil::WriteUrlToLog( 0, _L8("files are fine") );
       
   448 #endif 
       
   449         }
   614         }
   450     else
   615     else
   451         {
   616         {
   452         // corrupt entry. delete the file
   617         // Only the body file created, no header file, delete body file 
   453         if( statusBody == KErrNone )
   618         iRfs.Delete( bodyFileNamePtr );
       
   619         iRfs.Delete( headerFileName );
       
   620 
       
   621         aCacheEntry.SetBodySize( 0 );
       
   622         aCacheEntry.SetHeaderSize( 0 );
       
   623 
       
   624 #ifdef __CACHELOG__
       
   625         HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheEntry::CreateNewFilesL - DELETE body file, header file failed" ) );
       
   626 #endif
       
   627 //        __ASSERT_DEBUG( EFalse, User::Panic( _L("CHttpCacheHandler::CreateNewFilesL Panic"), KErrCorrupt )  );
       
   628         }
       
   629 
       
   630     CleanupStack::PopAndDestroy( bodyFileName );
       
   631 
       
   632     return fileOk;
       
   633     }
       
   634 
       
   635 // -----------------------------------------------------------------------------
       
   636 // CHttpCacheStreamHandler::FindCacheEntryIndex
       
   637 //
       
   638 // -----------------------------------------------------------------------------
       
   639 //
       
   640 void CHttpCacheStreamHandler::FindCacheEntryIndex(
       
   641     const CHttpCacheEntry& aCacheEntry,
       
   642     TInt* aIndex )
       
   643     {
       
   644     *aIndex = -1;
       
   645     for ( TInt i = 0; i < iActiveEntries->Count(); i++ )
       
   646         {
       
   647         CHttpCacheEntry* entry = iActiveEntries->At( i );
       
   648 
       
   649         if ( entry == &aCacheEntry )
   454             {
   650             {
   455             iRfs.Delete( bodyFileNamePtr );
   651             if ( aIndex )
   456             }
   652                 {
   457         // ???
   653                 *aIndex = i;
   458         __ASSERT_DEBUG( EFalse,
   654                 }
   459             User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
   655             break;
   460         }
   656             }
   461     CleanupStack::PopAndDestroy( bodyFileName );
   657         }
   462     return fileOk;
   658     }
   463     }
   659 
   464 
   660 // -----------------------------------------------------------------------------
   465 // -----------------------------------------------------------------------------
   661 // CHttpCacheStreamHandler::IsDiskSpaceAvailable
   466 // CHttpCacheStreamEntry::BodyFileName
   662 //
   467 //
   663 // -----------------------------------------------------------------------------
   468 // -----------------------------------------------------------------------------
   664 //
   469 //
   665 TBool CHttpCacheStreamHandler::IsDiskSpaceAvailable( TInt aContentSize )
   470 void CHttpCacheStreamEntry::BodyFileName(
   666     {
   471     TFileName& aBodyFileName )
   667     TBool diskSpaceAvailable( EFalse );
   472     {
       
   473     TFileName bodyFileName;
       
   474     aBodyFileName.Copy( iHttpCacheEntry->Filename()  );
       
   475     }
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // CHttpCacheStreamEntry::BodyFileName
       
   479 //
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 TBool CHttpCacheStreamEntry::DiskSpaceBelowCriticalLevel(
       
   483     TInt aContentSize )
       
   484     {
       
   485     TVolumeInfo vinfo;
   668     TVolumeInfo vinfo;
   486     TInt errorCode = iRfs.Volume( vinfo, iDrive );
   669     TInt errorCode = iRfs.Volume( vinfo, iDrive );
   487 
   670 
   488     return( errorCode != KErrNone || ( vinfo.iFree - aContentSize ) <= iCriticalLevel );
   671     if ( errorCode == KErrNone && ( vinfo.iFree - aContentSize ) > iCriticalLevel )
   489     }
   672         {
   490 
   673         // We have space on the disk for the content
   491 // -----------------------------------------------------------------------------
   674         diskSpaceAvailable = ETrue;
   492 // CHttpCacheStreamHandler::CHttpCacheStreamHandler
   675         }
   493 // C++ default constructor can NOT contain any code, that
   676 
   494 // might leave.
   677     return diskSpaceAvailable;
   495 // -----------------------------------------------------------------------------
       
   496 //
       
   497 CHttpCacheStreamHandler::CHttpCacheStreamHandler() : iDiskFull( EFalse )
       
   498     {
       
   499     // change the iDiskFull back to false if somebody freed some disk space.
       
   500     }
       
   501 
       
   502 // -----------------------------------------------------------------------------
       
   503 // CHttpCacheStreamHandler::ConstructL
       
   504 // Symbian 2nd phase constructor can leave.
       
   505 // -----------------------------------------------------------------------------
       
   506 //
       
   507 void CHttpCacheStreamHandler::ConstructL(
       
   508     const TDesC& aDirectory,
       
   509     TInt aCriticalLevel )
       
   510     {
       
   511     User::LeaveIfError( iRfs.Connect() );
       
   512     // set path for the entries
       
   513     iRfs.SetSessionPath( aDirectory );
       
   514     iActiveEntries = new( ELeave )CArrayPtrFlat<CHttpCacheStreamEntry>( KHttpCacheActiveCount );
       
   515     // get drive letter for sysutil
       
   516     TParsePtrC pathParser( aDirectory );
       
   517     iDrive = pathParser.Drive();
       
   518     iCriticalLevel = aCriticalLevel;
       
   519     }
       
   520 
       
   521 // -----------------------------------------------------------------------------
       
   522 // CHttpCacheStreamHandler::NewL
       
   523 // Two-phased constructor.
       
   524 // -----------------------------------------------------------------------------
       
   525 //
       
   526 CHttpCacheStreamHandler* CHttpCacheStreamHandler::NewL(
       
   527     const TDesC& aDirectory ,
       
   528     TInt aCriticalLevel)
       
   529     {
       
   530     CHttpCacheStreamHandler* self = new( ELeave ) CHttpCacheStreamHandler();
       
   531 
       
   532     CleanupStack::PushL( self );
       
   533     self->ConstructL( aDirectory , aCriticalLevel);
       
   534     CleanupStack::Pop();
       
   535 
       
   536     return self;
       
   537     }
       
   538 
       
   539 // Destructor
       
   540 CHttpCacheStreamHandler::~CHttpCacheStreamHandler()
       
   541     {
       
   542     if( iActiveEntries )
       
   543         {
       
   544         iActiveEntries->ResetAndDestroy();
       
   545         }
       
   546     delete iActiveEntries;
       
   547     iRfs.Close();
       
   548     }
       
   549 
       
   550 // -----------------------------------------------------------------------------
       
   551 // CHttpCacheStreamHandler::AttachL
       
   552 //
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 TBool CHttpCacheStreamHandler::AttachL(
       
   556     CHttpCacheEntry& aCacheEntry )
       
   557     {
       
   558 #ifdef __CACHELOG__
       
   559     // check for duplicates
       
   560     for( TInt i = 0; i < iActiveEntries->Count(); i++ )
       
   561         {
       
   562         __ASSERT_DEBUG( iActiveEntries->At( i )->CacheEntry() != &aCacheEntry,
       
   563             User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   564         }
       
   565 #endif // __CACHELOG__
       
   566     TBool attached( ETrue );
       
   567     // create and save stream entry
       
   568     CHttpCacheStreamEntry* streamEntry = NULL;
       
   569 
       
   570     TRAPD( err, streamEntry = CHttpCacheStreamEntry::NewL( iRfs, aCacheEntry, iDrive, iCriticalLevel ) );
       
   571     if( err == KErrCorrupt )
       
   572         {
       
   573         //
       
   574         attached = EFalse;
       
   575         }
       
   576     else if( err == KErrNoMemory )
       
   577         {
       
   578         User::Leave( err );
       
   579         }
       
   580     else if( streamEntry )
       
   581         {
       
   582         iActiveEntries->AppendL( streamEntry );
       
   583         }
       
   584     return attached;
       
   585     }
       
   586 
       
   587 // -----------------------------------------------------------------------------
       
   588 // CHttpCacheStreamHandler::Detach
       
   589 //
       
   590 // -----------------------------------------------------------------------------
       
   591 //
       
   592 void CHttpCacheStreamHandler::Detach(
       
   593     const CHttpCacheEntry& aCacheEntry )
       
   594     {
       
   595     TInt index;
       
   596     CHttpCacheStreamEntry* streamEntry = FindStreamEntry( aCacheEntry, &index );
       
   597     __ASSERT_DEBUG( streamEntry != NULL, User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   598 
       
   599     if( streamEntry )
       
   600         {
       
   601         delete streamEntry;
       
   602         iActiveEntries->Delete( index );
       
   603         }
       
   604     }
       
   605 
       
   606 // -----------------------------------------------------------------------------
       
   607 // CHttpCacheStreamHandler::Erase
       
   608 //
       
   609 // -----------------------------------------------------------------------------
       
   610 //
       
   611 void CHttpCacheStreamHandler::Erase(
       
   612     const CHttpCacheEntry& aCacheEntry )
       
   613     {
       
   614     TInt index;
       
   615     CHttpCacheStreamEntry* streamEntry = FindStreamEntry( aCacheEntry, &index );
       
   616     __ASSERT_DEBUG( streamEntry != NULL, User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   617 
       
   618     if( streamEntry )
       
   619         {
       
   620         streamEntry->Erase();
       
   621         //
       
   622         iContentSize-=aCacheEntry.Size();
       
   623         iContentSize-=aCacheEntry.HeaderSize();
       
   624         }
       
   625     }
       
   626 
       
   627 // -----------------------------------------------------------------------------
       
   628 // CHttpCacheStreamHandler::HeadersL
       
   629 //
       
   630 // -----------------------------------------------------------------------------
       
   631 //
       
   632 HBufC8* CHttpCacheStreamHandler::HeadersL(
       
   633     CHttpCacheEntry& aCacheEntry )
       
   634     {
       
   635     CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   636     HBufC8* headerStr = NULL;
       
   637     //
       
   638     __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   639     //
       
   640     if( entry )
       
   641         {
       
   642         headerStr = entry->HeadersL();
       
   643         }
       
   644     return headerStr;
       
   645     }
       
   646 
       
   647 // -----------------------------------------------------------------------------
       
   648 // CHttpCacheStreamHandler::NextChunkL
       
   649 //
       
   650 // -----------------------------------------------------------------------------
       
   651 //
       
   652 HBufC8* CHttpCacheStreamHandler::NextChunkL(
       
   653     CHttpCacheEntry& aCacheEntry,
       
   654     TBool& aLastChunk )
       
   655     {
       
   656     //
       
   657     CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   658     HBufC8* bodyStr = NULL;
       
   659     //
       
   660     __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   661     //
       
   662     if( entry )
       
   663         {
       
   664         bodyStr = entry->NextChunkL( aLastChunk );
       
   665         }
       
   666     return bodyStr;
       
   667     }
       
   668 
       
   669 // -----------------------------------------------------------------------------
       
   670 // CHttpCacheStreamHandler::SaveHeaders
       
   671 //
       
   672 // -----------------------------------------------------------------------------
       
   673 //
       
   674 TBool CHttpCacheStreamHandler::SaveHeaders(
       
   675     CHttpCacheEntry& aCacheEntry,
       
   676     const TDesC8& aHeaderStr )
       
   677     {
       
   678     TBool saved( KErrGeneral );
       
   679     if( !iDiskFull )
       
   680         {
       
   681         CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   682         //
       
   683         __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   684         //
       
   685         if( entry )
       
   686             {
       
   687             saved = entry->SaveHeaders( aHeaderStr );
       
   688             // update content size in cache
       
   689             if( saved == KErrNone )
       
   690                 {
       
   691                 iContentSize+=aHeaderStr.Length();
       
   692                 }
       
   693             else if( saved == KErrDiskFull )
       
   694                 {
       
   695                 iDiskFull = ETrue;
       
   696                 }
       
   697             }
       
   698         }
       
   699     return( saved == KErrNone );
       
   700     }
       
   701 
       
   702 // -----------------------------------------------------------------------------
       
   703 // CHttpCacheStreamHandler::RemoveHeaders
       
   704 //
       
   705 // -----------------------------------------------------------------------------
       
   706 //
       
   707 void CHttpCacheStreamHandler::RemoveHeaders(
       
   708     CHttpCacheEntry& aCacheEntry )
       
   709     {
       
   710     CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   711     //
       
   712     __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   713     //
       
   714     if( entry )
       
   715         {
       
   716         iContentSize-=aCacheEntry.HeaderSize();
       
   717         entry->RemoveHeaders();
       
   718         }
       
   719     }
       
   720 
       
   721 // -----------------------------------------------------------------------------
       
   722 // CHttpCacheStreamHandler::SaveBodyData
       
   723 //
       
   724 // -----------------------------------------------------------------------------
       
   725 //
       
   726 TBool CHttpCacheStreamHandler::SaveBodyData(
       
   727     CHttpCacheEntry& aCacheEntry,
       
   728     const TDesC8& aBodyStr )
       
   729     {
       
   730     TInt saved( KErrGeneral );
       
   731     if( !iDiskFull )
       
   732         {
       
   733         CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   734         //
       
   735         __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   736         //
       
   737         if( entry )
       
   738             {
       
   739             saved = entry->SaveBodyData( aBodyStr );
       
   740             if( saved == KErrNone )
       
   741                 {
       
   742                 iContentSize+=aBodyStr.Length();
       
   743                 }
       
   744             else if( saved == KErrDiskFull )
       
   745                 {
       
   746                 iDiskFull = ETrue;
       
   747                 }
       
   748             }
       
   749         }
       
   750     return( saved == KErrNone );
       
   751     }
       
   752 
       
   753 // -----------------------------------------------------------------------------
       
   754 // CHttpCacheStreamHandler::RemoveBodyData
       
   755 //
       
   756 // -----------------------------------------------------------------------------
       
   757 //
       
   758 void CHttpCacheStreamHandler::RemoveBodyData(
       
   759     CHttpCacheEntry& aCacheEntry )
       
   760     {
       
   761     CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   762     //
       
   763     __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   764     //
       
   765     if( entry )
       
   766         {
       
   767         iContentSize-=aCacheEntry.Size();
       
   768         entry->RemoveBodyData();
       
   769         }
       
   770     }
       
   771 
       
   772 // -----------------------------------------------------------------------------
       
   773 // CHttpCacheStreamHandler::Flush
       
   774 //
       
   775 // -----------------------------------------------------------------------------
       
   776 //
       
   777 TBool CHttpCacheStreamHandler::Flush(
       
   778     CHttpCacheEntry& aCacheEntry )
       
   779     {
       
   780     TInt saved( KErrGeneral );
       
   781     if( !iDiskFull )
       
   782         {
       
   783         CHttpCacheStreamEntry* entry = FindStreamEntry( aCacheEntry );
       
   784         //
       
   785         __ASSERT_DEBUG( entry != NULL,  User::Panic( _L("cacheHandler Panic"), KErrCorrupt )  );
       
   786         //
       
   787         if( entry )
       
   788             {
       
   789             saved = entry->Flush();
       
   790             //
       
   791             if( saved == KErrDiskFull )
       
   792                 {
       
   793                 iDiskFull = ETrue;
       
   794                 }
       
   795             }
       
   796         }
       
   797     return( saved == KErrNone );
       
   798     }
       
   799 
       
   800 // -----------------------------------------------------------------------------
       
   801 // CHttpCacheStreamHandler::FindStreamEntry
       
   802 //
       
   803 // -----------------------------------------------------------------------------
       
   804 //
       
   805 CHttpCacheStreamEntry* CHttpCacheStreamHandler::FindStreamEntry(
       
   806     const CHttpCacheEntry& aCacheEntry,
       
   807     TInt* aIndex )
       
   808     {
       
   809     CHttpCacheStreamEntry* streamEntry = NULL;
       
   810 
       
   811     for( TInt i = 0; i < iActiveEntries->Count(); i++ )
       
   812         {
       
   813         CHttpCacheStreamEntry* entry = iActiveEntries->At( i );
       
   814 
       
   815         if( entry && entry->CacheEntry() == &aCacheEntry )
       
   816             {
       
   817             streamEntry = entry;
       
   818             if( aIndex )
       
   819                 {
       
   820                 *aIndex = i;
       
   821                 }
       
   822             break;
       
   823             }
       
   824         }
       
   825     return streamEntry;
       
   826     }
   678     }
   827 
   679 
   828 //  End of File
   680 //  End of File