| author | Sebastian Brannstrom <sebastianb@symbian.org> | 
| Sat, 04 Dec 2010 15:35:24 +0000 | |
| branch | RCL_3 | 
| changeset 392 | 6a9baa40b241 | 
| parent 341 | a648d7ca5e27 | 
| 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" | |
| 114 | 17 | #include "Podcatcher.pan" | 
| 2 | 18 | |
| 19 | const TInt KTempBufferSize = 100; | |
| 20 | ||
| 21 | CHttpClient::~CHttpClient() | |
| 22 |   {
 | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 23 | |
| 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 24 | iPodcastModel.ConnectionEngine().RemoveObserver(this); | 
| 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
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 | 	{
 | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 117 | 	DP1("CHttpClient::ConnectCompleteL BEGIN, aErrorCode=%d", aErrorCode);
 | 
| 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 118 | 	DP1("    iWaitingForGet=%d", iWaitingForGet);
 | 
| 2 | 119 | if(iWaitingForGet) | 
| 120 | 		{
 | |
| 121 | iWaitingForGet = EFalse; | |
| 122 | if( aErrorCode == KErrNone) | |
| 123 | 			{
 | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 124 | TRAP_IGNORE(iSession.OpenL()); | 
| 2 | 125 | RHTTPConnectionInfo connInfo = iSession.ConnectionInfo(); | 
| 126 | RStringPool pool = iSession.StringPool(); | |
| 127 | // Attach to socket server | |
| 128 | connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), THTTPHdrVal(iPodcastModel.ConnectionEngine().SockServ().Handle())); | |
| 129 | // Attach to connection | |
| 130 | TInt connPtr = REINTERPRET_CAST(TInt, &iPodcastModel.ConnectionEngine().Connection()); | |
| 131 | connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable()), THTTPHdrVal(connPtr)); | |
| 132 | ||
| 133 | iPodcastModel.SetProxyUsageIfNeededL(iSession); | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 134 | DoGetAfterConnectL(); | 
| 2 | 135 | } | 
| 136 | else | |
| 137 | 			{
 | |
| 138 | ClientRequestCompleteL(KErrCouldNotConnect); | |
| 139 | iSession.Close(); | |
| 140 | } | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 141 | } | 
| 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 142 | 	DP("CHttpClient::ConnectCompleteL END");
 | 
| 2 | 143 | } | 
| 144 | ||
| 145 | void CHttpClient::Disconnected() | |
| 146 | 	{
 | |
| 147 | iIsActive = EFalse; | |
| 148 | iSession.Close(); | |
| 149 | } | |
| 150 | ||
| 151 | void CHttpClient::DoGetAfterConnectL() | |
| 152 | 	{	
 | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 153 | 	DP("CHttpClient::DoGetAfterConnectL BEGIN");
 | 
| 2 | 154 | // since nothing should be downloading now. Delete the handler | 
| 155 | if (iHandler) | |
| 156 | 		{
 | |
| 157 | delete iHandler; | |
| 158 | iHandler = NULL; | |
| 159 | } | |
| 160 | ||
| 161 | iHandler = CHttpEventHandler::NewL(this, iObserver, iPodcastModel.FsSession()); | |
| 162 | iHandler->SetSilent(iSilentGet); | |
| 163 | ||
| 164 | TEntry entry; | |
| 165 | TBuf8<KTempBufferSize> rangeText; | |
| 166 | ||
| 167 | 	if (iResumeEnabled && iPodcastModel.FsSession().Entry(iCurrentFileName, entry) == KErrNone) {
 | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 168 | 		DP1("    Found file, with size=%d", entry.iSize);
 | 
| 2 | 169 | // file exists, so we should probably resume | 
| 115 
d87e984bd8b8
Even more robustness improvements for HTTP client - specifically disk full problems
 teknolog parents: 
114diff
changeset | 170 | 		rangeText.Format(_L8("bytes=%d-"), (entry.iSize-KByteOverlap > 0 ? entry.iSize-KByteOverlap : 0));
 | 
| 2 | 171 | iHandler->SetSaveFileName(iCurrentFileName, ETrue); | 
| 172 | 	} else {
 | |
| 173 | // otherwise just make sure the directory exists | |
| 174 | BaflUtils::EnsurePathExistsL(iPodcastModel.FsSession(),iCurrentFileName); | |
| 175 | iHandler->SetSaveFileName(iCurrentFileName); | |
| 176 | } | |
| 177 | ||
| 178 | RStringPool strP = iSession.StringPool(); | |
| 179 | RStringF method; | |
| 180 | method = strP.StringF(HTTP::EGET, RHTTPSession::GetTable()); | |
| 181 | ||
| 182 | iTrans = iSession.OpenTransactionL(iUriParser, *iHandler, method); | |
| 183 | RHTTPHeaders hdr = iTrans.Request().GetHeaderCollection(); | |
| 184 | // Add headers appropriate to all methods | |
| 185 | SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent); | |
| 186 | SetHeaderL(hdr, HTTP::EAccept, KAccept); | |
| 187 | TBuf<KTempBufferSize> range16; | |
| 188 | range16.Copy(rangeText); | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 189 | 	DP1("    range text: %S", &range16);
 | 
| 2 | 190 | 	if (rangeText.Length() > 0) {
 | 
| 191 | SetHeaderL(hdr, HTTP::ERange, rangeText); | |
| 192 | } | |
| 193 | iTransactionCount++; | |
| 194 | // submit the transaction | |
| 195 | iTrans.SubmitL(); | |
| 196 | iIsActive = ETrue; | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 197 | 	DP("CHttpClient::DoGetAfterConnectL END");
 | 
| 2 | 198 | } | 
| 199 | ||
| 200 | TBool CHttpClient::GetL(const TDesC& aUrl, const TDesC& aFileName,  TBool aSilent) {
 | |
| 201 | 	DP("CHttpClient::Get START");
 | |
| 126 | 202 | 	DP2("Getting '%S' to '%S'", &aUrl, &aFileName);
 | 
| 203 | ||
| 204 | if (iIsActive) | |
| 205 | 		{
 | |
| 206 | return EFalse; | |
| 207 | } | |
| 341 
a648d7ca5e27
Small fixes after merge with Symbian1
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
310diff
changeset | 208 | |
| 
a648d7ca5e27
Small fixes after merge with Symbian1
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
310diff
changeset | 209 | 	DP1("Getting URL: %S", &aUrl);
 | 
| 
a648d7ca5e27
Small fixes after merge with Symbian1
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
310diff
changeset | 210 | 	DP1("Writing file: %S", &aFileName);
 | 
| 
a648d7ca5e27
Small fixes after merge with Symbian1
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
310diff
changeset | 211 | |
| 2 | 212 | iCurrentURL.Copy(aUrl); | 
| 213 | ||
| 214 | TInt urlError = iUriParser.Parse(iCurrentURL); | |
| 215 | ||
| 216 | if(urlError != KErrNone ||!iUriParser.IsSchemeValid()) | |
| 217 | 		{		
 | |
| 218 | iCurrentURL = KNullDesC8; | |
| 219 | iSession.Close(); | |
| 220 | iObserver.CompleteL(this, KErrHttpInvalidUri); | |
| 221 | return EFalse; | |
| 222 | } | |
| 223 | ||
| 224 | iSilentGet = aSilent; | |
| 225 | iCurrentFileName.Copy(aFileName); | |
| 226 | ||
| 227 | if (iTransactionCount == 0) | |
| 228 | 		{
 | |
| 229 | 		DP("CHttpClient::GetL\t*** Opening HTTP session ***");
 | |
| 230 | iSession.Close(); | |
| 231 | iSession.OpenL(); | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 232 | iWaitingForGet = ETrue; | 
| 2 | 233 | ConnectHttpSessionL(); | 
| 234 | } | |
| 235 | else | |
| 236 | 		{
 | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 237 | iWaitingForGet = EFalse; | 
| 2 | 238 | DoGetAfterConnectL(); | 
| 239 | } | |
| 240 | return ETrue; | |
| 241 | } | |
| 242 | ||
| 243 | void CHttpClient::Stop() | |
| 244 | 	{
 | |
| 245 | iIsActive = EFalse; | |
| 246 | if(iHandler != NULL) | |
| 247 | 		{
 | |
| 248 | // cancel the ongoing transaction | |
| 249 | iTrans.Cancel(); | |
| 250 | iTransactionCount = 0; | |
| 251 | ||
| 252 | // make sure that we save the file | |
| 253 | iHandler->CloseSaveFile(); | |
| 254 | ||
| 255 | // we could now delete the handler since a new will be created | |
| 256 | delete iHandler; | |
| 257 | iHandler = NULL; | |
| 258 | ||
| 259 | // close the session | |
| 260 | 		DP("CHttpClient::Stop\t*** Closing HTTP session ***");
 | |
| 261 | iSession.Close(); | |
| 262 | } | |
| 263 | ||
| 264 | TRAP_IGNORE(iObserver.CompleteL(this, KErrDisconnected)); | |
| 265 | ||
| 266 | } | |
| 267 | ||
| 268 | void CHttpClient::ClientRequestCompleteL(TInt aErrorCode) {
 | |
| 341 
a648d7ca5e27
Small fixes after merge with Symbian1
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
310diff
changeset | 269 | 	DP1("CHttpClient::ClientRequestCompleteL BEGIN, aErrorCode=%d", aErrorCode);
 | 
| 2 | 270 | iIsActive = EFalse; | 
| 271 | iObserver.CompleteL(this, aErrorCode); | |
| 285 
4d42a5e09930
Significant robustness improvements for ConnectionEngine
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
126diff
changeset | 272 | 	DP1("    iTransactionCount=%d", iTransactionCount);
 | 
| 2 | 273 | if(iTransactionCount>0) | 
| 274 | 		{
 | |
| 275 | iTransactionCount--; | |
| 276 | ||
| 277 | if(iTransactionCount == 0) | |
| 278 | 			{
 | |
| 279 | 			DP("CHttpClient::ClientRequestCompleteL\t*** Closing HTTP session ***");
 | |
| 280 | delete iHandler; | |
| 281 | iHandler = NULL; | |
| 282 | iSession.Close(); | |
| 283 | } | |
| 284 | } | |
| 341 
a648d7ca5e27
Small fixes after merge with Symbian1
 Sebastian Brannstrom <sebastianb@symbian.org> parents: 
310diff
changeset | 285 | 	DP("CHttpClient::ClientRequestCompleteL END");
 | 
| 2 | 286 | } |