|
1 /* |
|
2 * Copyright (c) 2002-2005 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 // INCLUDE FILES |
|
29 #include <http.h> |
|
30 |
|
31 #include "sentxnstate.h" |
|
32 #include "senhttpchannel.h" |
|
33 #include "sendebug.h" |
|
34 #include "senlogger.h" |
|
35 #include "senasynctimeout.h" |
|
36 |
|
37 |
|
38 // MACROS |
|
39 |
|
40 // =========================== MEMBER FUNCTIONS =============================== |
|
41 |
|
42 // ---------------------------------------------------------------------------- |
|
43 // CSenTxnState::CSenTxnState |
|
44 // C++ default constructor can NOT contain any code, that |
|
45 // might leave. |
|
46 // ---------------------------------------------------------------------------- |
|
47 // |
|
48 CSenTxnState::CSenTxnState(MSenResponseObserver& aObserver)//, |
|
49 //RFileLogger* aLog) |
|
50 : iObserver(&aObserver), |
|
51 //iLog(aLog), |
|
52 iHasRequestBody(EFalse), |
|
53 iPostBodyReleased(EFalse) |
|
54 { |
|
55 } |
|
56 |
|
57 // ---------------------------------------------------------------------------- |
|
58 // CSenTxnState::NewLC |
|
59 // ---------------------------------------------------------------------------- |
|
60 // |
|
61 |
|
62 CSenTxnState* CSenTxnState::NewLC(MSenResponseObserver& aObserver, |
|
63 //RFileLogger* aLog, |
|
64 const TDesC8* aUri, |
|
65 const TDesC8& aContentType, |
|
66 const TDesC8* aBody) |
|
67 { |
|
68 CSenTxnState* pNew = new (ELeave) CSenTxnState(aObserver);//, aLog); |
|
69 CleanupStack::PushL(pNew); |
|
70 pNew->ConstructL(aUri, aContentType, aBody); |
|
71 return pNew; |
|
72 } |
|
73 |
|
74 // ---------------------------------------------------------------------------- |
|
75 // CSenTxnState::NewL |
|
76 // ---------------------------------------------------------------------------- |
|
77 // |
|
78 |
|
79 CSenTxnState* CSenTxnState::NewL(MSenResponseObserver& aObserver, |
|
80 //RFileLogger* aLog, |
|
81 const TDesC8* aUri, |
|
82 const TDesC8& aContentType, |
|
83 const TDesC8* aBody) |
|
84 { |
|
85 CSenTxnState* pNew = NewLC( aObserver, /*aLog,*/ aUri, |
|
86 aContentType, aBody ); |
|
87 CleanupStack::Pop(pNew); |
|
88 return pNew; |
|
89 } |
|
90 |
|
91 // ---------------------------------------------------------------------------- |
|
92 // CSenTxnState::ConstructL |
|
93 // ---------------------------------------------------------------------------- |
|
94 // |
|
95 |
|
96 void CSenTxnState::ConstructL(const TDesC8* aUri, |
|
97 const TDesC8& aContentType, |
|
98 const TDesC8* aBody) |
|
99 { |
|
100 __ASSERT_ALWAYS(aUri != NULL, |
|
101 User::Panic(KRequestUriNullPanicText, |
|
102 CSenHttpChannel::ERequestUriNull)); |
|
103 iRequestUri = aUri->AllocL(); |
|
104 |
|
105 if(aContentType.Length()>0) |
|
106 { |
|
107 if(aBody && aBody->Length() > 0) |
|
108 { |
|
109 iPostBody = aBody->AllocL(); |
|
110 iPostContentType = aContentType.AllocL(); |
|
111 iHasRequestBody = ETrue; |
|
112 } |
|
113 } |
|
114 } |
|
115 |
|
116 // ---------------------------------------------------------------------------- |
|
117 // CSenTxnState::~CSenTxnState |
|
118 // ---------------------------------------------------------------------------- |
|
119 // |
|
120 CSenTxnState::~CSenTxnState() |
|
121 { |
|
122 delete iPostBody; |
|
123 delete iRequestUri; |
|
124 delete iPostContentType; |
|
125 |
|
126 // delete allocated body |
|
127 delete iResponseBody; |
|
128 delete iTP; |
|
129 delete iTimeOut; |
|
130 } |
|
131 |
|
132 // ---------------------------------------------------------------------------- |
|
133 // CSenTxnState::Id |
|
134 // ---------------------------------------------------------------------------- |
|
135 // |
|
136 TInt CSenTxnState::Id() const |
|
137 { |
|
138 TLSLOG(KSenHttpChannelLogChannelBase , KMinLogLevel,(_L("CSenTxnState::Id"))); |
|
139 return iId; |
|
140 } |
|
141 |
|
142 // ---------------------------------------------------------------------------- |
|
143 // CSenTxnState::SetId |
|
144 // ---------------------------------------------------------------------------- |
|
145 // |
|
146 void CSenTxnState::SetId(TInt aId) |
|
147 { |
|
148 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMinLogLevel, _L8("CSenTxnState::SetId( %d )"),aId)); |
|
149 iId = aId; |
|
150 } |
|
151 |
|
152 void CSenTxnState::SetTransaction(RHTTPTransaction aTransaction) |
|
153 { |
|
154 TLSLOG_L(KSenHttpChannelLogChannelBase , KMinLogLevel,"CSenTxnState::SetTransaction"); |
|
155 ipTransaction = aTransaction; |
|
156 } |
|
157 |
|
158 RHTTPTransaction CSenTxnState::Transaction() const |
|
159 { |
|
160 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMinLogLevel, _L8("CSenTxnState::Transaction"))); |
|
161 return ipTransaction; |
|
162 } |
|
163 |
|
164 TInt CSenTxnState::Cancel() |
|
165 { |
|
166 if (iTimeOut) |
|
167 iTimeOut->Cancel(); |
|
168 ipTransaction.Cancel(); |
|
169 return KErrNone; |
|
170 } |
|
171 void CSenTxnState::SetSession(RHTTPSession aSession) |
|
172 { |
|
173 TLSLOG_L(KSenHttpChannelLogChannelBase , KSenHttpChannelLogLevel,"CSenTxnState::SetSession"); |
|
174 iSession = aSession; |
|
175 } |
|
176 |
|
177 // ---------------------------------------------------------------------------- |
|
178 // CSenTxnState::CollectResponseBodyL |
|
179 // ---------------------------------------------------------------------------- |
|
180 // |
|
181 void CSenTxnState::CollectResponseBodyL(const TDesC8& aDataPart) |
|
182 { |
|
183 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMinLogLevel, _L8("CSenTxnState(%d)::CollectResponseBodyL"),iId)); |
|
184 // Allocate memory for the data |
|
185 if(!iResponseBody) |
|
186 { |
|
187 // this is the first call so create the buffer to retain response data |
|
188 iResponseBody = HBufC8::NewMaxL(0); |
|
189 } |
|
190 |
|
191 iResponseBody = iResponseBody->ReAllocL(iResponseBody->Length() + |
|
192 aDataPart.Length()); |
|
193 |
|
194 // Note: iResponseContent will be delete when this |
|
195 // Transaction (txn) is oved (deleted) |
|
196 |
|
197 // Append the new data to the buffer |
|
198 TPtr8 ptrResponseBody = iResponseBody->Des(); |
|
199 ptrResponseBody.Append(aDataPart); |
|
200 iDetails.iTotalBytesRecieved += iResponseBody->Size(); |
|
201 } |
|
202 |
|
203 // ---------------------------------------------------------------------------- |
|
204 // CSenTxnState::RequestUri |
|
205 // ---------------------------------------------------------------------------- |
|
206 // |
|
207 const TDesC8& CSenTxnState::RequestUri() const |
|
208 { |
|
209 TLSLOG(KSenHttpChannelLogChannelBase , KNormalLogLevel,(_L("CSenTxnState::RequestUri"))); |
|
210 __ASSERT_ALWAYS(iRequestUri != NULL, |
|
211 User::Panic(KRequestUriNullPanicText, |
|
212 CSenHttpChannel::ERequestUriNull)); |
|
213 return *iRequestUri; |
|
214 } |
|
215 |
|
216 // ---------------------------------------------------------------------------- |
|
217 // CSenTxnState::PostContentType |
|
218 // ---------------------------------------------------------------------------- |
|
219 // |
|
220 const TDesC8& CSenTxnState::PostContentType() const |
|
221 { |
|
222 TLSLOG(KSenHttpChannelLogChannelBase , KMinLogLevel,(_L("CSenTxnState::PostContentType"))); |
|
223 __ASSERT_ALWAYS(iPostContentType != NULL, |
|
224 User::Panic(KPostContTypeNullPanicText, |
|
225 CSenHttpChannel::EPostContentTypeNull)); |
|
226 return *iPostContentType; |
|
227 } |
|
228 |
|
229 // ---------------------------------------------------------------------------- |
|
230 // CSenTxnState::SetContentTypeHeaderL |
|
231 // ---------------------------------------------------------------------------- |
|
232 // |
|
233 void CSenTxnState::SetContentTypeHeaderL(const RHTTPSession aSession, RHTTPHeaders aHeaders) |
|
234 { |
|
235 RStringF contTypeStr = aSession.StringPool().OpenFStringL(PostContentType()); |
|
236 CleanupClosePushL(contTypeStr); |
|
237 THTTPHdrVal contType(contTypeStr); |
|
238 aHeaders.SetFieldL(aSession.StringPool().StringF( |
|
239 HTTP::EContentType, RHTTPSession::GetTable()), contType); |
|
240 // Close stringpool string |
|
241 CleanupStack::PopAndDestroy(); // contTypeStr |
|
242 } |
|
243 |
|
244 // ---------------------------------------------------------------------------- |
|
245 // CSenTxnState::ResponseReceivedL |
|
246 // ---------------------------------------------------------------------------- |
|
247 // |
|
248 void CSenTxnState::ResponseReceivedL( const TDesC8& aContentType ) |
|
249 { |
|
250 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMinLogLevel, _L8("CSenTxnState(%d)::ResponseReceivedL"), iId)); |
|
251 // Find the header size for the response aswell |
|
252 RHTTPHeaders responseHdr = ipTransaction.Response().GetHeaderCollection(); |
|
253 TInt responseHeaderSize = CalculateHeadersSizeL(responseHdr); |
|
254 iDetails.iTotalBytesRecieved += responseHeaderSize ; |
|
255 iObserver->SetTrafficDetails(iDetails) ; |
|
256 |
|
257 if( iResponseBody ) |
|
258 { |
|
259 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMaxLogLevel, _L8("- iResponseBody:"))); |
|
260 TLSLOG_ALL(KSenHttpChannelLogChannelBase , KMaxLogLevel,(iResponseBody->Des())); |
|
261 HBufC8* pResponse = iResponseBody; |
|
262 iResponseBody = NULL; |
|
263 iObserver->ResponseReceivedL( iId, &aContentType, pResponse ); |
|
264 } |
|
265 else |
|
266 { |
|
267 TLSLOG_L(KSenHttpChannelLogChannelBase , KNormalLogLevel,"CSenTxnState::ResponseReceivedL - NULL response received!"); |
|
268 iObserver->ResponseReceivedL( iId, &aContentType, NULL ); |
|
269 } |
|
270 } |
|
271 MSenProperties& CSenTxnState::HttpChannelPropertiesL() |
|
272 { |
|
273 return iObserver->PropertiesL(); |
|
274 } |
|
275 |
|
276 void CSenTxnState::SetTP(CSenHttpTransportProperties* tp) |
|
277 { |
|
278 |
|
279 delete iTP; |
|
280 iTP = tp; |
|
281 } |
|
282 TInt CSenTxnState::CalculateHeadersSizeL(RHTTPHeaders aHeaders) |
|
283 { |
|
284 TInt headersize(0); |
|
285 TBuf8<1024> buf; |
|
286 TPtrC8 fieldValue(buf.Ptr()); |
|
287 TInt err(0); |
|
288 _LIT8(KColon,": "); |
|
289 _LIT8(KNewLine,"\r\n"); |
|
290 |
|
291 RStringPool stringPool; |
|
292 // Store the string pool for this HTTP session |
|
293 stringPool = iSession.StringPool(); |
|
294 |
|
295 THTTPHdrFieldIter fields = aHeaders.Fields(); |
|
296 fields.First(); |
|
297 while (!fields.AtEnd()) |
|
298 { |
|
299 RStringF str = stringPool.StringF(fields()); |
|
300 |
|
301 headersize += str.DesC().Size(); |
|
302 |
|
303 headersize += KColon().Size(); |
|
304 |
|
305 err = aHeaders.GetRawField(str, fieldValue); |
|
306 |
|
307 headersize += fieldValue.Size(); |
|
308 headersize += KNewLine().Size(); |
|
309 |
|
310 ++fields; |
|
311 } |
|
312 |
|
313 return headersize; |
|
314 } |
|
315 // ---------------------------------------------------------------------------- |
|
316 // CSenTxnState::ResponseErrorL |
|
317 // ---------------------------------------------------------------------------- |
|
318 // |
|
319 void CSenTxnState::ResponseErrorL(TInt aError) |
|
320 { |
|
321 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMinLogLevel, _L8("CSenTxnState(%d)::ResponseErrorL( %d )"), iId, aError)); |
|
322 RHTTPHeaders ResponseHdr = ipTransaction.Response().GetHeaderCollection(); |
|
323 TInt ResponseHeaderSize = CalculateHeadersSizeL(ResponseHdr); |
|
324 iDetails.iTotalBytesRecieved += ResponseHeaderSize ; |
|
325 iObserver->SetTrafficDetails(iDetails) ; |
|
326 if(iResponseBody) |
|
327 { |
|
328 HBufC8* pError = iResponseBody; |
|
329 iResponseBody = NULL; |
|
330 iObserver->ResponseErrorL(iId, aError, pError, iTP); |
|
331 } |
|
332 else |
|
333 { |
|
334 TLSLOG_L(KSenHttpChannelLogChannelBase , KMinLogLevel,"CSenTxnState::ResponseErrorL - NULL error response received!"); |
|
335 iObserver->ResponseErrorL(iId, aError, NULL); |
|
336 } |
|
337 } |
|
338 void CSenTxnState::StateChanged(TInt aState) // Propagate http status code to remote service consumer |
|
339 { |
|
340 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KMinLogLevel, _L8("CSenTxnState(%d)::StateChanged( %d )"), iId, aState)); |
|
341 iObserver->StateChanged(iId,aState); |
|
342 } |
|
343 |
|
344 // ---------------------------------------------------------------------------- |
|
345 // CSenTxnState::GetNextDataPart |
|
346 // Implementation of the pure virtual method from MHTTPDataSupplier |
|
347 // ---------------------------------------------------------------------------- |
|
348 // |
|
349 TBool CSenTxnState::GetNextDataPart(TPtrC8& aDataPart) |
|
350 { |
|
351 TLSLOG(KSenHttpChannelLogChannelBase , KMinLogLevel,(_L("CSenTxnState::GetNextDataPart"))); |
|
352 if(iPostBody && (!iPostBodyReleased)) |
|
353 { |
|
354 aDataPart.Set(*iPostBody); |
|
355 } |
|
356 else |
|
357 { |
|
358 aDataPart.Set(NULL, 0); |
|
359 } |
|
360 iDetails.iTotalBytesSent += aDataPart.Size(); |
|
361 |
|
362 return ETrue; |
|
363 } |
|
364 |
|
365 // ---------------------------------------------------------------------------- |
|
366 // CSenTxnState::ReleaseData |
|
367 // Implementation of the pure virtual method from MHTTPDataSupplier |
|
368 // ---------------------------------------------------------------------------- |
|
369 // |
|
370 void CSenTxnState::ReleaseData() |
|
371 { |
|
372 TLSLOG(KSenHttpChannelLogChannelBase , KMinLogLevel,(_L("CSenTxnState::ReleaseData"))); |
|
373 // Don't delete the submit buffer here because call to |
|
374 // Reset always needs to start from the beginning of the buffer. |
|
375 iPostBodyReleased = ETrue; |
|
376 } |
|
377 |
|
378 // ---------------------------------------------------------------------------- |
|
379 // CSenTxnState::OverallDataSize |
|
380 // Implementation of the pure virtual method from MHTTPDataSupplier |
|
381 // ---------------------------------------------------------------------------- |
|
382 // |
|
383 TInt CSenTxnState::OverallDataSize() |
|
384 { |
|
385 TInt size = 0; |
|
386 if(iPostBody) |
|
387 { |
|
388 size = iPostBody->Length(); |
|
389 } |
|
390 TLSLOG_FORMAT((KSenHttpChannelLogChannelBase , KNormalLogLevel, _L8("CSenTxnState::OverallDataSize() [%d]"), size)); |
|
391 return size; |
|
392 } |
|
393 |
|
394 // ---------------------------------------------------------------------------- |
|
395 // CSenTxnState::Reset |
|
396 // Implementation of the pure virtual method from MHTTPDataSupplier |
|
397 // ---------------------------------------------------------------------------- |
|
398 // |
|
399 TInt CSenTxnState::Reset() |
|
400 { |
|
401 TLSLOG(KSenHttpChannelLogChannelBase , KMinLogLevel,(_L("CSenTxnState::Reset"))); |
|
402 iPostBodyReleased = EFalse; |
|
403 return KErrNone; |
|
404 } |
|
405 |
|
406 // ---------------------------------------------------------------------------- |
|
407 // CSenTxnState::Body |
|
408 // Returns reference to txn body, or KNullDesC8, if |
|
409 // body has not been set. |
|
410 // ---------------------------------------------------------------------------- |
|
411 // |
|
412 TPtrC8 CSenTxnState::Body() |
|
413 { |
|
414 if(iPostBody) |
|
415 { |
|
416 return *iPostBody; |
|
417 } |
|
418 else |
|
419 { |
|
420 return KNullDesC8(); |
|
421 } |
|
422 } |
|
423 |
|
424 void CSenTxnState::TransformBodyToUriL() |
|
425 { |
|
426 if(!iPostBody || iPostBody->Length()==0) |
|
427 { |
|
428 return; // nothing to do |
|
429 } |
|
430 |
|
431 TPtrC8 uri(KNullDesC8); |
|
432 if(iRequestUri) |
|
433 { |
|
434 uri.Set(*iRequestUri); |
|
435 } |
|
436 |
|
437 HBufC8* pUri = HBufC8::NewLC(uri.Length()+iPostBody->Length()); |
|
438 TPtr8 ptrUri = pUri->Des(); |
|
439 ptrUri.Append(uri); |
|
440 ptrUri.Append(*iPostBody); |
|
441 |
|
442 // replace current uri with the new one: |
|
443 delete iRequestUri; |
|
444 iRequestUri = pUri; |
|
445 CleanupStack::Pop(); |
|
446 |
|
447 delete iPostBody; |
|
448 iPostBody = NULL; |
|
449 iHasRequestBody = EFalse; |
|
450 TLSLOG_L(KSenHttpChannelLogChannelBase , KMaxLogLevel,"CSenTxnState::TransformBodyToUriL():"); |
|
451 |
|
452 TLSLOG_ALL(KSenHttpChannelLogChannelBase , KMaxLogLevel,(*iRequestUri)); |
|
453 } |
|
454 |
|
455 void CSenTxnState::EnableTimeOutL(TInt aTimeOutSec) |
|
456 { |
|
457 delete iTimeOut; |
|
458 iTimeOut = NULL; |
|
459 iTimeOut = CSenAsyncTimeOut::NewL(this); |
|
460 iTimeOut->StartL(aTimeOutSec); |
|
461 } |
|
462 |
|
463 void CSenTxnState::DisableTimeOutL() |
|
464 { |
|
465 delete iTimeOut; |
|
466 iTimeOut = NULL; |
|
467 } |
|
468 // End of File |