|
1 // Copyright (c) 2002-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 "cmessageparserdriver.h" |
|
17 |
|
18 #include "mdriverobserver.h" |
|
19 #include "cmessagedatasupplier.h" |
|
20 |
|
21 CMessageParserDriver* CMessageParserDriver::NewL(MDriverObserver& aObserver) |
|
22 { |
|
23 CMessageParserDriver* self = new(ELeave) CMessageParserDriver(aObserver); |
|
24 CleanupStack::PushL(self); |
|
25 self->ConstructL(); |
|
26 CleanupStack::Pop(self); |
|
27 return self; |
|
28 } |
|
29 |
|
30 CMessageParserDriver::~CMessageParserDriver() |
|
31 { |
|
32 Cancel(); |
|
33 delete iDataSupplier; |
|
34 iHeaders.Close(); |
|
35 iTrailers.Close(); |
|
36 iMessageParser.Close(); |
|
37 } |
|
38 |
|
39 CMessageParserDriver::CMessageParserDriver(MDriverObserver& aObserver) |
|
40 : CActive(CActive::EPriorityStandard), iObserver(aObserver) |
|
41 { |
|
42 CActiveScheduler::Add(this); |
|
43 } |
|
44 |
|
45 void CMessageParserDriver::ConstructL() |
|
46 { |
|
47 iMessageParser.OpenL(*this); |
|
48 } |
|
49 |
|
50 void CMessageParserDriver::SetMessageData(const TDesC8& aMessageData) |
|
51 { |
|
52 iMessageData.Set(aMessageData); |
|
53 } |
|
54 |
|
55 void CMessageParserDriver::SetStartLine(const TDesC8& aStartLine) |
|
56 { |
|
57 iStartLine.Set(aStartLine); |
|
58 } |
|
59 |
|
60 void CMessageParserDriver::SetHeaderL(const TDesC8& aFieldName, const TDesC8& aFieldValue) |
|
61 { |
|
62 User::LeaveIfError(iHeaders.Append(THeaderField(aFieldName, aFieldValue))); |
|
63 } |
|
64 |
|
65 void CMessageParserDriver::SetBodyData(const TDesC8& aBodyData, TBool aIsChunked, TBool aIsUnknownLength) |
|
66 { |
|
67 iEntityBody.Set(aBodyData); |
|
68 iIsUnknownLength = aIsUnknownLength; |
|
69 |
|
70 // Set the entity body info |
|
71 if( iEntityBody.Length() > 0 ) |
|
72 { |
|
73 if( aIsChunked ) |
|
74 { |
|
75 iExpectBodyState = EChunkEncodedBody; |
|
76 } |
|
77 else |
|
78 { |
|
79 if( iIsUnknownLength ) |
|
80 iExpectBodyState = EUnknownBodyLength; |
|
81 else |
|
82 iExpectBodyState = ENonEncodedBody; |
|
83 } |
|
84 } |
|
85 else |
|
86 { |
|
87 iExpectBodyState = ENoEntityBody; |
|
88 } |
|
89 } |
|
90 |
|
91 void CMessageParserDriver::SetTrailerL(const TDesC8& aFieldName, const TDesC8& aFieldValue) |
|
92 { |
|
93 User::LeaveIfError(iTrailers.Append(THeaderField(aFieldName, aFieldValue))); |
|
94 } |
|
95 |
|
96 void CMessageParserDriver::Start(TInt aExpectedError, TBool aTestReset) |
|
97 { |
|
98 iExpectedError = aExpectedError; |
|
99 iDoReset = aTestReset; |
|
100 |
|
101 // Inform the observer that the test has started |
|
102 iObserver.NotifyStart(); |
|
103 |
|
104 // Initially set the buffer size to the length of the message unless doing |
|
105 // reset test - use a buffer size of an eighth of the message size |
|
106 if( iDoReset ) |
|
107 { |
|
108 iBufferSize = iMessageData.Length()/8; |
|
109 if( iBufferSize < 1 ) |
|
110 iBufferSize = 1; |
|
111 } |
|
112 else |
|
113 iBufferSize = iMessageData.Length(); |
|
114 |
|
115 // Set the reset state to initial state - PendingStartLine. |
|
116 iResetState = EPendingStartLine; |
|
117 |
|
118 // Self-complete to kick-off the test. |
|
119 CompleteSelf(); |
|
120 } |
|
121 |
|
122 void CMessageParserDriver::CompleteSelf() |
|
123 { |
|
124 TRequestStatus* pStat = &iStatus; |
|
125 User::RequestComplete(pStat, KErrNone); |
|
126 SetActive(); |
|
127 } |
|
128 |
|
129 void CMessageParserDriver::DoReset() |
|
130 { |
|
131 // Reset the parser and update the reset state. |
|
132 iMessageParser.Reset(); |
|
133 iResetState = iState; |
|
134 |
|
135 iObserver.Log(_L("RESET - starting test again!!")); |
|
136 |
|
137 // Reset the data supplier |
|
138 delete iDataSupplier; |
|
139 iDataSupplier = NULL; |
|
140 |
|
141 if( iState == EPendingHeaders ) |
|
142 iResetIndex = iHeaderIndex; |
|
143 else if( iState == EPendingTrailers ) |
|
144 iResetIndex = iTrailerIndex; |
|
145 else if( iState == EPendingBodySize ) |
|
146 iResetIndex = 0; |
|
147 else if( iState == EPendingBody ) |
|
148 iResetIndex = iBodyIndex; |
|
149 |
|
150 // Start the test over... |
|
151 CompleteSelf(); |
|
152 } |
|
153 |
|
154 /* |
|
155 * Methods from CActive |
|
156 */ |
|
157 |
|
158 void CMessageParserDriver::RunL() |
|
159 { |
|
160 // Reset the test case data |
|
161 iHeaderIndex = 0; |
|
162 iTrailerIndex = 0; |
|
163 iExpectBodyData.Set(iEntityBody); |
|
164 |
|
165 // Set the reset flags |
|
166 iReset = EFalse; |
|
167 iBodyIndex = 0; |
|
168 iChunkCount = 0; |
|
169 |
|
170 // Reset the data supplier |
|
171 delete iDataSupplier; |
|
172 iDataSupplier = NULL; |
|
173 |
|
174 iDataSupplier = CMessageDataSupplier::NewL(*this, iMessageData, iBufferSize); |
|
175 |
|
176 TBuf<64> comment; |
|
177 comment.Format(_L("STARTING PARSING -> buffer size : %d"), iBufferSize); |
|
178 iObserver.Log(comment); |
|
179 |
|
180 // Start the parser... |
|
181 iMessageParser.ReceivedMessageData(); |
|
182 |
|
183 // Move to PendingStartLine state... |
|
184 iState = EPendingStartLine; |
|
185 } |
|
186 |
|
187 void CMessageParserDriver::DoCancel() |
|
188 { |
|
189 // Do nothing... |
|
190 } |
|
191 |
|
192 TInt CMessageParserDriver::RunError(TInt aError) |
|
193 { |
|
194 // Inform the observer of the error |
|
195 iObserver.NotifyError(aError); |
|
196 return KErrNone; |
|
197 } |
|
198 |
|
199 /* |
|
200 * Methods from MHttpMessageParserObserver |
|
201 */ |
|
202 |
|
203 void CMessageParserDriver::GetDataPacket(TPtrC8& aData) |
|
204 { |
|
205 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
206 |
|
207 if( iReset ) |
|
208 { |
|
209 // Not expecting a header. Test has failed - notify the observer. |
|
210 iObserver.Log(_L("FAIL - not expecting GetDataPacket")); |
|
211 iObserver.NotifyError(KErrNotFound); |
|
212 iTestFailed = ETrue; |
|
213 } |
|
214 |
|
215 iDataSupplier->GetData(aData); |
|
216 } |
|
217 |
|
218 void CMessageParserDriver::ReleaseDataPacket() |
|
219 { |
|
220 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
221 |
|
222 if( iReset ) |
|
223 { |
|
224 // Not expecting a header. Test has failed - notify the observer. |
|
225 iObserver.Log(_L("FAIL - not expecting ReleaseDataPacket")); |
|
226 iObserver.NotifyError(KErrNotFound); |
|
227 iTestFailed = ETrue; |
|
228 } |
|
229 |
|
230 iDataSupplier->ReleaseData(); |
|
231 |
|
232 // If we have recieved all the data and we were doing a 1.0 request of unknown length |
|
233 // tell the parser the data is complete and we are waiting for the body to be complete. |
|
234 if( iExpectBodyData.Length() == 0 && iIsUnknownLength && iState==EPendingBodyComplete) |
|
235 { |
|
236 TRAPD(err, iMessageParser.CompletedBodyDataL() ); |
|
237 if( err != KErrNone ) |
|
238 { |
|
239 // Not expecting a header. Test has failed - notify the observer. |
|
240 iObserver.Log(_L("FAIL - not expecting ReleaseDataPacket")); |
|
241 iObserver.NotifyError(err); |
|
242 iTestFailed = ETrue; |
|
243 } |
|
244 } |
|
245 |
|
246 } |
|
247 |
|
248 void CMessageParserDriver::StartLineL(const TDesC8& aStartLine) |
|
249 { |
|
250 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
251 |
|
252 iObserver.Log(_L("Start-line received")); |
|
253 TBuf<256> comment; |
|
254 comment.Copy(aStartLine); |
|
255 iObserver.Log(comment); |
|
256 |
|
257 // Was start-line expected? |
|
258 if( iState != EPendingStartLine || iReset ) |
|
259 { |
|
260 // Not expecting start-line. Test has failed - notify the observer. |
|
261 iObserver.Log(_L("FAIL - not expecting start-line")); |
|
262 iObserver.NotifyError(KErrNotFound); |
|
263 iTestFailed = ETrue; |
|
264 } |
|
265 else |
|
266 { |
|
267 // Check that the start-line is correct. |
|
268 if( aStartLine.Compare(iStartLine) == 0 ) |
|
269 { |
|
270 // Should a reset occur here? |
|
271 if( iDoReset && iResetState == iState ) |
|
272 iReset = ETrue; |
|
273 |
|
274 // Match - what is next? |
|
275 if( iHeaders.Count() == 0 ) |
|
276 iState = EPendingBodySize; |
|
277 else |
|
278 iState = EPendingHeaders; |
|
279 // We no longer parse line by line. We parse startline & headers (5) at one go. |
|
280 // We cannot reset here |
|
281 iReset = EFalse; |
|
282 // Reset? |
|
283 if( iDoReset && iReset ) |
|
284 DoReset(); |
|
285 } |
|
286 else |
|
287 { |
|
288 // The start-line does not match. Test has failed - notify the observer. |
|
289 iObserver.Log(_L("FAIL - received incorrect start-line")); |
|
290 iObserver.NotifyError(KErrNotFound); |
|
291 iTestFailed = ETrue; |
|
292 } |
|
293 } |
|
294 } |
|
295 |
|
296 void CMessageParserDriver::HeaderL(const TDesC8& aFieldName, TDesC8& aFieldValue) |
|
297 { |
|
298 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
299 |
|
300 iObserver.Log(_L("HeaderL received")); |
|
301 |
|
302 // Was a header expected? NOTE - this can be a trailer header... |
|
303 if( (iState != EPendingHeaders && iState != EPendingTrailers) || iReset ) |
|
304 { |
|
305 // Not expecting a header. Test has failed - notify the observer. |
|
306 iObserver.Log(_L("FAIL - not expecting HeaderL")); |
|
307 iObserver.NotifyError(KErrNotFound); |
|
308 iTestFailed = ETrue; |
|
309 } |
|
310 else |
|
311 { |
|
312 // Yep - log the parsed header... |
|
313 TBuf<64> name; |
|
314 name.Copy(aFieldName); |
|
315 TBuf<256> value; |
|
316 value.Copy(aFieldValue); |
|
317 TBuf<322> comment; |
|
318 comment.Format(_L("%S: %S"), &name, &value); |
|
319 iObserver.Log(comment); |
|
320 |
|
321 // Get the expected header info... |
|
322 THeaderField header; |
|
323 if( iState == EPendingHeaders ) |
|
324 { |
|
325 // Should a reset occur here? |
|
326 if( iDoReset && (iResetState == iState && iResetIndex == iHeaderIndex) ) |
|
327 iReset = ETrue; |
|
328 |
|
329 // Get the current header info... |
|
330 header = iHeaders[iHeaderIndex]; |
|
331 |
|
332 // Loop to next header... |
|
333 ++iHeaderIndex; |
|
334 |
|
335 // Set next state... |
|
336 if( iHeaderIndex < iHeaders.Count() ) |
|
337 iState = EPendingHeaders; |
|
338 else |
|
339 iState = EPendingBodySize; |
|
340 } |
|
341 else |
|
342 { |
|
343 // Should a reset occur here? |
|
344 if( iDoReset && (iResetState == iState && iResetIndex == iTrailerIndex) ) |
|
345 iReset = ETrue; |
|
346 |
|
347 // Get the current trailer info... |
|
348 header = iTrailers[iTrailerIndex]; |
|
349 |
|
350 // Loop to next header... |
|
351 ++iTrailerIndex; |
|
352 |
|
353 // Set next state... |
|
354 if( iTrailerIndex < iTrailers.Count() ) |
|
355 iState = EPendingTrailers; |
|
356 else |
|
357 iState = EPendingMessageComplete; |
|
358 } |
|
359 TPtrC8 expectedName = header.iName; |
|
360 TPtrC8 expectedValue = header.iValue; |
|
361 |
|
362 // Check that the parsed header info matches expected |
|
363 if( expectedName.Compare(aFieldName) != 0 || expectedValue.Compare(aFieldValue) != 0 ) |
|
364 { |
|
365 // The header info does not match. Test has failed - notify the observer. |
|
366 iObserver.Log(_L("FAIL - received incorrect header info")); |
|
367 iObserver.NotifyError(KErrNotFound); |
|
368 iTestFailed = ETrue; |
|
369 } |
|
370 else if( iDoReset && iReset ) |
|
371 DoReset(); |
|
372 } |
|
373 } |
|
374 |
|
375 TInt CMessageParserDriver::BodySizeL() |
|
376 { |
|
377 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
378 |
|
379 // Log the headers complete event |
|
380 iObserver.Log(_L("BodySizeL received")); |
|
381 |
|
382 // Was this expected? |
|
383 TInt size = MHttpMessageParserObserver::ENoBody; |
|
384 if( iState != EPendingBodySize || iReset ) |
|
385 { |
|
386 // Not expected! Test has failed - notify the observer. |
|
387 iObserver.Log(_L("FAIL - not expecting BodySizeL")); |
|
388 iObserver.NotifyError(KErrNotFound); |
|
389 iTestFailed = ETrue; |
|
390 } |
|
391 else |
|
392 { |
|
393 // Should a reset occur here? |
|
394 if( iDoReset && iResetState == iState ) |
|
395 iReset = ETrue; |
|
396 |
|
397 // Yep - workout what is next... |
|
398 switch( iExpectBodyState ) |
|
399 { |
|
400 case ENoEntityBody: |
|
401 { |
|
402 // No body - use default value of size |
|
403 iState = EPendingMessageComplete; |
|
404 } break; |
|
405 case EChunkEncodedBody: |
|
406 { |
|
407 // There is a body that is chunk encoded. |
|
408 size = MHttpMessageParserObserver::EChunked; |
|
409 iState = EPendingBody; |
|
410 } break; |
|
411 case ENonEncodedBody: |
|
412 { |
|
413 // There is a body that is not encoded. |
|
414 size = iExpectBodyData.Length(); |
|
415 iState = EPendingBody; |
|
416 } break; |
|
417 case EUnknownBodyLength: |
|
418 { |
|
419 // There is a body of unknown length |
|
420 size = MHttpMessageParserObserver::EUnknown; |
|
421 iState = EPendingBody; |
|
422 } break; |
|
423 default: |
|
424 User::Invariant(); |
|
425 break; |
|
426 } |
|
427 |
|
428 // Reset? |
|
429 if( iDoReset && iReset ) |
|
430 DoReset(); |
|
431 } |
|
432 return size; |
|
433 } |
|
434 |
|
435 void CMessageParserDriver::BodyChunkL(const TDesC8& aData) |
|
436 { |
|
437 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
438 |
|
439 iObserver.Log(_L("BodyChunkL received")); |
|
440 |
|
441 // Was this expected? |
|
442 if( iExpectBodyState != EChunkEncodedBody && (iState != EPendingBody || iReset )) |
|
443 { |
|
444 // No body data expected. Test has failed - notify the observer. |
|
445 iObserver.Log(_L("FAIL - not expecting BodyChunkL")); |
|
446 iObserver.NotifyError(KErrNotFound); |
|
447 iTestFailed = ETrue; |
|
448 } |
|
449 else |
|
450 { |
|
451 // Dump the body data... |
|
452 iObserver.Dump(aData); |
|
453 |
|
454 // Check to see if it matches the expected data. |
|
455 TBool match = EFalse; |
|
456 if( aData.Length() <= iExpectBodyData.Length() ) |
|
457 { |
|
458 // Is the content the same |
|
459 if( aData.Compare(iExpectBodyData.Left(aData.Length())) == 0 ) |
|
460 { |
|
461 // Data is the same - update the expected data |
|
462 iExpectBodyData.Set(iExpectBodyData.Mid(aData.Length())); |
|
463 match = ETrue; |
|
464 } |
|
465 } |
|
466 if( match ) |
|
467 { |
|
468 // Should a reset occur here? |
|
469 if( iDoReset && (iResetState == iState && iResetIndex == iBodyIndex) ) |
|
470 iReset = ETrue; |
|
471 |
|
472 ++iBodyIndex; |
|
473 |
|
474 // Has all the data been received? |
|
475 if( iExpectBodyData.Length() == 0 ) |
|
476 iState = EPendingBodyComplete; |
|
477 |
|
478 // Reset? |
|
479 if( iDoReset && iReset ) |
|
480 DoReset(); |
|
481 } |
|
482 else |
|
483 { |
|
484 // The body data does not match. Test has failed - notify the observer. |
|
485 iObserver.Log(_L("FAIL - received incorrect body data")); |
|
486 iObserver.NotifyError(KErrNotFound); |
|
487 iTestFailed = ETrue; |
|
488 } |
|
489 } |
|
490 } |
|
491 |
|
492 void CMessageParserDriver::BodyCompleteL() |
|
493 { |
|
494 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
495 |
|
496 iObserver.Log(_L("BodyCompleteL received")); |
|
497 |
|
498 // Was this expected? |
|
499 if( iState != EPendingBodyComplete || iReset ) |
|
500 { |
|
501 // Not expecting body complete. Test has failed - notify the observer. |
|
502 iObserver.Log(_L("FAIL - not expecting BodyCompleteL")); |
|
503 iObserver.NotifyError(KErrNotFound); |
|
504 iTestFailed = ETrue; |
|
505 } |
|
506 else |
|
507 { |
|
508 // Should a reset occur here? |
|
509 if( iDoReset && iResetState == iState ) |
|
510 iReset = ETrue; |
|
511 |
|
512 // What's next? |
|
513 if( iExpectBodyState == EChunkEncodedBody && iTrailers.Count() > 0 ) |
|
514 iState = EPendingTrailers; |
|
515 else |
|
516 iState = EPendingMessageComplete; |
|
517 |
|
518 // Reset? |
|
519 if( iDoReset && iReset ) |
|
520 DoReset(); |
|
521 } |
|
522 } |
|
523 |
|
524 void CMessageParserDriver::MessageCompleteL(const TPtrC8& /*aExcessData*/) |
|
525 { |
|
526 __ASSERT_DEBUG( iTestFailed == EFalse, User::Invariant() ); |
|
527 |
|
528 // Log the message complete event |
|
529 iObserver.Log(_L("Message Complete")); |
|
530 |
|
531 // Was this expected? |
|
532 if( iState != EPendingMessageComplete || iReset ) |
|
533 { |
|
534 // Not expecting message complete. Test has failed - notify the observer. |
|
535 iObserver.Log(_L("FAIL - not expecting message complete")); |
|
536 iObserver.NotifyError(KErrNotFound); |
|
537 iTestFailed = ETrue; |
|
538 } |
|
539 else |
|
540 { |
|
541 // Should a reset occur here? |
|
542 if( iDoReset && iResetState == iState ) |
|
543 iReset = ETrue; |
|
544 |
|
545 // Next state... |
|
546 iState = EDone; |
|
547 |
|
548 // Reset? Or is the buffer size small enough? |
|
549 if( iDoReset && iReset ) |
|
550 DoReset(); |
|
551 else if( !iDoReset && iBufferSize > 1 ) |
|
552 { |
|
553 // Nope - half and do test again!! |
|
554 iBufferSize /= 2; |
|
555 iResetState = EPendingStartLine; |
|
556 CompleteSelf(); |
|
557 |
|
558 iObserver.Log(_L("RE-PARSE WITH SMALLER BUFFER")); |
|
559 } |
|
560 else |
|
561 { |
|
562 // Yep - test done... |
|
563 iObserver.NotifyComplete(); |
|
564 } |
|
565 } |
|
566 } |
|
567 |
|
568 TInt CMessageParserDriver::HandleParserError(TInt aError) |
|
569 { |
|
570 // Is an error expected? |
|
571 if( iExpectedError == aError ) |
|
572 { |
|
573 TBuf<64> comment; |
|
574 comment.Format(_L("Received expected error : %d"), aError); |
|
575 iObserver.Log(comment); |
|
576 |
|
577 // Yep and received expected - is the buffer size small enough? |
|
578 if( !iDoReset && iBufferSize > 1 ) |
|
579 { |
|
580 // Nope - halve and do test again!! |
|
581 iBufferSize /= 2; |
|
582 CompleteSelf(); |
|
583 |
|
584 iObserver.Log(_L("RE-PARSE WITH SMALLER BUFFER")); |
|
585 } |
|
586 else |
|
587 { |
|
588 // Yep - test done... |
|
589 iObserver.NotifyComplete(); |
|
590 } |
|
591 } |
|
592 else |
|
593 { |
|
594 // Inform the observer of the error |
|
595 iObserver.NotifyError(aError); |
|
596 } |
|
597 return KErrNone; |
|
598 } |
|
599 |
|
600 void CMessageParserDriver::Reserved_MHttpMessageParserObserver() |
|
601 { |
|
602 User::Invariant(); |
|
603 } |
|
604 |
|
605 /* |
|
606 * Methods from MDataSupplierObserver |
|
607 */ |
|
608 |
|
609 void CMessageParserDriver::DataReady() |
|
610 { |
|
611 if( iDoReset && iChunkIndex == iChunkCount ) |
|
612 { |
|
613 // Reset the parser and update the chunk index |
|
614 iMessageParser.Reset(); |
|
615 ++iChunkIndex; |
|
616 |
|
617 iObserver.Log(_L("RESET - starting test again!!")); |
|
618 |
|
619 // Reset the data supplier |
|
620 delete iDataSupplier; |
|
621 iDataSupplier = NULL; |
|
622 |
|
623 // Start the test over... |
|
624 CompleteSelf(); |
|
625 } |
|
626 else |
|
627 { |
|
628 // Inc the chunk count and notify the parser of more data |
|
629 ++iChunkCount; |
|
630 iMessageParser.ReceivedMessageData(); |
|
631 } |
|
632 } |