upnp/upnpstack/controlpointbase/src/upnpcpbcurrenthttpclient.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:  CUpnpCpbCurrentHttpClient
       
    15  *
       
    16 */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <e32cons.h>
       
    20 #include <e32std.h>
       
    21 #include <upnpsettings.h>
       
    22 #include <charconv.h>
       
    23 #include <utf.h>
       
    24 
       
    25 #include "upnpcpstackrequestor.h"
       
    26 #include "upnphttpmessage.h"
       
    27 #include "upnpsoapmessage.h"
       
    28 #include "upnpicon.h"
       
    29 #include "upnpdevice.h"
       
    30 #include "upnpstring.h"
       
    31 #include "upnphttpmessagefactory.h"
       
    32 #include "upnpsoapmessagefactory.h"
       
    33 #include "upnpgenamessagefactory.h"
       
    34 #include "upnpserviceinfo.h"
       
    35 #include "upnpcpbhttpmessagecontroller.h"
       
    36 #include "upnpcpbcurrenthttpclient.h"
       
    37 #include "upnpcontrolpoint.h"
       
    38 #include "upnpcommonupnplits.h"
       
    39 #include "upnpcpbdevicedescription.h"
       
    40 #include "upnpdevicedescriptionhttptransaction.h"
       
    41 #include "upnpservicedescriptionhttptransaction.h"
       
    42 #include "upnpsoapactionhttptransaction.h"
       
    43 #include "upnpservicesubscriptionhttptransaction.h"
       
    44 #include "upnpserviceunsubscriptionhttptransaction.h"
       
    45 #include "upnpcphttprequestorimpl.h"
       
    46 #include "upnpcpstackrequestorimpl.h"
       
    47 #include "upnpfilesender.h"
       
    48 
       
    49 #define KLogFile _L("UPnPControlPoint.txt")
       
    50 #include <upnpcustomlog.h>
       
    51 
       
    52 using namespace UpnpHTTP;
       
    53 
       
    54 static const TInt KMessageTimeout( 10000000 );
       
    55 static const TInt KActionTimeout( 35000000 );
       
    56 
       
    57 // ============================ MEMBER FUNCTIONS ===============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CUpnpCpbCurrentHttpClient::CUpnpCurrentHttpClient
       
    61 // C++ default constructor
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CUpnpCpbCurrentHttpClient::CUpnpCpbCurrentHttpClient( CUpnpControlPoint& aControlPoint,
       
    65         MUpnpCpHttpRequestor* aHttpRequestor, MUpnpCpStackRequestor* aStackRequestor )
       
    66     : iControlPoint( aControlPoint ),
       
    67     iHttpRequestor( aHttpRequestor ), iStackRequestor( aStackRequestor )
       
    68     {
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CUpnpCpbCurrentHttpClient::~CUpnpCpbCurrentHttpClient
       
    73 // Destructor
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CUpnpCpbCurrentHttpClient::~CUpnpCpbCurrentHttpClient()
       
    77     {
       
    78     if ( iStackRequestor )
       
    79         {
       
    80         iStackRequestor->RemoveCustomer( *this );
       
    81         }
       
    82     iPendingHttpMessages.ResetAndDestroy();
       
    83     iPendingTransactions.ResetAndDestroy();
       
    84     delete iUserAgent;
       
    85     delete iHttpRequestorDefaultImpl;
       
    86     delete iStackRequestorDefaultImpl;
       
    87     delete iFileSender;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CUpnpCpbCurrentHttpClient::NewL
       
    92 //
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CUpnpCpbCurrentHttpClient* CUpnpCpbCurrentHttpClient::NewL(
       
    96     CUpnpControlPoint& aControlPoint,
       
    97     MUpnpCpHttpRequestor* aHttpRequestor, MUpnpCpStackRequestor* aStackRequestor)
       
    98     {
       
    99     LOG_FUNC_NAME;
       
   100     CUpnpCpbCurrentHttpClient* self =
       
   101         new (ELeave) CUpnpCpbCurrentHttpClient( aControlPoint, aHttpRequestor, aStackRequestor );
       
   102     CleanupStack::PushL( self );
       
   103     self->ConstructL();
       
   104     CleanupStack::Pop( self );
       
   105     return self;
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CUpnpCpbCurrentHttpClient::ConstructL
       
   110 //
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CUpnpCpbCurrentHttpClient::ConstructL()
       
   114     {
       
   115     iFileSender = CUpnpFileSender::NewL( iControlPoint );
       
   116     
       
   117     if ( !iHttpRequestor )
       
   118         {
       
   119         iHttpRequestorDefaultImpl = CUpnpCpHttpRequestorImpl::NewL();
       
   120         iHttpRequestor = iHttpRequestorDefaultImpl;
       
   121         }
       
   122     iHttpRequestor->InitialiseL( *this, CUpnpSettings::GetIapL() );
       
   123 
       
   124     if ( !iStackRequestor )
       
   125         {
       
   126         iStackRequestorDefaultImpl = CUpnpCpStackRequestorImpl::NewL( iControlPoint );
       
   127         iStackRequestor = iStackRequestorDefaultImpl;
       
   128         }
       
   129     iStackRequestor->AddCustomerL( *this );
       
   130     ASSERT( iHttpRequestor && iStackRequestor ); //ensure correct initialisation
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CUpnpCpbCurrentHttpClient::SendActionL
       
   135 //
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CUpnpCpbCurrentHttpClient::SendActionL( CUpnpAction* aAction )
       
   139     {
       
   140     if( !aAction )
       
   141         {
       
   142         User::Leave( KErrArgument );
       
   143         }
       
   144 
       
   145     CUpnpHttpMessage* msg = RUpnpSoapMessageFactory::SoapRequestL( aAction );
       
   146     CleanupStack::PushL( msg );
       
   147 
       
   148     // 7.2.32.1 UPnP action requests (sent by a control point) must include a
       
   149     // DLNA-CP-version in a USER-AGENT HTTP header value.
       
   150     if ( !iUserAgent )
       
   151         {
       
   152         HBufC8* userAgent = CUpnpSettings::GetUserAgentL();
       
   153         CleanupStack::PushL( userAgent );
       
   154 
       
   155         iUserAgent = HBufC8::NewL( userAgent->Length() );
       
   156         iUserAgent->Des().Append( *userAgent );
       
   157 
       
   158         CleanupStack::PopAndDestroy( userAgent );
       
   159         }
       
   160 
       
   161     msg->AddPairL( UpnpHTTP::KHdrUserAgent, *iUserAgent );
       
   162 
       
   163     msg->SetTcpTimeout( KActionTimeout );
       
   164     
       
   165     CleanupStack::Pop( msg );//ownership passed to Transaction and is safe
       
   166     CUpnpHttpTransaction* soapTransaction = 
       
   167          CUpnpSoapActionHttpTransaction::NewLC( msg, aAction->SessionId(), iControlPoint );
       
   168                     
       
   169     iPendingTransactions.AppendL( soapTransaction );
       
   170     CleanupStack::Pop( soapTransaction );
       
   171     iHttpRequestor->SendL( *soapTransaction );
       
   172     
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CUpnpCpbCurrentHttpClient::SendSubscribeL
       
   177 //
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CUpnpCpbCurrentHttpClient::SendSubscribeL( CUpnpServiceInfo* aServiceInfo )
       
   181     {
       
   182     const TDesC8& path = Path();
       
   183 
       
   184     TInetAddr addr = iStackRequestor->HttpServerAddress();
       
   185     HBufC8* longPath = aServiceInfo->ConstructSubscribtionPathLC( path , addr ); 
       
   186     CUpnpGenaMessage* gena = aServiceInfo->CreateSubscriptionMessageL(
       
   187                                    *longPath,
       
   188                                    ESubscription );
       
   189     
       
   190     gena->SetTcpTimeout( KMessageTimeout );
       
   191     CUpnpServiceSubscriptionHttpTransaction* transaction =
       
   192         CUpnpServiceSubscriptionHttpTransaction::NewLC( gena, iControlPoint );
       
   193     iPendingTransactions.AppendL( transaction );
       
   194     CleanupStack::Pop( transaction );
       
   195     iHttpRequestor->SendL( *transaction );
       
   196     CleanupStack::PopAndDestroy( longPath );
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CUpnpCpbCurrentHttpClient::SendUnsubscribeL
       
   201 //
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void CUpnpCpbCurrentHttpClient::SendUnsubscribeL( CUpnpServiceInfo* aServiceInfo )
       
   205     {
       
   206     if ( aServiceInfo->Sid() != KNullDesC8 ) 
       
   207         {
       
   208         CUpnpGenaMessage* gena = aServiceInfo->CreateSubscriptionMessageL( EUnSubscription );
       
   209         gena->SetTcpTimeout( KMessageTimeout );
       
   210         CUpnpServiceUnsubscriptionHttpTransaction* transaction = 
       
   211             CUpnpServiceUnsubscriptionHttpTransaction::NewLC( gena, iControlPoint) ;
       
   212         iPendingTransactions.AppendL( transaction );
       
   213         CleanupStack::Pop( transaction );
       
   214         iHttpRequestor->SendL( *transaction );
       
   215         }
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CUpnpCpbCurrentHttpClient::SendResubscribeL
       
   220 //
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 void CUpnpCpbCurrentHttpClient::SendResubscribeL( CUpnpServiceInfo* aServiceInfo )
       
   224     {
       
   225     CUpnpGenaMessage* gena = aServiceInfo->CreateSubscriptionMessageL( EResubscription );
       
   226     gena->SetTcpTimeout( KMessageTimeout );
       
   227     CUpnpServiceSubscriptionHttpTransaction* transaction =
       
   228          CUpnpServiceSubscriptionHttpTransaction::NewLC( gena, iControlPoint );
       
   229     iPendingTransactions.AppendL( transaction );
       
   230     CleanupStack::Pop( transaction );
       
   231     iHttpRequestor->SendL( *transaction );
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CUpnpCpbCurrentHttpClient::GetServiceDescriptionL
       
   236 //
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 TInt CUpnpCpbCurrentHttpClient::GetServiceDescriptionL( CUpnpDevice* aDevice,
       
   240     const TDesC8& aUrl )
       
   241     {
       
   242     CUpnpHttpMessage* result( NULL );
       
   243     TPtrC8 urlBase( aDevice->UrlBase() );
       
   244 
       
   245     if ( aUrl.Find( KHttp ) == 0 )
       
   246         {
       
   247         result = RUpnpHttpMessageFactory::HttpGetL( aUrl );
       
   248         }
       
   249     else
       
   250         {
       
   251         TInetAddr addr = aDevice->Address();
       
   252         if ( urlBase.Length() > 0 )
       
   253             {
       
   254             HBufC8* url = aDevice->ConcatWithUrlBaseL( aUrl );
       
   255             CleanupStack::PushL( url );
       
   256 
       
   257             if ( url->Find( KHttp ) )
       
   258                 {
       
   259                 result = RUpnpHttpMessageFactory::HttpGetL( addr, *url,
       
   260                     KNullDesC8 );
       
   261                 }
       
   262             else
       
   263                 {
       
   264                 result = RUpnpHttpMessageFactory::HttpGetL( *url );
       
   265                 }
       
   266 
       
   267             CleanupStack::PopAndDestroy( url );
       
   268             }
       
   269         else
       
   270             {
       
   271             result = RUpnpHttpMessageFactory::HttpGetL( addr,
       
   272                 aDevice->DescriptionUrlPath(), aUrl );
       
   273             }
       
   274         }
       
   275     result->SetTcpTimeout( KMessageTimeout );
       
   276 
       
   277     CUpnpServiceDescriptionHttpTransaction* transaction =
       
   278         CUpnpServiceDescriptionHttpTransaction::NewLC( result, iControlPoint );
       
   279 
       
   280     iPendingTransactions.AppendL( transaction );
       
   281     CleanupStack::Pop( transaction );
       
   282     iHttpRequestor->SendL( *transaction );
       
   283     return result->SessionId();
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CUpnpCpbCurrentHttpClient::GetDeviceDescriptionL
       
   288 //
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 TInt CUpnpCpbCurrentHttpClient::GetDeviceDescriptionL( CUpnpDevice* aDevice )
       
   292     {
       
   293     TInetAddr addr = aDevice->DescriptionUrlAddressL();
       
   294     addr.SetPort( aDevice->DescriptionUrlPort() );
       
   295 
       
   296     CUpnpHttpMessage* message = RUpnpHttpMessageFactory::HttpGetL( addr,
       
   297         aDevice->DescriptionUrlPath() );
       
   298     message->SetTcpTimeout( KMessageTimeout );
       
   299 
       
   300     CUpnpDeviceDescriptionHttpTransaction* transaction =
       
   301         CUpnpDeviceDescriptionHttpTransaction::NewLC( message, iControlPoint );
       
   302 
       
   303     iPendingTransactions.AppendL( transaction );
       
   304     CleanupStack::Pop( transaction );
       
   305     iHttpRequestor->SendL( *transaction );
       
   306 
       
   307     return message->SessionId();
       
   308     }
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // CUpnpCpbCurrentHttpClient::MessageReceivedLD
       
   312 //
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CUpnpCpbCurrentHttpClient::MessageReceivedLD( CUpnpHttpMessage* aHttpMessage )
       
   316     {
       
   317     if ( !aHttpMessage )
       
   318         {
       
   319         User::Leave( KErrArgument );
       
   320         }
       
   321     // match arrived response to pending request
       
   322 
       
   323     TIdentityRelation<CUpnpHttpMessage> matcher( CUpnpHttpMessage::SessionIdMatch );
       
   324     TInt idx = iPendingHttpMessages.Find ( aHttpMessage, matcher );
       
   325 
       
   326     CleanupStack::PushL( aHttpMessage );
       
   327 
       
   328     if ( aHttpMessage->IsGena() )
       
   329         {
       
   330         CleanupStack::Pop( aHttpMessage );
       
   331         iControlPoint.NotifyReceivedL( *aHttpMessage );
       
   332         }
       
   333     else
       
   334         {
       
   335         iControlPoint.MessageReceived( aHttpMessage );
       
   336         iPendingHttpMessages.Compress();
       
   337         CleanupStack::PopAndDestroy( aHttpMessage );
       
   338         }
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CUpnpCpbCurrentHttpClient::Path
       
   343 //
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 const TDesC8& CUpnpCpbCurrentHttpClient::Path()
       
   347     {
       
   348     return KPath();
       
   349     }
       
   350 
       
   351 // ----------------------------------------------------------------------------
       
   352 // CUpnpCpbCurrentHttpClient::StopIgnoringL
       
   353 //
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 void CUpnpCpbCurrentHttpClient::StopIgnoringL( CUpnpDevice* aDevice )
       
   357     {
       
   358     iStackRequestor->StopIgnoringL( aDevice->Uuid() );
       
   359     StopIgnoringEmbeddedL( aDevice );
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CUpnpCpbCurrentHttpClient::StopIgnoringEmbeddedL
       
   364 //
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 void CUpnpCpbCurrentHttpClient::StopIgnoringEmbeddedL( CUpnpDevice* aRoot )
       
   368     {
       
   369     RPointerArray<CUpnpDevice> devicesAll;
       
   370     aRoot->GetAllDevices( devicesAll );
       
   371     CleanupClosePushL( devicesAll );
       
   372     for ( TInt k = 0; k < devicesAll.Count(); k++ )
       
   373         {
       
   374         iStackRequestor->StopIgnoringL( devicesAll[k]->Uuid() );
       
   375         }
       
   376     CleanupStack::PopAndDestroy( &devicesAll );
       
   377     }
       
   378 
       
   379 // -----------------------------------------------------------------------------
       
   380 // CUpnpCpbCurrentHttpClient::StopIgnoringUuidL
       
   381 // Send stop ignoring message to MH
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 void CUpnpCpbCurrentHttpClient::StopIgnoringUuidL( const TDesC8& aUuids )
       
   385     {
       
   386     iStackRequestor->StopIgnoringL( aUuids );
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CUpnpCpbCurrentHttpClient::StartIPFilteringL
       
   391 //
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 void CUpnpCpbCurrentHttpClient::StartIPFilteringL()
       
   395     {
       
   396     iStackRequestor->StartIPFilteringL();
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CUpnpCpbCurrentHttpClient::StopIPFiltering
       
   401 //
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void CUpnpCpbCurrentHttpClient::StopIPFiltering()
       
   405     {
       
   406     iStackRequestor->StopIPFiltering();
       
   407     }
       
   408 
       
   409 // -----------------------------------------------------------------------------
       
   410 // CUpnpCpbCurrentHttpClient::SendL
       
   411 // Send response HTTP message
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 void CUpnpCpbCurrentHttpClient::SendL( CUpnpHttpMessage* aHttpMessage )
       
   415     {
       
   416     const TInt KUnused(0);
       
   417     iStackRequestor->SendMessageL( aHttpMessage, *this, KUnused );
       
   418     }
       
   419 
       
   420 // -----------------------------------------------------------------------------
       
   421 // CUpnpCpbCurrentHttpClient::SendFileByPostL
       
   422 // Send HTTP message
       
   423 // -----------------------------------------------------------------------------
       
   424 //
       
   425 void CUpnpCpbCurrentHttpClient::SendFileByPostL( CUpnpHttpMessage* aHttpMessage )
       
   426     {
       
   427     if ( !aHttpMessage )
       
   428         {
       
   429         User::Leave( KErrArgument );
       
   430         }
       
   431     iFileSender->SendL( *aHttpMessage );
       
   432     }
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // CUpnpCpbCurrentHttpClient::SearchL
       
   436 // Sends a SSDP Search with 8-bit search string.
       
   437 // -----------------------------------------------------------------------------
       
   438 //
       
   439 void CUpnpCpbCurrentHttpClient::SearchL( const TDesC8& aSearchString )
       
   440     {
       
   441     iStackRequestor->SearchL( aSearchString );
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CUpnpCpbCurrentHttpClient::ClientResponseRecivedLD
       
   446 // Callback indicating that response was received within transaction.
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CUpnpCpbCurrentHttpClient::ClientResponseRecivedLD(
       
   450     CUpnpHttpTransaction& aCompletedTrasaction )
       
   451     {
       
   452     TInt idx( iPendingTransactions.Find( &aCompletedTrasaction ) );
       
   453     //it should never happen that we received transaction that wasn't sent by us
       
   454     ASSERT( idx >= 0 );
       
   455     aCompletedTrasaction.ProcessResponseL();
       
   456     iPendingTransactions.Remove( idx );
       
   457     delete ( &aCompletedTrasaction );
       
   458     }
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CUpnpCpbCurrentHttpClient::WlanLostOccurs
       
   462 //
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 void CUpnpCpbCurrentHttpClient::WlanLostOccurs()
       
   466     {
       
   467     iStackRequestor->StopHttpServer();
       
   468     TRAP_IGNORE( SetNullRequestorsL() );
       
   469     }
       
   470     
       
   471 // -----------------------------------------------------------------------------
       
   472 // CUpnpCpbCurrentHttpClient::AddressChanged
       
   473 //
       
   474 // -----------------------------------------------------------------------------
       
   475 //
       
   476 void CUpnpCpbCurrentHttpClient::AddressChangedL()
       
   477     {
       
   478     iStackRequestor->StopHttpServer();
       
   479     iStackRequestor->StartHttpServerL();
       
   480     }    
       
   481 
       
   482 // -----------------------------------------------------------------------------
       
   483 // CUpnpCpbCurrentHttpClient::SetNullRequestorsL
       
   484 //
       
   485 // -----------------------------------------------------------------------------
       
   486 //
       
   487 void CUpnpCpbCurrentHttpClient::SetNullRequestorsL()
       
   488     {
       
   489     CUpnpCpHttpRequestorImplBase* httpReq = new ( ELeave ) CUpnpCpHttpRequestorImplBase();
       
   490     delete iHttpRequestorDefaultImpl;
       
   491     iHttpRequestorDefaultImpl = NULL;
       
   492     iHttpRequestorDefaultImpl = httpReq;
       
   493     iHttpRequestor = iHttpRequestorDefaultImpl;
       
   494 
       
   495     CUpnpCpStackRequestorImplBase* stackReq = new ( ELeave ) CUpnpCpStackRequestorImplBase();
       
   496     delete iStackRequestorDefaultImpl;
       
   497     iStackRequestorDefaultImpl = NULL;
       
   498     iStackRequestorDefaultImpl = stackReq;
       
   499     iStackRequestor = iStackRequestorDefaultImpl;
       
   500     }
       
   501 
       
   502 //  End of File
       
   503