| author | Sebastian Brannstrom <sebastianb@symbian.org> | 
| Sun, 24 Oct 2010 01:27:31 +0100 | |
| branch | symbian1 | 
| changeset 284 | 69385a7c9810 | 
| parent 164 | 000f9fc147b2 | 
| child 336 | 3d6c1417e8bd | 
| permissions | -rw-r--r-- | 
| 2 | 1 | // HttpClient.cpp | 
| 2 | ||
| 3 | ||
| 4 | #include <e32base.h> | |
| 5 | #include <http/rhttpheaders.h> | |
| 6 | #include <http.h> | |
| 7 | #include <commdb.h> | |
| 8 | #include <eikenv.h> | |
| 9 | #include <es_sock.h> | |
| 10 | #include <bautils.h> | |
| 11 | #include <CommDbConnPref.h> | |
| 12 | #include "debug.h" | |
| 13 | #include "constants.h" | |
| 14 | #include "HttpClient.h" | |
| 15 | #include "connectionengine.h" | |
| 16 | #include "settingsengine.h" | |
| 60 | 17 | #include "Podcatcher.pan" | 
| 2 | 18 | |
| 19 | const TInt KTempBufferSize = 100; | |
| 20 | ||
| 21 | CHttpClient::~CHttpClient() | |
| 22 |   {
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 23 | |
| 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 24 | iPodcastModel.ConnectionEngine().RemoveObserver(this); | 
| 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 25 | |
| 2 | 26 | if (iHandler) | 
| 27 |   	{
 | |
| 28 | iHandler->CloseSaveFile(); | |
| 29 | delete iHandler; | |
| 30 | } | |
| 31 | ||
| 32 | iSession.Close(); | |
| 33 | } | |
| 34 | ||
| 35 | CHttpClient* CHttpClient::NewL(CPodcastModel& aPodcastModel, MHttpClientObserver& aObserver) | |
| 36 |   {
 | |
| 37 | CHttpClient* me = NewLC(aPodcastModel, aObserver); | |
| 38 | CleanupStack::Pop(me); | |
| 39 | return me; | |
| 40 | } | |
| 41 | ||
| 42 | CHttpClient::CHttpClient(CPodcastModel& aPodcastModel, MHttpClientObserver& aObserver) : iPodcastModel(aPodcastModel), iObserver(aObserver) | |
| 43 |   {
 | |
| 44 | iResumeEnabled = EFalse; | |
| 45 | } | |
| 46 | ||
| 47 | CHttpClient* CHttpClient::NewLC(CPodcastModel& aPodcastModel, MHttpClientObserver& aObserver) | |
| 48 |   {
 | |
| 49 | CHttpClient* me = new (ELeave) CHttpClient(aPodcastModel, aObserver); | |
| 50 | CleanupStack::PushL(me); | |
| 51 | me->ConstructL(); | |
| 52 | return me; | |
| 53 | } | |
| 54 | ||
| 55 | void CHttpClient::ConstructL() | |
| 56 |   {
 | |
| 57 | iPodcastModel.ConnectionEngine().AddObserver(this); | |
| 58 | } | |
| 59 | ||
| 60 | void CHttpClient::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue) | |
| 61 | 	{
 | |
| 62 | RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue); | |
| 63 | THTTPHdrVal val(valStr); | |
| 64 | aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField, RHTTPSession::GetTable()), val); | |
| 65 | valStr.Close(); | |
| 66 | } | |
| 67 | ||
| 68 | TBool CHttpClient::IsActive() | |
| 69 | 	{
 | |
| 70 | return iIsActive; | |
| 71 | } | |
| 72 | ||
| 73 | void CHttpClient::SetResumeEnabled(TBool aEnabled) | |
| 74 | 	{
 | |
| 75 | iResumeEnabled = aEnabled; | |
| 76 | } | |
| 77 | ||
| 78 | ||
| 79 | void CHttpClient::ConnectHttpSessionL() | |
| 80 | {
 | |
| 81 | 	DP("ConnectHttpSessionL START");	
 | |
| 82 | CConnectionEngine::TConnectionState connState = iPodcastModel.ConnectionEngine().ConnectionState(); | |
| 83 | if(connState == CConnectionEngine::EConnected) | |
| 84 | 		{
 | |
| 85 | 		DP("ConnectionState == CConnectionEngine::EConnected");
 | |
| 86 | // Session already connected, call connect complete directly but return status since URLs or so might be faulty | |
| 87 | ConnectCompleteL(KErrNone); | |
| 88 | return; | |
| 89 | } | |
| 90 | ||
| 91 | if(connState == CConnectionEngine::ENotConnected) | |
| 92 | 		{
 | |
| 93 | 		DP1("SpecificIAP() == %d",iPodcastModel.SettingsEngine().SpecificIAP());
 | |
| 94 | ||
| 95 | if(iPodcastModel.SettingsEngine().SpecificIAP() == -1) | |
| 96 | 			{
 | |
| 97 | iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EUserSelectConnection); | |
| 98 | } | |
| 99 | else if ( iPodcastModel.SettingsEngine().SpecificIAP() == 0 ) | |
| 100 | 			{
 | |
| 101 | iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EDefaultConnection); | |
| 102 | } | |
| 103 | else if( (iPodcastModel.SettingsEngine().SpecificIAP()&KUseIAPFlag)) | |
| 104 | 			{
 | |
| 105 | iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EIAPConnection); | |
| 106 | } | |
| 107 | else | |
| 108 | 			{
 | |
| 109 | iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EMobilityConnection); | |
| 110 | } | |
| 111 | } | |
| 112 | 	DP("ConnectHttpSessionL END");	
 | |
| 113 | } | |
| 114 | ||
| 115 | void CHttpClient::ConnectCompleteL(TInt aErrorCode) | |
| 116 | 	{
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 117 | 	DP1("CHttpClient::ConnectCompleteL BEGIN, aErrorCode=%d", aErrorCode);
 | 
| 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 118 | 	DP1("    iWaitingForGet=%d", iWaitingForGet);
 | 
| 2 | 119 | if(iWaitingForGet) | 
| 120 | 		{
 | |
| 121 | iWaitingForGet = EFalse; | |
| 122 | if( aErrorCode == KErrNone) | |
| 123 | 			{
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 124 | TRAP_IGNORE(iSession.OpenL()); | 
| 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 125 | 			DP("    one");
 | 
| 2 | 126 | RHTTPConnectionInfo connInfo = iSession.ConnectionInfo(); | 
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 127 | 			DP("    one point five");
 | 
| 2 | 128 | RStringPool pool = iSession.StringPool(); | 
| 129 | // Attach to socket server | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 130 | 			DP("    two");
 | 
| 2 | 131 | connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), THTTPHdrVal(iPodcastModel.ConnectionEngine().SockServ().Handle())); | 
| 132 | // Attach to connection | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 133 | 			DP("    three");
 | 
| 2 | 134 | TInt connPtr = REINTERPRET_CAST(TInt, &iPodcastModel.ConnectionEngine().Connection()); | 
| 135 | connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable()), THTTPHdrVal(connPtr)); | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 136 | 			DP("    four");
 | 
| 2 | 137 | |
| 138 | iPodcastModel.SetProxyUsageIfNeededL(iSession); | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 139 | DoGetAfterConnectL(); | 
| 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 140 | //iWaitingForGet = EFalse; // set to true by DoGetAfterConnectL | 
| 2 | 141 | } | 
| 142 | else | |
| 143 | 			{
 | |
| 144 | ClientRequestCompleteL(KErrCouldNotConnect); | |
| 145 | iSession.Close(); | |
| 146 | } | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 147 | } | 
| 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 148 | 	DP("CHttpClient::ConnectCompleteL END");
 | 
| 2 | 149 | } | 
| 150 | ||
| 151 | void CHttpClient::Disconnected() | |
| 152 | 	{
 | |
| 153 | iIsActive = EFalse; | |
| 154 | iSession.Close(); | |
| 155 | } | |
| 156 | ||
| 157 | void CHttpClient::DoGetAfterConnectL() | |
| 158 | 	{	
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 159 | 	DP("CHttpClient::DoGetAfterConnectL BEGIN");
 | 
| 2 | 160 | // since nothing should be downloading now. Delete the handler | 
| 161 | if (iHandler) | |
| 162 | 		{
 | |
| 163 | delete iHandler; | |
| 164 | iHandler = NULL; | |
| 165 | } | |
| 166 | ||
| 167 | iHandler = CHttpEventHandler::NewL(this, iObserver, iPodcastModel.FsSession()); | |
| 168 | iHandler->SetSilent(iSilentGet); | |
| 169 | ||
| 170 | TEntry entry; | |
| 171 | TBuf8<KTempBufferSize> rangeText; | |
| 172 | ||
| 173 | 	if (iResumeEnabled && iPodcastModel.FsSession().Entry(iCurrentFileName, entry) == KErrNone) {
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 174 | 		DP1("    Found file, with size=%d", entry.iSize);
 | 
| 2 | 175 | // file exists, so we should probably resume | 
| 60 | 176 | 		rangeText.Format(_L8("bytes=%d-"), (entry.iSize-KByteOverlap > 0 ? entry.iSize-KByteOverlap : 0));
 | 
| 2 | 177 | iHandler->SetSaveFileName(iCurrentFileName, ETrue); | 
| 178 | 	} else {
 | |
| 179 | // otherwise just make sure the directory exists | |
| 180 | BaflUtils::EnsurePathExistsL(iPodcastModel.FsSession(),iCurrentFileName); | |
| 181 | iHandler->SetSaveFileName(iCurrentFileName); | |
| 182 | } | |
| 183 | ||
| 184 | RStringPool strP = iSession.StringPool(); | |
| 185 | RStringF method; | |
| 186 | method = strP.StringF(HTTP::EGET, RHTTPSession::GetTable()); | |
| 187 | ||
| 188 | iTrans = iSession.OpenTransactionL(iUriParser, *iHandler, method); | |
| 189 | RHTTPHeaders hdr = iTrans.Request().GetHeaderCollection(); | |
| 190 | // Add headers appropriate to all methods | |
| 191 | SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent); | |
| 192 | SetHeaderL(hdr, HTTP::EAccept, KAccept); | |
| 193 | TBuf<KTempBufferSize> range16; | |
| 194 | range16.Copy(rangeText); | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 195 | 	DP1("    range text: %S", &range16);
 | 
| 2 | 196 | 	if (rangeText.Length() > 0) {
 | 
| 197 | SetHeaderL(hdr, HTTP::ERange, rangeText); | |
| 198 | } | |
| 199 | iTransactionCount++; | |
| 200 | // submit the transaction | |
| 201 | iTrans.SubmitL(); | |
| 202 | iIsActive = ETrue; | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 203 | 	DP("CHttpClient::DoGetAfterConnectL END");
 | 
| 2 | 204 | } | 
| 205 | ||
| 206 | TBool CHttpClient::GetL(const TDesC& aUrl, const TDesC& aFileName,  TBool aSilent) {
 | |
| 207 | 	DP("CHttpClient::Get START");
 | |
| 69 | 208 | |
| 209 | if (iIsActive) | |
| 210 | 		{
 | |
| 211 | return EFalse; | |
| 212 | } | |
| 164 
000f9fc147b2
Catch up with default branch; New v 27 SIS
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
69diff
changeset | 213 | |
| 
000f9fc147b2
Catch up with default branch; New v 27 SIS
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
69diff
changeset | 214 | 	DP1("Getting URL: %S", &aUrl);
 | 
| 
000f9fc147b2
Catch up with default branch; New v 27 SIS
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
69diff
changeset | 215 | 	DP1("Writing file: %S", &aFileName);
 | 
| 
000f9fc147b2
Catch up with default branch; New v 27 SIS
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
69diff
changeset | 216 | |
| 2 | 217 | iCurrentURL.Copy(aUrl); | 
| 218 | ||
| 219 | TInt urlError = iUriParser.Parse(iCurrentURL); | |
| 220 | ||
| 221 | if(urlError != KErrNone ||!iUriParser.IsSchemeValid()) | |
| 222 | 		{		
 | |
| 223 | iCurrentURL = KNullDesC8; | |
| 224 | iSession.Close(); | |
| 225 | iObserver.CompleteL(this, KErrHttpInvalidUri); | |
| 226 | return EFalse; | |
| 227 | } | |
| 228 | ||
| 229 | iSilentGet = aSilent; | |
| 230 | iCurrentFileName.Copy(aFileName); | |
| 231 | ||
| 232 | if (iTransactionCount == 0) | |
| 233 | 		{
 | |
| 234 | 		DP("CHttpClient::GetL\t*** Opening HTTP session ***");
 | |
| 235 | iSession.Close(); | |
| 236 | iSession.OpenL(); | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 237 | iWaitingForGet = ETrue; | 
| 2 | 238 | ConnectHttpSessionL(); | 
| 239 | } | |
| 240 | else | |
| 241 | 		{
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 242 | iWaitingForGet = EFalse; | 
| 2 | 243 | DoGetAfterConnectL(); | 
| 244 | } | |
| 245 | return ETrue; | |
| 246 | } | |
| 247 | ||
| 248 | void CHttpClient::Stop() | |
| 249 | 	{
 | |
| 250 | iIsActive = EFalse; | |
| 251 | if(iHandler != NULL) | |
| 252 | 		{
 | |
| 253 | // cancel the ongoing transaction | |
| 254 | iTrans.Cancel(); | |
| 255 | iTransactionCount = 0; | |
| 256 | ||
| 257 | // make sure that we save the file | |
| 258 | iHandler->CloseSaveFile(); | |
| 259 | ||
| 260 | // we could now delete the handler since a new will be created | |
| 261 | delete iHandler; | |
| 262 | iHandler = NULL; | |
| 263 | ||
| 264 | // close the session | |
| 265 | 		DP("CHttpClient::Stop\t*** Closing HTTP session ***");
 | |
| 266 | iSession.Close(); | |
| 267 | } | |
| 268 | ||
| 269 | TRAP_IGNORE(iObserver.CompleteL(this, KErrDisconnected)); | |
| 270 | ||
| 271 | } | |
| 272 | ||
| 273 | void CHttpClient::ClientRequestCompleteL(TInt aErrorCode) {
 | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 274 | 	DP1("CHttpClient::ClientRequestCompleteL, aErrorCode=%d", aErrorCode);
 | 
| 2 | 275 | iIsActive = EFalse; | 
| 276 | iObserver.CompleteL(this, aErrorCode); | |
| 284 
69385a7c9810
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
164diff
changeset | 277 | 	DP1("    iTransactionCount=%d", iTransactionCount);
 | 
| 2 | 278 | if(iTransactionCount>0) | 
| 279 | 		{
 | |
| 280 | iTransactionCount--; | |
| 281 | ||
| 282 | if(iTransactionCount == 0) | |
| 283 | 			{
 | |
| 284 | 			DP("CHttpClient::ClientRequestCompleteL\t*** Closing HTTP session ***");
 | |
| 285 | delete iHandler; | |
| 286 | iHandler = NULL; | |
| 287 | iSession.Close(); | |
| 288 | } | |
| 289 | } | |
| 290 | } |