|
1 // Copyright (c) 2003-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 |
|
17 #include <minputstream.h> |
|
18 #include <moutputstream.h> |
|
19 |
|
20 #include "CTestServerStreamManager.h" |
|
21 #include "httptestutils.h" |
|
22 #include "MPipeliningTestCase.h" |
|
23 |
|
24 const TInt KTimeOut = 50000000; |
|
25 const TInt KResponseBatchSize = 5; |
|
26 _LIT8(KTxtConnectionClose, "Connection: Close"); |
|
27 |
|
28 CTestServerStreamManager* CTestServerStreamManager::NewL(CHTTPTestUtils& aTestUtils, TInt aConnectionIndex, MPipeliningTestCase* aTestCase, MInputStream* aInputStream, MOutputStream* aOutputStream) |
|
29 { |
|
30 CTestServerStreamManager* self = new (ELeave) CTestServerStreamManager(aTestUtils, aConnectionIndex, aTestCase, aInputStream, aOutputStream); |
|
31 CleanupStack::PushL(self); |
|
32 self->ConstructL(); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 CTestServerStreamManager::CTestServerStreamManager(CHTTPTestUtils& aTestUtils, TInt aConnectionIndex, MPipeliningTestCase* aTestCase, MInputStream* aInputStream, MOutputStream* aOutputStream) |
|
38 : CTimer(EPriorityNormal), iTestUtils(aTestUtils), iInputStream(aInputStream), iOutputStream(aOutputStream), iTestCase(aTestCase), iConnectionIndex(aConnectionIndex) |
|
39 { |
|
40 CActiveScheduler::Add(this); |
|
41 |
|
42 iTransCount = iTestCase->TransactionCount(iConnectionIndex); |
|
43 |
|
44 iInputStream->Bind(*this); |
|
45 iOutputStream->Bind(*this); |
|
46 } |
|
47 |
|
48 CTestServerStreamManager::~CTestServerStreamManager() |
|
49 { |
|
50 Cancel(); |
|
51 if(iInputStream != NULL) |
|
52 { |
|
53 iInputStream->Close(); |
|
54 } |
|
55 if(iOutputStream != NULL) |
|
56 { |
|
57 iOutputStream->Close(); |
|
58 } |
|
59 delete iDataStore; |
|
60 delete iDataToSend; |
|
61 |
|
62 delete iHttpTimer; |
|
63 delete iASW; |
|
64 } |
|
65 |
|
66 void CTestServerStreamManager::ConstructL() |
|
67 { |
|
68 CTimer::ConstructL(); |
|
69 iHttpTimer = new(ELeave) CHttpTimer(*this); |
|
70 iASW = new(ELeave) CActiveSchedulerWait(); |
|
71 } |
|
72 |
|
73 // From MInputStreamObserver |
|
74 void CTestServerStreamManager::ReceivedDataIndL(const TDesC8& aBuffer) |
|
75 { |
|
76 // Got data |
|
77 _LIT(KTxtGotData, "Server - received request data."); |
|
78 iTestUtils.LogIt(KTxtGotData()); |
|
79 iTestUtils.DumpData(aBuffer, ETrue); |
|
80 |
|
81 if(iDataStore == NULL) |
|
82 { |
|
83 iDataStore = aBuffer.AllocL(); // First data chunk received |
|
84 } |
|
85 else |
|
86 { |
|
87 // Already got data before so append the data to the existing data |
|
88 TInt newLength = (iDataStore->Length()) + aBuffer.Length(); |
|
89 iDataStore = iDataStore->ReAllocL( newLength ); |
|
90 TPtr8 buf = iDataStore->Des(); |
|
91 buf.Append(aBuffer); |
|
92 } |
|
93 |
|
94 iInputStream->ReceivedDataRes(); |
|
95 |
|
96 if(iTestCase->ErrorVal() == KErrDisconnected) |
|
97 { |
|
98 iInputStream->ShutdownReq(); |
|
99 } |
|
100 else |
|
101 { |
|
102 // Try processing the requests |
|
103 SendDataL(); |
|
104 } |
|
105 } |
|
106 |
|
107 void CTestServerStreamManager::SecureServerCnf() |
|
108 { |
|
109 } |
|
110 |
|
111 void CTestServerStreamManager::InputStreamCloseInd(TInt aError) |
|
112 { |
|
113 _LIT(KTxtInputClosed, "Server - Input stream %d closed. Error: %d."); |
|
114 iTestUtils.LogIt(KTxtInputClosed(), iConnectionIndex + 1, aError); |
|
115 |
|
116 iInputStream = NULL; |
|
117 } |
|
118 |
|
119 void CTestServerStreamManager::MInputStreamObserver_Reserved() |
|
120 { |
|
121 User::Invariant(); |
|
122 } |
|
123 |
|
124 // From MOutputStreamObserver |
|
125 void CTestServerStreamManager::SendDataCnfL() |
|
126 { |
|
127 _LIT(KTxtDataSent, "Server - Data Sent."); |
|
128 iTestUtils.LogIt(KTxtDataSent()); |
|
129 _LIT(KTxtTitle, "Defect Fix INC114315"); |
|
130 if (iTestCase->TestCaseName().Match(KTxtTitle) == 0) |
|
131 { |
|
132 _LIT(KTxtCloseConn, "Server - Closing connection."); |
|
133 iTestUtils.LogIt(KTxtCloseConn()); |
|
134 iInputStream->ShutdownReq(); |
|
135 } |
|
136 if( iMoreResponseBatches ) |
|
137 { |
|
138 // If there is more batches to process try and process them |
|
139 iMoreResponseBatches = EFalse; |
|
140 TRAPD(err, SendDataL()); |
|
141 if(err != KErrNone) |
|
142 { |
|
143 _LIT(KTxtSendError, "Server - Error %d sending data. Closing stream."); |
|
144 iTestUtils.LogIt(KTxtSendError(), err); |
|
145 iInputStream->ShutdownReq(); |
|
146 } |
|
147 } |
|
148 else |
|
149 { |
|
150 delete iDataToSend; |
|
151 iDataToSend = NULL; |
|
152 |
|
153 // Do we need to close the connection |
|
154 if(iCloseConnection) |
|
155 { |
|
156 _LIT(KTxtCloseConn, "Server - Closing connection."); |
|
157 iTestUtils.LogIt(KTxtCloseConn()); |
|
158 |
|
159 if( (iTestCase->TestCaseName().Match(_L("Test Case 3")) == 0) |
|
160 || (iTestCase->TestCaseName().Match(_L("CINC073400")) == 0) |
|
161 || (iTestCase->TestCaseName().Match(_L("Test Case 19")) == 0) |
|
162 || (iTestCase->TestCaseName().Match(_L("Test Case INC073400")) == 0) ) |
|
163 { |
|
164 TTimeIntervalMicroSeconds32 time(5000000); |
|
165 iHttpTimer->After(time); |
|
166 iASW->Start(); |
|
167 } |
|
168 |
|
169 if(iInputStream) |
|
170 iInputStream->ShutdownReq(); |
|
171 } |
|
172 _LIT(KTxtTitle, "Defect Fix CINC077703"); |
|
173 if (iTestCase->TestCaseName().Match(KTxtTitle) == 0) |
|
174 { |
|
175 |
|
176 iInputStream->ShutdownReq(); |
|
177 } |
|
178 } |
|
179 } |
|
180 |
|
181 void CTestServerStreamManager::SecureClientCnf() |
|
182 { |
|
183 } |
|
184 |
|
185 void CTestServerStreamManager::OutputStreamCloseInd(TInt aError) |
|
186 { |
|
187 _LIT(KTxtOutputClosed, "Server - Output stream %d closed. Error: %d."); |
|
188 iTestUtils.LogIt(KTxtOutputClosed(), iConnectionIndex + 1, aError); |
|
189 |
|
190 iOutputStream = NULL; |
|
191 } |
|
192 |
|
193 void CTestServerStreamManager::MOutputStreamObserver_Reserved() |
|
194 { |
|
195 } |
|
196 |
|
197 TBool CTestServerStreamManager::ProcessRequestL() |
|
198 { |
|
199 TBool processingRequest = ETrue; |
|
200 TInt currentBatch = 0; |
|
201 while( (processingRequest && (iCurrentTrans < iTransCount)) && (currentBatch<KResponseBatchSize) ) |
|
202 { |
|
203 // Do we have enough data to respond to the current transaction? |
|
204 TPtrC8 rawRequest = iTestCase->GetRawRequest(iConnectionIndex, iCurrentTrans); |
|
205 TInt requestLength = rawRequest.Length(); |
|
206 TPtrC8 dataWindow = iDataStore->Mid(iDataPos); |
|
207 if( requestLength <= dataWindow.Length() ) |
|
208 { |
|
209 // Check that the raw request and the actual request match |
|
210 if( dataWindow.FindF(rawRequest) != 0 ) |
|
211 { |
|
212 _LIT(KTxtRequestDataMismatch, "Server - Fail. Request data for transaction %d does not match expected data."); |
|
213 iTestUtils.LogIt(KTxtRequestDataMismatch(), iCurrentTrans + 1); |
|
214 _LIT(KTxtExpectedData, "Server - Expected data: %S"); |
|
215 HBufC* rawRequest16 = HBufC::NewL(rawRequest.Length()); |
|
216 HBufC* dataWindow16 = HBufC::NewL(dataWindow.Length()); |
|
217 (rawRequest16->Des()).Copy(rawRequest); |
|
218 (dataWindow16->Des()).Copy(dataWindow); |
|
219 TPtrC rawRequest16Ptr = rawRequest16->Des(); |
|
220 TPtrC dataWindow16Ptr = dataWindow16->Des(); |
|
221 iTestUtils.LogIt(KTxtExpectedData(), &rawRequest16Ptr); |
|
222 iTestUtils.LogIt(KTxtExpectedData(), &dataWindow16Ptr); |
|
223 |
|
224 User::Leave(KErrNotFound); |
|
225 } |
|
226 |
|
227 // Prepare the response data to send |
|
228 iDataPos += requestLength; |
|
229 processingRequest = ETrue; |
|
230 TPtrC8 rawResponse = iTestCase->GetRawResponse(iConnectionIndex, iCurrentTrans); |
|
231 if(iDataToSend == NULL) |
|
232 { |
|
233 iDataToSend = rawResponse.AllocL(); |
|
234 } |
|
235 else |
|
236 { |
|
237 TInt responseLength = rawResponse.Length(); |
|
238 iDataToSend = iDataToSend->ReAllocL( (iDataToSend->Length()) + responseLength ); |
|
239 TPtr8 buffer = iDataToSend->Des(); |
|
240 buffer.Append(rawResponse); |
|
241 } |
|
242 |
|
243 // Check for a Connection: Close in the request |
|
244 iCloseConnection = IsConnectionCloseInData(rawRequest, rawResponse); |
|
245 if(iCloseConnection) |
|
246 { |
|
247 processingRequest = EFalse; |
|
248 } |
|
249 |
|
250 ++iCurrentTrans; |
|
251 ++currentBatch; |
|
252 } |
|
253 else |
|
254 { |
|
255 // No more requests can be processed |
|
256 processingRequest = EFalse; |
|
257 } |
|
258 |
|
259 // Flag that we have more processing to do with we are still processing the data |
|
260 // but we have reached the maximum batch size. |
|
261 if( processingRequest && currentBatch == KResponseBatchSize) |
|
262 { |
|
263 iMoreResponseBatches = ETrue; |
|
264 } |
|
265 else |
|
266 { |
|
267 iMoreResponseBatches = EFalse; |
|
268 } |
|
269 } |
|
270 |
|
271 if(iDataToSend != NULL) |
|
272 { |
|
273 return ETrue; |
|
274 } |
|
275 |
|
276 return EFalse; |
|
277 } |
|
278 |
|
279 TBool CTestServerStreamManager::IsConnectionCloseInData(const TDesC8& aRequest, const TDesC8& aResponse) const |
|
280 { |
|
281 if( (aRequest.FindF(KTxtConnectionClose()) != KErrNotFound) || (aResponse.FindF(KTxtConnectionClose()) != KErrNotFound) ) |
|
282 { |
|
283 // Either the request or response has Connection: Close header |
|
284 _LIT(KTxtConnectionCloseDetected, "Server - Connection: Close detected in request or response, transaction: %d"); |
|
285 iTestUtils.LogIt(KTxtConnectionCloseDetected(), iCurrentTrans + 1); |
|
286 |
|
287 return ETrue; |
|
288 } |
|
289 |
|
290 return EFalse; |
|
291 } |
|
292 |
|
293 void CTestServerStreamManager::SendDataL() |
|
294 { |
|
295 Cancel(); |
|
296 |
|
297 _LIT(KTcTitle, "CRecvTimeOut"); |
|
298 if (iTestCase->TestCaseName().Match(KTcTitle) == 0) |
|
299 { |
|
300 _LIT(KTxtTimeout, "Server - Data Send Timeout."); |
|
301 iTestUtils.LogIt(KTxtTimeout()); |
|
302 After(61000000); |
|
303 return; |
|
304 } |
|
305 |
|
306 if( !iMoreResponseBatches ) |
|
307 { |
|
308 // Try processing the requests |
|
309 if( ProcessRequestL() ) |
|
310 { |
|
311 // We have enough, send the response. |
|
312 iOutputStream->SendDataReqL(*iDataToSend); |
|
313 |
|
314 _LIT(KTxtSendData, "Server - Sending response data."); |
|
315 iTestUtils.LogIt(KTxtSendData()); |
|
316 iTestUtils.DumpData(*iDataToSend, ETrue); |
|
317 } |
|
318 else |
|
319 { |
|
320 After(KTimeOut); // Start the inactivity timer |
|
321 } |
|
322 } |
|
323 } |
|
324 |
|
325 void CTestServerStreamManager::RunL() |
|
326 { |
|
327 _LIT(KTcTitle, "CRecvTimeOut"); |
|
328 if (iTestCase->TestCaseName().Match(KTcTitle) == 0) |
|
329 { |
|
330 _LIT(KTxtTimeout, "TC-Receive Timeout Timer RunL."); |
|
331 iTestUtils.LogIt(KTxtTimeout()); |
|
332 return; |
|
333 } |
|
334 |
|
335 if( iStatus.Int() == KErrNone ) |
|
336 { |
|
337 // The connection has timed out. |
|
338 _LIT(KTxtTimedOut, "Server - Fail, Connection %d timed out. Current transaction: %d"); |
|
339 iTestUtils.LogIt(KTxtTimedOut(), iConnectionIndex + 1, iCurrentTrans + 1); |
|
340 |
|
341 iInputStream->ShutdownReq(); |
|
342 } |
|
343 else |
|
344 { |
|
345 User::LeaveIfError(iStatus.Int()); |
|
346 } |
|
347 } |
|
348 |
|
349 MHttpResponse* CTestServerStreamManager::CurrentResponse() |
|
350 { |
|
351 return NULL; |
|
352 } |
|
353 |
|
354 void CTestServerStreamManager::OnReceiveTimeOut() |
|
355 { |
|
356 |
|
357 } |
|
358 |
|
359 void CTestServerStreamManager::OnSendTimeOut() |
|
360 { |
|
361 |
|
362 } |
|
363 |
|
364 TInt CTestServerStreamManager::SendTimeOutVal() |
|
365 { |
|
366 return 0; |
|
367 } |
|
368 |
|
369 |
|
370 void CTestServerStreamManager::TimeOut() |
|
371 { |
|
372 iASW->AsyncStop(); |
|
373 } |
|
374 |
|
375 |
|
376 CHttpTimer::CHttpTimer(MTimerClient& aClient) |
|
377 :CActive(EPriorityStandard), iClient(aClient) |
|
378 { |
|
379 CActiveScheduler::Add(this); |
|
380 iTimer.CreateLocal(); |
|
381 } |
|
382 |
|
383 CHttpTimer::~CHttpTimer() |
|
384 { |
|
385 Cancel(); |
|
386 iTimer.Close(); |
|
387 } |
|
388 |
|
389 void CHttpTimer::After(TTimeIntervalMicroSeconds32 anInterval) |
|
390 { |
|
391 iTimer.After(iStatus, anInterval); |
|
392 SetActive(); |
|
393 } |
|
394 |
|
395 void CHttpTimer::DoCancel() |
|
396 { |
|
397 |
|
398 } |
|
399 |
|
400 void CHttpTimer::RunL() |
|
401 { |
|
402 iClient.TimeOut(); |
|
403 } |