|
1 /* |
|
2 * Copyright (c) 2006-2009 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: HttpHandler is used for HTTP connection components testing |
|
15 * |
|
16 */ |
|
17 |
|
18 // INCLUDE FILES |
|
19 #include <commdb.h> |
|
20 |
|
21 #include "HttpHandler.h" |
|
22 #include "uinotify.h" |
|
23 #include "SettingData.h" |
|
24 #include "Utils.h" |
|
25 #include "ConnTest.pan" |
|
26 #include "conntest.hrh" |
|
27 |
|
28 // CONSTANTS |
|
29 |
|
30 // Size of buffer used when submitting request bodies |
|
31 //const TInt KMaxSubmitSize = 2048; |
|
32 const TInt KMaxHeaderNameLen = 32; |
|
33 const TInt KMaxHeaderValueLen = 128; |
|
34 |
|
35 |
|
36 |
|
37 // ================= MEMBER FUNCTIONS ======================= |
|
38 |
|
39 |
|
40 |
|
41 // Standard headers used by default |
|
42 _LIT8(KUserAgent, "User-Agent: Nokia6600/1.0"); |
|
43 _LIT8(KAccept, "*/*"); |
|
44 |
|
45 |
|
46 enum THttpExampleClientPanics |
|
47 { |
|
48 EReqBodySumitBufferNotAllocated, |
|
49 KBodyWithInvalidSize, |
|
50 KCouldntNotifyBodyDataPart |
|
51 }; |
|
52 |
|
53 |
|
54 |
|
55 //============================================================ |
|
56 // |
|
57 // Implementation of CHttpClient |
|
58 // |
|
59 //============================================================ |
|
60 |
|
61 |
|
62 |
|
63 // ---------------------------------------------------------------------------- |
|
64 // CHttpClient::CHttpClient(MUINotify& aConsole) |
|
65 // Constructor |
|
66 // ---------------------------------------------------------------------------- |
|
67 // |
|
68 CHttpClient::CHttpClient(MUINotify& aConsole) |
|
69 : iReqBodySubmitBufferPtr(0,0), iConsole(aConsole) |
|
70 { |
|
71 } |
|
72 |
|
73 |
|
74 // ---------------------------------------------------------------------------- |
|
75 // CHttpClient::~CHttpClient() |
|
76 // Destructor |
|
77 // ---------------------------------------------------------------------------- |
|
78 // |
|
79 CHttpClient::~CHttpClient() |
|
80 { |
|
81 delete iReqBodySubmitBuffer; |
|
82 delete iTransObs; |
|
83 iHttpSession.Close(); |
|
84 } |
|
85 |
|
86 |
|
87 // ---------------------------------------------------------------------------- |
|
88 // CHttpClient::NewLC(MUINotify& aConsole) |
|
89 // Two-phase constructor |
|
90 // ---------------------------------------------------------------------------- |
|
91 // |
|
92 CHttpClient* CHttpClient::NewLC(MUINotify& aConsole) |
|
93 { |
|
94 CHttpClient* me = new(ELeave) CHttpClient(aConsole); |
|
95 CleanupStack::PushL(me); |
|
96 me->ConstructL(); |
|
97 return me; |
|
98 } |
|
99 |
|
100 // ---------------------------------------------------------------------------- |
|
101 // CHttpClient::NewL(MUINotify& aConsole) |
|
102 // Two-phase constructor |
|
103 // ---------------------------------------------------------------------------- |
|
104 // |
|
105 CHttpClient* CHttpClient::NewL(MUINotify& aConsole) |
|
106 { |
|
107 CHttpClient* me = NewLC(aConsole); |
|
108 CleanupStack::Pop(me); |
|
109 return me; |
|
110 } |
|
111 |
|
112 // ---------------------------------------------------------------------------- |
|
113 // CHttpClient::ConstructL() |
|
114 // EPOC two-phased constructor |
|
115 // ---------------------------------------------------------------------------- |
|
116 // |
|
117 void CHttpClient::ConstructL() |
|
118 { |
|
119 iHttpSession.OpenL(); |
|
120 |
|
121 // Install this class as the callback for authentication requests |
|
122 InstallAuthenticationL(iHttpSession); |
|
123 |
|
124 iTransObs = CHttpEventHandler::NewL(iConsole); |
|
125 } |
|
126 |
|
127 |
|
128 // ---------------------------------------------------------------------------- |
|
129 // CHttpClient::SetHttpConnectionInfo() |
|
130 // Set RConnection and RSocketServ as session properties, if |
|
131 // http fw does not use it's own connection. |
|
132 // ---------------------------------------------------------------------------- |
|
133 // |
|
134 void CHttpClient::SetHttpConnectionInfoL( TBool aUseOwnConnection, |
|
135 RConnection& aConnection, |
|
136 RSocketServ& aSocketServ ) |
|
137 { |
|
138 TInt result; |
|
139 TBuf<16> serviceType; |
|
140 TUint32 serviceId; |
|
141 TBuf<100> query; |
|
142 TBuf<100> proxyAddr; |
|
143 TBuf8<100> proxyAddr2; |
|
144 TUint32 proxyPort; |
|
145 TUint connCount; |
|
146 CCommsDatabase* TheDb; |
|
147 RStringF proxyName; |
|
148 |
|
149 // Trick to get new values into use |
|
150 iHttpSession.Close(); |
|
151 iHttpSession.OpenL(); |
|
152 |
|
153 RStringPool strPool = iHttpSession.StringPool(); |
|
154 |
|
155 // Remove first session properties just in case. |
|
156 RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo(); |
|
157 |
|
158 // Clear RConnection and Socket Server instances |
|
159 connInfo.RemoveProperty(strPool.StringF(HTTP::EHttpSocketServ,RHTTPSession::GetTable())); |
|
160 connInfo.RemoveProperty(strPool.StringF(HTTP::EHttpSocketConnection,RHTTPSession::GetTable())); |
|
161 |
|
162 // Clear the proxy settings |
|
163 THTTPHdrVal proxyUsage(strPool.StringF(HTTP::EUseProxy,RHTTPSession::GetTable())); |
|
164 connInfo.RemoveProperty(strPool.StringF(HTTP::EProxyUsage,RHTTPSession::GetTable())); |
|
165 connInfo.RemoveProperty(strPool.StringF(HTTP::EProxyAddress,RHTTPSession::GetTable())); |
|
166 |
|
167 if(!aUseOwnConnection) |
|
168 { |
|
169 // RConnection has been started, set proxy (if defined) and RConnection and |
|
170 // Socket Server session properties. |
|
171 |
|
172 // Proxy |
|
173 result = aConnection.EnumerateConnections(connCount); |
|
174 User::LeaveIfError(result); |
|
175 |
|
176 // |
|
177 // Get service and service type for this connection |
|
178 // |
|
179 query.Format(_L("%s\\%s"), IAP, IAP_SERVICE); |
|
180 result = aConnection.GetIntSetting(query, serviceId); |
|
181 |
|
182 query.Format(_L("%s\\%s"), IAP, IAP_SERVICE_TYPE); |
|
183 result = aConnection.GetDesSetting(query, serviceType); |
|
184 User::LeaveIfError(result); |
|
185 |
|
186 TheDb = CCommsDatabase::NewL(); |
|
187 CleanupStack::PushL(TheDb); |
|
188 |
|
189 CCommsDbTableView* view = TheDb->OpenViewOnProxyRecordLC(serviceId, serviceType); |
|
190 result = view->GotoFirstRecord(); |
|
191 |
|
192 if(result == KErrNone) |
|
193 { |
|
194 // This IAP uses proxy, set it to http session |
|
195 view->ReadUintL(TPtrC(PROXY_PORT_NUMBER), proxyPort); |
|
196 HBufC* k = view->ReadLongTextLC(TPtrC(PROXY_SERVER_NAME)); |
|
197 proxyAddr.Copy(k->Des()); |
|
198 proxyAddr.AppendFormat(_L(":%d"), proxyPort); |
|
199 |
|
200 proxyAddr2.Copy(proxyAddr); |
|
201 |
|
202 CleanupClosePushL(proxyName); |
|
203 proxyName = iHttpSession.StringPool().OpenFStringL(proxyAddr2); |
|
204 connInfo.SetPropertyL( strPool.StringF(HTTP::EProxyUsage,RHTTPSession::GetTable()), |
|
205 proxyUsage ); |
|
206 connInfo.SetPropertyL( strPool.StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()), |
|
207 proxyName ); |
|
208 CleanupStack::PopAndDestroy(&proxyName); // proxyName |
|
209 CleanupStack::PopAndDestroy(k); //k |
|
210 |
|
211 RDebug::Print(_L("ConnTest: Proxy address: %S"), &proxyAddr); |
|
212 } |
|
213 CleanupStack::PopAndDestroy(view); // view |
|
214 CleanupStack::PopAndDestroy(TheDb); // TheDb |
|
215 |
|
216 // RConnection and Socket Server |
|
217 connInfo.SetPropertyL ( |
|
218 strPool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), |
|
219 THTTPHdrVal (aSocketServ.Handle()) ); |
|
220 |
|
221 TInt connPtr1 = REINTERPRET_CAST(TInt, &aConnection); |
|
222 connInfo.SetPropertyL ( |
|
223 strPool.StringF(HTTP::EHttpSocketConnection, |
|
224 RHTTPSession::GetTable() ), THTTPHdrVal (connPtr1) ); |
|
225 |
|
226 } |
|
227 } |
|
228 |
|
229 |
|
230 // ---------------------------------------------------------------------------- |
|
231 // CHttpClient::InvokeHttpMethodL() |
|
232 // Create the transaction, set the headers and body and start the transaction |
|
233 // ---------------------------------------------------------------------------- |
|
234 // |
|
235 void CHttpClient::InvokeHttpMethodL(const CSettingData* aData, TBool aHasBody, TBool aIsSecure) |
|
236 { |
|
237 |
|
238 iSettingData = (CSettingData*)aData; |
|
239 |
|
240 RStringPool strPool = iHttpSession.StringPool(); |
|
241 RStringF method = strPool.StringF(HTTP::EGET,RHTTPSession::GetTable()); |
|
242 |
|
243 if(aHasBody) |
|
244 { |
|
245 method = strPool.StringF(HTTP::EPOST,RHTTPSession::GetTable()); |
|
246 |
|
247 delete iReqBodySubmitBuffer; |
|
248 iReqBodySubmitBuffer = NULL; |
|
249 iReqBodySubmitBuffer = HBufC8::NewMaxL(KSendDataSize); |
|
250 iReqBodySubmitBufferPtr.Set(iReqBodySubmitBuffer->Des()); |
|
251 |
|
252 // Create body chunk |
|
253 Utils::CreateDataChunk(iReqBodySubmitBufferPtr, aData->iPacketSize); |
|
254 iDataChunkCount = 0; |
|
255 } |
|
256 else |
|
257 { |
|
258 method = strPool.StringF(HTTP::EGET,RHTTPSession::GetTable()); |
|
259 } |
|
260 |
|
261 TBuf8<256> aUri; |
|
262 |
|
263 if(aIsSecure) |
|
264 aUri.Copy(_L8("https://")); |
|
265 else |
|
266 aUri.Copy(_L8("http://")); |
|
267 |
|
268 aUri.Append(aData->iServerName); |
|
269 |
|
270 // Don't add the port for https |
|
271 if(!aIsSecure) |
|
272 aUri.AppendFormat(_L8(":%d"), aData->iPort); |
|
273 |
|
274 // Add '/' if it is not included in the given page name |
|
275 if(!((TChar)aData->iHttpPage[0] == '/')) |
|
276 { |
|
277 aUri.Append(_L8("/")); |
|
278 } |
|
279 aUri.Append(aData->iHttpPage); |
|
280 |
|
281 TUriParser8 uri; |
|
282 uri.Parse(aUri); |
|
283 iTrans = iHttpSession.OpenTransactionL(uri, *iTransObs, method); |
|
284 RHTTPHeaders hdr = iTrans.Request().GetHeaderCollection(); |
|
285 |
|
286 // Add headers appropriate to all methods |
|
287 SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent); |
|
288 SetHeaderL(hdr, HTTP::EAccept, KAccept); |
|
289 |
|
290 if (aHasBody) |
|
291 { |
|
292 // Content type header |
|
293 TBuf8<KMaxContentTypeSize> contTypeBuf; |
|
294 contTypeBuf.Copy(iReqBodyContentType); |
|
295 RStringF contTypeStr = iHttpSession.StringPool().OpenFStringL(contTypeBuf); |
|
296 THTTPHdrVal contType(contTypeStr); |
|
297 hdr.SetFieldL( iHttpSession.StringPool().StringF( HTTP::EContentType, |
|
298 RHTTPSession::GetTable() ), |
|
299 contType ); |
|
300 contTypeStr.Close(); |
|
301 |
|
302 MHTTPDataSupplier* dataSupplier = this; |
|
303 iTrans.Request().SetBody(*dataSupplier); |
|
304 } |
|
305 |
|
306 iTrans.SubmitL(); |
|
307 } |
|
308 |
|
309 |
|
310 // ---------------------------------------------------------------------------- |
|
311 // CHttpClient::GetNextDataPart |
|
312 // Return next data chunk to be posted. |
|
313 // ---------------------------------------------------------------------------- |
|
314 // |
|
315 TBool CHttpClient::GetNextDataPart(TPtrC8& aDataPart) |
|
316 { |
|
317 |
|
318 aDataPart.Set(iReqBodySubmitBufferPtr); |
|
319 |
|
320 if(iDataChunkCount == 0) |
|
321 { |
|
322 iConsole.PrintNotify(_L("Sending body...\n")); |
|
323 iLastTimeStamp.UniversalTime(); |
|
324 } |
|
325 |
|
326 ++iDataChunkCount; |
|
327 iNoMoreDate = iDataChunkCount < iSettingData->iPackets ? EFalse : ETrue; |
|
328 |
|
329 return iNoMoreDate; |
|
330 } |
|
331 |
|
332 |
|
333 // ---------------------------------------------------------------------------- |
|
334 // CHttpClient::ReleaseData |
|
335 // Data has been posted, release the data chunk. |
|
336 // ---------------------------------------------------------------------------- |
|
337 // |
|
338 void CHttpClient::ReleaseData() |
|
339 { |
|
340 if (iNoMoreDate==EFalse) |
|
341 { |
|
342 TRAPD(err, iTrans.NotifyNewRequestBodyPartL()); |
|
343 if (err != KErrNone) |
|
344 User::Panic(KPanicConnTest, KCouldntNotifyBodyDataPart); |
|
345 } |
|
346 else |
|
347 { |
|
348 DisplayTimeElapsed(); |
|
349 } |
|
350 return; |
|
351 } |
|
352 |
|
353 |
|
354 // ---------------------------------------------------------------------------- |
|
355 // CHttpClient::OverallDataSize |
|
356 // Return size of the data to be posted. |
|
357 // ---------------------------------------------------------------------------- |
|
358 // |
|
359 TInt CHttpClient::OverallDataSize() |
|
360 { |
|
361 TInt size = (iSettingData->iPackets)*(iSettingData->iPacketSize); |
|
362 return size; |
|
363 } |
|
364 |
|
365 |
|
366 // ---------------------------------------------------------------------------- |
|
367 // CHttpClient::Reset() |
|
368 // Method from MHTTPDataSupplier |
|
369 // ---------------------------------------------------------------------------- |
|
370 // |
|
371 TInt CHttpClient::Reset() |
|
372 { |
|
373 return KErrNotSupported; |
|
374 } |
|
375 |
|
376 |
|
377 // ---------------------------------------------------------------------------- |
|
378 // CHttpClient::DisplayTimeElapsed() |
|
379 // Calculate and display throughput for POST. |
|
380 // ---------------------------------------------------------------------------- |
|
381 // |
|
382 void CHttpClient::DisplayTimeElapsed() |
|
383 { |
|
384 // Throughput calculation |
|
385 TInt size = OverallDataSize(); |
|
386 TBuf8<128> b(_L8("Body sent\n")); |
|
387 Utils::CalculateThroughput(b, iLastTimeStamp, size); |
|
388 |
|
389 b.Append(_L("\n\n")); |
|
390 iConsole.PrintNotify(b); |
|
391 } |
|
392 |
|
393 |
|
394 // ---------------------------------------------------------------------------- |
|
395 // CHttpClient::SetHeaderL() |
|
396 // Set HTTP request header for http fw. |
|
397 // ---------------------------------------------------------------------------- |
|
398 // |
|
399 void CHttpClient::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue) |
|
400 { |
|
401 RStringF valStr = iHttpSession.StringPool().OpenFStringL(aHdrValue); |
|
402 THTTPHdrVal val(valStr); |
|
403 aHeaders.SetFieldL(iHttpSession.StringPool().StringF(aHdrField,RHTTPSession::GetTable()), val); |
|
404 valStr.Close(); |
|
405 } |
|
406 |
|
407 |
|
408 // ---------------------------------------------------------------------------- |
|
409 // CHttpClient::SetPerformance |
|
410 // Turn on/off performance measurement |
|
411 // ---------------------------------------------------------------------------- |
|
412 // |
|
413 void CHttpClient::SetPerformance(const TBool aValue) |
|
414 { |
|
415 iDoPerformance = aValue; |
|
416 iTransObs->SetPerformance(aValue); |
|
417 } |
|
418 |
|
419 |
|
420 // ---------------------------------------------------------------------------- |
|
421 // CHttpClient::GetCredentialsL() |
|
422 // Called when a authenticated page is requested. Asks the user for a username |
|
423 // and password that would be appropriate for the url that was supplied. |
|
424 // ---------------------------------------------------------------------------- |
|
425 // |
|
426 TBool CHttpClient::GetCredentialsL(const TUriC8& aURI, RString aRealm, |
|
427 RStringF aAuthenticationType, |
|
428 RString& aUsername, |
|
429 RString& aPassword) |
|
430 |
|
431 { |
|
432 TBuf<80> scratch; |
|
433 TBuf8<80> scratch8; |
|
434 scratch8.Format(_L8("Enter credentials for URL %S, realm %S"), &aURI.UriDes(), &aRealm.DesC()); |
|
435 scratch.Copy(scratch8); |
|
436 // iUtils->Test().Printf(_L("%S\n"), &scratch); |
|
437 scratch.Copy(aAuthenticationType.DesC()); |
|
438 // iUtils->Test().Printf(_L("Using %S authentication\n"), &scratch); |
|
439 // iUtils->GetAnEntry(_L("Username (or QUIT to give up): "), scratch); |
|
440 scratch8.Copy(scratch); |
|
441 if (scratch8.CompareF(_L8("quit"))) |
|
442 { |
|
443 TRAPD(err, aUsername = aRealm.Pool().OpenStringL(scratch8)); |
|
444 if (!err) |
|
445 { |
|
446 // iUtils->GetAnEntry(_L("Password: "), scratch); |
|
447 scratch8.Copy(scratch); |
|
448 TRAP(err, aPassword = aRealm.Pool().OpenStringL(scratch8)); |
|
449 if (!err) |
|
450 return ETrue; |
|
451 } |
|
452 } |
|
453 return EFalse; |
|
454 } |
|
455 |
|
456 |
|
457 |
|
458 //============================================================================= |
|
459 // |
|
460 // Implementation of class CHttpEventHandler |
|
461 // |
|
462 //============================================================================= |
|
463 |
|
464 |
|
465 |
|
466 // ---------------------------------------------------------------------------- |
|
467 // CHttpEventHandler::NewLC(MUINotify& aConsole) |
|
468 // Two-phase constructor |
|
469 // ---------------------------------------------------------------------------- |
|
470 // |
|
471 CHttpEventHandler* CHttpEventHandler::NewLC(MUINotify& aConsole) |
|
472 { |
|
473 CHttpEventHandler* me = new(ELeave)CHttpEventHandler(aConsole); |
|
474 CleanupStack::PushL(me); |
|
475 me->ConstructL(); |
|
476 return me; |
|
477 } |
|
478 |
|
479 // ---------------------------------------------------------------------------- |
|
480 // CHttpEventHandler::NewL(MUINotify& aConsole) |
|
481 // Two-phase constructor |
|
482 // ---------------------------------------------------------------------------- |
|
483 // |
|
484 CHttpEventHandler* CHttpEventHandler::NewL(MUINotify& aConsole) |
|
485 { |
|
486 CHttpEventHandler* me = NewLC(aConsole); |
|
487 CleanupStack::Pop(me); |
|
488 return me; |
|
489 } |
|
490 |
|
491 // ---------------------------------------------------------------------------- |
|
492 // CHttpEventHandler::ConstructL() |
|
493 // EPOC two-phased constructor |
|
494 // ---------------------------------------------------------------------------- |
|
495 // |
|
496 void CHttpEventHandler::ConstructL() |
|
497 { |
|
498 } |
|
499 |
|
500 // ---------------------------------------------------------------------------- |
|
501 // CHttpEventHandler::CHttpEventHandler(MUINotify& aConsole) |
|
502 // Constructor |
|
503 // ---------------------------------------------------------------------------- |
|
504 // |
|
505 CHttpEventHandler::CHttpEventHandler(MUINotify& aConsole) |
|
506 : iConsole(aConsole) |
|
507 { |
|
508 } |
|
509 |
|
510 // ---------------------------------------------------------------------------- |
|
511 // CHttpEventHandler::~CHttpEventHandler() |
|
512 // Destructor |
|
513 // ---------------------------------------------------------------------------- |
|
514 // |
|
515 CHttpEventHandler::~CHttpEventHandler() |
|
516 { |
|
517 } |
|
518 |
|
519 |
|
520 // ---------------------------------------------------------------------------- |
|
521 // CHttpEventHandler::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent) |
|
522 // HTTP event receiver. |
|
523 // ---------------------------------------------------------------------------- |
|
524 // |
|
525 void CHttpEventHandler::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent) |
|
526 { |
|
527 switch (aEvent.iStatus) |
|
528 { |
|
529 case THTTPEvent::EGotResponseHeaders: |
|
530 { |
|
531 // HTTP response headers have been received. We can determine now if there is |
|
532 // going to be a response body to save. |
|
533 RHTTPResponse resp = aTransaction.Response(); |
|
534 TInt status = resp.StatusCode(); |
|
535 RStringF statusStr = resp.StatusText(); |
|
536 TBuf<32> statusStr16; |
|
537 statusStr16.Copy(statusStr.DesC()); |
|
538 TBuf<64> st; |
|
539 st.Format(_L("Status: %d (%S)\n"), status, &statusStr16); |
|
540 iConsole.PrintNotify(st); |
|
541 |
|
542 |
|
543 // Dump the headers |
|
544 if(!iDoPerformance) |
|
545 DumpRespHeadersL(aTransaction); |
|
546 |
|
547 // Note! For some reason resp.HasBody() returns False although |
|
548 // there is body (Tomcat, index.jsp), so this doesn't work. |
|
549 // Maybe it checks the Content-Length header? |
|
550 if (resp.HasBody() && (status >= 200) && (status < 300) && (status != 204)) |
|
551 { |
|
552 TInt dataSize = resp.Body()->OverallDataSize(); |
|
553 if (dataSize >= 0) |
|
554 { |
|
555 TBuf<64> st; |
|
556 st.Format(_L("Response body size is %d\n"), dataSize); |
|
557 iConsole.PrintNotify(st); |
|
558 } |
|
559 else |
|
560 { |
|
561 iConsole.PrintNotify(_L("Response body size is unknown\n")); |
|
562 } |
|
563 } |
|
564 |
|
565 if(iDoPerformance) |
|
566 { |
|
567 iConsole.PrintNotify(_L("Getting body...\n")); |
|
568 iStartTime.UniversalTime(); |
|
569 } |
|
570 iBodySize = 0; |
|
571 |
|
572 } break; |
|
573 case THTTPEvent::EGotResponseBodyData: |
|
574 { |
|
575 // Get the body data supplier |
|
576 iRespBody = aTransaction.Response().Body(); |
|
577 |
|
578 TPtrC8 dataChunk; |
|
579 iRespBody->GetNextDataPart(dataChunk); |
|
580 iBodySize += dataChunk.Length(); |
|
581 |
|
582 if(!iDoPerformance) |
|
583 DumpRespBody(aTransaction); |
|
584 |
|
585 iRespBody->ReleaseData(); |
|
586 } break; |
|
587 case THTTPEvent::EResponseComplete: |
|
588 { |
|
589 // The transaction's response is complete |
|
590 |
|
591 TBuf8<128> b(_L8("Got body\n")); |
|
592 |
|
593 if(iDoPerformance) |
|
594 Utils::CalculateThroughput(b, iStartTime, iBodySize); |
|
595 |
|
596 iConsole.PrintNotify(b); |
|
597 |
|
598 } break; |
|
599 case THTTPEvent::ESucceeded: |
|
600 { |
|
601 iConsole.PrintNotify(_L("Transaction Successful\n")); |
|
602 aTransaction.Close(); |
|
603 //CActiveScheduler::Stop(); |
|
604 } break; |
|
605 case THTTPEvent::EFailed: |
|
606 { |
|
607 iConsole.PrintNotify(_L("Transaction Failed\n")); |
|
608 aTransaction.Close(); |
|
609 //CActiveScheduler::Stop(); |
|
610 } break; |
|
611 case THTTPEvent::ERedirectedPermanently: |
|
612 { |
|
613 iConsole.PrintNotify(_L("Permanent Redirection\n")); |
|
614 } break; |
|
615 case THTTPEvent::ERedirectedTemporarily: |
|
616 { |
|
617 iConsole.PrintNotify(_L("Temporary Redirection\n")); |
|
618 } break; |
|
619 default: |
|
620 { |
|
621 TBuf<32> text; |
|
622 text.Format(_L("<unrecognised event: %d>\n"), aEvent.iStatus); |
|
623 iConsole.PrintNotify(text); |
|
624 // close off the transaction if it's an error |
|
625 if (aEvent.iStatus < 0) |
|
626 { |
|
627 aTransaction.Close(); |
|
628 //CActiveScheduler::Stop(); |
|
629 } |
|
630 } break; |
|
631 } |
|
632 } |
|
633 |
|
634 |
|
635 // ---------------------------------------------------------------------------- |
|
636 // CHttpEventHandler::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/) |
|
637 // Error handler |
|
638 // ---------------------------------------------------------------------------- |
|
639 // |
|
640 TInt CHttpEventHandler::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/) |
|
641 { |
|
642 TBuf<64> text; |
|
643 text.Format(_L("MHFRunError fired with error code %d\n"), aError); |
|
644 iConsole.PrintNotify(text); |
|
645 return KErrNone; |
|
646 } |
|
647 |
|
648 |
|
649 // ---------------------------------------------------------------------------- |
|
650 // CHttpEventHandler::DumpRespHeadersL(RHTTPTransaction& aTrans) |
|
651 // Print HTTP headers on console. |
|
652 // ---------------------------------------------------------------------------- |
|
653 // |
|
654 void CHttpEventHandler::DumpRespHeadersL(RHTTPTransaction& aTrans) |
|
655 { |
|
656 RHTTPResponse resp = aTrans.Response(); |
|
657 RStringPool strP = aTrans.Session().StringPool(); |
|
658 RHTTPHeaders hdr = resp.GetHeaderCollection(); |
|
659 THTTPHdrFieldIter it = hdr.Fields(); |
|
660 |
|
661 TBuf<KMaxHeaderNameLen> fieldName16; |
|
662 TBuf<KMaxHeaderValueLen> fieldVal16; |
|
663 |
|
664 while (it.AtEnd() == EFalse) |
|
665 { |
|
666 RStringTokenF fieldName = it(); |
|
667 RStringF fieldNameStr = strP.StringF(fieldName); |
|
668 THTTPHdrVal fieldVal; |
|
669 |
|
670 //TPtrC8 rawField; |
|
671 //if (hdr.GetRawField(fieldNameStr,rawField) == KErrNone) |
|
672 |
|
673 if (hdr.GetField(fieldNameStr,0,fieldVal) == KErrNone) |
|
674 { |
|
675 const TDesC8& fieldNameDesC = fieldNameStr.DesC(); |
|
676 fieldName16.Copy(fieldNameDesC.Left(KMaxHeaderNameLen)); |
|
677 switch (fieldVal.Type()) |
|
678 { |
|
679 case THTTPHdrVal::KTIntVal: |
|
680 { |
|
681 TBuf<200> a; |
|
682 a.Format(_L("%S: %d\n"), &fieldName16, fieldVal.Int()); |
|
683 iConsole.PrintNotify(a); |
|
684 } |
|
685 break; |
|
686 case THTTPHdrVal::KStrFVal: |
|
687 { |
|
688 RStringF fieldValStr = strP.StringF(fieldVal.StrF()); |
|
689 const TDesC8& fieldValDesC = fieldValStr.DesC(); |
|
690 fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen)); |
|
691 TBuf<200> a; |
|
692 a.Format(_L("%S: %S\n"), &fieldName16, &fieldVal16); |
|
693 iConsole.PrintNotify(a); |
|
694 } |
|
695 break; |
|
696 case THTTPHdrVal::KStrVal: |
|
697 { |
|
698 RString fieldValStr = strP.String(fieldVal.Str()); |
|
699 const TDesC8& fieldValDesC = fieldValStr.DesC(); |
|
700 fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen)); |
|
701 TBuf<200> a; |
|
702 a.Format(_L("%S: %S\n"), &fieldName16, &fieldVal16); |
|
703 iConsole.PrintNotify(a); |
|
704 } |
|
705 break; |
|
706 case THTTPHdrVal::KDateVal: |
|
707 { |
|
708 _LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3"); |
|
709 TDateTime date = fieldVal.DateTime(); |
|
710 TBuf<40> dateTimeString; |
|
711 TTime t(date); |
|
712 t.FormatL(dateTimeString,KDateString); |
|
713 TBuf<200> a; |
|
714 a.Format(_L("%S: %S\n"), &fieldName16, &dateTimeString); |
|
715 iConsole.PrintNotify(a); |
|
716 } |
|
717 break; |
|
718 default: |
|
719 { |
|
720 TBuf<200> a; |
|
721 a.Format(_L("%S: <unrecognised value type>\n"), &fieldName16); |
|
722 iConsole.PrintNotify(a); |
|
723 } |
|
724 break; |
|
725 } |
|
726 |
|
727 // Display realm for WWW-Authenticate header |
|
728 RStringF wwwAuth = strP.StringF(HTTP::EWWWAuthenticate,RHTTPSession::GetTable()); |
|
729 if (fieldNameStr == wwwAuth) |
|
730 { |
|
731 // check the auth scheme is 'basic' |
|
732 RStringF basic = strP.StringF(HTTP::EBasic,RHTTPSession::GetTable()); |
|
733 RStringF realm = strP.StringF(HTTP::ERealm,RHTTPSession::GetTable()); |
|
734 THTTPHdrVal realmVal; |
|
735 if ((fieldVal.StrF() == basic) && |
|
736 (!hdr.GetParam(wwwAuth, realm, realmVal))) |
|
737 { |
|
738 RStringF realmValStr = strP.StringF(realmVal.StrF()); |
|
739 fieldVal16.Copy(realmValStr.DesC()); |
|
740 TBuf<200> a; |
|
741 a.Format(_L("Realm is: %S\n"), &fieldVal16); |
|
742 iConsole.PrintNotify(a); |
|
743 } |
|
744 } |
|
745 } |
|
746 ++it; |
|
747 } |
|
748 } |
|
749 |
|
750 // ---------------------------------------------------------------------------- |
|
751 // CHttpEventHandler::DumpRespBody(RHTTPTransaction& aTrans) |
|
752 // Print body size on console. |
|
753 // ---------------------------------------------------------------------------- |
|
754 // |
|
755 void CHttpEventHandler::DumpRespBody(RHTTPTransaction& aTrans) |
|
756 { |
|
757 MHTTPDataSupplier* body = aTrans.Response().Body(); |
|
758 TPtrC8 dataChunk; |
|
759 TBool isLast = body->GetNextDataPart(dataChunk); |
|
760 |
|
761 TBuf<32> b; |
|
762 b.Format(_L("-body size: %d\n"), dataChunk.Length()); |
|
763 iConsole.PrintNotify(b); |
|
764 //DumpIt(dataChunk); |
|
765 if (isLast) |
|
766 iConsole.PrintNotify(_L("Got last data chunk.\n")); |
|
767 } |
|
768 |
|
769 |
|
770 |