zeroconf/zeroconfsharing/bjrhttpserver.cpp
changeset 21 ff5174af067c
equal deleted inserted replaced
14:da856f45b798 21:ff5174af067c
       
     1 /*
       
     2 * Copyright (c) 2001-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <uri8.h>
       
    20 #include <e32base.h>
       
    21 //#include <http.h>
       
    22 #include <httpclientserver.h>
       
    23 #include <chttpformencoder.h>
       
    24 #include <ssl.h>
       
    25 #include <signed.h>
       
    26 #include <commdbconnpref.h>
       
    27 #include "bjrhttpserver.h"
       
    28 #include <escapeutils.h>
       
    29 
       
    30 const TChar KHttpSlash          = '/';
       
    31 const TChar KFileSlash          = '\\';
       
    32 
       
    33 // format for output of data/time values
       
    34 //_LIT(KDateFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3");
       
    35 
       
    36 // Format for hook file name and location for hooks
       
    37 //_LIT(KHookFormat, "Z:\\httptest\\%S.esk");
       
    38 //_LIT(KHookDir, "C:\\System\\Data\\");
       
    39 
       
    40 //_LIT(KEnterProx, "Enter Proxy authority");
       
    41 //_LIT(KEnterHook, "Enter Hook name (preface with '-' to delete)");
       
    42 //_LIT(KEnterSessId, "Enter Session ID (positive integer or 'none')");
       
    43 
       
    44 _LIT8 (KAbsDocumentRoot, "/Images/");
       
    45 _LIT8 (KRelDocumentRoot, "/Images");
       
    46 _LIT8 (KRoot, "c:\\data\\");
       
    47 _LIT8 (KHomePage, "Images/index.html");
       
    48 _LIT8 (KFound, "Found");
       
    49 _LIT8 (KOk, "OK");
       
    50 _LIT8 (KNotFound, "Not Found");
       
    51 _LIT8(KTextContent, "text/");
       
    52 _LIT8(KImageContent, "image/");
       
    53 _LIT(KHtml, "html");
       
    54 _LIT8(KGif, "gif");
       
    55 /*  If the file httpexampleclient.txt exists in the root directory of a drive
       
    56     it will be used to provide default initial values for when setting the URL, 
       
    57     proxy, etc. The file format is:
       
    58         [var] = [value]
       
    59     for example:
       
    60         PROXY = my.proxy.co.uk:5473
       
    61         URL = http://www.example.org/dir/file.html
       
    62     whitespace is ignored.
       
    63     
       
    64     Note: these are the the values set at run time, they are merely the default
       
    65     options presented to the user. In the above example the proxy is not turned
       
    66     on by default, but the string "my.proxy.co.uk:5473" is presetned as the default
       
    67     option when the use hits "p", saving them the need to have to type it in. 
       
    68     */
       
    69 // Defaults
       
    70 _LIT(KDefaultFileFormat,"%c:\\httpexampleserver.txt");
       
    71 _LIT(KHookString, "HOOK");
       
    72 _LIT(KProxyString, "PROXY");
       
    73 _LIT(KURLString, "URL");
       
    74 _LIT(KSessionIdString, "SESSIONID");
       
    75 //_LIT(KNone, "none");
       
    76 //_LIT(KSchemeHttps, "https");
       
    77 
       
    78 
       
    79 
       
    80 // File system root
       
    81 //_LIT(KFileSystemRoot,"C:\\");
       
    82 //added on July13,09
       
    83 _LIT(KFileName,"c:\\logs\\logs.txt");
       
    84 _LIT(KFileName2,"c:\\logs\\logs2.txt");
       
    85 
       
    86 _LIT(KServerLog,"c:\\httpserverlog.txt");
       
    87 
       
    88 const TInt endpos = 0;
       
    89 enum THttpExampleClientPanics
       
    90     {
       
    91     EReqBodySumitBufferNotAllocated,
       
    92     KBodyWithInvalidSize,
       
    93     KCouldntNotifyBodyDataPart
       
    94     };
       
    95 
       
    96 // Size of buffer used when submitting request bodies
       
    97 //const TInt KMaxSubmitSize = 1024;
       
    98 //const TInt KMaxHeaderNameLen = 32;
       
    99 //const TInt KMaxHeaderValueLen = 128;
       
   100 
       
   101 
       
   102 //
       
   103 // Implementation of CHttpServer
       
   104 //
       
   105 
       
   106 
       
   107 // Supplied as the name of the test program to CHttpExampleUtils
       
   108 _LIT(KHttpExampleClientTestName, "HttpExampleServer");
       
   109 
       
   110 CHttpServer::CHttpServer () 
       
   111     {
       
   112     __FLOG_OPEN ( "http", "httpexampleserver.txt" );
       
   113     __FLOG_0 ( _T8 ( "CHttpServer::CHttpServer "));
       
   114     // Initial timestamp is time now
       
   115     ResetTimeElapsed(); 
       
   116     }
       
   117 
       
   118 
       
   119 CHttpServer::~CHttpServer()
       
   120     {
       
   121     __FLOG_0 ( _T8 ( "CHttpServer::~CHttpServer"));
       
   122     CleanupTransactions ();
       
   123     //iLogFile.Close();
       
   124     iSess.Close();
       
   125     //delete iUtils;
       
   126     iFileServ.Close();
       
   127     iConnection.Close();
       
   128     iSocketServ.Close();
       
   129     
       
   130     iIPAddr.Close();
       
   131     }
       
   132 
       
   133 CHttpServer* CHttpServer::NewLC()
       
   134     {
       
   135     CHttpServer* me = new(ELeave) CHttpServer;
       
   136     CleanupStack::PushL(me);
       
   137     me->ConstructL();
       
   138     return me;
       
   139     }
       
   140 
       
   141 CHttpServer* CHttpServer::NewL()
       
   142     {   
       
   143     CHttpServer* me = NewLC();
       
   144     CleanupStack::Pop(me);
       
   145     return me;
       
   146     }
       
   147 
       
   148 void CHttpServer::ConstructL()
       
   149     {
       
   150     __FLOG_0 ( _T8 ( "CHttpServer::ConstructL"));
       
   151     //iUtils = CHttpExampleUtils::NewL(KHttpExampleClientTestName, *this );   
       
   152     User::LeaveIfError(iFileServ.Connect());
       
   153     iLogFile.Replace(iFileServ,KServerLog,EFileWrite);
       
   154     _LIT8(Kmsg,"\nChttpServer::ConstructL");
       
   155     iLogFile.Write(Kmsg);
       
   156     User::LeaveIfError( iSocketServ.Connect() );
       
   157     
       
   158     }
       
   159 
       
   160 RHTTPTransaction CHttpServer::NewTransactionL ( const TUriC8& aUri, RStringF aMethod )
       
   161     {
       
   162     __FLOG_0 ( _T8 ( "CHttpServer::NewTransactionL "));
       
   163     CHttpServerTransactionBase* trans = CHttpDataTransmitter::NewL (*this, /*iUtils,*/ iFileServ, iSess, aUri, aMethod);
       
   164     iTransactions.Append(trans);
       
   165     return trans->Transaction ();
       
   166     }
       
   167 
       
   168 // to avoid code bloat
       
   169 #define CHECKVAL(name)  if(var==K ##name##String)   {a##name.Copy(val);}
       
   170 
       
   171 void CHttpServer::SetDefaults(TDes& aURL, TDes& aProxy, TDes& aHook, TDes& aSessionId)
       
   172     {
       
   173     __FLOG_4 ( _T8 ( "CHttpServer::SetDefaults [aURL=%S,aProxy=%S,aHook=%S,aSessionId=%S]"), &aURL, &aProxy, &aHook, &aSessionId);
       
   174     TFileName filename;
       
   175     RFile file;
       
   176     filename.Format(KDefaultFileFormat, 'C');
       
   177     TInt err = file.Open(iFileServ, filename, EFileRead);
       
   178     if(err != KErrNone) 
       
   179         {
       
   180         filename.Format(KDefaultFileFormat, 'Z');
       
   181         err = file.Open(iFileServ, filename, EFileRead);
       
   182         }
       
   183     if(err == KErrNone) 
       
   184         {
       
   185         TFileText line;
       
   186         line.Set(file);
       
   187         TInt err = line.Read(filename);
       
   188         while(err == KErrNone || err ==KErrTooBig) 
       
   189             {
       
   190             filename.Trim();
       
   191             TInt div = filename.Locate('=');
       
   192             if(div>0) 
       
   193                 {
       
   194                 TInt i;
       
   195                 for (i=div-1;i>0 && filename[i]==' ';i--);  // ibid
       
   196                 TPtrC var = filename.Left(i+1);
       
   197                 for (i=div+1;i<filename.Length() && filename[i]==' ';i++);  //ibid
       
   198                 TPtrC val = filename.Right(filename.Length()-i);
       
   199                 CHECKVAL(Hook)
       
   200                 else CHECKVAL(Proxy)
       
   201                 else CHECKVAL(URL)
       
   202                 else CHECKVAL(SessionId);   
       
   203                 __FLOG_3 ( _T8 ( "CHttpServer::SetDefaults [URL=%S,Proxy=%S,SessionId=%S]"), &aURL, &aProxy, &aSessionId);
       
   204                 }
       
   205             err = line.Read(filename);
       
   206             }
       
   207         }
       
   208         file.Close();
       
   209     }
       
   210 
       
   211 
       
   212 void CHttpServer::SetupClientL ()
       
   213     {
       
   214     __FLOG_0 ( _T8 ( "CHttpServer::SetupClientL "));
       
   215     ResetTimeElapsed();
       
   216     TBuf<256> url;
       
   217     TBuf<256> prox;
       
   218     TBuf<256> hook;
       
   219     TBuf<16> sessid;
       
   220     SetDefaults(url, prox, hook, sessid);
       
   221     
       
   222     
       
   223     //iUtils->Test().Console()->ClearScreen();
       
   224     _LIT(KSelectOption, "\n\n Select an option \n\n");
       
   225     //iUtils->Test().Printf(KSelectOption);
       
   226     _LIT(KPossibleSelectionsText, " 1 Start \n 2 Stop \n 3 Exit\n");
       
   227     _LIT(KPossibleSelections,"123");
       
   228     //iUtils->RequestSelection ( KPossibleSelectionsText, KPossibleSelections );
       
   229     }
       
   230 
       
   231 void CHttpServer::StartConnectionL(TInt aIapId)
       
   232     {
       
   233     // Connection preferences
       
   234     TCommDbConnPref commDbConnPref;
       
   235     TUint32 iap(aIapId);
       
   236     _LIT8(Kmsg,"\nCHttpServer::StartConnectionL");
       
   237     iLogFile.Write(Kmsg);
       
   238     commDbConnPref.SetIapId( iap );
       
   239     commDbConnPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   240     commDbConnPref.SetBearerSet( ECommDbBearerWLAN );
       
   241     
       
   242     User::LeaveIfError( iConnection.Open( iSocketServ ) );
       
   243     User::LeaveIfError( iConnection.Start( commDbConnPref ) );
       
   244     _LIT8(Kmsg2,"\nConnection started");
       
   245     iLogFile.Write(Kmsg2);
       
   246     }
       
   247 
       
   248 
       
   249 TInt CHttpServer::GetLocalHost ( TSockAddr& aAddr )
       
   250     {
       
   251     RSocket socket;
       
   252     TInt err = socket.Open ( iSocketServ, KAfInet, KSockStream, KProtocolInetTcp );
       
   253 
       
   254     if ( err == KErrNone )
       
   255         {
       
   256         TInetAddr localHost;
       
   257         localHost.SetAddress ( KInetAddrAny );
       
   258         TPckgBuf<TSoInetIfQuery> query;
       
   259         query ().iDstAddr = localHost;
       
   260 
       
   261         err = socket.GetOpt ( KSoInetIfQueryByDstAddr, KSolInetIfQuery, query );
       
   262 
       
   263         if ( err == KErrNone )
       
   264             {
       
   265             // its local IP address
       
   266             localHost = query ().iSrcAddr;
       
   267             aAddr = localHost;
       
   268             }
       
   269         }
       
   270     socket.Close ();
       
   271     return err;
       
   272     }
       
   273 
       
   274 TDesC8& CHttpServer::GetIPAddress()
       
   275     {
       
   276     return iIPAddr;
       
   277     }
       
   278 
       
   279 
       
   280 void CHttpServer::ProcessSelectionL ( TInt aSelection )
       
   281     {   
       
   282     __FLOG_1 ( _T8 ( "CHttpServer::ProcessSelection  [aSelection =%d]"), aSelection );
       
   283     //_LIT8(Kmsg,"\n CHttpServer::ProcessSelection");
       
   284     //iLogFile.Write(Kmsg);
       
   285     switch ( aSelection )
       
   286         {
       
   287         case EStart: 
       
   288             {
       
   289             _LIT8 ( KHTTPServer, "HTTd/TCP" );
       
   290             
       
   291             //_LIT8(Kmsg2,"Opening server RHTTPClientServerSession - ");
       
   292             //iLogFile.Write(Kmsg2);
       
   293             //iLogFile.Write(KHTTPServer);
       
   294             // Open the RHTTPClientServerSession
       
   295             _LIT8(Kmsg2,"\nTrying to connect...");
       
   296             iLogFile.Write(Kmsg2);
       
   297             //StartConnectionL(aIapId);
       
   298             
       
   299             iSess.OpenL ( KHTTPServer () );
       
   300             _LIT8(Kmsg3,"\nRHTTPClientServerSession opened");
       
   301             iLogFile.Write(Kmsg3);
       
   302             /*
       
   303             RHTTPConnectionInfo connInfo = iSess.ConnectionInfo();
       
   304 
       
   305             //Attach to socket server
       
   306             connInfo.SetPropertyL( iSess.StringPool().StringF(HTTP::EHttpSocketServ,
       
   307                                      RHTTPClientServerSession::GetTable() ),
       
   308                                      THTTPHdrVal(iSocketServ.Handle() ) );
       
   309 
       
   310             //Attach to connection
       
   311             TInt connPtr = reinterpret_cast<TInt>( &iConnection );
       
   312             connInfo.SetPropertyL( iSess.StringPool().StringF(
       
   313                                      HTTP::EHttpSocketConnection,
       
   314                                      RHTTPClientServerSession::GetTable() ),
       
   315                                      THTTPHdrVal(connPtr) );
       
   316             */
       
   317             
       
   318             iSess.RegisterDocumentRootL ( KAbsDocumentRoot, *this );
       
   319             _LIT8(Kmsg4,"\nDocumentRoot registered");
       
   320             iLogFile.Write(Kmsg4);
       
   321             iSess.SetSessionEventCallback ( this );
       
   322             
       
   323             //iUtils->Test ().Console ()->ClearScreen ();         
       
   324             _LIT ( KServerStartedMsg, "Http Server Started at Root..." );
       
   325             
       
   326             //_LIT8(Kmsg3,"server started");
       
   327             //iLogFile.Write(Kmsg3);
       
   328             
       
   329             //iUtils->Test ().Printf ( KServerStartedMsg );
       
   330             __FLOG_0 ( _T8 ( "Http Server Started at Root... "));
       
   331                         
       
   332             RStringPool stringPool = iSess.StringPool ();
       
   333             THTTPHdrVal ipValue;
       
   334             iSess.PropertySet ().Property ( stringPool.StringF ( HTTP::ELocalHost, RHTTPClientServerSession::GetTable () ), ipValue );
       
   335             //RBuf ipBuf;
       
   336             //ipBuf.Create ( ipValue.StrF ().DesC ().Length () );
       
   337             //ipBuf.Copy ( ipValue.StrF ().DesC () );
       
   338             //iUtils->Test ().Printf ( _L ( "http://" ) );
       
   339             //iUtils->Test ().Printf ( ipBuf );
       
   340             //iUtils->Test ().Printf ( _L ( "/pws/" ) );
       
   341             //ipBuf.Close ();
       
   342             TInt length = ipValue.StrF ().DesC ().Length ();
       
   343             
       
   344             iIPAddr.Create(length);
       
   345             iIPAddr.Copy(ipValue.StrF ().DesC ());
       
   346             
       
   347             _LIT8(Kmsg5,"\nIP addr obtained");
       
   348             iLogFile.Write(Kmsg5);
       
   349             
       
   350           
       
   351             _LIT ( KSelectOption, "\n\n Select an option \n\n" );
       
   352             //iUtils->Test ().Printf ( KSelectOption );
       
   353             _LIT ( KPossibleSelectionsText, " 2 Stop \n 3 Exit\n" );            
       
   354             _LIT ( KPossibleSelections,"23" );
       
   355             //iUtils->RequestSelection ( KPossibleSelectionsText, KPossibleSelections );  
       
   356             break;
       
   357             }
       
   358         case EStop:
       
   359             {           
       
   360             //iUtils->Test ().Console ()->ClearScreen ();
       
   361             _LIT ( KServerStoppedMsg, "Server Stopped..." );
       
   362             //iUtils->Test ().Printf ( KServerStoppedMsg );
       
   363             
       
   364             CleanupTransactions ();
       
   365             iSess.Close ();
       
   366             
       
   367             //SetupClientL ();
       
   368             
       
   369             break;
       
   370             }
       
   371         case EExit:
       
   372             {
       
   373             //CActiveScheduler::Stop ();
       
   374             }
       
   375             break;
       
   376         default: 
       
   377              break;
       
   378         } 
       
   379     }
       
   380 
       
   381 
       
   382 void CHttpServer::CleanupTransactions ()
       
   383     {
       
   384     __FLOG_0 ( _T8 ( "CHttpServer::CleanupTransactions "));
       
   385     TInt i = iTransactions.Count () - 1;
       
   386     while ( iTransactions.Count () )
       
   387         {
       
   388         iTransactions[i]->Transaction ().Cancel ();
       
   389         CHttpServerTransactionBase* trans = iTransactions[i];
       
   390         iTransactions.Remove ( i );
       
   391         delete trans;
       
   392         --i;
       
   393         }
       
   394     iTransactions.Close (); 
       
   395     }
       
   396     
       
   397 void CHttpServer::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
       
   398     {
       
   399     __FLOG_1 ( _T8 ( "CHttpServer::SetHeaderL [aHdrField=%d]"), aHdrField);
       
   400     RStringF valStr = iSess.StringPool().OpenFStringL(aHdrValue);
       
   401     CleanupClosePushL(valStr);
       
   402     THTTPHdrVal val(valStr);
       
   403     aHeaders.SetFieldL(iSess.StringPool().StringF(aHdrField,RHTTPClientServerSession::GetTable()), val);
       
   404     CleanupStack::PopAndDestroy(&valStr); 
       
   405     }
       
   406 
       
   407 void CHttpServer::ResetTimeElapsed()
       
   408 // Resets timestamp to time now
       
   409     {
       
   410     __FLOG_0 ( _T8 ( "CHttpServer::ResetTimeElapsed"));
       
   411     iLastTimeStamp.UniversalTime(); 
       
   412     }
       
   413 
       
   414 
       
   415 void CHttpServer::DisplayTimeElapsed()
       
   416 // Calculate elapsed time since last measurement, and display
       
   417     {
       
   418     __FLOG_0 ( _T8 ( "CHttpServer::DisplayTimeElapsed"));
       
   419     TTime timeNow;
       
   420     timeNow.UniversalTime();
       
   421     TTimeIntervalMicroSeconds elapsedMicroSec =
       
   422                                     timeNow.MicroSecondsFrom(iLastTimeStamp);
       
   423     iLastTimeStamp = timeNow;
       
   424     /*
       
   425     iUtils->Test().Printf(
       
   426         _L("Time elapsed since last measurement is: %d ms\n"),
       
   427         elapsedMicroSec.Int64()/1000
       
   428         );
       
   429     */
       
   430     }
       
   431 
       
   432 void CHttpServer::OnTransactionCompleted ( CHttpServerTransactionBase* iTrans )
       
   433     {
       
   434     __FLOG_1 ( _T8 ( "CHttpServer::OnTransactionCompleted  [iTrans =0x%X]"), iTrans );
       
   435     for ( TInt i = 0; i < iTransactions.Count (); ++i)
       
   436         {
       
   437         if (iTransactions[i] == iTrans)
       
   438             {
       
   439             delete iTrans;
       
   440             iTransactions.Remove (i);
       
   441             break;              
       
   442             }
       
   443         }
       
   444     }
       
   445 
       
   446 void CHttpServer::MHFSessionRunL ( const THTTPSessionEvent& aEvent )
       
   447     {
       
   448     if ( aEvent.iStatus < 0 )
       
   449         {
       
   450         iSess.Close ();
       
   451         }
       
   452     }
       
   453     
       
   454 TInt CHttpServer::MHFSessionRunError ( TInt /*aError*/, const THTTPSessionEvent& /*aEvent*/ )
       
   455     {
       
   456     return KErrNone;
       
   457     }
       
   458 /*
       
   459 void CHttpServer::StartHttpServer()
       
   460     {
       
   461     SetupClientL();
       
   462     }
       
   463 */
       
   464 
       
   465 // ------------------------------------------------------
       
   466 
       
   467 CHttpServerTransactionBase::~CHttpServerTransactionBase ()
       
   468     {
       
   469     __FLOG_0 ( _T8 ( "CHttpServerTransactionBase::~CHttpServerTransactionBase   "));
       
   470     iFile.Close ();     
       
   471     }
       
   472 
       
   473 CHttpServerTransactionBase::CHttpServerTransactionBase ( MTransactionNotify& aNotify,/*CHttpExampleUtils& aUtils,*/ RFs& aFileServ )
       
   474 : iNotify (aNotify), 
       
   475 iFileServ ( aFileServ )
       
   476 //iUtils ( aUtils )
       
   477     {
       
   478     __FLOG_OPEN ( "http", "httpexampleserver.txt" );
       
   479     __FLOG_0 ( _T8 ( "CHttpServerTransactionBase::CHttpServerTransactionBase "));       
       
   480     }
       
   481 
       
   482 void CHttpServerTransactionBase::ConstructL ( RHTTPClientServerSession& aSession, const TUriC8& aUri, RStringF aMethod )
       
   483     {
       
   484     __FLOG_0 ( _T8 ( "CHttpServerTransactionBase::ConstructL "));
       
   485     iTransaction = aSession.OpenTransactionL(aUri, *this, aMethod);
       
   486     }
       
   487 
       
   488 
       
   489 void CHttpServerTransactionBase::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   490     {
       
   491     __FLOG_0 ( _T8 ( "CHttpServerTransactionBase::MHFRunL"));
       
   492     switch(aEvent.iStatus)
       
   493     {
       
   494     case THTTPEvent::ESucceeded:
       
   495     case THTTPEvent::EFailed:
       
   496         {
       
   497         aTransaction.Close();
       
   498         iNotify.OnTransactionCompleted(this);
       
   499         } break;
       
   500     default:
       
   501         {
       
   502         if (aEvent.iStatus < 0)
       
   503             {
       
   504             aTransaction.Close ();
       
   505             iNotify.OnTransactionCompleted ( this );            
       
   506             }
       
   507         } break;        
       
   508     }
       
   509     }
       
   510 
       
   511 
       
   512 
       
   513 
       
   514 TInt CHttpServerTransactionBase::MHFRunError(TInt aError, RHTTPTransaction /* aTransaction */, const THTTPEvent& /* aEvent */)
       
   515     {
       
   516     __FLOG_1 ( _T8 ( "CHttpServerTransactionBase::MHFRunError [aError=%d]"), aError);
       
   517     //iUtils.Test().Printf(_L("MHFRunError fired with error code %d\n"), aError);
       
   518     return KErrNone;    
       
   519     }
       
   520 
       
   521 void CHttpServerTransactionBase::SetHeaderL(RStringPool& aStringPool, RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
       
   522     {
       
   523     __FLOG_1 ( _T8 ( "CHttpServerTransactionBase::SetHeaderL [aHdrField=%d]"), aHdrField);
       
   524     RStringF valStr = aStringPool.OpenFStringL(aHdrValue);
       
   525     CleanupClosePushL(valStr);
       
   526     THTTPHdrVal val(valStr);
       
   527     aHeaders.SetFieldL(aStringPool.StringF(aHdrField,RHTTPClientServerSession::GetTable()), val);
       
   528     CleanupStack::PopAndDestroy(&valStr); 
       
   529     }
       
   530 
       
   531 
       
   532 // -------------------------------------------------------
       
   533 
       
   534 CHttpDataTransmitter* CHttpDataTransmitter::NewL ( MTransactionNotify& aNotify,/*CHttpExampleUtils& aUtils,*/ RFs& aFileServ, RHTTPClientServerSession& aSession, const TUriC8& aUri, RStringF aMethod )
       
   535     {   
       
   536     CHttpDataTransmitter* self = new (ELeave) CHttpDataTransmitter (aNotify, /*aUtils,*/ aFileServ);
       
   537     CleanupStack::PushL ( self );
       
   538     self->ConstructL ( aSession, aUri, aMethod );
       
   539     CleanupStack::Pop (); // self
       
   540     return self;    
       
   541     }
       
   542 
       
   543 //
       
   544 // methods from MHTTPTransactionCallback
       
   545 //
       
   546 void CHttpDataTransmitter::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   547     {
       
   548     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::MHFRunL"));
       
   549     switch (aEvent.iStatus)
       
   550         {
       
   551         case THTTPEvent::EGotRequestHeaders:
       
   552             {
       
   553             const TUriC8& uri = aTransaction.Request ().URI ();
       
   554             const TDesC8& uriDes = uri.UriDes ();
       
   555             
       
   556             if ( uriDes.CompareF ( KAbsDocumentRoot ) == 0 
       
   557                     || uriDes.CompareF ( KRelDocumentRoot ) == 0 )
       
   558                 {
       
   559                 RHTTPResponse response = aTransaction.Response ();
       
   560                 RStringPool stringPool = aTransaction.Session ().StringPool ();
       
   561                 RHTTPHeaders headers = response.GetHeaderCollection();
       
   562                 TBuf8<128> referer;
       
   563                 referer.Copy (_L("http://"));
       
   564                 RStringF hostStr = stringPool.StringF (HTTP::EHost, RHTTPClientServerSession::GetTable());
       
   565                 THTTPHdrVal hostVal;
       
   566                 aTransaction.Request().GetHeaderCollection().GetField(hostStr, 0, hostVal);
       
   567                 referer.Append(hostVal.StrF().DesC());
       
   568                 referer.Append(KHttpSlash);
       
   569 //              SetHeaderL (stringPool, headers, HTTP::EReferer, referer);
       
   570                 referer.Append(KHomePage);
       
   571                 // Root path. Redirect to the home page.
       
   572                 SetHeaderL (stringPool, headers, HTTP::ELocation, referer);
       
   573                 THTTPHdrVal hdr (0);
       
   574                 headers.SetFieldL (stringPool.StringF (HTTP::EContentLength, RHTTPClientServerSession::GetTable()), hdr);
       
   575                 response.SetStatusCode(HTTPStatus::EFound);
       
   576                 RStringF statusText = stringPool.OpenFStringL ( KFound );
       
   577                 
       
   578                 response.SetStatusText (statusText);
       
   579                 statusText.Close ();
       
   580                 aTransaction.SubmitL ();
       
   581                 //iTransSubmitted = ETrue;
       
   582                 }           
       
   583             } 
       
   584             break;
       
   585         case THTTPEvent::EGotRequestBodyData:
       
   586             {
       
   587             User::Invariant ();
       
   588             }
       
   589             break;
       
   590         case THTTPEvent::ERequestComplete:
       
   591             {
       
   592             if ( !iTransSubmitted )
       
   593             {
       
   594             const TDesC8& uriDes = aTransaction.Request ().URI ().UriDes();
       
   595 //          __FLOG_1 ( _T8 ("CHttpdProtocolHandler::MHFRunL: [uriDes = %S]"), uriDes );
       
   596                 
       
   597             if ( OpenFile (uriDes) == KErrNone )
       
   598                 {
       
   599                 __FLOG_0 ( _T8 ( "CHttpDataTransmitter::MHFRunL: Sending Ok"));             
       
   600                 RHTTPResponse response = aTransaction.Response();
       
   601                 response.SetBody(*this);
       
   602                 RHTTPHeaders headers = aTransaction.Response().GetHeaderCollection();
       
   603                 RStringPool stringPool = aTransaction.Session().StringPool();
       
   604                 SetHeaderL(stringPool, headers, HTTP::EContentType, iContentType);
       
   605                 response.SetStatusCode (HTTPStatus::EOk);   
       
   606                 RStringF statusText = stringPool.OpenFStringL ( KOk );
       
   607                 
       
   608                 response.SetStatusText (statusText);
       
   609                 statusText.Close ();    
       
   610                 THTTPHdrVal hdr (OverallDataSize());
       
   611                 headers.SetFieldL (stringPool.StringF (HTTP::EContentLength, RHTTPClientServerSession::GetTable()), hdr);                                           
       
   612                 }
       
   613             else
       
   614                 {
       
   615                 __FLOG_0 ( _T8 ( "CHttpDataTransmitter::MHFRunL: Sending Not Found"));
       
   616                 
       
   617                 RHTTPResponse response = aTransaction.Response();
       
   618                 response.SetStatusCode (HTTPStatus::ENotFound);
       
   619                 RStringF statusText = aTransaction.Session().StringPool().OpenFStringL ( KNotFound );
       
   620                 
       
   621                 response.SetStatusText (statusText);
       
   622                 statusText.Close ();
       
   623                 THTTPHdrVal hdr (0);
       
   624                 response.GetHeaderCollection().SetFieldL (aTransaction.Session().StringPool().StringF (HTTP::EContentLength, RHTTPClientServerSession::GetTable()), hdr);                                                                                   
       
   625                 }
       
   626             aTransaction.SubmitL ();
       
   627             iTransSubmitted = ETrue;
       
   628                 
       
   629             }
       
   630             } 
       
   631             break;
       
   632         default:
       
   633             CHttpServerTransactionBase::MHFRunL(aTransaction, aEvent);
       
   634         }       
       
   635     }
       
   636 
       
   637 TInt CHttpDataTransmitter::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   638     {
       
   639     __FLOG_1 ( _T8 ( "CHttpDataTransmitter::MHFRunError [aError=%d]"), aError);
       
   640     return CHttpServerTransactionBase::MHFRunError(aError, aTransaction, aEvent);   
       
   641     }
       
   642 
       
   643 TInt CHttpDataTransmitter::OpenFile( const TDesC8& aHttpPath )
       
   644     {
       
   645     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::OpenFile"));
       
   646 
       
   647     TBuf8<256> fileName;
       
   648     fileName.Copy(KRoot);           
       
   649     if (aHttpPath.Locate(KHttpSlash) == 0)  
       
   650         {
       
   651         // Skip that
       
   652         TPtrC8 ptr (aHttpPath.Mid(1));
       
   653         fileName.Append(ptr);
       
   654         //replace forward slash with backward
       
   655         for ( TInt index = 0; index < fileName.Length (); index++ )
       
   656             {
       
   657             if ( fileName[index] == TChar ( KHttpSlash ) )
       
   658                 {
       
   659                 fileName[index] = TChar ( '\\' );
       
   660                 }
       
   661             }   
       
   662         }           
       
   663         
       
   664     HBufC* fileName16 = EscapeUtils::ConvertToUnicodeFromUtf8L (fileName);  
       
   665     TInt err = iFile.Open(iFileServ, *fileName16, EFileShareReadersOnly);
       
   666     
       
   667     TPtr ptr2 ( fileName16->Des() );
       
   668     //iUtils.Test().Printf(_L("File Name: %S"), &ptr2);
       
   669     __FLOG_1 ( _T8 ( "CHttpDataTransmitter::OpenFile [Error=%d]"), err);
       
   670     
       
   671     // For Hardware
       
   672     if ( err != KErrNone )
       
   673         {
       
   674         ptr2[0] = TChar ( 'Z' );
       
   675         err = iFile.Open ( iFileServ, ptr2, EFileShareReadersOnly );
       
   676         }
       
   677     __FLOG_1 ( _T8 ( "CHttpDataTransmitter::OpenFile [Error=%d]"), err);
       
   678     
       
   679     if ( err != KErrNone )  
       
   680         {       
       
   681         delete fileName16;
       
   682         return err;
       
   683         }
       
   684     
       
   685     TParse parser;
       
   686     parser.Set(ptr2, NULL, NULL);
       
   687     
       
   688     if ( ptr2.Find(KHtml) != KErrNotFound )
       
   689         {
       
   690         iContentType.Copy(KTextContent);            
       
   691         iContentType.Copy(KHtml);
       
   692         }
       
   693     else
       
   694         {
       
   695         iContentType.Copy(KImageContent);   
       
   696         iContentType.Copy(KGif);
       
   697         }
       
   698     delete fileName16;
       
   699     iFile.Size(iDataToSend);
       
   700     return KErrNone;
       
   701     }
       
   702 
       
   703 CHttpDataTransmitter::CHttpDataTransmitter (MTransactionNotify& aNotify,/*CHttpExampleUtils& aUtils,*/ RFs& aFileServ)
       
   704 : CHttpServerTransactionBase (aNotify, /*aUtils,*/ aFileServ)
       
   705     {
       
   706     __FLOG_OPEN ( "http", "httpexampleserver.txt" );
       
   707     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::CHttpDataTransmitter "));       
       
   708     }
       
   709 
       
   710 TBool CHttpDataTransmitter::GetNextDataPart(TPtrC8& aDataPart)
       
   711     {
       
   712     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::GetNextDataPart"));
       
   713     iFile.Read (iData); 
       
   714     iDataToSend -= iData.Length ();
       
   715     aDataPart.Set (iData);
       
   716     return iDataToSend == 0;
       
   717     }
       
   718 
       
   719 void CHttpDataTransmitter::ReleaseData()
       
   720     {
       
   721     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::ReleaseData"));
       
   722     iData.SetLength (0);
       
   723     if (iDataToSend > 0)
       
   724         iTransaction.NotifyNewRequestBodyPartL ();
       
   725     else
       
   726         iFile.Close ();     
       
   727     }
       
   728 
       
   729 TInt CHttpDataTransmitter::OverallDataSize()
       
   730     {
       
   731     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::OverallDataSize"));
       
   732     TInt size;
       
   733     iFile.Size (size);
       
   734     return size;    
       
   735     }
       
   736 
       
   737 TInt CHttpDataTransmitter::Reset()
       
   738     {
       
   739     __FLOG_0 ( _T8 ( "CHttpDataTransmitter::Reset"));
       
   740     return KErrNone;    
       
   741     }
       
   742 
       
   743 
       
   744 
       
   745 /*
       
   746 // ------------------------------------------------------------
       
   747 
       
   748 LOCAL_D void TestL()
       
   749 // Create a test object, invoke the tests using it and remove
       
   750     {
       
   751         
       
   752     //TRAPD(err,CHttpExampleUtils::InitCommsL()); 
       
   753     // create an active scheduler to use
       
   754     CActiveScheduler* scheduler = new(ELeave) CActiveScheduler();
       
   755     CleanupStack::PushL(scheduler);
       
   756     CActiveScheduler::Install(scheduler);
       
   757   
       
   758     // Create and start the client
       
   759     CHttpServer* httpCli = CHttpServer::NewL();         
       
   760     
       
   761     CZeroconfClient* browser = CZeroconfClient::NewL(*httpCli);
       
   762     browser->ConnectL();
       
   763     
       
   764     CActiveScheduler::Start();
       
   765     //
       
   766     CleanupStack::PopAndDestroy(1); //  scheduler
       
   767     delete httpCli;
       
   768     delete browser;
       
   769     }
       
   770 
       
   771 
       
   772 GLDEF_C TInt E32Main()
       
   773 // Main program - run the tests within a TRAP harness, reporting any errors that
       
   774 // occur via the panic mechanism. Test for memory leaks using heap marking.
       
   775     {
       
   776 
       
   777     __UHEAP_MARK;
       
   778     CTrapCleanup* tc=CTrapCleanup::New();
       
   779     
       
   780     TRAPD(err,TestL());
       
   781     
       
   782     if (err!=KErrNone)
       
   783         User::Panic(_L("Test failed with error code: %i"), err);
       
   784     delete tc;
       
   785     __UHEAP_MARKEND;
       
   786     return KErrNone;
       
   787     }
       
   788 */
       
   789