hotspotfw/internetconnectivitytestservice/src/ictshttphandler.cpp
branchRCL_3
changeset 18 aaabc7526ded
parent 10 dff6ebfd236f
equal deleted inserted replaced
17:024ee8b21fe2 18:aaabc7526ded
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:   HTTP class for HTTP::HEAD testing
    14 * Description:   Class for HTTP GET testing
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 
    19 
    31 
    31 
    32 const TInt KMovedPermanently = 301;
    32 const TInt KMovedPermanently = 301;
    33 const TInt KFound = 302;
    33 const TInt KFound = 302;
    34 const TInt KSeeOther = 303;
    34 const TInt KSeeOther = 303;
    35 const TInt KTemporaryRedirect = 307;
    35 const TInt KTemporaryRedirect = 307;
       
    36 
    36 // ======== MEMBER FUNCTIONS ========
    37 // ======== MEMBER FUNCTIONS ========
    37 
       
    38 
    38 
    39 // ---------------------------------------------------------------------------
    39 // ---------------------------------------------------------------------------
    40 // CIctsHttpHandler::CIctsHttpHandler
    40 // CIctsHttpHandler::CIctsHttpHandler
    41 // C++ default constructor can NOT contain any code, that
    41 // C++ default constructor can NOT contain any code, that
    42 // might leave.
    42 // might leave.
    74 //
    74 //
    75 CIctsHttpHandler* CIctsHttpHandler::NewL( CIctsEngine& aOwner, 
    75 CIctsHttpHandler* CIctsHttpHandler::NewL( CIctsEngine& aOwner, 
    76                                             TInt aHttpResponseTime )
    76                                             TInt aHttpResponseTime )
    77     {
    77     {
    78     DEBUG("CIctsHttpHandler::NewL()");
    78     DEBUG("CIctsHttpHandler::NewL()");
    79     CIctsHttpHandler* self = new( ELeave ) CIctsHttpHandler( aOwner, aHttpResponseTime );
    79     CIctsHttpHandler* self = new( ELeave ) CIctsHttpHandler( aOwner, 
       
    80                                                               aHttpResponseTime );
    80     CleanupStack::PushL( self );
    81     CleanupStack::PushL( self );
    81     self->ConstructL();
    82     self->ConstructL();
    82     CleanupStack::Pop( self );
    83     CleanupStack::Pop( self );
    83     return self;
    84     return self;
    84     }
    85     }
   124     
   125     
   125     // Remove first session properties just in case.
   126     // Remove first session properties just in case.
   126     RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
   127     RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
   127     
   128     
   128     // Clear RConnection and Socket Server instances
   129     // Clear RConnection and Socket Server instances
   129     connInfo.RemoveProperty(strPool.StringF(HTTP::EHttpSocketServ,RHTTPSession::GetTable()));
   130     connInfo.RemoveProperty( strPool.StringF( HTTP::EHttpSocketServ, 
   130     connInfo.RemoveProperty(strPool.StringF(HTTP::EHttpSocketConnection,RHTTPSession::GetTable()));
   131                                                RHTTPSession::GetTable() ) );
       
   132     connInfo.RemoveProperty( strPool.StringF( HTTP::EHttpSocketConnection, 
       
   133                                                RHTTPSession::GetTable() ) );
   131     
   134     
   132     // Clear the proxy settings
   135     // Clear the proxy settings
   133     THTTPHdrVal proxyUsage(strPool.StringF(HTTP::EUseProxy,RHTTPSession::GetTable()));
   136     THTTPHdrVal proxyUsage( strPool.StringF( HTTP::EUseProxy, 
   134     connInfo.RemoveProperty(strPool.StringF(HTTP::EProxyUsage,RHTTPSession::GetTable()));
   137                                               RHTTPSession::GetTable() ) );
   135     connInfo.RemoveProperty(strPool.StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()));
   138     connInfo.RemoveProperty( strPool.StringF( HTTP::EProxyUsage, 
       
   139                                                RHTTPSession::GetTable() ) );
       
   140     connInfo.RemoveProperty( strPool.StringF( HTTP::EProxyAddress, 
       
   141                                                RHTTPSession::GetTable() ) );
   136     
   142     
   137     // RConnection has been started, set proxy (if defined) and RConnection and
   143     // RConnection has been started, set proxy (if defined) and RConnection and
   138     // Socket Server session properties.
   144     // Socket Server session properties.
   139      
   145      
   140     // Proxy
   146     // Proxy
   141     result = aConnection.EnumerateConnections(connCount);
   147     result = aConnection.EnumerateConnections( connCount );
   142     User::LeaveIfError(result);
   148     User::LeaveIfError( result) ;
   143         
   149         
   144     // Get service and service type for this connection
   150     // Get service and service type for this connection
   145     //
   151     //
   146     _LIT(string, "%s\\%s");
   152     _LIT(string, "%s\\%s");
   147     query.Format(string, IAP, IAP_SERVICE);
   153     query.Format( string, IAP, IAP_SERVICE );
   148     result = aConnection.GetIntSetting(query, serviceId);
   154     result = aConnection.GetIntSetting( query, serviceId );
   149       
   155       
   150     query.Format(string, IAP, IAP_SERVICE_TYPE);
   156     query.Format( string, IAP, IAP_SERVICE_TYPE );
   151     result = aConnection.GetDesSetting(query, serviceType);
   157     result = aConnection.GetDesSetting( query, serviceType );
   152     User::LeaveIfError(result);
   158     User::LeaveIfError( result );
   153         
   159         
   154         
   160         
   155     // RConnection and Socket Server
   161     // RConnection and Socket Server
   156     // Now bind the HTTP session with the socket server connection
   162     // Now bind the HTTP session with the socket server connection
   157     connInfo.SetPropertyL ( 
   163     connInfo.SetPropertyL ( 
   158         strPool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), 
   164         strPool.StringF( HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), 
   159         THTTPHdrVal (aSocketServ.Handle()) );
   165         THTTPHdrVal (aSocketServ.Handle() ) );
   160         
   166         
   161     TInt connPtr1 = reinterpret_cast<TInt>(&aConnection);
   167     TInt connPtr1 = reinterpret_cast<TInt>( &aConnection) ;
   162     connInfo.SetPropertyL ( 
   168     connInfo.SetPropertyL ( 
   163         strPool.StringF(HTTP::EHttpSocketConnection, 
   169         strPool.StringF( HTTP::EHttpSocketConnection, 
   164         RHTTPSession::GetTable() ), THTTPHdrVal (connPtr1) );    
   170         RHTTPSession::GetTable() ), THTTPHdrVal ( connPtr1 ) );    
   165    
   171    
   166     }
   172     }
   167 
   173 
   168 // ---------------------------------------------------------------------------
   174 // ---------------------------------------------------------------------------
   169 // CIctsHttpHandler::SendHttpRequestL
   175 // CIctsHttpHandler::SendHttpRequestL
   186     
   192     
   187     TInt err( KErrNone );
   193     TInt err( KErrNone );
   188   	
   194   	
   189     if ( !iAttachDone )
   195     if ( !iAttachDone )
   190         {
   196         {
   191         err = iConnection.Attach( pckgInfo, RConnection::EAttachTypeMonitor);
   197         err = iConnection.Attach( pckgInfo, RConnection::EAttachTypeMonitor );
   192         DEBUG1("CIctsHttpHandler::SendHttpRequestL attach: %d", err);
   198         DEBUG1("CIctsHttpHandler::SendHttpRequestL attach: %d", err);
   193         }
   199         }
   194     
   200     
   195     if ( KErrNone == err )
   201     if ( KErrNone == err )
   196         {
   202         {
   197         iAttachDone = ETrue;
   203         iAttachDone = ETrue;
   198         SetHttpConnectionInfoL( iConnection, iSocketServ );
   204         SetHttpConnectionInfoL( iConnection, iSocketServ );
   199     
   205     
   200         // Remove redirect filter
   206         // Remove redirect filter
   201         RStringPool stringPool = iHttpSession.StringPool();
   207         RStringPool stringPool = iHttpSession.StringPool();
   202         RStringF filterName = stringPool.StringF(HTTP::ERedirect, RHTTPSession::GetTable());
   208         RStringF filterName = 
   203         iHttpSession.FilterCollection().RemoveFilter(filterName);
   209                 stringPool.StringF( HTTP::ERedirect, RHTTPSession::GetTable() );
   204     
   210         iHttpSession.FilterCollection().RemoveFilter( filterName );
       
   211 
   205         RStringPool strPool = iHttpSession.StringPool();
   212         RStringPool strPool = iHttpSession.StringPool();
   206         RStringF method = strPool.StringF(HTTP::EHEAD,RHTTPSession::GetTable());
   213         RStringF method = 
       
   214                 strPool.StringF( HTTP::EGET, RHTTPSession::GetTable() );
   207     
   215     
   208         TBuf8<KMaxIpLength> ip;
   216         TBuf8<KMaxIpLength> ip;
   209         
   217         
   210         TBool httpExists = CheckHttp( aIPAddress );
   218         TBool httpExists = CheckHttp( aIPAddress );
   211         if ( !httpExists )        
   219         if ( !httpExists )        
   215         ip.Append( aIPAddress );
   223         ip.Append( aIPAddress );
   216                
   224                
   217         // Parse string to URI
   225         // Parse string to URI
   218         TUriParser8 uri; 
   226         TUriParser8 uri; 
   219         uri.Parse(ip);
   227         uri.Parse(ip);
   220         iHttpTransaction = iHttpSession.OpenTransactionL(uri, *this, method);
   228         iHttpTransaction = iHttpSession.OpenTransactionL( uri, *this, method );
   221         RHTTPHeaders hdr = iHttpTransaction.Request().GetHeaderCollection();
   229         RHTTPHeaders hdr = iHttpTransaction.Request().GetHeaderCollection();
   222     
   230     
   223         RStringF headerStrName = strPool.OpenFStringL( KHeaderName() );
   231         RStringF headerStrName = strPool.OpenFStringL( KHeaderName() );
   224         CleanupClosePushL( headerStrName );
   232         CleanupClosePushL( headerStrName );
   225         
   233         
   277 //
   285 //
   278 // Inherited from MHTTPTransactionCallback
   286 // Inherited from MHTTPTransactionCallback
   279 // Called by framework to pass transaction events.
   287 // Called by framework to pass transaction events.
   280 // ---------------------------------------------------------------------------
   288 // ---------------------------------------------------------------------------
   281 //   
   289 //   
   282 void CIctsHttpHandler::MHFRunL(RHTTPTransaction aTransaction, 
   290 void CIctsHttpHandler::MHFRunL( RHTTPTransaction aTransaction, 
   283                             const THTTPEvent& aEvent)
   291                             const THTTPEvent& aEvent )
   284     {
   292     {
   285     DEBUG("CIctsHttpHandler::MHFRunL");
   293     DEBUG1("CIctsHttpHandler::MHFRunL aEvent.iStatus: %d", 
   286    
   294             aEvent.iStatus);
       
   295     // See HTTP event statuses. Only ESucceeded and EFailed are needed. 
   287     switch (aEvent.iStatus) 
   296     switch (aEvent.iStatus) 
   288         {
   297         {
   289         
       
   290         case THTTPEvent::EGotResponseHeaders:
   298         case THTTPEvent::EGotResponseHeaders:
   291             {
   299             {
   292             DEBUG("CIctsHttpHandler::THTTPEvent::EGotResponseHeaders");
   300             DEBUG("CIctsHttpHandler::THTTPEvent::EGotResponseHeaders");
       
   301             if ( CheckStatusCodeL( aTransaction ) )
       
   302                 {
       
   303                 // Redirection found. Transaction can be canceled.
       
   304                 aTransaction.Cancel();
       
   305                 }
   293             }
   306             }
   294             break;
   307             break;
   295         
       
   296         case THTTPEvent::EGotResponseBodyData:
       
   297             {
       
   298             DEBUG("CIctsHttpHandler::THTTPEvent::EGotResponseBodyData");
       
   299             }
       
   300             break;
       
   301 
       
   302         case THTTPEvent::EResponseComplete:
       
   303             {
       
   304             DEBUG("CIctsHttpHandler::THTTPEvent::EResponseComplete");
       
   305             }
       
   306             break;
       
   307         
       
   308         case THTTPEvent::ESucceeded:
   308         case THTTPEvent::ESucceeded:
   309             {
   309             {
   310             DEBUG("CIctsHttpHandler::THTTPEvent::ESucceeded");
   310             DEBUG("CIctsHttpHandler::MHFRunL Succeeded");
   311             CTimer::Cancel();
   311             CTimer::Cancel();
   312             iOwner.HttpEventL( EConnectionOk, iString );
   312             iOwner.HttpEventL( EConnectionOk, iString );
   313             iString = KNullDesC;
   313             iString = KNullDesC;
   314             }
   314             }
   315             break;
   315             break;
   316 
   316 
   317         case THTTPEvent::EFailed:
   317         case THTTPEvent::EFailed:
   318             {
   318             {
       
   319             DEBUG("CIctsHttpHandler::MHFRunL Failed");
   319             CTimer::Cancel();
   320             CTimer::Cancel();
   320             DEBUG("CIctsHttpHandler::THTTPEvent::EFailed");
   321             iOwner.HttpEventL( EConnectionNotOk, iString );    
   321             
   322             iString = KNullDesC;
   322             RHTTPResponse resp = aTransaction.Response();
       
   323             TInt status = resp.StatusCode();
       
   324             
       
   325             // Check if redirect was cause of EFailed
       
   326             if( status == KMovedPermanently || status == KFound || 
       
   327                 status == KSeeOther || status == KTemporaryRedirect )
       
   328                 {
       
   329             
       
   330                 // Inform the hotspot server that authentication is needed    
       
   331                 RHTTPHeaders hdr =aTransaction.Response().GetHeaderCollection();
       
   332                 RStringPool strP = aTransaction.Session().StringPool();
       
   333                 RStringF location = strP.StringF(HTTP::ELocation,RHTTPSession::GetTable());
       
   334                 
       
   335                 //parse the headers and look for location header
       
   336                 THTTPHdrVal hVal;
       
   337                 if(hdr.GetField(location,0,hVal)== KErrNone) 
       
   338                     {
       
   339                    DEBUG("CIctsHttpHandler::THTTPEvent::GetField");
       
   340                     // Location header is present
       
   341                     RStringF fieldValStr = strP.StringF(hVal.StrF());
       
   342                     const TDesC8& fieldValDesC = fieldValStr.DesC();
       
   343                     iString.Copy(fieldValDesC);
       
   344                     iOwner.HttpEventL( EHttpAuthenticationNeeded, iString );
       
   345                     }
       
   346                 else
       
   347                     {
       
   348                     // No location header. Can't use authentication -> redirect.
       
   349                     iOwner.HttpEventL( EConnectionNotOk, iString );    
       
   350                     }
       
   351                 }
       
   352              else
       
   353                 {
       
   354                 // Failed for other reason than redirect
       
   355                 iOwner.HttpEventL( EConnectionNotOk, iString );
       
   356                 }
       
   357               
       
   358               iString = KNullDesC;
       
   359             }
   323             }
   360             break;
   324             break;
   361     
   325     
   362         case THTTPEvent::ERedirectedPermanently:
       
   363             {
       
   364             // Nothing here
       
   365             DEBUG("CIctsHttpHandler::THTTPEvent::ERedirectedPermanently");
       
   366             } 
       
   367             break;
       
   368     
       
   369         case THTTPEvent::ERedirectedTemporarily:
       
   370             {
       
   371             // Nothing here
       
   372             DEBUG("CIctsHttpHandler::THTTPEvent::ERedirectedTemporarily");
       
   373             } 
       
   374             break;
       
   375     
       
   376         default:
   326         default:
   377             {
   327             {
   378             DEBUG1( "CIctsHttpHandler::MHFRunL::default iStatus: %d", aEvent.iStatus ); 
   328             DEBUG1( "CIctsHttpHandler::MHFRunL default: %d", aEvent.iStatus ); 
   379             CTimer::Cancel();
   329             // Do nothing. Timer will take care of error situations.
   380             if ( aEvent.iStatus < 0 )
       
   381                 {
       
   382                 _LIT(string, "Unknown error");
       
   383                 iString = string;
       
   384                 iOwner.HttpEventL( EConnectionNotOk, iString );
       
   385                 }
       
   386             else
       
   387                 {
       
   388                 _LIT(string, "Default");
       
   389                 iString = string;
       
   390                 iOwner.HttpEventL( EConnectionNotOk, iString );
       
   391                 }
       
   392             } 
   330             } 
   393             break;
   331             break;
   394         }
   332         }
   395     }
   333     }
   396 
   334 
   416     {
   354     {
   417     // The "http://" prefix is expected to be at the beginning of the URI.
   355     // The "http://" prefix is expected to be at the beginning of the URI.
   418     return ( 0 == aIPAddress.Find( KHttpPrefix ) );
   356     return ( 0 == aIPAddress.Find( KHttpPrefix ) );
   419     }
   357     }
   420 
   358 
       
   359 // ----------------------------------------------------------------------------
       
   360 // CIctsHttpHandler::CheckStatusCodeL
       
   361 // ----------------------------------------------------------------------------    
       
   362 TBool CIctsHttpHandler::CheckStatusCodeL( RHTTPTransaction aTransaction )
       
   363     {
       
   364     DEBUG1("CIctsHttpHandler::CheckStatusCodeL response statuscode: %d", 
       
   365                 aTransaction.Response().StatusCode());
       
   366           
       
   367     TBool ret ( EFalse );
       
   368     
       
   369     switch ( aTransaction.Response().StatusCode() )
       
   370         {
       
   371         case KMovedPermanently:
       
   372         case KFound:
       
   373         case KSeeOther:
       
   374         case KTemporaryRedirect:
       
   375             {
       
   376             DEBUG("CIctsHttpHandler::CheckStatusCodeL Redirect");
       
   377             CTimer::Cancel();
       
   378             ret = ETrue;
       
   379             
       
   380             //parse the headers and look for location header
       
   381             RHTTPHeaders hdr =aTransaction.Response().GetHeaderCollection();
       
   382             RStringPool strP = aTransaction.Session().StringPool();
       
   383             RStringF location = 
       
   384                     strP.StringF( HTTP::ELocation,RHTTPSession::GetTable() );
       
   385             THTTPHdrVal hVal;
       
   386             if( hdr.GetField( location, 0, hVal ) == KErrNone ) 
       
   387                 {
       
   388                 DEBUG("CIctsHttpHandler::CheckStatusCodeL location header");
       
   389                 // Location header is present
       
   390                 RStringF fieldValStr = strP.StringF(hVal.StrF());
       
   391                 const TDesC8& fieldValDesC = fieldValStr.DesC();
       
   392                 iString.Copy( fieldValDesC );
       
   393                 iOwner.HttpEventL( EHttpAuthenticationNeeded, iString );
       
   394                 }
       
   395             else
       
   396                 {
       
   397                 DEBUG("CIctsHttpHandler::CheckStatusCodeL no location header");
       
   398                 // No location header. Can't redirect.
       
   399                 iOwner.HttpEventL( EConnectionNotOk, iString );
       
   400                 iString = KNullDesC;
       
   401                 }
       
   402             }
       
   403             break;
       
   404         
       
   405         default:
       
   406             // Do nothing
       
   407             break;
       
   408         }
       
   409     return ret;
       
   410     }
       
   411 
   421 //  End of File
   412 //  End of File