|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <httpstringconstants.h> |
|
17 #include <http/thttptable.h> |
|
18 #include <stringpool.h> |
|
19 #include <utf.h> |
|
20 #include <inetprottextutils.h> |
|
21 #include <http/thttphdrval.h> |
|
22 #include <uri8.h> |
|
23 #include <http/framework/cheadercodecplugin.h> |
|
24 #include <upnp/tupnptable.h> |
|
25 |
|
26 #include "cupnprequestcomposertest.h" |
|
27 #include "rhttpheaders.h" |
|
28 |
|
29 _LIT(KFileName, "C:\\logs\\UpnpMessageTestLogs\\composedrequests.dump"); |
|
30 _LIT(KRCTest, "REQUEST_COMPOSER_TEST_1"); |
|
31 |
|
32 _LIT8(KUpnpCodecName, "HTTP/UPnP"); |
|
33 |
|
34 |
|
35 CUpnpRequestComposerTest* CUpnpRequestComposerTest::NewL() |
|
36 { |
|
37 return (new(ELeave)CUpnpRequestComposerTest()); |
|
38 } |
|
39 |
|
40 |
|
41 CUpnpRequestComposerTest::CUpnpRequestComposerTest() |
|
42 { |
|
43 SetTestStepName(KRequestComposerTest); |
|
44 } |
|
45 |
|
46 CUpnpRequestComposerTest::~CUpnpRequestComposerTest() |
|
47 { |
|
48 } |
|
49 |
|
50 TVerdict CUpnpRequestComposerTest::doTestStepPreambleL() |
|
51 { |
|
52 #ifdef __CFLOG_ACTIVE |
|
53 __CFLOG_CREATEL; |
|
54 __CFLOG_OPEN; |
|
55 #endif |
|
56 |
|
57 iSched = new(ELeave) CActiveScheduler; |
|
58 CActiveScheduler::Install(iSched); |
|
59 |
|
60 //iMBufMgr = CMBufManager::NewL(256 * 1024, iMBufSzAllctr); |
|
61 //iMBufSzAllctr->AddL(KMBuf_MBufSize, (256 * 1024/(KMBuf_MBufSize+sizeof(RMBuf)))-1, KMBuf_MinGrowth, KMBuf_GrowthThreshold); |
|
62 iMBufMgr = CChunkManager::NewL ( 256 * 1024 ); |
|
63 iMBufMgr->AddPoolL(KMBuf_MBufSize, (256 * 1024/(KMBuf_MBufSize+sizeof(RMBuf)))-1, KMBuf_MinGrowth, KMBuf_GrowthThreshold); |
|
64 |
|
65 iRequestBuffer.Create(KCMaxMsgLen); |
|
66 |
|
67 iStringPool.OpenL(TUPnPTable::Table()); |
|
68 iStringPool.OpenL(THTTPTable::Table()); |
|
69 iCodec = CHeaderCodecPlugin::NewL(KUpnpCodecName, iStringPool); |
|
70 iRequest = CRequest::NewL(*iCodec, iStringPool); |
|
71 iRequestComposer = CUpnpRequestComposer::NewL(*this); |
|
72 |
|
73 iRequestBody = NULL; |
|
74 |
|
75 return TestStepResult(); |
|
76 } |
|
77 |
|
78 TVerdict CUpnpRequestComposerTest::doTestStepL() |
|
79 { |
|
80 TBool requestBodyPresence; |
|
81 TPtrC requestMethod; |
|
82 TPtrC uri; |
|
83 TInt noOfHdrValPairs; |
|
84 TInt noOfHdrParamValPairs; |
|
85 RMemoryAllocator allocator ( iMBufMgr ); |
|
86 |
|
87 if( !GetStringFromConfig(ConfigSection(), KRequestMethod, requestMethod) || |
|
88 !GetStringFromConfig(ConfigSection(), KUri, uri) || |
|
89 !GetBoolFromConfig(ConfigSection(), KRequestBodyPresence, requestBodyPresence) || |
|
90 !GetIntFromConfig(ConfigSection(), KNoOfHdrValPairs, noOfHdrValPairs) || |
|
91 !GetIntFromConfig(ConfigSection(), KNoOfHdrParamValPairs, noOfHdrParamValPairs) |
|
92 ) |
|
93 { |
|
94 ERR_PRINTF6(_L("===> Problem in reading values from ini. \ |
|
95 \nExpected fields are: \n%S\n%S\n%S\n%S\n%S <===" |
|
96 ), &KRequestMethod, &KUri, &KRequestBodyPresence, |
|
97 &KNoOfHdrValPairs, &KNoOfHdrParamValPairs); |
|
98 |
|
99 SetTestStepResult(EFail); |
|
100 return TestStepResult(); |
|
101 } |
|
102 |
|
103 SetTestStepResult(EPass); |
|
104 |
|
105 // Construct and set the request method |
|
106 TRAPD(err, ConstructRequestMethodL(requestMethod)); |
|
107 if(err != KErrNone) |
|
108 { |
|
109 SetTestStepResult(EFail); |
|
110 return TestStepResult(); |
|
111 } |
|
112 |
|
113 // Construct and set the request uri |
|
114 TRAP(err, ConstructRequestUriL(uri)); |
|
115 if(err != KErrNone) |
|
116 { |
|
117 SetTestStepResult(EFail); |
|
118 return TestStepResult(); |
|
119 } |
|
120 |
|
121 // Construct and set the request headers |
|
122 TRAP(err, ConstructRequestHeadersL(noOfHdrValPairs, noOfHdrParamValPairs)); |
|
123 if( err != KErrNone) |
|
124 { |
|
125 SetTestStepResult(EFail); |
|
126 return TestStepResult(); |
|
127 } |
|
128 |
|
129 // Construct the request body reqd from the ini file |
|
130 TRAP(err, ConstructRequestBodyL(requestBodyPresence)); |
|
131 if(err != KErrNone) |
|
132 { |
|
133 SetTestStepResult(EFail); |
|
134 return TestStepResult(); |
|
135 } |
|
136 |
|
137 if(requestBodyPresence) |
|
138 { |
|
139 RMemChunk tmpRequestBody; |
|
140 tmpRequestBody.CreateL(*iRequestBody, allocator); |
|
141 iRequest->AddBody(this); |
|
142 } |
|
143 |
|
144 TRAPD(error, iRequestComposer->ComposeRequest(iRequest)); |
|
145 if(error != KErrNone) |
|
146 { |
|
147 SetTestStepResult(EFail); |
|
148 return TestStepResult(); |
|
149 } |
|
150 |
|
151 CRequest& request = iRequestComposer->Request(); |
|
152 CActiveScheduler::Start(); |
|
153 return TestStepResult(); |
|
154 } |
|
155 |
|
156 |
|
157 TVerdict CUpnpRequestComposerTest::doTestStepPostambleL() |
|
158 { |
|
159 if(iRequest) |
|
160 delete iRequest; |
|
161 |
|
162 iStringPool.Close(); |
|
163 |
|
164 if(iRequestComposer) |
|
165 delete iRequestComposer; |
|
166 |
|
167 if(iMBufMgr) |
|
168 delete iMBufMgr; |
|
169 |
|
170 delete iCodec; |
|
171 delete iSched; |
|
172 |
|
173 if(iRequestBody) |
|
174 delete iRequestBody; |
|
175 |
|
176 iRequestBuffer.Close(); |
|
177 |
|
178 #ifdef __CFLOG_ACTIVE |
|
179 __CFLOG_CLOSE; |
|
180 __CFLOG_DELETE; |
|
181 #endif |
|
182 |
|
183 REComSession::FinalClose(); |
|
184 return TestStepResult(); |
|
185 } |
|
186 |
|
187 |
|
188 void CUpnpRequestComposerTest::ConstructRequestMethodL(const TDesC& aRequestMethod) |
|
189 { |
|
190 RRequest request = iRequest->Handle(); |
|
191 |
|
192 // set the request method |
|
193 HBufC8* requestMethod8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(aRequestMethod); |
|
194 CleanupStack::PushL(requestMethod8); |
|
195 RStringF method = iStringPool.OpenFStringL(requestMethod8->Des()); |
|
196 request.SetMethod(method); |
|
197 method.Close(); |
|
198 CleanupStack::PopAndDestroy(requestMethod8); |
|
199 } |
|
200 |
|
201 |
|
202 void CUpnpRequestComposerTest::ConstructRequestUriL(const TDesC& aRequestUri) |
|
203 { |
|
204 RRequest request = iRequest->Handle(); |
|
205 |
|
206 // set the request Uri |
|
207 HBufC8* uri8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(aRequestUri); |
|
208 CleanupStack::PushL(uri8); |
|
209 TUriParser8 uri; |
|
210 uri.Parse(uri8->Des()); |
|
211 request.SetURIL(uri); |
|
212 CleanupStack::PopAndDestroy(uri8); |
|
213 } |
|
214 |
|
215 |
|
216 void CUpnpRequestComposerTest::ConstructRequestHeadersL(const TInt aNoOfHdrValPairs, const TInt aNoOfHdrParamValPairs) |
|
217 { |
|
218 // Create the "Header: Value" pair array. |
|
219 TInt headersCounter = 1; |
|
220 while(headersCounter <= aNoOfHdrValPairs) |
|
221 { |
|
222 TPtrC headerValuePair; |
|
223 TBuf<20> buf(KNullDesC); |
|
224 |
|
225 // The below line is to create formats such as |
|
226 // HeaderValuePair1, HeaderValuePair2... |
|
227 buf.Format(KHeaderValuePair, headersCounter); |
|
228 |
|
229 GetStringFromConfig(ConfigSection(), buf, headerValuePair); |
|
230 HBufC8* headerValuePairBuf; |
|
231 headerValuePairBuf = CnvUtfConverter::ConvertFromUnicodeToUtf8L(headerValuePair); |
|
232 CleanupStack::PushL(headerValuePairBuf); |
|
233 TPtrC8 headerValPairPtr(headerValuePairBuf->Des()); |
|
234 TInt consumed = headerValPairPtr.Locate(colon); |
|
235 User::LeaveIfError(consumed); |
|
236 |
|
237 // Get the header. i.e, the string before the ':' |
|
238 TPtrC8 header = headerValPairPtr.Left(consumed); |
|
239 |
|
240 if(header.Compare(_L8("EXT")) == 0) |
|
241 { |
|
242 SetHeaderL(header, KNullDesC8); |
|
243 } |
|
244 else |
|
245 { |
|
246 headerValPairPtr.Set(headerValPairPtr.Mid(consumed+1)); |
|
247 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(headerValPairPtr, InetProtTextUtils::ERemoveBoth)); |
|
248 |
|
249 // The left over data is the value for the header. |
|
250 SetHeaderL(header, headerValPairPtr); |
|
251 } |
|
252 CleanupStack::PopAndDestroy(headerValuePairBuf); |
|
253 headersCounter++; |
|
254 } |
|
255 |
|
256 // Create the "Header: param=value" array. |
|
257 TInt headerParamsCounter = 1; |
|
258 while(headerParamsCounter <= aNoOfHdrParamValPairs) |
|
259 { |
|
260 TPtrC headerParamValPair; |
|
261 TBuf<30> buf(KNullDesC); |
|
262 |
|
263 // The below line is to create formats such as |
|
264 // HeaderParamValPair1, HeaderParamValPair2... |
|
265 buf.Format(KHeaderParamValPair, headerParamsCounter); |
|
266 |
|
267 GetStringFromConfig(ConfigSection(), buf, headerParamValPair); |
|
268 HBufC8* hdrParamValPairBuf; |
|
269 hdrParamValPairBuf = CnvUtfConverter::ConvertFromUnicodeToUtf8L(headerParamValPair); |
|
270 CleanupStack::PushL(hdrParamValPairBuf); |
|
271 |
|
272 TPtrC8 hdrParamValPairPtr(hdrParamValPairBuf->Des()); |
|
273 TInt consumed = hdrParamValPairPtr.Locate(colon); |
|
274 User::LeaveIfError(consumed); |
|
275 |
|
276 // Get the header. i.e, the string before the ':' |
|
277 TPtrC8 header = hdrParamValPairPtr.Left(consumed); |
|
278 |
|
279 hdrParamValPairPtr.Set(hdrParamValPairPtr.Mid(consumed+1)); |
|
280 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(hdrParamValPairPtr, InetProtTextUtils::ERemoveBoth)); |
|
281 |
|
282 // The left over data has the "param=value" pair |
|
283 consumed = hdrParamValPairPtr.Locate(equal); |
|
284 User::LeaveIfError(consumed); |
|
285 |
|
286 // Get the param name. i.e, the string before the '=' |
|
287 TPtrC8 param = hdrParamValPairPtr.Left(consumed); |
|
288 |
|
289 hdrParamValPairPtr.Set(hdrParamValPairPtr.Mid(consumed+1)); |
|
290 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(hdrParamValPairPtr, InetProtTextUtils::ERemoveBoth)); |
|
291 |
|
292 consumed = hdrParamValPairPtr.Locate(partIdx); |
|
293 |
|
294 TPtrC8 paramVal = hdrParamValPairPtr.Left(consumed-1); |
|
295 |
|
296 hdrParamValPairPtr.Set(hdrParamValPairPtr.Mid(consumed+1)); |
|
297 |
|
298 // hdrParamValPairPtr now has the part index. Convert it to Integer |
|
299 TInt partIndex; |
|
300 InetProtTextUtils::ConvertDescriptorToInt(hdrParamValPairPtr, partIndex); |
|
301 |
|
302 if( partIndex > (aNoOfHdrValPairs-1) ) |
|
303 { |
|
304 ERR_PRINTF1(_L("===> Illegal index value for the parameter value <====")); |
|
305 User::Leave(KErrGeneral); |
|
306 } |
|
307 |
|
308 SetParamL(header, param, paramVal, partIndex); |
|
309 |
|
310 CleanupStack::PopAndDestroy(hdrParamValPairBuf); |
|
311 headerParamsCounter++; |
|
312 } |
|
313 } |
|
314 |
|
315 void CUpnpRequestComposerTest::SetHeaderL(const TDesC8& aHeader, const TDesC8& aValue) |
|
316 { |
|
317 RRequest request = iRequest->Handle(); |
|
318 RHTTPHeaders headers = request.GetHeaderCollection(); |
|
319 THTTPHdrVal val; |
|
320 |
|
321 RStringF headerSF = iStringPool.OpenFStringL(aHeader); |
|
322 CleanupClosePushL(headerSF); |
|
323 |
|
324 if( (headerSF == iStringPool.StringF(UPnP::EMX, TUPnPTable::Table())) || (headerSF == iStringPool.StringF(HTTP::EContentLength, THTTPTable::Table())) || (headerSF == iStringPool.StringF(UPnP::ETimeout, TUPnPTable::Table()))) |
|
325 { |
|
326 TInt intVal; |
|
327 InetProtTextUtils::ConvertDescriptorToInt(aValue, intVal); |
|
328 val.SetInt(intVal); |
|
329 headers.SetFieldL(headerSF, val); |
|
330 } |
|
331 |
|
332 else |
|
333 { |
|
334 RStringF ValSF = iStringPool.OpenFStringL(aValue); |
|
335 CleanupClosePushL(ValSF); |
|
336 |
|
337 val.SetStrF(ValSF); |
|
338 headers.SetFieldL(headerSF, val); |
|
339 |
|
340 CleanupStack::PopAndDestroy(&ValSF); |
|
341 } |
|
342 |
|
343 CleanupStack::PopAndDestroy(&headerSF); |
|
344 } |
|
345 |
|
346 |
|
347 void CUpnpRequestComposerTest::SetParamL(const TDesC8& aHeader, const TDesC8& aParam, const TDesC8& aValue, const TInt aPartIndex) |
|
348 { |
|
349 RRequest request = iRequest->Handle(); |
|
350 RHTTPHeaders headers = request.GetHeaderCollection(); |
|
351 |
|
352 RStringF headerSF = iStringPool.OpenFStringL(aHeader); |
|
353 CleanupClosePushL(headerSF); |
|
354 RStringF paramSF = iStringPool.OpenFStringL(aParam); |
|
355 CleanupClosePushL(paramSF); |
|
356 |
|
357 THTTPHdrVal val; |
|
358 |
|
359 if((aParam.Compare(iStringPool.StringF(HTTP::EQ, THTTPTable::Table()).DesC()) == 0)) |
|
360 { |
|
361 if((aValue.Compare(_L8("0.1"))) == 0) |
|
362 { |
|
363 val.SetInt(THTTPHdrVal::TQConv(0.5)); |
|
364 } |
|
365 else if((aValue.Compare(_L8("0.2"))) == 0) |
|
366 { |
|
367 val.SetInt(THTTPHdrVal::TQConv(0.2)); |
|
368 } |
|
369 else if((aValue.Compare(_L8("0.3"))) == 0) |
|
370 { |
|
371 val.SetInt(THTTPHdrVal::TQConv(0.3)); |
|
372 } |
|
373 else if((aValue.Compare(_L8("0.4"))) == 0) |
|
374 { |
|
375 val.SetInt(THTTPHdrVal::TQConv(0.4)); |
|
376 } |
|
377 else if((aValue.Compare(_L8("0.5"))) == 0) |
|
378 { |
|
379 val.SetInt(THTTPHdrVal::TQConv(0.5)); |
|
380 } |
|
381 else if((aValue.Compare(_L8("0.6"))) == 0) |
|
382 { |
|
383 val.SetInt(THTTPHdrVal::TQConv(0.6)); |
|
384 } |
|
385 else if((aValue.Compare(_L8("0.7"))) == 0) |
|
386 { |
|
387 val.SetInt(THTTPHdrVal::TQConv(0.7)); |
|
388 } |
|
389 else if((aValue.Compare(_L8("0.8"))) == 0) |
|
390 { |
|
391 val.SetInt(THTTPHdrVal::TQConv(0.8)); |
|
392 } |
|
393 else if((aValue.Compare(_L8("0.9"))) == 0) |
|
394 { |
|
395 val.SetInt(THTTPHdrVal::TQConv(0.9)); |
|
396 } |
|
397 |
|
398 headers.SetParamL(headerSF, paramSF, val, aPartIndex); |
|
399 CleanupStack::PopAndDestroy(¶mSF); |
|
400 CleanupStack::PopAndDestroy(&headerSF); |
|
401 } |
|
402 |
|
403 else |
|
404 { |
|
405 RStringF valueSF = iStringPool.OpenFStringL(aValue); |
|
406 CleanupClosePushL(valueSF); |
|
407 val.SetStrF(valueSF); |
|
408 |
|
409 headers.SetParamL(headerSF, paramSF, val, aPartIndex); |
|
410 |
|
411 CleanupStack::PopAndDestroy(&valueSF); |
|
412 CleanupStack::PopAndDestroy(¶mSF); |
|
413 CleanupStack::PopAndDestroy(&headerSF); |
|
414 } |
|
415 } |
|
416 |
|
417 |
|
418 void CUpnpRequestComposerTest::ConstructRequestBodyL(const TBool aRequestBodyPresence) |
|
419 { |
|
420 TPtrC body; |
|
421 if(iRequestBody) |
|
422 { |
|
423 delete iRequestBody; |
|
424 iRequestBody = NULL; |
|
425 } |
|
426 |
|
427 if(aRequestBodyPresence) |
|
428 { |
|
429 if(!GetStringFromConfig(ConfigSection(), KBody, body)) |
|
430 { |
|
431 ERR_PRINTF1(_L("===> Problem in reading request body from ini file <====")); |
|
432 SetTestStepResult(EFail); |
|
433 } |
|
434 iRequestBody = CnvUtfConverter::ConvertFromUnicodeToUtf8L(body); |
|
435 } |
|
436 else |
|
437 { |
|
438 iRequestBody = CnvUtfConverter::ConvertFromUnicodeToUtf8L(KNullDesC); |
|
439 } |
|
440 } |
|
441 |
|
442 |
|
443 void CUpnpRequestComposerTest::ComposingConcluded() |
|
444 { |
|
445 INFO_PRINTF1(_L("\n********** REQUEST COMPOSING CONCLUDED **********")); |
|
446 INFO_PRINTF1(_L("\n===> Entire Request: C:\\logs\\UpnpMessageTestLogs\\composedrequests.dump <===")); |
|
447 CActiveScheduler::Stop(); |
|
448 |
|
449 RFs fs; |
|
450 TInt error = fs.Connect(); |
|
451 if(error == KErrNone) |
|
452 { |
|
453 error = fs.MkDir(KDirName); |
|
454 if(error == KErrNone) |
|
455 { |
|
456 RFile file; |
|
457 TInt err = file.Create(fs, KFileName, EFileWrite); |
|
458 if(err == KErrAlreadyExists ) |
|
459 { |
|
460 if(ConfigSection().Compare(KRCTest) == 0) |
|
461 { |
|
462 file.Replace(fs, KFileName, EFileWrite); |
|
463 } |
|
464 else |
|
465 { |
|
466 file.Open(fs, KFileName, EFileWrite); |
|
467 } |
|
468 } |
|
469 TInt pos = 0; |
|
470 file.Seek(ESeekEnd, pos); |
|
471 file.Write(pos, _L8("\n")); |
|
472 file.Write(iRequestBuffer); |
|
473 file.Close(); |
|
474 } |
|
475 fs.Close(); |
|
476 } |
|
477 iRequestComposer->ResetComposer(); |
|
478 } |
|
479 |
|
480 |
|
481 void CUpnpRequestComposerTest::MessageDataReadyL(RBuf8& aData) |
|
482 { |
|
483 INFO_PRINTF1(_L("\n********** MESSAGE DATA READY **********")); |
|
484 |
|
485 /* |
|
486 TBuf8<KMaxMsgLen> buf; |
|
487 buf.SetLength(aData.Length()); |
|
488 buf.FillZ(aData.Length()); |
|
489 aData.CopyOut(buf); |
|
490 */ |
|
491 |
|
492 TInt responseBufferLen = iRequestBuffer.Length(); |
|
493 if( (responseBufferLen + aData.Length()) > KCMaxMsgLen ) |
|
494 { |
|
495 iRequestBuffer.ReAlloc(responseBufferLen + aData.Length()); |
|
496 } |
|
497 |
|
498 iRequestBuffer.Append(aData); |
|
499 iRequestComposer->RequestDataSent(); |
|
500 } |
|
501 |
|
502 |
|
503 void CUpnpRequestComposerTest::ComposerError(TInt aError) |
|
504 { |
|
505 INFO_PRINTF1(_L("\n********** REQUEST COMPOSER ERROR **********")); |
|
506 INFO_PRINTF2(_L("\n===> Error: %d <==="), aError); |
|
507 SetTestStepResult(EFail); |
|
508 CActiveScheduler::Stop(); |
|
509 } |
|
510 |
|
511 |
|
512 TBool CUpnpRequestComposerTest::GetNextDataPart(TPtrC8& aDataPart) |
|
513 { |
|
514 aDataPart.Set(iRequestBody->Des()); |
|
515 return ETrue; |
|
516 } |
|
517 |
|
518 |
|
519 void CUpnpRequestComposerTest::ReleaseData() |
|
520 { |
|
521 // do nothing |
|
522 } |
|
523 |
|
524 |
|
525 TInt CUpnpRequestComposerTest::OverallDataSize() |
|
526 { |
|
527 return iRequestBody->Length(); |
|
528 } |
|
529 |
|
530 |
|
531 TInt CUpnpRequestComposerTest::Reset() |
|
532 { |
|
533 return KErrNotSupported; |
|
534 } |