upnp/upnpstack/dlnawebserver/src/upnphttpserver.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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:  Declares HttpServer class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <upnpsettings.h>
       
    22 
       
    23 #include "upnphttpserver.h"
       
    24 #include "upnphttpsession.h"
       
    25 #include "upnphttpfileaccess.h"
       
    26 #include "upnpstring.h"
       
    27 #include "upnpcommonupnplits.h"
       
    28 #define KLogFile _L("DLNAWebServer.txt")
       
    29 #include "upnpcustomlog.h"
       
    30 #include "upnpcons.h"
       
    31 #include "upnphttpmessagefactory.h"
       
    32 #include "upnphttpservertransactioncreator.h"
       
    33 #ifdef RD_UPNP_REMOTE_ACCESS
       
    34 #include "upnpipfiltermanager.h"
       
    35 #endif
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CUpnpHttpServer::CUpnpHttpServer
       
    41 // C++ default constructor
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CUpnpHttpServer::CUpnpHttpServer( MUpnpHttpServerObserver* aObserver,
       
    45                                   MUpnpHttpServerTransactionCreator* aCreator,
       
    46                                   RSocketServ* aSocketServ,
       
    47                                   TInt aActiveIap )
       
    48     : CUpnpTcpServer( aSocketServ, KDefaultHttpPort, aActiveIap )
       
    49     {
       
    50     iObserver = aObserver;
       
    51     iTransactionCreator = aCreator;
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CUpnpHttpServer::~CUpnpHttpServer
       
    56 // C++ default destructor
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CUpnpHttpServer::~CUpnpHttpServer()
       
    60     {
       
    61     delete iServerDescription;
       
    62     iFs.Close();
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CUpnpHttpServer::NewL
       
    67 // Two-phased constructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CUpnpHttpServer* CUpnpHttpServer::NewL( MUpnpHttpServerObserver* aObserver,
       
    71                                                  RSocketServ* aSocketServ,
       
    72                                                  TInt aActiveIap )
       
    73     {
       
    74     LOG_FUNC_NAME;
       
    75        
       
    76     CUpnpHttpServer* self = new (ELeave) CUpnpHttpServer( aObserver, NULL,           
       
    77             aSocketServ, aActiveIap );
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop( self );
       
    81 
       
    82     return self;
       
    83     }
       
    84 
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CUpnpHttpServer::ConstructL
       
    88 // Two-phased constructor
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CUpnpHttpServer::ConstructL()
       
    92     {
       
    93     BaseConstructL();
       
    94     User::LeaveIfError(iFs.Connect());
       
    95     iServerDescription =  CUpnpSettings::GetServerHeaderL();
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CUpnpHttpServer::StartL
       
   100 // Start tcp server
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CUpnpHttpServer::StartL( const TInt aPort )
       
   104     {
       
   105     LOG_FUNC_NAME;
       
   106     OpenSocketL();
       
   107     if ( aPort )
       
   108         {
       
   109         BindL( aPort );
       
   110         }
       
   111         else
       
   112         {
       
   113         BindRandomPortL();
       
   114         }
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CUpnpHttpServer::Stop
       
   119 // Stop tcp server
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CUpnpHttpServer::Stop()
       
   123     {
       
   124     LOG_FUNC_NAME;
       
   125     TRAP_IGNORE( StopTcpServerL() );
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CUpnpHttpServer::ToReceiveStackD
       
   130 //
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CUpnpHttpServer::ToReceiveStackD( CUpnpHttpMessage* aMsg )
       
   134     {
       
   135     LOG_FUNC_NAME;
       
   136     //to indicate that the upper layer can give a response for it is acting as a host in this session
       
   137     if ( !aMsg )
       
   138         {
       
   139         return;
       
   140         }
       
   141 	aMsg->SetClientRequest(ETrue);
       
   142 	//for redirection codes
       
   143 
       
   144     #ifdef _DEBUG
       
   145         TInt err;
       
   146         TRAP( err, iObserver->HttpEventLD( aMsg ) );
       
   147         LOGS1H( iHandle, "CUpnpHttpServer::ToReceiveStack() Error %i", err );
       
   148 
       
   149     #else
       
   150         TRAP_IGNORE(  iObserver->HttpEventLD( aMsg ) );
       
   151     #endif
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CUpnpHttpServer::SendMessageL
       
   156 // Send HTTP message
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 TInt CUpnpHttpServer::SendMessageL( CUpnpHttpMessage* aMessage )
       
   160     {
       
   161     LOG_FUNC_NAME;
       
   162 
       
   163     if (!aMessage)
       
   164 		{
       
   165 		LOGS("CUpnpHttpServer::SendMessageL - Tried to send Null");
       
   166 		return KErrNotFound;
       
   167 		}
       
   168 
       
   169     TInt trapError( KErrNone );
       
   170     TInt sessId( KErrNone );
       
   171 
       
   172     TRAP(trapError, sessId = TrapSendMessageL( aMessage ) );
       
   173 
       
   174     if( trapError < KErrNone )
       
   175         {
       
   176         LOGS1( "HTTP *** Sending of message failed. Error: %i", trapError );
       
   177         // HttpSession errors that cannot be forwarded to upper layers
       
   178         if( -trapError >= EHttpBadRequest && -trapError <= EHttpExpectationFailed )
       
   179             {
       
   180             CUpnpHttpMessage* notify = NULL;
       
   181             notify = RUpnpHttpMessageFactory::HttpResponseErrorL( aMessage, -trapError );
       
   182             CUpnpHttpSession* sess = GetSession( aMessage->SessionId() );
       
   183             sess->DeleteThisSessionL( sess );
       
   184 
       
   185             ToReceiveStackD( notify ); //Upper layer takes care about cleanup
       
   186             }
       
   187         else
       
   188             {
       
   189             User::Leave(trapError);
       
   190             }
       
   191         }
       
   192     // nobody use the value
       
   193     return sessId;
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CUpnpHttpServer::TrapSendMessageL
       
   198 // Send HTTP message
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 TInt CUpnpHttpServer::TrapSendMessageL( CUpnpHttpMessage* aMessage )
       
   202     {
       
   203     CUpnpHttpSession* sess = GetSession( aMessage->SessionId() );
       
   204     if( sess )
       
   205         {
       
   206         sess->SendL( aMessage );
       
   207         return sess->Id();
       
   208         }
       
   209     else
       
   210         {
       
   211         // If running out of memory, there is nothing else to do but leave
       
   212         if ( KErrNoMemory == aMessage->SessionId() )
       
   213             {
       
   214             LOGS("CUpnpHttpServer::TrapSendMessageL - OUT OF MEMORY");
       
   215             User::Leave( KErrNoMemory );
       
   216             }        
       
   217         }
       
   218     return KErrNotFound;
       
   219     }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CUpnpHttpServer::GetSession
       
   223 //
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 CUpnpHttpSession* CUpnpHttpServer::GetSession( TInt aSessionId )
       
   227     {
       
   228     CUpnpHttpSession* sess = NULL;
       
   229 
       
   230     for ( TInt i=0; i < iSessionList.Count(); i++ )
       
   231         {
       
   232         if ( ( (CUpnpHttpSession*) iSessionList[i] )->Id() == aSessionId )
       
   233             {
       
   234             sess = (CUpnpHttpSession*) iSessionList[i];
       
   235             return sess;
       
   236             }
       
   237         }
       
   238 
       
   239     return sess;
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CUpnpHttpServer::ServerDescription
       
   244 // Returns server's description
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 TDesC8& CUpnpHttpServer::ServerDescription()
       
   248     {
       
   249     return *iServerDescription;
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CUpnpHttpServer::ConnectionAcceptedL
       
   254 //
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 CUpnpTcpSession* CUpnpHttpServer::ConnectionAcceptedL( RSocket aSocket )
       
   258     {
       
   259     LOG_FUNC_NAME;
       
   260 
       
   261     #ifdef _DEBUG
       
   262     TInetAddr tempAddr;
       
   263     aSocket.RemoteName( tempAddr );
       
   264     tempAddr.ConvertToV4();
       
   265 
       
   266     const TInt KMaxAdressLength = 20;
       
   267     TBuf<KMaxAdressLength> addrBuf;
       
   268     tempAddr.Output( addrBuf );
       
   269 
       
   270     HBufC8* addrBuf8 = UpnpString::FromUnicodeL( addrBuf );
       
   271     CleanupStack::PushL( addrBuf8 );
       
   272 
       
   273     LOGS( "CUpnpHttpServer::ConnectionAcceptedL - Remote socket connected" );
       
   274     LOGT( addrBuf8->Des() );
       
   275 
       
   276     LOGS1("CUpnpHttpServer::ConnectionAcceptedL - Creating a new Http session. Session count: %i", iSessionList.Count());
       
   277 
       
   278     CleanupStack::PopAndDestroy(addrBuf8);
       
   279     #endif //_DEBUG
       
   280     CUpnpHttpSession* sess = CUpnpHttpSession::NewL( aSocket, this,
       
   281         CUpnpHttpMessage::NewSessionIdL(), EPriorityNormal );
       
   282 
       
   283     return sess;
       
   284     }
       
   285 
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CUpnpHttpServer::SetCdDataFinder
       
   289 //
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CUpnpHttpServer::SetTransactionCreator( 
       
   293                                  MUpnpHttpServerTransactionCreator* aTransactionCreator )
       
   294     {
       
   295     iTransactionCreator = aTransactionCreator;
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CUpnpHttpServer::HttpFilter
       
   300 //
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 MUpnpHttpServerTransactionCreator* CUpnpHttpServer::TransactionCreator()
       
   304     {
       
   305     //ASSERT( iHttpFilter ) set in constructor defaultHttpFilter to ensure not null
       
   306     return iTransactionCreator;
       
   307     }
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CUpnpHttpServer::FileSession
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 RFs& CUpnpHttpServer::FileSession()
       
   314     {
       
   315     return iFs;
       
   316     }
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CUpnpHttpServer::ConnectionL
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 RConnection& CUpnpHttpServer::ConnectionL()
       
   323     {
       
   324     return iConnectionManagerProxy->ConnectionL();
       
   325     }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CUpnpHttpServer::StartIPFilteringL
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 EXPORT_C void CUpnpHttpServer::StartIPFilteringL()
       
   332     {
       
   333     #ifdef RD_UPNP_REMOTE_ACCESS
       
   334     if( !iIPFilteringManager )
       
   335     {
       
   336     iIPFilteringManager = CUpnpIPFilterManager::NewL();
       
   337     }
       
   338     #endif
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CUpnpHttpServer::StopIPFiltering
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 EXPORT_C void CUpnpHttpServer::StopIPFiltering()
       
   346     {
       
   347     #ifdef RD_UPNP_REMOTE_ACCESS
       
   348     delete iIPFilteringManager;
       
   349     iIPFilteringManager = NULL;
       
   350     #endif
       
   351     }
       
   352 
       
   353 //  End of File