webengine/osswebengine/cache/src/HttpCacheHandler.cpp
changeset 1 7c90e6132015
parent 0 dd21522fd290
child 5 10e98eab6f85
equal deleted inserted replaced
0:dd21522fd290 1:7c90e6132015
   144     CleanupStack::Pop();
   144     CleanupStack::Pop();
   145 
   145 
   146     return self;
   146     return self;
   147     }
   147     }
   148 
   148 
       
   149 // -----------------------------------------------------------------------------
   149 // Destructor
   150 // Destructor
       
   151 // -----------------------------------------------------------------------------
       
   152 //
   150 CHttpCacheHandler::~CHttpCacheHandler()
   153 CHttpCacheHandler::~CHttpCacheHandler()
   151     {
   154     {
   152    
   155     TRAP_IGNORE( SaveLookupTableL() );
   153         TRAP_IGNORE( SaveLookupTableL() );
       
   154    
       
   155     //
   156     //
   156     delete iHttpCacheObserver;
   157     delete iHttpCacheObserver;
   157     //
   158     //
   158     if (iEvictionHandler)
   159     if ( iEvictionHandler )
   159         {
   160         {
   160         iEvictionHandler->RemoveAll();
   161         iEvictionHandler->RemoveAll();
   161         }
   162         }
   162 
       
   163     //
   163     //
   164     delete iLookupTable;
   164     delete iLookupTable;
   165     //
   165     //
   166     delete iEvictionHandler;
   166     delete iEvictionHandler;
   167     //
   167     //
   182 TInt CHttpCacheHandler::RequestL(
   182 TInt CHttpCacheHandler::RequestL(
   183     RHTTPTransaction& aTrans,
   183     RHTTPTransaction& aTrans,
   184     TBrCtlDefs::TBrCtlCacheMode aCacheMode,
   184     TBrCtlDefs::TBrCtlCacheMode aCacheMode,
   185     THttpCacheEntry& aCacheEntry )
   185     THttpCacheEntry& aCacheEntry )
   186     {
   186     {
       
   187 #ifdef __CACHELOG__
   187     HttpCacheUtil::WriteUrlToLog( 0, _L( "request item" ), aTrans.Request().URI().UriDes() );
   188     HttpCacheUtil::WriteUrlToLog( 0, _L( "request item" ), aTrans.Request().URI().UriDes() );
   188     //
   189 #endif
       
   190 
   189     TInt status( KErrNotFound );
   191     TInt status( KErrNotFound );
   190     CHttpCacheEntry* entry = NULL;
   192     CHttpCacheEntry* entry = NULL;
   191     // 0. check if we need to check cache at all (protected vs no cache mode)
   193     // 0. check if we need to check cache at all (protected vs no cache mode)
   192     // 1. check if the url is in the cache
   194     // 1. check if the url is in the cache
   193     // 2. check if it is complete
   195     // 2. check if it is complete
   194     // 3. see if it is useable
   196     // 3. see if it is useable
   195 
   197 
   196     // use protected item on reload???
   198     // use protected item on reload
   197     // currently not
   199     // currently not and do not use cache for post
   198     // and do not use cache for post
       
   199     if( aCacheMode != TBrCtlDefs::ECacheModeNoCache &&
   200     if( aCacheMode != TBrCtlDefs::ECacheModeNoCache &&
   200         HttpCacheUtil::MethodFromStr( aTrans.Request().Method(), aTrans.Session().StringPool() ) != EMethodPost )
   201         HttpCacheUtil::MethodFromStr( aTrans.Request().Method(), aTrans.Session().StringPool() ) != EMethodPost )
   201         {
   202         {
   202         // If the cacheMode is noCache then it must ignore the cached entry.
   203         // If the cacheMode is noCache then it must ignore the cached entry.
   203         entry = iLookupTable->Find( aTrans.Request().URI().UriDes() );
   204         entry = iLookupTable->Find( aTrans.Request().URI().UriDes() );
   240                 entry->Accessed();
   241                 entry->Accessed();
   241                 }
   242                 }
   242             else
   243             else
   243                 {
   244                 {
   244                 // cleanup on the corrupt entry
   245                 // cleanup on the corrupt entry
   245                 HandleCorruptEntry( *entry );
   246                 DeleteCacheEntry( *entry );
   246                 entry = NULL;
   247                 entry = NULL;
   247                 // item is not in cache
   248                 // item is not in cache
   248                 status = KErrNotFound;
   249                 status = KErrNotFound;
   249                 }
   250                 }
   250             }
   251             }
   344 //
   345 //
   345 void CHttpCacheHandler::RequestClosed(
   346 void CHttpCacheHandler::RequestClosed(
   346     RHTTPTransaction* aTrans,
   347     RHTTPTransaction* aTrans,
   347     THttpCacheEntry& aCacheEntry )
   348     THttpCacheEntry& aCacheEntry )
   348     {
   349     {
   349     if (aTrans)
   350 #ifdef __CACHELOG__
   350         HttpCacheUtil::WriteUrlToLog( 0, _L( "Request is closed" ), aTrans->Request().URI().UriDes() );
   351     if ( aTrans )
   351     // fine
   352         HttpCacheUtil::WriteUrlToLog( 0, _L( "CHttpCacheHandler::RequestClosed on url =" ), aTrans->Request().URI().UriDes() );
   352     // make sure transaction is moved to the complete list
   353 #endif
       
   354 
       
   355     // Fine, make sure transaction is moved to the complete list
   353     CHttpCacheEntry* entry = aCacheEntry.iCacheEntry;
   356     CHttpCacheEntry* entry = aCacheEntry.iCacheEntry;
   354 
   357 
   355     if( entry )
   358     if ( entry )
   356         {
   359         {
   357         // normal close on a request - when the content is loaded from the cache
   360         // normal close on a request - when the content is loaded from the cache
   358         if( entry->State() == CHttpCacheEntry::ECacheRequesting )
   361         if ( entry->State() == CHttpCacheEntry::ECacheRequesting )
   359             {
   362             {
   360             entry->SetState( CHttpCacheEntry::ECacheComplete );
   363             entry->SetState( CHttpCacheEntry::ECacheComplete );
   361             iStreamHandler->Detach( *entry );
   364             iStreamHandler->Detach( *entry );
   362             }
   365             }
   363         // transaction is closed without being completed
   366         // transaction is closed without being completed
   364         else if( entry->State() == CHttpCacheEntry::ECacheResponding ||
   367         else if ( entry->State() == CHttpCacheEntry::ECacheResponding ||
   365             entry->State() == CHttpCacheEntry::ECacheDestroyed )
   368                   entry->State() == CHttpCacheEntry::ECacheDestroyed )
   366             {
   369             {
   367             // remove uncompleted/destroyed entry
   370             // remove uncompleted/destroyed entry
   368             iStreamHandler->Detach( *entry );
   371             iStreamHandler->Detach( *entry );
   369             HandleCorruptEntry( *entry );
   372             DeleteCacheEntry( *entry );
   370             entry = NULL;
   373             entry = NULL;
   371             aCacheEntry.iCacheEntry = NULL;
   374             aCacheEntry.iCacheEntry = NULL;
   372 #ifdef __CACHELOG__
   375 #ifdef __CACHELOG__
   373             HttpCacheUtil::WriteLog( 0, _L( "uncompleted entry" ) );
   376             HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheHandler::RequestClosed - uncompleted entry" ) );
   374 #endif
   377 #endif
   375             }
   378             }
   376         else if( entry->State() == CHttpCacheEntry::ECacheComplete )
   379         else if ( entry->State() == CHttpCacheEntry::ECacheComplete )
   377             {
   380             {
   378             // normal close on the request - when the contet is saved to the cache
   381             // normal close on the request - when the content is saved to the cache
   379             // ResponseComplete has already been called
   382             // ResponseComplete has already been called
   380             // check if the stream is released
   383             // check if the stream is released
   381             __ASSERT_DEBUG( !iStreamHandler->Find( *entry ) , PanicCacheHandler( KErrCorrupt ) );
   384             // __ASSERT_DEBUG( !entry->CacheFilesOpened() ) , PanicCacheHandler( KErrCorrupt ) );
   382             }
   385             }
   383         else
   386         else
   384             {
   387             {
   385             __ASSERT_DEBUG( EFalse , PanicCacheHandler( KErrCorrupt ) );
   388             __ASSERT_DEBUG( EFalse , PanicCacheHandler( KErrCorrupt ) );
   386             }
   389             }
   387         }
   390         }
   388     }
   391     }
   389 
       
   390 
   392 
   391 // -----------------------------------------------------------------------------
   393 // -----------------------------------------------------------------------------
   392 // CHttpCacheHandler::AdjustResponseTime
   394 // CHttpCacheHandler::AdjustResponseTime
   393 //
   395 //
   394 // -----------------------------------------------------------------------------
   396 // -----------------------------------------------------------------------------
   451 void CHttpCacheHandler::ReceivedResponseHeadersL(
   453 void CHttpCacheHandler::ReceivedResponseHeadersL(
   452     RHTTPTransaction& aTrans,
   454     RHTTPTransaction& aTrans,
   453     THttpCacheEntry& aCacheEntry )
   455     THttpCacheEntry& aCacheEntry )
   454     {
   456     {
   455 #ifdef __CACHELOG__
   457 #ifdef __CACHELOG__
   456     HttpCacheUtil::WriteUrlToLog( 0, _L( "received http headers" ), aTrans.Request().URI().UriDes() );
   458     HttpCacheUtil::WriteLog( 0, _L("---> CHttpCacheHandler::ReceivedResponseHeadersL"), 0);
   457 #endif
   459     HttpCacheUtil::WriteUrlToLog( 0, _L( "CHttpCacheHandler::ReceivedResponseHeadersL - received http headers" ), aTrans.Request().URI().UriDes() );
   458     //
   460 #endif
       
   461 
   459     TBool protectedEntry( EFalse );
   462     TBool protectedEntry( EFalse );
   460     // check if the item is cacheable
   463     // check if the item is cacheable
   461     // no item should be bigger than the 1/3 of the cache size
   464     // no item should be bigger than the 1/3 of the cache size
   462     if( HttpCacheUtil::IsCacheable( aTrans, ( iSize / 3 ), protectedEntry ) )
   465     if( HttpCacheUtil::IsCacheable( aTrans, ( iSize / 3 ), protectedEntry ) )
   463         {
   466         {
   464         // check if the entry is already in the cache
   467         // check if the entry is already in the cache
   465         CHttpCacheEntry* entry = iLookupTable->Find( aTrans.Request().URI().UriDes() );
   468         CHttpCacheEntry* entry = iLookupTable->Find( aTrans.Request().URI().UriDes() );
   466         if( entry )
   469         if( entry )
   467             {
   470             {
   468 #ifdef __CACHELOG__
   471 #ifdef __CACHELOG__
   469             HttpCacheUtil::WriteUrlToLog( 0, _L( "item is already in the cache" ), entry->Url() );
   472         	HttpCacheUtil::WriteLogFilenameAndUrl( 0,
   470 #endif            
   473                                        _L("CHttpCacheHandler::ReceivedResponseHeadersL"),
   471             //
   474                                        entry->Filename(),
       
   475                                        entry->Url(),
       
   476                                        entry->BodySize(), 
       
   477                                        ELogEntrySize );
       
   478 #endif
       
   479 
   472             if( entry->State() != CHttpCacheEntry::ECacheComplete )
   480             if( entry->State() != CHttpCacheEntry::ECacheComplete )
   473                 {
   481                 {
   474                 // multiple incoming entries? doh.
   482                 // multiple incoming entries doh.
   475 #ifdef __CACHELOG__
   483 #ifdef __CACHELOG__
   476                 HttpCacheUtil::WriteLog( 0, _L( "MULTIPLE REQUEST!!!!!!!!!!!!!!!!!!!!!!!!!" ) );
   484         		HttpCacheUtil::WriteLogFilenameAndUrl( 0,
   477 #endif                
   485                                        _L("CHttpCacheHandler::ReceivedResponseHeadersL - ERROR MULTIPLE requests"),
   478                 // __ASSERT_DEBUG( EFalse, PanicCacheHandler( KErrCorrupt ) );
   486                                        entry->Filename(),
       
   487                                        entry->Url(),
       
   488                                        entry->BodySize(), 
       
   489                                        ELogEntrySize );
       
   490 #endif
   479                 // ignore this one and the first will proceed.
   491                 // ignore this one and the first will proceed.
   480                 entry = NULL;
   492                 entry = NULL;
   481                 }
   493                 }
   482             }
   494             }
   483         else
   495         else
   484             {
   496             {
   485 #ifdef __CACHELOG__
   497 #ifdef __CACHELOG__
   486             HttpCacheUtil::WriteLog( 0, _L( "create new cache item" ) );
   498             HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheHandler::ReceivedResponseHeadersL - create new cache item" ) );
   487 #endif
   499 #endif
   488             //Check adjustment of response time is required or not.
   500             //Check adjustment of response time is required or not.
   489             AdjustResponseTime( aTrans );
   501             AdjustResponseTime( aTrans );
   490             // hash it
   502             // hash it
   491             entry = iLookupTable->InsertL( aTrans.Request().URI().UriDes() );
   503             entry = iLookupTable->InsertL( aTrans.Request().URI().UriDes() );
   493                 {
   505                 {
   494                 // protect this entry
   506                 // protect this entry
   495                 if( protectedEntry )
   507                 if( protectedEntry )
   496                     {
   508                     {
   497 #ifdef __CACHELOG__
   509 #ifdef __CACHELOG__
   498                     HttpCacheUtil::WriteLog( 0, _L( "this item is protected" ) );
   510         			HttpCacheUtil::WriteLogFilenameAndUrl( 0,
   499 #endif
   511                                        _L("CHttpCacheHandler::ReceivedResponseHeadersL - this is protected item"),
   500                     //
   512                                        entry->Filename(),
       
   513                                        entry->Url(),
       
   514                                        entry->BodySize(), 
       
   515                                        ELogEntrySize );
       
   516 #endif
   501                     entry->SetProtected();
   517                     entry->SetProtected();
   502                     RHTTPHeaders responseHeaders = aTrans.Response().GetHeaderCollection();
   518                     RHTTPHeaders responseHeaders = aTrans.Response().GetHeaderCollection();
   503                     RStringPool strP = aTrans.Session().StringPool();
   519                     RStringPool strP = aTrans.Session().StringPool();
   504 
   520 
   505                     // double expiration time
   521                     // double expiration time
   522                 // 1. handle only 304 and 200
   538                 // 1. handle only 304 and 200
   523                 // 2. check if either the header or the body ( or both ) need to be updated
   539                 // 2. check if either the header or the body ( or both ) need to be updated
   524                 // 3. update the headers anyway in case of notmodified (304)
   540                 // 3. update the headers anyway in case of notmodified (304)
   525                 // 4. remove the old body in case of bodyupdate
   541                 // 4. remove the old body in case of bodyupdate
   526                 TInt httpStatus( aTrans.Response().StatusCode() );
   542                 TInt httpStatus( aTrans.Response().StatusCode() );
   527 #ifdef __CACHELOG__
   543 
   528                 HttpCacheUtil::WriteLog( 0, _L( "status code: " ), httpStatus );
   544 #ifdef __CACHELOG__
   529 #endif
   545                 HttpCacheUtil::WriteLog( 0,  _L("CHttpCacheHandler::ReceivedResponseHeadersL - status code ="), httpStatus );
   530                 //
   546 #endif
   531                 TBool ok( EFalse );
   547                 TBool ok( EFalse );
   532                 if( httpStatus == HTTPStatus::EOk )
   548                 if( httpStatus == HTTPStatus::EOk )
   533                     {
   549                     {
   534                     ok = HandleResponseOkL( *entry, aTrans );
   550                     ok = HandleResponseOkL( *entry, aTrans );
   535                     }
   551                     }
   536                 else if( httpStatus == HTTPStatus::ENotModified )
   552                 else if( httpStatus == HTTPStatus::ENotModified )
   537                     {
   553                     {
   538                     ok = HandleResponseNotModifiedL( *entry, aTrans );
   554                     ok = HandleResponseNotModifiedL( *entry, aTrans );
   539                     }
   555                     }
   540                 //
   556 
   541                 // entry could be corrupted at this point
   557                 // entry could be corrupted at this point
   542                 if( ok )
   558                 if( ok )
   543                     {
   559                     {
   544                     // save handler and entry so that
   560                     // save handler and entry so that
   545                     // on next call we don't have to start a lookup again
   561                     // on next call we don't have to start a lookup again
   547                     aCacheEntry.iCacheEntry = entry;
   563                     aCacheEntry.iCacheEntry = entry;
   548                     }
   564                     }
   549                 else
   565                 else
   550                     {
   566                     {
   551                     iStreamHandler->Detach( *entry );
   567                     iStreamHandler->Detach( *entry );
   552                     HandleCorruptEntry( *entry );
   568                     DeleteCacheEntry( *entry );
   553                     entry = NULL;
   569                     entry = NULL;
   554                     }
   570                     }
   555                 }
   571                 }
   556             else
   572             else
   557                 {
   573                 {
   558                 HandleCorruptEntry( *entry );
   574                 DeleteCacheEntry( *entry );
   559                 entry = NULL;
   575                 entry = NULL;
   560                 }
   576                 }
   561             }
   577             }
   562         }
   578         }
   563     }
   579     }
   570 void CHttpCacheHandler::ReceivedResponseBodyDataL(
   586 void CHttpCacheHandler::ReceivedResponseBodyDataL(
   571     RHTTPTransaction& aTrans,
   587     RHTTPTransaction& aTrans,
   572     MHTTPDataSupplier& aBodyDataSupplier,
   588     MHTTPDataSupplier& aBodyDataSupplier,
   573     THttpCacheEntry& aCacheEntry )
   589     THttpCacheEntry& aCacheEntry )
   574     {
   590     {
   575     HttpCacheUtil::WriteUrlToLog( 0, _L( "received body" ), aTrans.Request().URI().UriDes() );
   591 #ifdef __CACHELOG__
       
   592     HttpCacheUtil::WriteUrlToLog( 0, _L( "CHttpCacheHandler::ReceivedResponseBodyDataL - received body from url =" ), aTrans.Request().URI().UriDes() );
       
   593 #endif
       
   594 
   576     // 1. check if we are caching this resource
   595     // 1. check if we are caching this resource
   577     // 2. update the body data
   596     // 2. update the body data
   578     CHttpCacheEntry* entry = aCacheEntry.iCacheEntry;
   597     CHttpCacheEntry* entry = aCacheEntry.iCacheEntry;
   579 
   598 
   580     if( entry && entry->State() == CHttpCacheEntry::ECacheResponding )
   599     if ( entry && entry->State() == CHttpCacheEntry::ECacheResponding )
   581         {
   600         {
       
   601 #ifdef __CACHELOG__
       
   602         HttpCacheUtil::WriteLog( 0, _L("---> CHttpCacheHandler::ReceivedResponseBodyDataL"), entry->BodySize() );
       
   603 #endif
   582         HBufC8* bodyStr = HttpCacheUtil::BodyToBufferL( aBodyDataSupplier );
   604         HBufC8* bodyStr = HttpCacheUtil::BodyToBufferL( aBodyDataSupplier );
   583         if( bodyStr )
   605         if ( bodyStr )
   584             {
   606             {
       
   607             // Do we have old body data to remove first
       
   608             if ( entry->BodyFileDeleteNeeded() ) {
       
   609                 iStreamHandler->RemoveBodyData( *entry );
       
   610                 entry->SetBodyFileDeleteNeeded( EFalse );
       
   611             	}
       
   612             
   585             // erase entry if we are unable to save it (low disk space)
   613             // erase entry if we are unable to save it (low disk space)
   586             if( !SaveBuffer( *entry, bodyStr->Des(), ETrue ) )
   614             if( !SaveBuffer( *entry, bodyStr->Des(), ETrue ) )
   587                 {
   615                 {
   588                 // detach it from the stream and erase it
   616                 // detach it from the stream and erase it
   589                 iStreamHandler->Detach( *entry );
   617                 iStreamHandler->Detach( *entry );
   590                 HandleCorruptEntry( *entry );
   618                 DeleteCacheEntry( *entry );
   591 #ifdef __CACHELOG__                
   619 #ifdef __CACHELOG__                
   592                 HttpCacheUtil::WriteLog( 0, _L( "body cannot be saved" ) );
   620                 HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheHandler::ReceivedResponseBodyDataL - body cannot be saved" ) );
   593 #endif                
   621 #endif                
   594                 entry = NULL;
   622                 entry = NULL;
   595                 // remove entry
   623                 // remove entry
   596                 aCacheEntry.iCacheEntry = NULL;
   624                 aCacheEntry.iCacheEntry = NULL;
   597                 }
   625                 }
   598 #ifdef __CACHELOG__
   626 #ifdef __CACHELOG__
   599             else
   627             else
   600                 {
   628                 {
   601                 HttpCacheUtil::WriteLog( 0, _L( "body is saved" ) );
   629                 HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheHandler::ReceivedResponseBodyDataL - body is saved" ) );
   602                 }
   630                 }
   603 #endif // __CACHELOG__
   631 #endif // __CACHELOG__
   604             //
   632             //
   605             delete bodyStr;
   633             delete bodyStr;
   606             }
   634             }
   619     HttpCacheUtil::WriteUrlToLog( 0, _L( "response complete" ), aTrans.Request().URI().UriDes() );
   647     HttpCacheUtil::WriteUrlToLog( 0, _L( "response complete" ), aTrans.Request().URI().UriDes() );
   620     // 1. check if we are caching this resource
   648     // 1. check if we are caching this resource
   621     // 2. mark the entry as complete
   649     // 2. mark the entry as complete
   622     CHttpCacheEntry* entry = aCacheEntry.iCacheEntry;
   650     CHttpCacheEntry* entry = aCacheEntry.iCacheEntry;
   623 
   651 
   624     if( entry )
   652     if ( entry )
   625         {
   653         {
   626         if( entry->State() == CHttpCacheEntry::ECacheResponding )
   654 #ifdef __CACHELOG__
   627             {
   655         HttpCacheUtil::WriteLog( 0, _L("---> CHttpCacheHandler::ResponseComplete"), entry->BodySize() );
   628             // flush the entry
   656         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
   629             if( !iStreamHandler->Flush( *entry ) )
   657                                            _L("CHttpCacheHandler::ResponseComplete"),
   630                 {
   658                                            entry->Filename(),
       
   659                                            entry->Url(),
       
   660                                            entry->BodySize(),
       
   661                                            ELogEntrySize );
       
   662 #endif
       
   663 
       
   664         if ( entry->State() == CHttpCacheEntry::ECacheResponding )
       
   665             {
       
   666             // Flush the entry
       
   667             if ( !iStreamHandler->Flush( *entry ) )
       
   668                 {
       
   669                 // We failed saving (flush), cleanup
   631                 iStreamHandler->Detach( *entry );
   670                 iStreamHandler->Detach( *entry );
   632                 HandleCorruptEntry( *entry );
   671 
   633 #ifdef __CACHELOG__
   672                 // Deleting the entry frees cache buffer
   634                 HttpCacheUtil::WriteLog( 0, _L( "body cannot be saved" ) );
   673                 DeleteCacheEntry( *entry );
   635 #endif
       
   636                 entry = NULL;
   674                 entry = NULL;
   637                 // remove entry
       
   638                 aCacheEntry.iCacheEntry = NULL;
   675                 aCacheEntry.iCacheEntry = NULL;
   639                 }
   676                 }
   640             else
   677             else
   641                 {
   678                 {
       
   679                 // We successfully saved (flush) body
   642                 entry->SetState( CHttpCacheEntry::ECacheComplete );
   680                 entry->SetState( CHttpCacheEntry::ECacheComplete );
   643                 iStreamHandler->Detach( *entry );
   681                 iStreamHandler->Detach( *entry );
       
   682 
       
   683                 // Clear the flushed cache buffer, we were using for incoming body
       
   684                 entry->SetCacheBufferL( KBufferSizeZero );
   644                 }
   685                 }
   645             }
   686             }
   646         else if( entry->State() == CHttpCacheEntry::ECacheDestroyed )
   687         else if( entry->State() == CHttpCacheEntry::ECacheDestroyed )
   647             {
   688             {
   648             iStreamHandler->Detach( *entry );
   689             iStreamHandler->Detach( *entry );
   649             HandleCorruptEntry( *entry, EFalse );
   690 
       
   691             // Deleting the entry frees cache buffer
       
   692             DeleteCacheEntry( *entry, EFalse );
       
   693             entry = NULL;
   650             aCacheEntry.iCacheEntry = NULL;
   694             aCacheEntry.iCacheEntry = NULL;
   651             }
   695             }
   652         }
   696         }
   653     }
   697     }
   654 
   698 
   683 // -----------------------------------------------------------------------------
   727 // -----------------------------------------------------------------------------
   684 // CHttpCacheHandler::RemoveL
   728 // CHttpCacheHandler::RemoveL
   685 //
   729 //
   686 // -----------------------------------------------------------------------------
   730 // -----------------------------------------------------------------------------
   687 //
   731 //
   688 TInt CHttpCacheHandler::RemoveL(
   732 TInt CHttpCacheHandler::RemoveL( const TDesC8& aUrl )
   689     const TDesC8& aUrl )
       
   690     {
   733     {
   691     TInt status( KErrNotFound );
   734     TInt status( KErrNotFound );
   692     HttpCacheUtil::WriteUrlToLog( 0, _L( "remove item:" ), aUrl );
   735     HttpCacheUtil::WriteUrlToLog( 0, _L( "remove item:" ), aUrl );
   693     CHttpCacheEntry* entry = iLookupTable->Find( aUrl );
   736     CHttpCacheEntry* entry = iLookupTable->Find( aUrl );
   694 
   737 
   695     if( entry )
   738     if ( entry )
   696         {
   739         {
   697         if( entry->State() == CHttpCacheEntry::ECacheComplete )
   740         if ( entry->State() == CHttpCacheEntry::ECacheComplete )
   698             {
   741             {
   699             // delete
   742             // Delete from lookup table
   700             status = iLookupTable->Remove( aUrl );
   743             status = iLookupTable->Remove( aUrl );
   701             }
   744             }
   702         else
   745         else
   703             {
   746             {
   704             // mark it as deleted and erase it when the
   747             // Mark it as deleted and erase it when the
   705             // trans is complete
   748             // trans is complete
   706             entry->SetState( CHttpCacheEntry::ECacheDestroyed );
   749             entry->SetState( CHttpCacheEntry::ECacheDestroyed );
   707             status = KErrNone;
   750             status = KErrNone;
   708             }
   751             }
   709         }
   752         }
   713 // -----------------------------------------------------------------------------
   756 // -----------------------------------------------------------------------------
   714 // CHttpCacheHandler::Find
   757 // CHttpCacheHandler::Find
   715 //
   758 //
   716 // -----------------------------------------------------------------------------
   759 // -----------------------------------------------------------------------------
   717 //
   760 //
   718 TBool CHttpCacheHandler::Find(
   761 TBool CHttpCacheHandler::Find( const TDesC8& aUrl )
   719     const TDesC8& aUrl )
       
   720     {
   762     {
   721     // find
   763     // find
   722     CHttpCacheEntry* entry = iLookupTable->Find( aUrl );
   764     CHttpCacheEntry* entry = iLookupTable->Find( aUrl );
   723     return ( entry ? ( entry->State() == CHttpCacheEntry::ECacheComplete ) : EFalse );
   765     return ( entry ? ( entry->State() == CHttpCacheEntry::ECacheComplete ) : EFalse );
   724     }
   766     }
   756             iStreamHandler->Detach( *entry );
   798             iStreamHandler->Detach( *entry );
   757             }
   799             }
   758         // cleanup
   800         // cleanup
   759         if( !saved && entry )
   801         if( !saved && entry )
   760             {
   802             {
   761             HandleCorruptEntry( *entry );
   803             DeleteCacheEntry( *entry );
   762             }
   804             }
   763         }
   805         }
   764     return saved;
   806     return saved;
   765     }
   807     }
   766 
   808 
   804 
   846 
   805                     if( !saveOk )
   847                     if( !saveOk )
   806                         {
   848                         {
   807                         // sorry, we made this entry corrupt. remove it
   849                         // sorry, we made this entry corrupt. remove it
   808                         iStreamHandler->Detach( *entry );
   850                         iStreamHandler->Detach( *entry );
   809                         HandleCorruptEntry( *entry );
   851                         DeleteCacheEntry( *entry );
   810                         entry = NULL;
   852                         entry = NULL;
   811                         }
   853                         }
   812                     }
   854                     }
   813                 else
   855                 else
   814                     {
   856                     {
   884                         {
   926                         {
   885 #ifdef __CACHELOG__
   927 #ifdef __CACHELOG__
   886                         HttpCacheUtil::WriteLog( 0, _L( "cache item is not fresh. needs revalidation" ) );
   928                         HttpCacheUtil::WriteLog( 0, _L( "cache item is not fresh. needs revalidation" ) );
   887 #endif
   929 #endif
   888                         // MKLE-7PRD27: Avoid removing cache entry here 
   930                         // MKLE-7PRD27: Avoid removing cache entry here 
   889                         
       
   890                         mustRevalidate = ETrue;
   931                         mustRevalidate = ETrue;
   891                         // add headers like EIfModifiedSince, EETag, EIfNoneMatch
   932                         // add headers like EIfModifiedSince, EETag, EIfNoneMatch
   892                         HttpCacheUtil::AddValidationHeaders( responseHeaders, requestHeaders, strP );
   933                         HttpCacheUtil::AddValidationHeaders( responseHeaders, requestHeaders, strP );
   893                         }
   934                         }
   894                     else
   935                     else
   913             }
   954             }
   914         CleanupStack::PopAndDestroy(); // headersStr
   955         CleanupStack::PopAndDestroy(); // headersStr
   915         }
   956         }
   916     else
   957     else
   917         {
   958         {
   918         HandleCorruptEntry( aCacheEntry );
   959         DeleteCacheEntry( aCacheEntry );
   919         // needs validation
   960         // needs validation
   920         mustRevalidate = ETrue;
   961         mustRevalidate = ETrue;
   921         }
   962         }
   922     return mustRevalidate;
   963     return mustRevalidate;
   923     }
   964     }
   925 // -----------------------------------------------------------------------------
   966 // -----------------------------------------------------------------------------
   926 // CHttpCacheHandler::CacheNeedsSpaceL
   967 // CHttpCacheHandler::CacheNeedsSpaceL
   927 //
   968 //
   928 // -----------------------------------------------------------------------------
   969 // -----------------------------------------------------------------------------
   929 //
   970 //
   930 TBool CHttpCacheHandler::CacheNeedsSpaceL(
   971 TBool CHttpCacheHandler::CacheNeedsSpaceL( TInt aSize )
   931     TInt aSize )
       
   932     {
   972     {
   933     TBool ok( ETrue );
   973     TBool ok( ETrue );
   934 #ifdef __CACHELOG__
   974 #ifdef __CACHELOG__
   935     HttpCacheUtil::WriteLog( 0, _L( "the cache is this big" ), iSize );
   975     HttpCacheUtil::WriteLog( 0, _L( "the cache is this big" ), iSize );
   936     HttpCacheUtil::WriteLog( 0, _L( "we occupy" ), iStreamHandler->SavedContentSize()  );
   976     HttpCacheUtil::WriteLog( 0, _L( "we occupy" ), iStreamHandler->SavedContentSize()  );
   937     HttpCacheUtil::WriteLog( 0, _L( "this item needs space:" ), aSize );
   977     HttpCacheUtil::WriteLog( 0, _L( "this item needs space:" ), aSize );
   938 #endif // __CACHELOG__
   978 #endif // __CACHELOG__
   939 
   979 
   940     // check if we need extra space
   980     // check if we need extra space
   941     if( iStreamHandler->SavedContentSize() + aSize > iSize )
   981     if ( iStreamHandler->SavedContentSize() + aSize > iSize )
   942         {
   982         {
       
   983 
   943 #ifdef __CACHELOG__
   984 #ifdef __CACHELOG__
   944         // items in the cache
   985         // items in the cache
   945         TInt size( 0 );
   986         TInt size( 0 );
   946         HttpCacheUtil::WriteLog( 0, _L( "cached items" ) );
   987         HttpCacheUtil::WriteLog( 0, _L( "cached items" ) );
       
   988 
   947         const CArrayPtrFlat<CHttpCacheEntry>& entries = iLookupTable->Entries();
   989         const CArrayPtrFlat<CHttpCacheEntry>& entries = iLookupTable->Entries();
   948         for( TInt i = 0; i < entries.Count(); i++ )
   990         for( TInt i = 0; i < entries.Count(); i++ )
   949             {
   991             {
   950             CHttpCacheEntry* entry = entries.At( i );
   992             CHttpCacheEntry* entry = entries.At( i );
   951             if( entry && entry != (CHttpCacheEntry*)0xffffffff )
   993             if( entry && entry != (CHttpCacheEntry*)0xffffffff )
   952                 {
   994                 {
   953                 HttpCacheUtil::WriteUrlToLog( 0, entry->Url(), entry->Size() );
   995                 HttpCacheUtil::WriteUrlToLog( 0, entry->Url(), entry->BodySize() );
   954                 size+=entry->Size();
   996                 size += entry->BodySize();
   955                 size+=entry->HeaderSize();
   997                 size += entry->HeaderSize();
   956                 }
   998                 }
   957             }
   999             }
   958         HttpCacheUtil::WriteLog( 0, _L( "occupy with headers:" ), size );
  1000         HttpCacheUtil::WriteLog( 0, _L( "occupy with headers:" ), size );
   959 #endif // __CACHELOG__
  1001 #endif // __CACHELOG__
       
  1002 
   960         CArrayPtrFlat<CHttpCacheEntry>* evictedList = iEvictionHandler->EvictL( aSize );
  1003         CArrayPtrFlat<CHttpCacheEntry>* evictedList = iEvictionHandler->EvictL( aSize );
   961         if( evictedList && evictedList->Count() )
  1004         if ( evictedList && evictedList->Count() )
   962             {
  1005             {
   963             // destroy items
  1006             // destroy items
   964             CHttpCacheEntry* entry;
  1007             CHttpCacheEntry* entry;
   965             for( TInt i = 0; i < evictedList->Count(); i++ )
  1008             for( TInt i = 0; i < evictedList->Count(); i++ )
   966                 {
  1009                 {
   970                     {
  1013                     {
   971                     // destroy
  1014                     // destroy
   972                     iLookupTable->Remove( entry->Url() );
  1015                     iLookupTable->Remove( entry->Url() );
   973                     }
  1016                     }
   974                 }
  1017                 }
   975             // __ASSERT_DEBUG( iStreamHandler->SavedContentSize() + aSize < iSize, PanicCacheHandler( KErrCorrupt ) );
  1018 
   976             // ok = ETrue if there is enough space
  1019             // ok = ETrue if there is enough space
   977             ok = ( iStreamHandler->SavedContentSize() + aSize < iSize );
  1020             ok = ( iStreamHandler->SavedContentSize() + aSize < iSize );
   978             }
  1021             }
   979         else
  1022         else
   980             {
  1023             {
  1002     {
  1045     {
  1003     TBool saveOk( ETrue );
  1046     TBool saveOk( ETrue );
  1004     RHTTPHeaders responseHeader = aTrans.Response().GetHeaderCollection();
  1047     RHTTPHeaders responseHeader = aTrans.Response().GetHeaderCollection();
  1005     RStringPool strP = aTrans.Session().StringPool();
  1048     RStringPool strP = aTrans.Session().StringPool();
  1006     HBufC8* responseHeaderStr = HttpCacheUtil::HeadersToBufferLC( responseHeader, strP );
  1049     HBufC8* responseHeaderStr = HttpCacheUtil::HeadersToBufferLC( responseHeader, strP );
  1007     //
  1050 
  1008     TBool update( ETrue );
  1051     TBool update( ETrue );
  1009     // get cached headers to compare
  1052     // get cached headers to compare
  1010     HBufC8* cachedHeaderStr = iStreamHandler->HeadersL( aEntry );
  1053     HBufC8* cachedHeaderStr = iStreamHandler->HeadersL( aEntry );
  1011     // we've got some headers to update,
  1054     
  1012     // check if we really need to update them
  1055     // we've got some headers to update, check if we really need to update them
  1013     if( cachedHeaderStr )
  1056     if ( cachedHeaderStr )
  1014         {
  1057         {
  1015         CleanupStack::PushL( cachedHeaderStr );
  1058         CleanupStack::PushL( cachedHeaderStr );
  1016         //
  1059 
  1017         update = HttpCacheUtil::CacheNeedsUpdateL( responseHeader, cachedHeaderStr->Des(), strP );
  1060         update = HttpCacheUtil::CacheNeedsUpdateL( responseHeader, cachedHeaderStr->Des(), strP );
  1018 
  1061 
  1019         CleanupStack::PopAndDestroy(); // cachedHeaderStr
  1062         CleanupStack::PopAndDestroy(); // cachedHeaderStr
  1020         }
  1063         }
  1021     //
  1064 
  1022     if( update )
  1065     if ( update )
  1023         {
  1066         {
  1024 #ifdef __CACHELOG__
  1067 #ifdef __CACHELOG__
  1025         HttpCacheUtil::WriteLog( 0, _L( "udpate headers" ) );
  1068         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
  1026 #endif
  1069                                        _L("CHttpCacheHandler::HandleResponseOkL - cache UPDATE needed"),
  1027         if( aEntry.HeaderSize() )
  1070                                        aEntry.Filename(),
  1028             {
  1071                                        aEntry.Url(),
  1029             // remove it first
  1072                                        aEntry.BodySize(), 
       
  1073                                        ELogEntrySize );
       
  1074 #endif
       
  1075         if ( aEntry.HeaderSize() )
       
  1076             {
       
  1077             // remove headers from headerFile first
  1030             iStreamHandler->RemoveHeaders( aEntry );
  1078             iStreamHandler->RemoveHeaders( aEntry );
  1031             }
  1079             }
  1032         // save
  1080 
       
  1081         // save new headerFile
  1033         saveOk = SaveBuffer( aEntry, responseHeaderStr->Des() );
  1082         saveOk = SaveBuffer( aEntry, responseHeaderStr->Des() );
  1034 
  1083         
  1035         if( aEntry.Size() )
  1084         if ( aEntry.BodySize() )
  1036             {
  1085             {
  1037 #ifdef __CACHELOG__
  1086             // We will remove this body data, after we confirm that we get new
  1038             HttpCacheUtil::WriteLog( 0, _L( "remove body" ) );
  1087             // body data
  1039 #endif            
  1088             aEntry.SetBodyFileDeleteNeeded( ETrue );
  1040             //
  1089             }
  1041             iStreamHandler->RemoveBodyData( aEntry );
  1090 
  1042             }
  1091         // Setup a cache buffer to hold the incoming body
       
  1092         aEntry.SetCacheBufferL( KBufferSize32k );
  1043         }
  1093         }
  1044     else
  1094     else
  1045         {
  1095         {
  1046         // if neither the header nor the body need to be updated, then
  1096         // if neither the header nor the body need to be updated, then
  1047         // detach entry to protect from being updated
  1097         // detach entry to protect from being updated
  1048 #ifdef __CACHELOG__
  1098 #ifdef __CACHELOG__
  1049         HttpCacheUtil::WriteLog( 0, _L( "no udpate needed, ignore response" ) );
  1099         HttpCacheUtil::WriteLog( 0, _L( "CHttpCacheHandler::HandleResponseOkL - no update needed, ignore response" ) );
  1050 #endif        
  1100 #endif        
  1051         //
  1101         //
  1052         aEntry.SetState( CHttpCacheEntry::ECacheComplete );
  1102         aEntry.SetState( CHttpCacheEntry::ECacheComplete );
  1053         iStreamHandler->Detach( aEntry );
  1103         iStreamHandler->Detach( aEntry );
  1054         // pretend that save was ok.
  1104         // pretend that save was ok.
  1055         saveOk = ETrue;
  1105         saveOk = ETrue;
  1056         }
  1106         }
       
  1107 
       
  1108     CleanupStack::PopAndDestroy(); // responseHeaderStr
       
  1109 
  1057     // destroy corrupt entry by returning EFalse
  1110     // destroy corrupt entry by returning EFalse
  1058     CleanupStack::PopAndDestroy(); // responseHeaderStr
       
  1059     return saveOk;
  1111     return saveOk;
  1060     }
  1112     }
  1061 
  1113 
  1062 // -----------------------------------------------------------------------------
  1114 // -----------------------------------------------------------------------------
  1063 // CHttpCacheHandler::HandleResponseNotModifiedL
  1115 // CHttpCacheHandler::HandleResponseNotModifiedL
  1161         if( ret == KErrNone )
  1213         if( ret == KErrNone )
  1162             {
  1214             {
  1163             CleanupClosePushL( readStream );
  1215             CleanupClosePushL( readStream );
  1164             aLookupTable->InternalizeL( readStream, iDirectory->Des() );
  1216             aLookupTable->InternalizeL( readStream, iDirectory->Des() );
  1165             CleanupStack::PopAndDestroy(1); // readStream
  1217             CleanupStack::PopAndDestroy(1); // readStream
  1166         }
  1218             }
  1167     }
  1219     }
  1168 
  1220 
  1169 // -----------------------------------------------------------------------------
  1221 // -----------------------------------------------------------------------------
  1170 // CHttpCacheHandler::SaveLookupTableL
  1222 // CHttpCacheHandler::SaveLookupTableL
  1171 //
  1223 //
  1206         }
  1258         }
  1207     iHttpCacheObserver->StartObserver();
  1259     iHttpCacheObserver->StartObserver();
  1208     }
  1260     }
  1209 
  1261 
  1210 // -----------------------------------------------------------------------------
  1262 // -----------------------------------------------------------------------------
  1211 // CHttpCacheHandler::HandleCorruptEntry
  1263 // CHttpCacheHandler::DeleteCacheEntry
  1212 //
  1264 //
  1213 // -----------------------------------------------------------------------------
  1265 // -----------------------------------------------------------------------------
  1214 //
  1266 //
  1215 void CHttpCacheHandler::HandleCorruptEntry(
  1267 void CHttpCacheHandler::DeleteCacheEntry(
  1216     CHttpCacheEntry& aStrayEntry,
  1268     CHttpCacheEntry& aEntry,
  1217     TBool aUpdate )
  1269     TBool aUpdate )
  1218     {
  1270     {
  1219     (void)aUpdate;//suppress compiler and PC-lint warnings
  1271     // suppress compiler and PC-lint warnings
  1220 #ifdef __CACHELOG__
  1272     (void)aUpdate;
  1221     HttpCacheUtil::WriteLog( 0, _L( "delete this stray entry" ) );
  1273 
  1222 #endif
  1274 #ifdef __CACHELOG__
  1223     // remove from the lookuptable
  1275         HttpCacheUtil::WriteLogFilenameAndUrl( 0,
  1224     iLookupTable->EraseCorruptEntry( aStrayEntry.Url() );
  1276                                        _L("CHttpCacheHandler::DeleteCacheEntry"),
       
  1277                                        aEntry.Filename(),
       
  1278                                        aEntry.Url(),
       
  1279                                        aEntry.BodySize(), 
       
  1280                                        ELogEntrySize );
       
  1281 #endif
       
  1282 
       
  1283     // Remove from the lookuptable
       
  1284     iLookupTable->EraseCacheEntry( aEntry.Url() );
  1225     }
  1285     }
  1226 
  1286 
  1227 // -----------------------------------------------------------------------------
  1287 // -----------------------------------------------------------------------------
  1228 // There used to be a CHttpCacheHandler::FixLookupTableL here. 
  1288 // There used to be a CHttpCacheHandler::FixLookupTableL here. 
  1229 // Go back in SVN to re-discover it :)
  1289 // Go back in SVN to re-discover it :)
  1239     CHttpCacheEntry& aEntry,
  1299     CHttpCacheEntry& aEntry,
  1240     const TDesC8& aBuffer,
  1300     const TDesC8& aBuffer,
  1241     TBool aBody )
  1301     TBool aBody )
  1242     {
  1302     {
  1243     TBool ok( EFalse );
  1303     TBool ok( EFalse );
       
  1304     
  1244     TRAPD( err, ok = CacheNeedsSpaceL( aBuffer.Length() ) );
  1305     TRAPD( err, ok = CacheNeedsSpaceL( aBuffer.Length() ) );
  1245     if( err == KErrNone && ok )
  1306     if ( err == KErrNone && ok )
  1246         {
  1307         {
  1247         // safe save
  1308         // Did we have a safe save of the body or header
  1248         ok = aBody ? iStreamHandler->SaveBodyData( aEntry, aBuffer ) : iStreamHandler->SaveHeaders( aEntry, aBuffer );
  1309         ok = aBody ? iStreamHandler->SaveBodyData( aEntry, aBuffer ) : iStreamHandler->SaveHeaders( aEntry, aBuffer );
  1249         }
  1310         }
  1250 #ifdef __CACHELOG__
  1311 #ifdef __CACHELOG__
  1251     else
  1312     else
  1252         {
  1313         {