webengine/wmlengine/src/FileLoader/src/fileloader_csaveddeckhandler.cpp
changeset 0 dd21522fd290
child 26 cb62a4f66ebe
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Handler for loading file scheme.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32std.h>
       
    22 #include <e32base.h>
       
    23 #include <uri16.h>
       
    24 #include <apmrec.h>
       
    25 #include <apgcli.h>
       
    26 
       
    27 #include "fileloader_csaveddeckhandler.h"
       
    28 #include "fileloader_cfileloader.h"
       
    29 #include "urlloader_loaderutils.h"
       
    30 #include "nwx_http_defs.h"
       
    31 #include "nwx_settings.h"
       
    32 
       
    33 
       
    34 
       
    35 // EXTERNAL DATA STRUCTURES
       
    36 
       
    37 // EXTERNAL FUNCTION PROTOTYPES
       
    38 
       
    39 // CONSTANTS
       
    40 _LIT8(KUtf8, "utf-8");
       
    41 _LIT8(KUsAscii, "us-ascii");
       
    42 _LIT8(KIso10646Ucs2, "iso-10646-ucs-2");
       
    43 _LIT8(KIso88591, "iso-8859-1");
       
    44 _LIT8(KBig5, "big5");
       
    45 _LIT8(KGb2312, "gb2312");
       
    46 _LIT8(KIso88592, "iso-8859-2");
       
    47 _LIT8(KIso88593, "iso-8859-3");
       
    48 _LIT8(KIso88594, "iso-8859-4");
       
    49 _LIT8(KIso88595, "iso-8859-5");
       
    50 _LIT8(KIso88596, "iso-8859-6");
       
    51 _LIT8(KIso88597, "iso-8859-7");
       
    52 _LIT8(KIso88598, "iso-8859-8");
       
    53 _LIT8(KIso88599, "iso-8859-9");
       
    54 _LIT8(KShiftJIS, "shift_JIS");
       
    55 _LIT8(KWindows1250, "windows-1250");
       
    56 _LIT8(KWindows1251, "windows-1251");
       
    57 _LIT8(KWindows1253, "windows-1253");
       
    58 _LIT8(KWindows1254, "windows-1254");
       
    59 _LIT8(KWindows1255, "windows-1255");
       
    60 _LIT8(KWindows1256, "windows-1256");
       
    61 _LIT8(KWindows1257, "windows-1257");
       
    62 _LIT8(KTis620,		"Tis-620");
       
    63 _LIT8(KJisX0201_1997,    "jis_x0201-1997");
       
    64 _LIT8(KJisX0208_1997,    "jis_x0208-1997");
       
    65 _LIT8(KIso2022Jp,	"iso-2022-jp");
       
    66 _LIT8(KEucJp,		"euc-jp");
       
    67 _LIT8(KWindows874,	"windows-874");
       
    68 _LIT8(KOI8R, "koi8-r");
       
    69 _LIT8(KOI8U, "koi8-u");
       
    70 
       
    71 
       
    72 // MACROS
       
    73 
       
    74 // LOCAL CONSTANTS AND MACROS
       
    75 
       
    76 // MODULE DATA STRUCTURES
       
    77 
       
    78 // LOCAL FUNCTION PROTOTYPES
       
    79 
       
    80 // FORWARD DECLARATIONS
       
    81 
       
    82 // ============================= LOCAL FUNCTIONS ===============================
       
    83 
       
    84 
       
    85 // ============================ MEMBER FUNCTIONS ===============================
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CSavedDeckPart::Reset
       
    89 // Zero out all members of the structure CSavedDeckPart.
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CSavedDeckPart::Reset()
       
    93 {
       
    94     iUrl.Set(NULL, 0);
       
    95     iData.Set(NULL, 0);
       
    96     iContentTypeString.Set(NULL, 0);
       
    97     iCharset = 0;
       
    98 	iOriCharset = 0;
       
    99 }
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CSavedDeckHandler::CSavedDeckHandler
       
   104 // C++ default constructor can NOT contain any code, that
       
   105 // might leave.
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 CSavedDeckHandler::CSavedDeckHandler(TPtrC8& aData) : iData(aData), iDataReader(aData)
       
   109 {
       
   110 }
       
   111 
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CSavedDeckHandler::NewLC
       
   115 // Two-phased constructor.
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 CSavedDeckHandler* CSavedDeckHandler::NewLC(TPtrC8& aData)
       
   119 {
       
   120     CSavedDeckHandler* self = new( ELeave ) CSavedDeckHandler(aData);
       
   121 
       
   122     CleanupStack::PushL( self );
       
   123 
       
   124     return self;
       
   125 }
       
   126 
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CSavedDeckHandler::~CSavedDeckHandler
       
   130 // Destructor.
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 CSavedDeckHandler::~CSavedDeckHandler()
       
   134 {
       
   135 }
       
   136 
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CSavedDeckHandler::ParseSavedDeck
       
   140 // Parse the saved deck buffer into the parts and store each part in cache.
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 TInt CSavedDeckHandler::ParseSavedDeck(TPtrC8& aData, TPtrC& aRequestUrl,
       
   144                                        TPtr8& aResponseData,
       
   145                                        TPtr& aResponseUrl,
       
   146                                        TPtr8& aContentTypeString,
       
   147                                        TUint& aCharset, TBool& aSavedDeck)
       
   148 {
       
   149     TRAPD(ret, ParseSavedDeckL(aData, aRequestUrl, aResponseData, aResponseUrl,
       
   150                                aContentTypeString, aCharset, aSavedDeck));
       
   151     LoaderUtils::LogLoaderEvents(ELogTypeSavedDeck, ret);
       
   152     return ret;
       
   153 }
       
   154 
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CSavedDeckHandler::ParseSavedDeckL
       
   158 // Parse the saved deck buffer into the parts and store each part in cache.
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CSavedDeckHandler::ParseSavedDeckL(TPtrC8& aData, TPtrC& aRequestUrl,
       
   162                                         TPtr8& aResponseData,
       
   163                                         TPtr& aResponseUrl,
       
   164                                         TPtr8& aContentTypeString,
       
   165                                         TUint& aCharset, TBool& aSavedDeck)
       
   166 {
       
   167     // Is this a SavedDeck?
       
   168     aSavedDeck = IsSavedDeck(aRequestUrl);
       
   169     if (!(aSavedDeck))
       
   170     {
       
   171         return;
       
   172     }
       
   173 
       
   174     CSavedDeckHandler* self = CSavedDeckHandler::NewLC(aData);
       
   175     TUint i;
       
   176     CSavedDeckPart savedDeckPart;
       
   177 
       
   178     // Get the number of parts
       
   179     TUint count = self->GetUintFromBuffer(self->iDataReader);
       
   180 
       
   181     for (i = 0; i < count; i++)
       
   182     {
       
   183         self->GetNextPartL(savedDeckPart);
       
   184         if (i == 0)
       
   185         {
       
   186             NW_Settings_SetOriginalEncoding(savedDeckPart.iOriCharset);
       
   187 			// First time through loop prepare and then push aResponseData,
       
   188 			// aResponseUrl, aContentTypeString onto CleanupStack
       
   189             self->PrepareResponseLC(savedDeckPart, aResponseData, aResponseUrl,
       
   190                                     aContentTypeString, aCharset);
       
   191         }
       
   192         else
       
   193         {
       
   194             self->LoadPartToCacheL( savedDeckPart);
       
   195         }
       
   196     }
       
   197 
       
   198     CleanupStack::Pop(3); // aResponseData, aResponseUrl, aContentTypeString
       
   199     CleanupStack::PopAndDestroy(); // self
       
   200 }
       
   201 
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CSavedDeckHandler::IsSavedDeck
       
   205 // Check if the loaded file is a saved deck, based on the path and extension.
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 TBool CSavedDeckHandler::IsSavedDeck(TPtrC& aRequestUrl)
       
   209 {
       
   210     _LIT(KFileExtension, ".saveddeck");
       
   211     _LIT(KFilePath, "saveddecks");
       
   212     return (aRequestUrl.FindF(KFileExtension()) != KErrNotFound &&
       
   213             aRequestUrl.FindF(KFilePath()) != KErrNotFound);
       
   214 }
       
   215 
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CSavedDeckHandler::GetNextPartL
       
   219 // Extract the next data part from the buffer.
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CSavedDeckHandler::GetNextPartL(CSavedDeckPart& aSavedDeckPart)
       
   223 {
       
   224     TInt i;
       
   225 
       
   226     aSavedDeckPart.Reset();
       
   227     // Get the version
       
   228     const TUint8* version = (const TUint8*)SAVED_DECK_VERSION;
       
   229     TUint versionLen = User::StringLength(version);
       
   230 
       
   231     // Is it the correct version?
       
   232     if (iDataReader.Find(version, versionLen) != 0)
       
   233     {
       
   234         User::Leave(KErrCorrupt);
       
   235     }
       
   236     // The version is contained, so the buffer must be big enough
       
   237     iDataReader.Set(iDataReader.Mid(versionLen));
       
   238 
       
   239     // Get the URL
       
   240     if (iDataReader.Length() < (TInt)sizeof(TUint))
       
   241     {
       
   242         User::Leave(KErrCorrupt);
       
   243     }
       
   244     i = GetUintFromBuffer(iDataReader) + 1; // 1 for NULL terminator
       
   245     if (iDataReader.Length() < i)
       
   246     {
       
   247         User::Leave(KErrCorrupt);
       
   248     }
       
   249     aSavedDeckPart.iUrl.Set(iDataReader.Ptr(), i);
       
   250     // Find that NULL terminator is still there
       
   251     if (aSavedDeckPart.iUrl[i - 1] != '\0')
       
   252     {
       
   253         User::Leave(KErrCorrupt);
       
   254     }
       
   255     iDataReader.Set(iDataReader.Mid(i));
       
   256 
       
   257     // Get the content type string
       
   258     if (iDataReader.Length() < (TInt)sizeof(TInt)) // length constant type string
       
   259     {
       
   260         User::Leave(KErrCorrupt);
       
   261     }
       
   262     i = GetUintFromBuffer(iDataReader) + 1; // 1 for NULL terminator
       
   263     if (iDataReader.Length() < i)
       
   264     {
       
   265         User::Leave(KErrCorrupt);
       
   266     }
       
   267     aSavedDeckPart.iContentTypeString.Set(iDataReader.Ptr(), i);
       
   268     // Find that NULL terminator is still there
       
   269     if (aSavedDeckPart.iContentTypeString[i - 1] != '\0')
       
   270     {
       
   271         User::Leave(KErrCorrupt);
       
   272     }
       
   273     iDataReader.Set(iDataReader.Mid(i));
       
   274 
       
   275     // Get Charset
       
   276     aSavedDeckPart.iCharset = GetUintFromBuffer(iDataReader);
       
   277 
       
   278     // Get original Charset setting
       
   279    aSavedDeckPart.iOriCharset = GetUintFromBuffer(iDataReader);
       
   280 
       
   281     // Get the body
       
   282     if (iDataReader.Length() < (TInt)sizeof(TInt))
       
   283     {
       
   284         User::Leave(KErrCorrupt);
       
   285     }
       
   286     i = GetUintFromBuffer(iDataReader);
       
   287     if (iDataReader.Length() < i + 1) // 1 for NULL terminator
       
   288     {
       
   289         User::Leave(KErrCorrupt);
       
   290     }
       
   291     aSavedDeckPart.iData.Set(iDataReader.Ptr(), i);
       
   292     // Find that NULL terminator is still there
       
   293     if (iDataReader[i] != '\0')
       
   294     {
       
   295         User::Leave(KErrCorrupt);
       
   296     }
       
   297     iDataReader.Set(iDataReader.Mid(i+ 1));
       
   298 }
       
   299 
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CSavedDeckHandler::PrepareResponseLC
       
   303 // Copy the data and URL of the first part, to be returned as the response.
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 void CSavedDeckHandler::PrepareResponseLC(CSavedDeckPart& aSavedDeckPart,
       
   307 										  TPtr8& aResponseData,
       
   308 										  TPtr& aResponseUrl,
       
   309 										  TPtr8& aContentTypeString,
       
   310 										  TUint& aCharset)
       
   311 {
       
   312     // Copy data
       
   313     TUint8* data = new(ELeave) TUint8 [aSavedDeckPart.iData.Length()];
       
   314     CleanupStack::PushL(data);
       
   315     aResponseData.Set(data, 0, aSavedDeckPart.iData.Length());
       
   316     aResponseData.Copy(aSavedDeckPart.iData);
       
   317 
       
   318     // Copy URL
       
   319     TUint16* url = new(ELeave) TUint16 [aSavedDeckPart.iUrl.Length()];
       
   320     CleanupStack::PushL(url);
       
   321     aResponseUrl.Set(url, 0, aSavedDeckPart.iUrl.Length());
       
   322     aResponseUrl.Copy(aSavedDeckPart.iUrl);
       
   323 
       
   324     // Copy content type string
       
   325     // note: string deleted in 'destructor' of reponse class
       
   326     TUint8* cts = new(ELeave) TUint8 [aSavedDeckPart.iContentTypeString.Length()];
       
   327     CleanupStack::PushL(cts);
       
   328     aContentTypeString.Set(cts, 0, aSavedDeckPart.iContentTypeString.Length());
       
   329     aContentTypeString.Copy(aSavedDeckPart.iContentTypeString);
       
   330 
       
   331     // Charset
       
   332     aCharset = aSavedDeckPart.iCharset;
       
   333 }
       
   334 
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CSavedDeckHandler::LoadPartToCacheL
       
   338 // Send a response part to cache.
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 void CSavedDeckHandler::LoadPartToCacheL(CSavedDeckPart& aSavedDeckPart)
       
   342 {
       
   343     TPtrC8 charsetStr = ConvertCharsetToStr(aSavedDeckPart.iCharset);
       
   344     aSavedDeckPart.iUrl.Set(aSavedDeckPart.iUrl.Ptr(), aSavedDeckPart.iUrl.Length() - 1);
       
   345     aSavedDeckPart.iContentTypeString.Set(aSavedDeckPart.iContentTypeString.Ptr(), aSavedDeckPart.iContentTypeString.Length() - 1);
       
   346 }
       
   347 
       
   348 
       
   349 TPtrC8 CSavedDeckHandler::ConvertCharsetToStr(TUint32 aCharset)
       
   350 {
       
   351     if (aCharset == HTTP_utf_8)
       
   352         return KUtf8().Ptr();
       
   353     if (aCharset == HTTP_us_ascii)
       
   354         return KUsAscii().Ptr();
       
   355     if (aCharset ==HTTP_iso_10646_ucs_2)
       
   356         return KIso10646Ucs2().Ptr();
       
   357     if (aCharset == HTTP_iso_8859_1)
       
   358         return KIso88591().Ptr();
       
   359     if (aCharset == HTTP_gb2312)
       
   360         return KGb2312().Ptr();
       
   361     if (aCharset == HTTP_big5)
       
   362         return KBig5().Ptr();
       
   363     if (aCharset == HTTP_iso_8859_2)
       
   364         return KIso88592().Ptr();
       
   365     if (aCharset == HTTP_iso_8859_3)
       
   366         return KIso88593().Ptr();
       
   367     if (aCharset == HTTP_iso_8859_4)
       
   368         return KIso88594().Ptr();
       
   369     if (aCharset == HTTP_iso_8859_5)
       
   370         return KIso88595().Ptr();
       
   371     if (aCharset == HTTP_iso_8859_6)
       
   372         return KIso88596().Ptr();
       
   373     if (aCharset == HTTP_iso_8859_7)
       
   374         return KIso88597().Ptr();
       
   375     if (aCharset == HTTP_iso_8859_8)
       
   376         return KIso88598().Ptr();
       
   377     if (aCharset == HTTP_iso_8859_9)
       
   378         return KIso88599().Ptr();
       
   379     if (aCharset == HTTP_shift_JIS )
       
   380         return KShiftJIS().Ptr();
       
   381     if (aCharset == HTTP_windows_1250)
       
   382         return KWindows1250().Ptr();
       
   383     if (aCharset == HTTP_windows_1251)
       
   384         return KWindows1251().Ptr();
       
   385     if (aCharset == HTTP_windows_1253)
       
   386         return KWindows1253().Ptr();
       
   387     if (aCharset == HTTP_windows_1254)
       
   388         return KWindows1254().Ptr();
       
   389     if (aCharset == HTTP_windows_1255)
       
   390         return KWindows1255().Ptr();
       
   391     if (aCharset == HTTP_windows_1256)
       
   392         return KWindows1256().Ptr();
       
   393     if (aCharset == HTTP_windows_1257)
       
   394         return KWindows1257().Ptr();
       
   395     if (aCharset == HTTP_tis_620)
       
   396         return KTis620().Ptr();
       
   397     if (aCharset == HTTP_jis_x0201_1997)
       
   398         return KJisX0201_1997().Ptr();
       
   399     if (aCharset == HTTP_jis_x0208_1997)
       
   400         return KJisX0208_1997().Ptr();
       
   401     if (aCharset == HTTP_iso_2022_jp)
       
   402         return KIso2022Jp().Ptr();
       
   403     if (aCharset == HTTP_euc_jp)
       
   404         return KEucJp().Ptr();
       
   405     if (aCharset == HTTP_windows_874)
       
   406         return KWindows874().Ptr();
       
   407     if (aCharset == HTTP_Koi8_r)
       
   408         return KOI8R().Ptr();
       
   409     if (aCharset == HTTP_Koi8_u)
       
   410         return KOI8U().Ptr();
       
   411     return KUtf8().Ptr();
       
   412 }
       
   413 
       
   414 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   415 
       
   416 //  End of File