engine/src/HttpEventHandler.cpp
branchsymbian1
changeset 167 4bfc2fcec5f6
parent 164 000f9fc147b2
child 245 3f57982a6f10
equal deleted inserted replaced
166:58268495eb35 167:4bfc2fcec5f6
    69 			DP1("Status: %d", iLastStatusCode);
    69 			DP1("Status: %d", iLastStatusCode);
    70 
    70 
    71 			// Dump the headers if we're being verbose
    71 			// Dump the headers if we're being verbose
    72 			//DumpRespHeadersL(aTransaction);
    72 			//DumpRespHeadersL(aTransaction);
    73 
    73 
    74 			// Determine if the body will be saved to disk
       
    75 			iSavingResponseBody = ETrue;
       
    76 			TBool cancelling = EFalse;
       
    77 			if (resp.HasBody() && (iLastStatusCode >= 200) && (iLastStatusCode < 300) && (iLastStatusCode != 204))
    74 			if (resp.HasBody() && (iLastStatusCode >= 200) && (iLastStatusCode < 300) && (iLastStatusCode != 204))
    78 				{
    75 				{
    79 				//iBytesDownloaded = 0;
       
    80 				TInt dataSize = resp.Body()->OverallDataSize();
    76 				TInt dataSize = resp.Body()->OverallDataSize();
    81 				if (dataSize >= 0) {
    77 				if (dataSize >= 0) {
    82 					DP1("Response body size is %d", dataSize);
    78 					DP1("Response body size is %d", dataSize);
    83 					iBytesTotal = dataSize;	
    79 					iBytesTotal = dataSize;	
    84 				} else {
    80 				} else {
    85 					DP("Response body size is unknown");
    81 					DP("Response body size is unknown");
    86 					iBytesTotal = -1;
    82 					iBytesTotal = -1;
    87 				}
    83 				}
    88 				iCallbacks.DownloadInfo(iHttpClient, dataSize);
    84 				iCallbacks.DownloadInfo(iHttpClient, dataSize);
    89 
    85 
    90 				cancelling = EFalse;
    86 				}
    91 				}
    87 
    92 
    88 			DP1("iFileOpen=%d", iFileOpen);
    93 			// If we're cancelling, must do it now..
    89 			if (!iFileOpen)
    94 			if (cancelling)
       
    95 				{
       
    96 				DP("Transaction Cancelled");
       
    97 				aTransaction.Close();
       
    98 				iHttpClient->ClientRequestCompleteL(KErrCancel);
       
    99 				}
       
   100 			else if (iSavingResponseBody) // If we're saving, then open a file handle for the new file
       
   101 				{
    90 				{
   102 				iFileServ.Parse(iFileName, iParsedFileName);
    91 				iFileServ.Parse(iFileName, iParsedFileName);
   103 				TInt valid = iFileServ.IsValidName(iFileName);
    92 				TInt valid = iFileServ.IsValidName(iFileName);
   104 				
    93 				
   105 				if (!valid)
    94 				if (!valid)
   106 					{
    95 					{
   107 					DP("The specified filename is not valid!.");
    96 					DP("The specified filename is not valid!.");
   108 					iSavingResponseBody = EFalse;
       
   109 					iHttpClient->ClientRequestCompleteL(KErrBadName);
    97 					iHttpClient->ClientRequestCompleteL(KErrBadName);
   110 					}
    98 					}
   111 				else
    99 				else
   112 					{
   100 					{
   113 					if (iContinue) {
   101 					if (iContinue) {
   114 						TInt err = iRespBodyFile.Open(iFileServ, iParsedFileName.FullName(),EFileWrite);
   102 						TInt err = iRespBodyFile.Open(iFileServ, iParsedFileName.FullName(),EFileWrite);
   115 						if (err)
   103 						if (err)
   116 							{
   104 							{
   117 							DP2("There was an error=%d opening file '%S'", err, &iParsedFileName.FullName());
   105 							DP2("There was an error=%d opening file '%S'", err, &iParsedFileName.FullName());
   118 							iSavingResponseBody = EFalse;
       
   119 							iHttpClient->ClientRequestCompleteL(KErrInUse);
   106 							iHttpClient->ClientRequestCompleteL(KErrInUse);
   120 							User::Leave(err);
   107 							User::Leave(err);
   121 							} 
   108 							} 
   122 						else
   109 						else
   123 							{
   110 							{
       
   111 							iFileOpen = ETrue;
   124 							int pos = -KByteOverlap;
   112 							int pos = -KByteOverlap;
   125 							if((err=iRespBodyFile.Seek(ESeekEnd, pos)) != KErrNone)
   113 							if((err=iRespBodyFile.Seek(ESeekEnd, pos)) != KErrNone)
   126 								{
   114 								{
   127 								DP("Failed to set position!");
   115 								DP("Failed to set position!");
   128 								iHttpClient->ClientRequestCompleteL(KErrWrite);
   116 								iHttpClient->ClientRequestCompleteL(KErrWrite);
   129 								User::Leave(err);
   117 								User::Leave(err);
   130 								}
   118 								}
   131 						iBytesDownloaded = (pos > 0) ? pos : 0;
   119 							iBytesDownloaded = (pos > 0) ? pos : 0;
   132 						iBytesTotal += iBytesDownloaded;
   120 							iBytesTotal += iBytesDownloaded;
   133 						DP1("Total bytes is now %u", iBytesTotal);
   121 							DP1("Total bytes is now %u", iBytesTotal);
   134 						DP1("Seeking end: %d", pos);
   122 							DP1("Seeking end: %d", pos);
   135 							}
   123 							}
   136 						}
   124 						}
   137 					else 
   125 					else 
   138 						{
   126 						{
   139 						TInt err = iRespBodyFile.Replace(iFileServ,
   127 						TInt err = iRespBodyFile.Replace(iFileServ,
   140 														 iParsedFileName.FullName(),
   128 														 iParsedFileName.FullName(),
   141 														 EFileWrite);
   129 														 EFileWrite);
   142 						if (err)
   130 						if (err)
   143 							{
   131 							{
   144 							DP("There was an error replacing file");
   132 							DP("There was an error replacing file");
   145 							iSavingResponseBody = EFalse;
       
   146 							User::Leave(err);
   133 							User::Leave(err);
       
   134 							}
       
   135 						else
       
   136 							{
       
   137 							iFileOpen = ETrue;
   147 							}
   138 							}
   148 						}
   139 						}
   149 					}
   140 					}
   150 				}
   141 				}
   151 
       
   152 			} break;
   142 			} break;
   153 		case THTTPEvent::EGotResponseBodyData:
   143 		case THTTPEvent::EGotResponseBodyData:
   154 			{
   144 			{
   155 			// Get the body data supplier
   145 			// Get the body data supplier
   156 			iRespBody = aTransaction.Response().Body();
   146 			iRespBody = aTransaction.Response().Body();
   157 
   147 
   158 			// Some (more) body data has been received (in the HTTP response)
   148 			// Some (more) body data has been received (in the HTTP response)
   159 			//DumpRespBody(aTransaction);
   149 			//DumpRespBody(aTransaction);
   160 			//DP1("Saving: %d", iSavingResponseBody);
   150 			//DP1("Saving: %d", iSavingResponseBody);
   161 			// Append to the output file if we're saving responses
   151 			// Append to the output file if we're saving responses
   162 			if (iSavingResponseBody)
   152 			if (iFileOpen)
   163 				{
   153 				{
   164 				TPtrC8 bodyData;
   154 				TPtrC8 bodyData;
   165 				iRespBody->GetNextDataPart(bodyData);
   155 				iRespBody->GetNextDataPart(bodyData);
   166 				iBytesDownloaded += bodyData.Length();
   156 				iBytesDownloaded += bodyData.Length();
   167 				TInt error = iRespBodyFile.Write(bodyData);
   157 				TInt error = iRespBodyFile.Write(bodyData);
   168 
   158 
   169 				// on writing error we close connection 
   159 				// on writing error we close connection 
   170 				if (error != KErrNone) {
   160 				if (error != KErrNone) {
       
   161 					iFileOpen = EFalse;
   171 					iRespBodyFile.Close();
   162 					iRespBodyFile.Close();
   172 					iCallbacks.FileError(error);
   163 					iCallbacks.FileError(error);
   173 					iHttpClient->ClientRequestCompleteL(error);
   164 					iHttpClient->ClientRequestCompleteL(error);
   174 					return;
   165 					return;
   175 				}
   166 				}
   183 			iRespBody->ReleaseData();
   174 			iRespBody->ReleaseData();
   184 			} break;
   175 			} break;
   185 		case THTTPEvent::EResponseComplete:
   176 		case THTTPEvent::EResponseComplete:
   186 			{
   177 			{
   187 			// The transaction's response is complete
   178 			// The transaction's response is complete
   188 
       
   189 			DP("Transaction Complete");
   179 			DP("Transaction Complete");
   190 			DP("Closing file");
   180 			DP("Closing file");
       
   181 			iFileOpen = EFalse;
   191 			iRespBodyFile.Close();
   182 			iRespBodyFile.Close();
   192 			} break;
   183 			} break;
   193 		case THTTPEvent::ESucceeded:
   184 		case THTTPEvent::ESucceeded:
   194 			{
   185 			{
   195 			DP("Transaction Successful");
   186 			DP("Transaction Successful");
       
   187 			iFileOpen = EFalse;
   196 			iRespBodyFile.Close();
   188 			iRespBodyFile.Close();
   197 			aTransaction.Close();
   189 			aTransaction.Close();
   198 			iHttpClient->ClientRequestCompleteL(KErrNone);
   190 			iHttpClient->ClientRequestCompleteL(KErrNone);
   199 			} break;
   191 			} break;
   200 		case THTTPEvent::EFailed:
   192 		case THTTPEvent::EFailed:
   201 			{
   193 			{
   202 			DP("Transaction Failed");
   194 			DP("Transaction Failed");
       
   195 			iFileOpen = EFalse;
   203 			iRespBodyFile.Close();
   196 			iRespBodyFile.Close();
   204 			aTransaction.Close();
   197 			aTransaction.Close();
   205 			
   198 			
   206 			if(iLastStatusCode == HTTPStatus::EOk || iLastStatusCode == HTTPStatus::ECreated || iLastStatusCode == HTTPStatus::EAccepted)
   199 			if(iLastStatusCode == HTTPStatus::EOk || iLastStatusCode == HTTPStatus::ECreated || iLastStatusCode == HTTPStatus::EAccepted)
   207 				{
   200 				{
   222 			{
   215 			{
   223 			DP1("<unrecognised event: %d>", aEvent.iStatus);
   216 			DP1("<unrecognised event: %d>", aEvent.iStatus);
   224 			// close off the transaction if it's an error
   217 			// close off the transaction if it's an error
   225 			if (aEvent.iStatus < 0)
   218 			if (aEvent.iStatus < 0)
   226 				{
   219 				{
       
   220 				iFileOpen = EFalse;
   227 				iRespBodyFile.Close();
   221 				iRespBodyFile.Close();
   228 				aTransaction.Close();
   222 				aTransaction.Close();
   229 				iHttpClient->ClientRequestCompleteL(aEvent.iStatus);
   223 				iHttpClient->ClientRequestCompleteL(aEvent.iStatus);
   230 				}
   224 				}
   231 			} break;
   225 			} break;