webengine/osswebengine/cache/src/HttpCacheUtil.cpp
changeset 1 7c90e6132015
parent 0 dd21522fd290
child 10 a359256acfc6
equal deleted inserted replaced
0:dd21522fd290 1:7c90e6132015
    13 *
    13 *
    14 * Description:   Implementation of HttpCacheUtil
    14 * Description:   Implementation of HttpCacheUtil
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
       
    19 // INCLUDE FILES
    18 // INCLUDE FILES
    20 #include "HttpCacheUtil.h"
    19 #include "HttpCacheUtil.h"
    21 #include <http/rhttpheaders.h>
    20 #include <http/rhttpheaders.h>
    22 #include <http/RHTTPTransaction.h>
    21 #include <http/RHTTPTransaction.h>
    23 #include <http.h>
    22 #include <http.h>
   499     age = Age( aCachedHeaders, cachedRequestTime, cachedResponseTime, aStrP );
   498     age = Age( aCachedHeaders, cachedRequestTime, cachedResponseTime, aStrP );
   500 #ifdef __CACHELOG__
   499 #ifdef __CACHELOG__
   501         TBuf<50> dateString;
   500         TBuf<50> dateString;
   502         TTime fr( freshness );
   501         TTime fr( freshness );
   503 
   502 
   504         fr.FormatL( dateString, KDateString );
   503         TRAP(err, fr.FormatL( dateString, KDateString ) );
   505         HttpCacheUtil::WriteLog( 0, _L( "fresness" ) );
   504         if ( err == KErrNone ) {
   506         HttpCacheUtil::WriteLog( 0, dateString );
   505             HttpCacheUtil::WriteLog( 0, _L( "freshness" ) );
       
   506             HttpCacheUtil::WriteLog( 0, dateString );
       
   507             }
   507 
   508 
   508         TTime ca( age );
   509         TTime ca( age );
   509         ca.FormatL( dateString, KDateString );
   510         TRAP( err, ca.FormatL( dateString, KDateString ) );
   510         HttpCacheUtil::WriteLog( 0, _L( "age" ) );
   511         if ( err == KErrNone ) {
   511         HttpCacheUtil::WriteLog( 0, dateString );
   512             HttpCacheUtil::WriteLog( 0, _L( "age" ) );
       
   513             HttpCacheUtil::WriteLog( 0, dateString );
       
   514             }
   512 #endif // __CACHELOG__
   515 #endif // __CACHELOG__
   513 
   516 
   514     // Get useful cache-control directives from the requestHeaders
   517     // Get useful cache-control directives from the requestHeaders
   515     status = GetCacheControls( aRequestHeaders, &maxAge, &minFresh, &maxStale, NULL, NULL, NULL, aStrP );
   518     status = GetCacheControls( aRequestHeaders, &maxAge, &minFresh, &maxStale, NULL, NULL, NULL, aStrP );
   516     if( status == KErrNone )
   519     if( status == KErrNone )
   702     TInt noStoreField( requestHeaders.GetField( fieldName, 0, headerVal ) );
   705     TInt noStoreField( requestHeaders.GetField( fieldName, 0, headerVal ) );
   703     //
   706     //
   704     return( noCacheField == KErrNone || noStoreField == KErrNone );
   707     return( noCacheField == KErrNone || noStoreField == KErrNone );
   705     }
   708     }
   706 
   709 
       
   710         
   707 // -----------------------------------------------------------------------------
   711 // -----------------------------------------------------------------------------
   708 // HttpCacheUtil::GetHeaderFileName
   712 // HttpCacheUtil::GetHeaderFileName
   709 //
   713 //
   710 // -----------------------------------------------------------------------------
   714 // -----------------------------------------------------------------------------
   711 //
   715 //
   718     aHeaderFileName.Copy( aBodyFileName );
   722     aHeaderFileName.Copy( aBodyFileName );
   719     // take the filename and append the new extension
   723     // take the filename and append the new extension
   720     aHeaderFileName.Append( KHttpCacheHeaderExt() );
   724     aHeaderFileName.Append( KHttpCacheHeaderExt() );
   721     }
   725     }
   722 
   726 
       
   727 
   723 // -----------------------------------------------------------------------------
   728 // -----------------------------------------------------------------------------
   724 // HttpCacheUtil::AdjustExpirationTime
   729 // HttpCacheUtil::AdjustExpirationTime
   725 //
   730 //
   726 // -----------------------------------------------------------------------------
   731 // -----------------------------------------------------------------------------
   727 //
   732 //
   732     const TStringTable& stringTable = RHTTPSession::GetTable();
   737     const TStringTable& stringTable = RHTTPSession::GetTable();
   733     THTTPHdrVal hdrVal;
   738     THTTPHdrVal hdrVal;
   734 
   739 
   735     if( aResponseHeaders.GetField( aStrP.StringF( HTTP::EExpires, stringTable ), 0, hdrVal ) == KErrNone )
   740     if( aResponseHeaders.GetField( aStrP.StringF( HTTP::EExpires, stringTable ), 0, hdrVal ) == KErrNone )
   736         {
   741         {
       
   742         TTime expDate( hdrVal.DateTime() );
       
   743 
       
   744 #ifdef __CACHELOG__
   737         HttpCacheUtil::WriteLog( 0, _L( "adjust expiration time from" ) );
   745         HttpCacheUtil::WriteLog( 0, _L( "adjust expiration time from" ) );
   738 
   746 
   739         TTime expDate( hdrVal.DateTime() );
       
   740 #ifdef __CACHELOG__
       
   741         TBuf<50> dateString;
   747         TBuf<50> dateString;
   742         TTime expTime( hdrVal.DateTime() );
   748         TTime expTime( hdrVal.DateTime() );
   743 
   749 
   744         expTime.FormatL( dateString, KDateString );
   750         expTime.FormatL( dateString, KDateString );
   745         HttpCacheUtil::WriteLog( 0, dateString );
   751         HttpCacheUtil::WriteLog( 0, dateString );
   746 #endif // __CACHELOG__
   752 #endif // __CACHELOG__
       
   753 
   747         // double it
   754         // double it
   748         TTimeIntervalMinutes minutes;
   755         TTimeIntervalMinutes minutes;
   749         TTimeIntervalHours hours;
   756         TTimeIntervalHours hours;
   750         TTime now;
   757         TTime now;
   751         now.UniversalTime();
   758         now.UniversalTime();
   752 
   759 
   753         if( expDate.MinutesFrom( now, minutes ) == KErrNone )
   760         if( expDate.MinutesFrom( now, minutes ) == KErrNone )
   754             {
   761             {
       
   762 
   755 #ifdef __CACHELOG__
   763 #ifdef __CACHELOG__
   756         //
       
   757         now.FormatL( dateString, KDateString );
   764         now.FormatL( dateString, KDateString );
       
   765 
   758         HttpCacheUtil::WriteLog( 0, _L( "current time" ) );
   766         HttpCacheUtil::WriteLog( 0, _L( "current time" ) );
   759         HttpCacheUtil::WriteLog( 0, dateString );
   767         HttpCacheUtil::WriteLog( 0, dateString );
   760         //
   768         //
   761         now.FormatL( dateString, KDateString );
   769         now.FormatL( dateString, KDateString );
   762         HttpCacheUtil::WriteLog( 0, _L( "expires in (minutes)" ), minutes.Int() );
   770         HttpCacheUtil::WriteLog( 0, _L( "expires in (minutes)" ), minutes.Int() );
   763 #endif // __CACHELOG__
   771 #endif // __CACHELOG__
   764             //
   772 
   765             expDate+=minutes;
   773             expDate+=minutes;
   766             }
   774             }
   767         // minutes owerflow? take hours instead
   775         // minutes owerflow? take hours instead
   768         else if( expDate.HoursFrom( now, hours ) == KErrNone )
   776         else if( expDate.HoursFrom( now, hours ) == KErrNone )
   769             {
   777             {
   961     // check if this is a noncacheable content type
   969     // check if this is a noncacheable content type
   962     if( respHeaders.GetField( fieldName, 0, contType ) == KErrNone &&
   970     if( respHeaders.GetField( fieldName, 0, contType ) == KErrNone &&
   963         contType.StrF() == strP.StringF( HttpFilterCommonStringsExt::EApplicationVndOmaDrm,
   971         contType.StrF() == strP.StringF( HttpFilterCommonStringsExt::EApplicationVndOmaDrm,
   964         HttpFilterCommonStringsExt::GetTable() ) )
   972         HttpFilterCommonStringsExt::GetTable() ) )
   965         {
   973         {
   966         HttpCacheUtil::WriteLog( 0, _L( "sensitive content. do not cache" ) );
   974 #ifdef __CACHELOG__
       
   975         HttpCacheUtil::WriteLog( 0, _L( "HttpCacheUtil::IsCacheable - sensitive content. do not cache" ) );
       
   976 #endif
   967         // drm == nocache
   977         // drm == nocache
   968         isCacheable = EFalse;
   978         isCacheable = EFalse;
   969         }
   979         }
   970     else
   980     else
   971         {
   981         {
   974         fieldName = strP.StringF( HTTP::EContentLength, RHTTPSession::GetTable() );
   984         fieldName = strP.StringF( HTTP::EContentLength, RHTTPSession::GetTable() );
   975 
   985 
   976         if( respHeaders.GetField( fieldName, 0, contLen ) != KErrNotFound &&
   986         if( respHeaders.GetField( fieldName, 0, contLen ) != KErrNotFound &&
   977             ( contLen.Type() == THTTPHdrVal::KTIntVal && contLen.Int() > aMaxSize ) )
   987             ( contLen.Type() == THTTPHdrVal::KTIntVal && contLen.Int() > aMaxSize ) )
   978             {
   988             {
       
   989 #ifdef __CACHELOG__
   979             HttpCacheUtil::WriteLog( 0, _L( "oversized content. do not cache" ) );
   990             HttpCacheUtil::WriteLog( 0, _L( "oversized content. do not cache" ) );
       
   991 #endif
   980             // oversized content
   992             // oversized content
   981             return EFalse;
   993             return EFalse;
   982             }
   994             }
   983         // check if this is a proteced entry
   995         // check if this is a proteced entry
   984         aProtectedEntry = ProtectedEntry( respHeaders, strP );
   996         aProtectedEntry = ProtectedEntry( respHeaders, strP );
   990             // non-cachable.  Reject if they are present.
  1002             // non-cachable.  Reject if they are present.
   991             //
  1003             //
   992             // If no-cache or no-store directives exist -> don't cache.
  1004             // If no-cache or no-store directives exist -> don't cache.
   993             if( noCache || noStore )
  1005             if( noCache || noStore )
   994                 {
  1006                 {
   995                 HttpCacheUtil::WriteLog( 0, _L( "no cache/no store header. do not cache" ) );
  1007 #ifdef __CACHELOG__
       
  1008                 HttpCacheUtil::WriteLog( 0, _L( "HttpCacheUtil::IsCacheable - no cache/no store header. do not cache" ) );
       
  1009 #endif
   996                 // no protection on this entry
  1010                 // no protection on this entry
   997                 aProtectedEntry = EFalse;
  1011                 aProtectedEntry = EFalse;
   998                 return EFalse;
  1012                 return EFalse;
   999                 }
  1013                 }
  1000             // Get the current time
  1014             // Get the current time
  1106     {
  1120     {
  1107 #ifdef __CACHELOG__
  1121 #ifdef __CACHELOG__
  1108     TBool log( aLogLevel <= KCurrentLogLevel );
  1122     TBool log( aLogLevel <= KCurrentLogLevel );
  1109     TPtrC fileName( KHttpCacheGeneralFileName );
  1123     TPtrC fileName( KHttpCacheGeneralFileName );
  1110 
  1124 
  1111     if( aLogLevel == 1 )
  1125     if ( aLogLevel == 1 )
  1112         {
  1126         {
  1113         // hash
  1127         // write logging to hash.txt
  1114         fileName.Set( KHttpCacheHashFileName );
  1128         fileName.Set( KHttpCacheHashFileName );
  1115         log = ETrue;
  1129         log = ETrue;
  1116         }
  1130         }
  1117     if( log )
  1131 
  1118         {
  1132     if ( log )
  1119         if( aAny != 0xffff )
  1133         {
       
  1134         if ( aAny != 0xffff )
  1120             {
  1135             {
  1121             RFileLogger::WriteFormat(_L("Browser"), fileName, EFileLoggingModeAppend,
  1136             RFileLogger::WriteFormat(_L("Browser"), fileName, EFileLoggingModeAppend,
  1122                     _L("%S %d"), &aBuf, aAny );
  1137                     _L("%S %d"), &aBuf, aAny );
  1123             }
  1138             }
  1124         else
  1139         else
  1130 #else // __CACHELOG__
  1145 #else // __CACHELOG__
  1131     (void)aLogLevel;
  1146     (void)aLogLevel;
  1132     (void)aBuf;
  1147     (void)aBuf;
  1133     (void)aAny;
  1148     (void)aAny;
  1134 #endif // __CACHELOG__
  1149 #endif // __CACHELOG__
       
  1150     }
       
  1151 
       
  1152 // -----------------------------------------------------------------------------
       
  1153 // HttpCacheUtil::WriteLogFilenameAndUrl
       
  1154 //
       
  1155 // -----------------------------------------------------------------------------
       
  1156 //
       
  1157 void HttpCacheUtil::WriteLogFilenameAndUrl(
       
  1158     TInt aLogLevel,
       
  1159     TPtrC aMethodName,
       
  1160     const TPtrC aFilename,
       
  1161     const TDesC8& aUrl,
       
  1162     TInt aAny,
       
  1163     TLogItemType aItemType )
       
  1164     {
       
  1165 #ifdef __CACHELOG__
       
  1166     // Create a buffer for method name, filename, and url string
       
  1167     _LIT(KColonSpace, " : ");
       
  1168     _LIT(KSpace, " ");
       
  1169     TInt itemTypeStringLen( 30 );
       
  1170     TInt tmpLen( aMethodName.Length() + aFilename.Length() +
       
  1171                  3*KColonSpace().Length() + aUrl.Length() + itemTypeStringLen );
       
  1172     HBufC* tmp = HBufC::New( tmpLen );
       
  1173     
       
  1174     if ( tmp ) {
       
  1175         TPtr tmpPtr( tmp->Des() );
       
  1176         tmpPtr.Copy( aMethodName );
       
  1177         tmpPtr.Append( KColonSpace );
       
  1178 
       
  1179         TChar backSlash('\\');
       
  1180         TInt filenamePos( aFilename.LocateReverse( TChar('\\') ) );
       
  1181         if ( filenamePos > 0 ) {
       
  1182             tmpPtr.Append( aFilename.Right( aFilename.Length() - filenamePos - 1 ) );
       
  1183             tmpPtr.Append( KColonSpace );
       
  1184         }
       
  1185 
       
  1186         // Convert url to TPtr
       
  1187         HBufC* tmpUrl = HBufC::New( aUrl.Length() );
       
  1188         if ( tmpUrl ) {
       
  1189             TPtr tmpUrlPtr( tmpUrl->Des() );
       
  1190             tmpUrlPtr.Copy( aUrl );
       
  1191             TInt urlPos( tmpUrlPtr.LocateReverse( TChar('/') ) );
       
  1192             if ( urlPos > 0 && (aUrl.Length()-2 > urlPos) ) {
       
  1193                 tmpPtr.Append( tmpUrlPtr.Right( aUrl.Length() - urlPos - 1 ) );
       
  1194                 tmpPtr.Append( KColonSpace );
       
  1195             }
       
  1196             else {
       
  1197                 tmpPtr.Append( tmpUrlPtr );
       
  1198                 tmpPtr.Append( KColonSpace );
       
  1199             }
       
  1200         }
       
  1201         
       
  1202         // Append the bucketIndex, lookup table pos, etc...
       
  1203         switch ( aItemType )
       
  1204         {
       
  1205             case ELogItemTypeNone:
       
  1206                 break;
       
  1207         
       
  1208             case ELogBucketIndex:
       
  1209                 tmpPtr.Append( _L("bucketIndex =") );
       
  1210                 break;
       
  1211                 
       
  1212             case ELogEntrySize:
       
  1213                 tmpPtr.Append( _L("entrySize =") );
       
  1214                 break;
       
  1215 
       
  1216             case ELogLookupTablePos:
       
  1217                 tmpPtr.Append( _L("lookupTable pos =") );
       
  1218                 break;
       
  1219 
       
  1220             case ELogFileErrorCode:
       
  1221                 tmpPtr.Append( _L("file errorCode =") );
       
  1222                 break;
       
  1223 
       
  1224             default:
       
  1225                 break;
       
  1226         }
       
  1227 
       
  1228         HttpCacheUtil::WriteLog( aLogLevel, tmpPtr, aAny );
       
  1229 
       
  1230         delete tmp;
       
  1231         delete tmpUrl;
       
  1232         }
       
  1233 #else // __CACHELOG__
       
  1234     (void)aLogLevel;
       
  1235     (void)aMethodName;
       
  1236     (void)aFilename;
       
  1237     (void)aUrl;
       
  1238     (void)aAny;
       
  1239     (void)aItemType;
       
  1240 #endif
  1135     }
  1241     }
  1136 
  1242 
  1137 // -----------------------------------------------------------------------------
  1243 // -----------------------------------------------------------------------------
  1138 // HttpCacheUtil::WriteUrlToLog
  1244 // HttpCacheUtil::WriteUrlToLog
  1139 //
  1245 //
  1219 // The format is <cache base dir> + <subdirectory> + <file name>. 
  1325 // The format is <cache base dir> + <subdirectory> + <file name>. 
  1220 // Caller must free the returned HBufC* when done. 
  1326 // Caller must free the returned HBufC* when done. 
  1221 // -----------------------------------------------------------------------------
  1327 // -----------------------------------------------------------------------------
  1222 HBufC* HttpCacheUtil::GenerateNameLC(
  1328 HBufC* HttpCacheUtil::GenerateNameLC(
  1223         const TDesC8& aUrl, const TDesC& aBaseDir)
  1329         const TDesC8& aUrl, const TDesC& aBaseDir)
  1224         {
  1330     {
  1225 
  1331    
  1226     TUint32 crc (0);
  1332     TUint32 crc (0);
  1227 
  1333     
  1228     //use the entire URL for CRC calculation: maximizes source entropy/avoids collisions
  1334     //use the entire URL for CRC calculation: maximizes source entropy/avoids collisions
  1229     Mem::Crc32(crc, aUrl.Ptr(), aUrl.Size()); 
  1335     Mem::Crc32(crc, aUrl.Ptr(), aUrl.Size()); 
  1230     TUint32 nibble (crc & (KCacheSubdirCount-1)); // extract least significant 4 bits (nibble) for subdirectory
  1336     TUint32 nibble (crc & (KCacheSubdirCount-1)); // extract least significant 4 bits (nibble) for subdirectory
  1231 
  1337     
  1232     HBufC* fileName = HBufC::NewLC( KMaxPath ); // e.g E\078AFEFE
  1338     HBufC* fileName = HBufC::NewLC( KMaxPath ); // e.g E\078AFEFE
  1233     _LIT(KFormat,"%S%x%c%08x"); // Note the %08x : a 32-bit value can represented as 0xFFFFFFFF 
  1339     _LIT(KFormat,"%S%x%c%08x"); // Note the %08x : a 32-bit value can represented as 0xFFFFFFFF 
  1234     fileName->Des().Format(KFormat, &aBaseDir, nibble, KPathDelimiter, crc);
  1340     fileName->Des().Format(KFormat, &aBaseDir, nibble, KPathDelimiter, crc);
  1235     return fileName;
  1341     return fileName;
  1236 
  1342     
  1237     }
  1343     }
  1238 
  1344 
  1239 
  1345 
  1240 // -----------------------------------------------------------------------------
  1346 // -----------------------------------------------------------------------------
  1241 // HttpCacheUtil::Freshness
  1347 // HttpCacheUtil::Freshness
  1349     {
  1455     {
  1350     TTime now;
  1456     TTime now;
  1351     TDateTime date;
  1457     TDateTime date;
  1352     TTime time;
  1458     TTime time;
  1353 
  1459 
  1354     // Int64 timeinsec;
       
  1355     TInt64 correctedRecvAge;
  1460     TInt64 correctedRecvAge;
  1356     TInt64 apparentAge;
  1461     TInt64 apparentAge;
  1357     TInt64 responseDelay;
  1462     TInt64 responseDelay;
  1358     TInt64 correctedInitialAge;
  1463     TInt64 correctedInitialAge;
  1359     TInt64 residentTime;
  1464     TInt64 residentTime;
  1368     // Get the current time. All internet dates are GMT
  1473     // Get the current time. All internet dates are GMT
  1369     now.UniversalTime();
  1474     now.UniversalTime();
  1370 #ifdef __CACHELOG__
  1475 #ifdef __CACHELOG__
  1371     TBuf<50> dateString;
  1476     TBuf<50> dateString;
  1372     //
  1477     //
  1373     now.FormatL( dateString, KDateString );
  1478     TRAP( err, now.FormatL( dateString, KDateString ) );
  1374     HttpCacheUtil::WriteLog( 0, _L( "current time" ) );
  1479     if ( err == KErrNone ) {
  1375     HttpCacheUtil::WriteLog( 0, dateString );
  1480         HttpCacheUtil::WriteLog( 0, _L( "current time" ) );
       
  1481         HttpCacheUtil::WriteLog( 0, dateString );
       
  1482         }
  1376 #endif // __CACHELOG__
  1483 #endif // __CACHELOG__
  1377 
  1484 
  1378     // The aRequestTime is same as that of headers time.
  1485     // The aRequestTime is same as that of headers time.
  1379 
  1486 
  1380     time = aRequestTime;
  1487     time = aRequestTime;
  1595                 }
  1702                 }
  1596             }
  1703             }
  1597         else
  1704         else
  1598             {
  1705             {
  1599             aDirective = cacheDir.Copy();
  1706             aDirective = cacheDir.Copy();
  1600             // Directives which MUST have values;
  1707             // Directives which MUST have values
  1601             if( ( aDirective == aStrP.StringF( HTTP::EMaxAge, RHTTPSession::GetTable() ) ) ||
  1708             if( ( aDirective == aStrP.StringF( HTTP::EMaxAge, RHTTPSession::GetTable() ) ) ||
  1602                 ( aDirective == aStrP.StringF( HTTP::EMinFresh, RHTTPSession::GetTable() ) ) ||
  1709                 ( aDirective == aStrP.StringF( HTTP::EMinFresh, RHTTPSession::GetTable() ) ) ||
  1603                 ( aDirective == aStrP.StringF( HTTP::ESMaxAge, RHTTPSession::GetTable() ) ) )
  1710                 ( aDirective == aStrP.StringF( HTTP::ESMaxAge, RHTTPSession::GetTable() ) ) )
  1604                 {
  1711                 {
  1605                 aDirective.Close();
  1712                 aDirective.Close();