24
|
1 |
// Copyright (c) 2007-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 "chttpdownload.h"
|
|
18 |
#include "cctsytestlogging.h"
|
|
19 |
#include <cs_subconparams.h>
|
|
20 |
#include <networking/qos3gpp_subconparams.h>
|
|
21 |
|
|
22 |
// Declare a static, single, server socket session to be used with all active downloads
|
|
23 |
RSocketServ CHTTPDownload::iSocketServer;
|
|
24 |
// Declare a static, counter fot active downloads, to know when to open and close the server socket session
|
|
25 |
TInt CHTTPDownload::iActiveDownload = 0;
|
|
26 |
|
|
27 |
_LIT(KDefaultHost, "developer.symbian.org");
|
|
28 |
_LIT(KDefaultPage, "/wiki/images/1/12/Common_Design_Patterns_for_Symbian_OS_Sample_Chapter.pdf");
|
|
29 |
const TInt KDefaultPort = 80;
|
|
30 |
const TInt KBufferSize = 4096;
|
|
31 |
|
|
32 |
CHTTPDownload::CHTTPDownload(CCTSYIntegrationTestSuiteStepBase *aTestStep): iOpenSocketServer(EFalse),
|
|
33 |
iTestStep(aTestStep),
|
|
34 |
iSocketOpen(EFalse),
|
|
35 |
iHostResolverOpen(EFalse),
|
|
36 |
iConnectionOpen(EFalse),
|
|
37 |
iSubConnectionOpen(EFalse),
|
|
38 |
iWebPageOpen(EFalse)
|
|
39 |
/*
|
|
40 |
* Constructor
|
|
41 |
*
|
|
42 |
* @param aTestStep a pointer to the test step object
|
|
43 |
*/
|
|
44 |
|
|
45 |
{
|
|
46 |
}
|
|
47 |
|
|
48 |
CHTTPDownload::~CHTTPDownload()
|
|
49 |
/*
|
|
50 |
* Destructor
|
|
51 |
*
|
|
52 |
*/
|
|
53 |
{
|
|
54 |
StopDownload();
|
|
55 |
if(iWebPageOpen)
|
|
56 |
{
|
|
57 |
iWebPage.Close();
|
|
58 |
iWebPageOpen=EFalse;
|
|
59 |
}
|
|
60 |
if(iOpenSocketServer)
|
|
61 |
{
|
|
62 |
iActiveDownload--;
|
|
63 |
TEST_FRAMEWORK_LOG2(_L("iActiveDownload changed iActiveDownload = %d"),iActiveDownload);
|
|
64 |
if(iActiveDownload==0)
|
|
65 |
{
|
|
66 |
iSocketServer.Close();
|
|
67 |
TEST_FRAMEWORK_LOG2(_L("iSocketServer Closed iActiveDownload = %d"),iActiveDownload);
|
|
68 |
}
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
|
|
73 |
TBool CHTTPDownload::StartDownloadL()
|
|
74 |
/*
|
|
75 |
* Start download an http page with default parameters
|
|
76 |
*
|
|
77 |
*
|
|
78 |
* @return ETrue if the download started
|
|
79 |
*/
|
|
80 |
{
|
|
81 |
iHost = &KDefaultHost;
|
|
82 |
iPage = &KDefaultPage;
|
|
83 |
iPort = KDefaultPort;
|
|
84 |
iUseSubConnection = EFalse;
|
|
85 |
if(InitSocket()!=KErrNone)
|
|
86 |
{
|
|
87 |
Close();
|
|
88 |
return EFalse;
|
|
89 |
}
|
|
90 |
return ETrue;
|
|
91 |
}
|
|
92 |
|
|
93 |
TBool CHTTPDownload::StartDownloadL(const TDesC &aHost, const TDesC &aPage)
|
|
94 |
/*
|
|
95 |
* Start download a requested http page with default port
|
|
96 |
*
|
|
97 |
* @param aHost a descriptor of the web host (i.e. www.symbian.com)
|
|
98 |
* @param aPage The actual page (i.e. /symbianos/index.html)
|
|
99 |
*
|
|
100 |
* @return ETrue if the download started
|
|
101 |
*/
|
|
102 |
{
|
|
103 |
iHost = &aHost;
|
|
104 |
iPage = &aPage;
|
|
105 |
iPort = KDefaultPort;
|
|
106 |
iUseSubConnection = EFalse;
|
|
107 |
if(InitSocket()!=KErrNone)
|
|
108 |
{
|
|
109 |
Close();
|
|
110 |
return EFalse;
|
|
111 |
}
|
|
112 |
return ETrue;
|
|
113 |
}
|
|
114 |
|
|
115 |
TBool CHTTPDownload::StartDownloadL(const TDesC &aHost, const TDesC &aPage,TInt aPort)
|
|
116 |
/*
|
|
117 |
* Start download a requested http page with a requested port
|
|
118 |
*
|
|
119 |
* @param aHost a descriptor of the web host (i.e. www.symbian.com)
|
|
120 |
* @param aPage The actual page (i.e. /symbianos/index.html)
|
|
121 |
* @param aPort the protocol port (80 for HTTP)
|
|
122 |
*
|
|
123 |
* @return ETrue if the download started
|
|
124 |
*/
|
|
125 |
{
|
|
126 |
iHost = &aHost;
|
|
127 |
iPage = &aPage;
|
|
128 |
iPort = aPort;
|
|
129 |
iUseSubConnection = EFalse;
|
|
130 |
if(InitSocket()!=KErrNone)
|
|
131 |
{
|
|
132 |
Close();
|
|
133 |
return EFalse;
|
|
134 |
}
|
|
135 |
return ETrue;
|
|
136 |
}
|
|
137 |
|
|
138 |
|
|
139 |
TBool CHTTPDownload::StartDownloadWithSecondaryContextL(const TDesC &aHost, const TDesC &aPage, TInt aPort)
|
|
140 |
/*
|
|
141 |
* Start download a requested http page with a requested port using a sub connection
|
|
142 |
*
|
|
143 |
* @param aHost a descriptor of the web host (i.e. www.symbian.com)
|
|
144 |
* @param aPage The actual page (i.e. /symbianos/index.html)
|
|
145 |
* @param aPort the protocol port (80 for HTTP)
|
|
146 |
*
|
|
147 |
* @return ETrue if the download started
|
|
148 |
*/
|
|
149 |
{
|
|
150 |
iHost = &aHost;
|
|
151 |
iPage = &aPage;
|
|
152 |
iPort = aPort;
|
|
153 |
iUseSubConnection = ETrue;
|
|
154 |
if(InitSocket()!=KErrNone)
|
|
155 |
{
|
|
156 |
Close();
|
|
157 |
return EFalse;
|
|
158 |
}
|
|
159 |
return ETrue;
|
|
160 |
}
|
|
161 |
|
|
162 |
TInt CHTTPDownload::GetByteCount()
|
|
163 |
/*
|
|
164 |
* Return the number of bytes downloaded so far
|
|
165 |
*
|
|
166 |
* @return The number of bytes downloaded so far
|
|
167 |
*/
|
|
168 |
{
|
|
169 |
VerifyDownloading();
|
|
170 |
return iByteCount;
|
|
171 |
}
|
|
172 |
|
|
173 |
void CHTTPDownload::StopDownload()
|
|
174 |
/*
|
|
175 |
* Stop a active download; waits for the current socket read to complete, rather than force an immediate terminate.
|
|
176 |
*
|
|
177 |
*/
|
|
178 |
{
|
|
179 |
if(iSocketOpen)
|
|
180 |
{
|
|
181 |
iSocket.CancelRecv();
|
|
182 |
}
|
|
183 |
Close();
|
|
184 |
}
|
|
185 |
|
|
186 |
TBool CHTTPDownload::VerifyDownloading()
|
|
187 |
/*
|
|
188 |
* Return ETrue if the download is still active
|
|
189 |
*
|
|
190 |
* @return ETrue if the download is still active
|
|
191 |
*/
|
|
192 |
{
|
|
193 |
if(iSocketOpen)
|
|
194 |
{
|
|
195 |
if(iStatus.Int()==KRequestPending)
|
|
196 |
{
|
|
197 |
return ETrue;
|
|
198 |
}
|
|
199 |
else if (iStatus.Int()==KErrNone)
|
|
200 |
{
|
|
201 |
iByteCount += iWebPage.Length();
|
|
202 |
|
|
203 |
// Logging the first 180 characters of the text...
|
|
204 |
TBuf<200> temp;
|
|
205 |
temp.Copy(iWebPage.Left(180));
|
|
206 |
TEST_FRAMEWORK_LOG3(_L("Got %S... in %d chars"),&temp,iWebPage.Length());
|
|
207 |
|
|
208 |
iSocket.RecvOneOrMore(iWebPage,0,iStatus,iHttpHeaderLen);
|
|
209 |
return ETrue;
|
|
210 |
}
|
|
211 |
else
|
|
212 |
{
|
|
213 |
iByteCount += iWebPage.Length();
|
|
214 |
Close();
|
|
215 |
return EFalse;
|
|
216 |
}
|
|
217 |
|
|
218 |
}
|
|
219 |
else
|
|
220 |
{
|
|
221 |
return EFalse;
|
|
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
TInt CHTTPDownload::InitSocket()
|
|
229 |
/*
|
|
230 |
* Initilize a socket
|
|
231 |
*
|
|
232 |
* @return KErrNone if it had no problem, or the specific system error code that cause the problem.
|
|
233 |
*/
|
|
234 |
{
|
|
235 |
TInt status;
|
|
236 |
if(!iWebPageOpen)
|
|
237 |
{
|
|
238 |
status = iWebPage.Create(KBufferSize);
|
|
239 |
if(status !=KErrNone)
|
|
240 |
{
|
|
241 |
TEST_FRAMEWORK_LOG2(_L("RRBuf8::Create Could not create a buffer, got %d"),status);
|
|
242 |
return status;
|
|
243 |
}
|
|
244 |
iWebPageOpen = ETrue;
|
|
245 |
}
|
|
246 |
if(!iOpenSocketServer)
|
|
247 |
{
|
|
248 |
if(iActiveDownload==0)
|
|
249 |
{
|
|
250 |
status = iSocketServer.Connect();
|
|
251 |
TEST_FRAMEWORK_LOG3(_L("iSocketServer.Connected, status = %d, iActiveDownload = %d"),status, iActiveDownload);
|
|
252 |
if(status !=KErrNone)
|
|
253 |
{
|
|
254 |
TEST_FRAMEWORK_LOG2(_L("RConnection::Open Could not open a socket servet session, got %d"),status);
|
|
255 |
return status;
|
|
256 |
}
|
|
257 |
}
|
|
258 |
iActiveDownload++;
|
|
259 |
iOpenSocketServer = ETrue;
|
|
260 |
}
|
|
261 |
TEST_FRAMEWORK_LOG2(_L("iActiveDownload changed iActiveDownload = %d"),iActiveDownload);
|
|
262 |
if(iUseSubConnection)
|
|
263 |
{
|
|
264 |
if(!iConnectionOpen)
|
|
265 |
{
|
|
266 |
status = iConnection.Open(iSocketServer);
|
|
267 |
if(status != KErrNone)
|
|
268 |
{
|
|
269 |
TEST_FRAMEWORK_LOG2(_L("RConnection::Open Could not open a connection, got %d"),status);
|
|
270 |
return status;
|
|
271 |
}
|
|
272 |
iConnectionOpen=ETrue;
|
|
273 |
}
|
|
274 |
status=iConnection.Start();
|
|
275 |
if(status != KErrNone)
|
|
276 |
{
|
|
277 |
TEST_FRAMEWORK_LOG2(_L("RConnection::Start Could not start a connection, got %d"),status);
|
|
278 |
return status;
|
|
279 |
}
|
|
280 |
if(!iSubConnectionOpen)
|
|
281 |
{
|
|
282 |
status = iSubConnection.Open(iSocketServer,RSubConnection::ECreateNew,iConnection);
|
|
283 |
if(status != KErrNone)
|
|
284 |
{
|
|
285 |
TEST_FRAMEWORK_LOG2(_L("RSubConnection::Open Could not open a sub connection, got %d"),status);
|
|
286 |
return status;
|
|
287 |
}
|
|
288 |
iSubConnectionOpen=ETrue;
|
|
289 |
}
|
|
290 |
}
|
|
291 |
if(!iSocketOpen)
|
|
292 |
{
|
|
293 |
if(iSubConnectionOpen)
|
|
294 |
{
|
|
295 |
status = iSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp,iSubConnection);
|
|
296 |
}
|
|
297 |
else
|
|
298 |
{
|
|
299 |
status = iSocket.Open(iSocketServer, KAfInet, KSockStream, KProtocolInetTcp);
|
|
300 |
}
|
|
301 |
if(status != KErrNone)
|
|
302 |
{
|
|
303 |
TEST_FRAMEWORK_LOG2(_L("RSocket::Open Could not open a socket, got %d"),status);
|
|
304 |
return status;
|
|
305 |
}
|
|
306 |
iSocketOpen=ETrue;
|
|
307 |
}
|
|
308 |
if(!iHostResolverOpen)
|
|
309 |
{
|
|
310 |
status = iHostResolver.Open(iSocketServer, KAfInet, KProtocolInetUdp);
|
|
311 |
if(status != KErrNone)
|
|
312 |
{
|
|
313 |
TEST_FRAMEWORK_LOG2(_L("RHostResolver::Open Could not open an host resolver, got %d"),status);
|
|
314 |
return status;
|
|
315 |
}
|
|
316 |
iHostResolverOpen=ETrue;
|
|
317 |
}
|
|
318 |
iHostResolver.GetByName(*iHost, iHostNameEntry, iStatus);
|
|
319 |
User::WaitForRequest(iStatus);
|
|
320 |
if(iStatus.Int()!=KErrNone)
|
|
321 |
{
|
|
322 |
TEST_FRAMEWORK_LOG2(_L("RHostResolver::GetByName Could get host by name, got %d"),iStatus.Int());
|
|
323 |
return iStatus.Int();
|
|
324 |
}
|
|
325 |
return Connect();
|
|
326 |
}
|
|
327 |
|
|
328 |
TInt CHTTPDownload::Connect()
|
|
329 |
/*
|
|
330 |
* Connect to the host
|
|
331 |
*
|
|
332 |
* @return KErrNone, if it had no problem, or the specific system error code that cause the problem.
|
|
333 |
*/
|
|
334 |
{
|
|
335 |
TInetAddr addr(iHostNameEntry().iAddr);
|
|
336 |
addr.SetPort(iPort);
|
|
337 |
|
|
338 |
TCoreEtelRequestStatus<RSocket> connectStatus(iSocket, &RSocket::CancelConnect);
|
|
339 |
iSocket.Connect(addr, connectStatus);
|
|
340 |
iTestStep->WaitForRequestWithTimeOut(connectStatus,ETimeLong);
|
|
341 |
if(connectStatus.Int()!=KErrNone)
|
|
342 |
{
|
|
343 |
TEST_FRAMEWORK_LOG2(_L("RSocket::Connect Could not connect to the host, got %d"),connectStatus.Int());
|
|
344 |
return connectStatus.Int();
|
|
345 |
}
|
|
346 |
return Get();
|
|
347 |
}
|
|
348 |
|
|
349 |
TInt CHTTPDownload::Get()
|
|
350 |
/*
|
|
351 |
* sends the GET request to the server
|
|
352 |
*
|
|
353 |
* @return KErrNone, if it had no problem, or the specific system error code that cause the problem.
|
|
354 |
*/
|
|
355 |
{
|
|
356 |
TBuf8<200> httpGetLine;
|
|
357 |
TBuf8<200> host8;
|
|
358 |
TBuf8<200> page8;
|
|
359 |
host8.Copy(*iHost);
|
|
360 |
page8.Copy(*iPage);
|
|
361 |
httpGetLine.Format(_L8("GET %S HTTP/1.0\r\nHost: %S\r\n\r\n"),&page8,&host8);
|
|
362 |
|
|
363 |
TCoreEtelRequestStatus<RSocket> getStatus(iSocket, &RSocket::CancelWrite);
|
|
364 |
iSocket.Write(httpGetLine, getStatus);
|
|
365 |
iTestStep->WaitForRequestWithTimeOut(getStatus,ETimeLong);
|
|
366 |
if(getStatus.Int()!=KErrNone)
|
|
367 |
{
|
|
368 |
TEST_FRAMEWORK_LOG2(_L("RSocket::Write Could not write to the host, got %d"),getStatus.Int());
|
|
369 |
return getStatus.Int();
|
|
370 |
}
|
|
371 |
|
|
372 |
iByteCount = 0;
|
|
373 |
iSocket.RecvOneOrMore(iWebPage,0,iStatus,iHttpHeaderLen);
|
|
374 |
if(iStatus==KRequestPending)
|
|
375 |
{
|
|
376 |
return KErrNone;
|
|
377 |
}
|
|
378 |
return iStatus.Int();
|
|
379 |
}
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
void CHTTPDownload::Close()
|
|
384 |
/*
|
|
385 |
* Close everything...
|
|
386 |
*
|
|
387 |
*/
|
|
388 |
{
|
|
389 |
if(iHostResolverOpen)
|
|
390 |
{
|
|
391 |
iHostResolver.Close();
|
|
392 |
iHostResolverOpen=EFalse;
|
|
393 |
}
|
|
394 |
if(iSocketOpen)
|
|
395 |
{
|
|
396 |
iSocket.Close();
|
|
397 |
iSocketOpen=EFalse;
|
|
398 |
}
|
|
399 |
if(iSubConnectionOpen)
|
|
400 |
{
|
|
401 |
iSubConnection.Close();
|
|
402 |
iSubConnectionOpen = EFalse;
|
|
403 |
}
|
|
404 |
if(iConnectionOpen)
|
|
405 |
{
|
|
406 |
iConnection.Close();
|
|
407 |
iConnectionOpen = EFalse;
|
|
408 |
}
|
|
409 |
}
|
|
410 |
|