2
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB
|
|
3 |
*
|
|
4 |
* All rights reserved.
|
|
5 |
* This component and the accompanying materials are made available
|
|
6 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
7 |
* which accompanies this distribution, and is available
|
|
8 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
9 |
*
|
|
10 |
* Initial Contributors:
|
|
11 |
* EmbedDev AB - initial contribution.
|
|
12 |
*
|
|
13 |
* Contributors:
|
|
14 |
*
|
|
15 |
* Description:
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
// HttpEventHandler.cpp
|
|
20 |
#include <e32debug.h>
|
|
21 |
#include <httperr.h>
|
|
22 |
#include "HttpEventHandler.h"
|
|
23 |
#include "bautils.h"
|
|
24 |
#include "Httpclient.h"
|
|
25 |
|
|
26 |
void CHttpEventHandler::ConstructL()
|
|
27 |
{
|
|
28 |
//iVerbose = ETrue;
|
|
29 |
}
|
|
30 |
|
|
31 |
|
|
32 |
CHttpEventHandler::CHttpEventHandler(CHttpClient* aClient, MHttpClientObserver &aCallbacks, RFs& aFs):
|
|
33 |
iFileServ(aFs), iHttpClient(aClient), iCallbacks(aCallbacks)
|
|
34 |
{
|
|
35 |
}
|
|
36 |
|
|
37 |
|
|
38 |
CHttpEventHandler::~CHttpEventHandler()
|
|
39 |
{
|
|
40 |
}
|
|
41 |
|
|
42 |
|
|
43 |
CHttpEventHandler* CHttpEventHandler::NewLC(CHttpClient* aClient, MHttpClientObserver &aCallbacks, RFs& aFs)
|
|
44 |
{
|
|
45 |
CHttpEventHandler* me = new(ELeave)CHttpEventHandler(aClient, aCallbacks, aFs);
|
|
46 |
CleanupStack::PushL(me);
|
|
47 |
me->ConstructL();
|
|
48 |
return me;
|
|
49 |
}
|
|
50 |
|
|
51 |
|
|
52 |
CHttpEventHandler* CHttpEventHandler::NewL(CHttpClient* aClient, MHttpClientObserver &aCallbacks, RFs& aFs)
|
|
53 |
{
|
|
54 |
CHttpEventHandler* me = NewLC(aClient, aCallbacks, aFs);
|
|
55 |
CleanupStack::Pop(me);
|
|
56 |
return me;
|
|
57 |
}
|
|
58 |
|
|
59 |
void CHttpEventHandler::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
|
|
60 |
{
|
|
61 |
switch (aEvent.iStatus)
|
|
62 |
{
|
|
63 |
case THTTPEvent::EGotResponseHeaders:
|
|
64 |
{
|
|
65 |
// HTTP response headers have been received. We can determine now if there is
|
|
66 |
// going to be a response body to save.
|
|
67 |
RHTTPResponse resp = aTransaction.Response();
|
|
68 |
iLastStatusCode = resp.StatusCode();
|
60
|
69 |
DP1("Status: %d", iLastStatusCode);
|
2
|
70 |
|
|
71 |
// Dump the headers if we're being verbose
|
|
72 |
//DumpRespHeadersL(aTransaction);
|
|
73 |
|
|
74 |
if (resp.HasBody() && (iLastStatusCode >= 200) && (iLastStatusCode < 300) && (iLastStatusCode != 204))
|
|
75 |
{
|
|
76 |
TInt dataSize = resp.Body()->OverallDataSize();
|
|
77 |
if (dataSize >= 0) {
|
|
78 |
DP1("Response body size is %d", dataSize);
|
|
79 |
iBytesTotal = dataSize;
|
|
80 |
} else {
|
|
81 |
DP("Response body size is unknown");
|
|
82 |
iBytesTotal = -1;
|
|
83 |
}
|
|
84 |
iCallbacks.DownloadInfo(iHttpClient, dataSize);
|
|
85 |
|
|
86 |
}
|
|
87 |
|
167
|
88 |
DP1("iFileOpen=%d", iFileOpen);
|
|
89 |
if (!iFileOpen)
|
2
|
90 |
{
|
|
91 |
iFileServ.Parse(iFileName, iParsedFileName);
|
|
92 |
TInt valid = iFileServ.IsValidName(iFileName);
|
60
|
93 |
|
2
|
94 |
if (!valid)
|
|
95 |
{
|
|
96 |
DP("The specified filename is not valid!.");
|
60
|
97 |
iHttpClient->ClientRequestCompleteL(KErrBadName);
|
2
|
98 |
}
|
|
99 |
else
|
|
100 |
{
|
|
101 |
if (iContinue) {
|
|
102 |
TInt err = iRespBodyFile.Open(iFileServ, iParsedFileName.FullName(),EFileWrite);
|
|
103 |
if (err)
|
|
104 |
{
|
164
|
105 |
DP2("There was an error=%d opening file '%S'", err, &iParsedFileName.FullName());
|
60
|
106 |
iHttpClient->ClientRequestCompleteL(KErrInUse);
|
2
|
107 |
User::Leave(err);
|
60
|
108 |
}
|
|
109 |
else
|
|
110 |
{
|
167
|
111 |
iFileOpen = ETrue;
|
2
|
112 |
int pos = -KByteOverlap;
|
60
|
113 |
if((err=iRespBodyFile.Seek(ESeekEnd, pos)) != KErrNone)
|
|
114 |
{
|
2
|
115 |
DP("Failed to set position!");
|
60
|
116 |
iHttpClient->ClientRequestCompleteL(KErrWrite);
|
2
|
117 |
User::Leave(err);
|
60
|
118 |
}
|
167
|
119 |
iBytesDownloaded = (pos > 0) ? pos : 0;
|
|
120 |
iBytesTotal += iBytesDownloaded;
|
|
121 |
DP1("Total bytes is now %u", iBytesTotal);
|
|
122 |
DP1("Seeking end: %d", pos);
|
2
|
123 |
}
|
60
|
124 |
}
|
|
125 |
else
|
|
126 |
{
|
2
|
127 |
TInt err = iRespBodyFile.Replace(iFileServ,
|
|
128 |
iParsedFileName.FullName(),
|
|
129 |
EFileWrite);
|
|
130 |
if (err)
|
|
131 |
{
|
|
132 |
DP("There was an error replacing file");
|
|
133 |
User::Leave(err);
|
|
134 |
}
|
167
|
135 |
else
|
|
136 |
{
|
|
137 |
iFileOpen = ETrue;
|
|
138 |
}
|
2
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
|
142 |
} break;
|
|
143 |
case THTTPEvent::EGotResponseBodyData:
|
|
144 |
{
|
|
145 |
// Get the body data supplier
|
|
146 |
iRespBody = aTransaction.Response().Body();
|
|
147 |
|
|
148 |
// Some (more) body data has been received (in the HTTP response)
|
|
149 |
//DumpRespBody(aTransaction);
|
|
150 |
//DP1("Saving: %d", iSavingResponseBody);
|
|
151 |
// Append to the output file if we're saving responses
|
167
|
152 |
if (iFileOpen)
|
2
|
153 |
{
|
|
154 |
TPtrC8 bodyData;
|
|
155 |
iRespBody->GetNextDataPart(bodyData);
|
|
156 |
iBytesDownloaded += bodyData.Length();
|
|
157 |
TInt error = iRespBodyFile.Write(bodyData);
|
60
|
158 |
|
2
|
159 |
// on writing error we close connection
|
|
160 |
if (error != KErrNone) {
|
167
|
161 |
iFileOpen = EFalse;
|
60
|
162 |
iRespBodyFile.Close();
|
2
|
163 |
iCallbacks.FileError(error);
|
|
164 |
iHttpClient->ClientRequestCompleteL(error);
|
|
165 |
return;
|
|
166 |
}
|
|
167 |
|
|
168 |
if (!iSilent) {
|
|
169 |
iCallbacks.Progress(iHttpClient, iBytesDownloaded, iBytesTotal);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
// Done with that bit of body data
|
|
174 |
iRespBody->ReleaseData();
|
|
175 |
} break;
|
|
176 |
case THTTPEvent::EResponseComplete:
|
|
177 |
{
|
|
178 |
// The transaction's response is complete
|
|
179 |
DP("Transaction Complete");
|
|
180 |
DP("Closing file");
|
167
|
181 |
iFileOpen = EFalse;
|
2
|
182 |
iRespBodyFile.Close();
|
|
183 |
} break;
|
|
184 |
case THTTPEvent::ESucceeded:
|
|
185 |
{
|
|
186 |
DP("Transaction Successful");
|
167
|
187 |
iFileOpen = EFalse;
|
80
ea2321db6cb6
Forgot to close file handle on failed download. Fixed now. Also fix for SQLite static library.
teknolog
diff
changeset
|
188 |
iRespBodyFile.Close();
|
2
|
189 |
aTransaction.Close();
|
|
190 |
iHttpClient->ClientRequestCompleteL(KErrNone);
|
|
191 |
} break;
|
|
192 |
case THTTPEvent::EFailed:
|
|
193 |
{
|
|
194 |
DP("Transaction Failed");
|
167
|
195 |
iFileOpen = EFalse;
|
80
ea2321db6cb6
Forgot to close file handle on failed download. Fixed now. Also fix for SQLite static library.
teknolog
diff
changeset
|
196 |
iRespBodyFile.Close();
|
2
|
197 |
aTransaction.Close();
|
|
198 |
|
|
199 |
if(iLastStatusCode == HTTPStatus::EOk || iLastStatusCode == HTTPStatus::ECreated || iLastStatusCode == HTTPStatus::EAccepted)
|
|
200 |
{
|
|
201 |
iLastStatusCode = KErrNone;
|
|
202 |
}
|
|
203 |
|
|
204 |
iHttpClient->ClientRequestCompleteL(iLastStatusCode);
|
|
205 |
} break;
|
|
206 |
case THTTPEvent::ERedirectedPermanently:
|
|
207 |
{
|
|
208 |
DP("Permanent Redirection");
|
|
209 |
} break;
|
|
210 |
case THTTPEvent::ERedirectedTemporarily:
|
|
211 |
{
|
|
212 |
DP("Temporary Redirection");
|
|
213 |
} break;
|
|
214 |
default:
|
|
215 |
{
|
|
216 |
DP1("<unrecognised event: %d>", aEvent.iStatus);
|
|
217 |
// close off the transaction if it's an error
|
164
|
218 |
if (aEvent.iStatus < 0)
|
2
|
219 |
{
|
167
|
220 |
iFileOpen = EFalse;
|
80
ea2321db6cb6
Forgot to close file handle on failed download. Fixed now. Also fix for SQLite static library.
teknolog
diff
changeset
|
221 |
iRespBodyFile.Close();
|
2
|
222 |
aTransaction.Close();
|
|
223 |
iHttpClient->ClientRequestCompleteL(aEvent.iStatus);
|
|
224 |
}
|
|
225 |
} break;
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
60
|
229 |
TInt CHttpEventHandler::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& /*aEvent*/)
|
2
|
230 |
{
|
|
231 |
DP1("MHFRunError fired with error code %d", aError);
|
60
|
232 |
aTransaction.Close();
|
|
233 |
TRAP_IGNORE(iHttpClient->ClientRequestCompleteL(aError));
|
2
|
234 |
return KErrNone;
|
|
235 |
}
|
|
236 |
|
|
237 |
void CHttpEventHandler::SetSaveFileName(const TDesC &fName, TBool aContinue)
|
|
238 |
{
|
164
|
239 |
DP1("CHttpEventHandler::SetSaveFileName, aContinue=%d", aContinue);
|
2
|
240 |
iFileName.Copy(fName);
|
|
241 |
iContinue = aContinue;
|
|
242 |
}
|
|
243 |
|
|
244 |
void CHttpEventHandler::DumpRespHeadersL(RHTTPTransaction& aTrans)
|
|
245 |
{
|
|
246 |
RHTTPResponse resp = aTrans.Response();
|
|
247 |
RStringPool strP = aTrans.Session().StringPool();
|
|
248 |
RHTTPHeaders hdr = resp.GetHeaderCollection();
|
|
249 |
THTTPHdrFieldIter it = hdr.Fields();
|
|
250 |
|
|
251 |
TBuf<KMaxHeaderNameLen> fieldName16;
|
|
252 |
TBuf<KMaxHeaderValueLen> fieldVal16;
|
|
253 |
|
|
254 |
while (it.AtEnd() == EFalse)
|
|
255 |
{
|
|
256 |
RStringTokenF fieldName = it();
|
|
257 |
RStringF fieldNameStr = strP.StringF(fieldName);
|
|
258 |
THTTPHdrVal fieldVal;
|
|
259 |
if (hdr.GetField(fieldNameStr,0,fieldVal) == KErrNone)
|
|
260 |
{
|
|
261 |
const TDesC8& fieldNameDesC = fieldNameStr.DesC();
|
|
262 |
fieldName16.Copy(fieldNameDesC.Left(KMaxHeaderNameLen));
|
|
263 |
switch (fieldVal.Type())
|
|
264 |
{
|
|
265 |
case THTTPHdrVal::KTIntVal:
|
|
266 |
DP2("%S: %d", &fieldName16, fieldVal.Int());
|
|
267 |
break;
|
|
268 |
case THTTPHdrVal::KStrFVal:
|
|
269 |
{
|
|
270 |
RStringF fieldValStr = strP.StringF(fieldVal.StrF());
|
|
271 |
const TDesC8& fieldValDesC = fieldValStr.DesC();
|
|
272 |
fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen));
|
|
273 |
DP2("%S: %S", &fieldName16, &fieldVal16);
|
|
274 |
}
|
|
275 |
break;
|
|
276 |
case THTTPHdrVal::KStrVal:
|
|
277 |
{
|
|
278 |
RString fieldValStr = strP.String(fieldVal.Str());
|
|
279 |
const TDesC8& fieldValDesC = fieldValStr.DesC();
|
|
280 |
fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen));
|
|
281 |
DP2("%S: %S", &fieldName16, &fieldVal16);
|
|
282 |
}
|
|
283 |
break;
|
|
284 |
case THTTPHdrVal::KDateVal:
|
|
285 |
{
|
|
286 |
TDateTime date = fieldVal.DateTime();
|
|
287 |
}
|
|
288 |
break;
|
|
289 |
default:
|
|
290 |
DP1("%S: <unrecognised value type>", &fieldName16);
|
|
291 |
break;
|
|
292 |
}
|
|
293 |
|
|
294 |
// Display realm for WWW-Authenticate header
|
|
295 |
RStringF wwwAuth = strP.StringF(HTTP::EWWWAuthenticate,RHTTPSession::GetTable());
|
|
296 |
if (fieldNameStr == wwwAuth)
|
|
297 |
{
|
|
298 |
// check the auth scheme is 'basic'
|
|
299 |
RStringF basic = strP.StringF(HTTP::EBasic,RHTTPSession::GetTable());
|
|
300 |
RStringF realm = strP.StringF(HTTP::ERealm,RHTTPSession::GetTable());
|
|
301 |
THTTPHdrVal realmVal;
|
|
302 |
if ((fieldVal.StrF() == basic) &&
|
|
303 |
(!hdr.GetParam(wwwAuth, realm, realmVal)))
|
|
304 |
{
|
|
305 |
RStringF realmValStr = strP.StringF(realmVal.StrF());
|
|
306 |
fieldVal16.Copy(realmValStr.DesC());
|
|
307 |
DP1("Realm is: %S", &fieldVal16);
|
|
308 |
}
|
|
309 |
}
|
|
310 |
}
|
|
311 |
++it;
|
|
312 |
}
|
|
313 |
}
|
|
314 |
|
|
315 |
void CHttpEventHandler::DumpRespBody(RHTTPTransaction& aTrans)
|
|
316 |
{
|
|
317 |
MHTTPDataSupplier* body = aTrans.Response().Body();
|
|
318 |
TPtrC8 dataChunk;
|
|
319 |
TBool isLast = body->GetNextDataPart(dataChunk);
|
|
320 |
DumpIt(dataChunk);
|
|
321 |
if (isLast)
|
|
322 |
DP("Got last data chunk.");
|
|
323 |
}
|
|
324 |
|
|
325 |
|
|
326 |
void CHttpEventHandler::DumpIt(const TDesC8& aData)
|
|
327 |
//Do a formatted dump of binary data
|
|
328 |
{
|
|
329 |
// Iterate the supplied block of data in blocks of cols=80 bytes
|
|
330 |
const TInt cols=16;
|
|
331 |
TInt pos = 0;
|
|
332 |
TBuf<KMaxFileName - 2> logLine;
|
|
333 |
TBuf<KMaxFileName - 2> anEntry;
|
|
334 |
const TInt dataLength = aData.Length();
|
|
335 |
|
|
336 |
while (pos < dataLength)
|
|
337 |
{
|
|
338 |
//start-line hexadecimal( a 4 digit number)
|
|
339 |
anEntry.Format(TRefByValue<const TDesC>_L("%04x : "), pos);
|
|
340 |
logLine.Append(anEntry);
|
|
341 |
|
|
342 |
// Hex output
|
|
343 |
TInt offset;
|
|
344 |
for (offset = 0; offset < cols; ++offset)
|
|
345 |
{
|
|
346 |
if (pos + offset < aData.Length())
|
|
347 |
{
|
|
348 |
TInt nextByte = aData[pos + offset];
|
|
349 |
anEntry.Format(TRefByValue<const TDesC>_L("%02x "), nextByte);
|
|
350 |
logLine.Append(anEntry);
|
|
351 |
}
|
|
352 |
else
|
|
353 |
{
|
|
354 |
//fill the remaining spaces with blanks untill the cols-th Hex number
|
|
355 |
anEntry.Format(TRefByValue<const TDesC>_L(" "));
|
|
356 |
logLine.Append(anEntry);
|
|
357 |
}
|
|
358 |
}
|
|
359 |
anEntry.Format(TRefByValue<const TDesC>_L(": "));
|
|
360 |
logLine.Append(anEntry);
|
|
361 |
|
|
362 |
// Char output
|
|
363 |
for (offset = 0; offset < cols; ++offset)
|
|
364 |
{
|
|
365 |
if (pos + offset < aData.Length())
|
|
366 |
{
|
|
367 |
TInt nextByte = aData[pos + offset];
|
|
368 |
if ((nextByte >= ' ') && (nextByte <= '~'))
|
|
369 |
{
|
|
370 |
anEntry.Format(TRefByValue<const TDesC>_L("%c"), nextByte);
|
|
371 |
logLine.Append(anEntry);
|
|
372 |
}
|
|
373 |
else
|
|
374 |
{
|
|
375 |
anEntry.Format(TRefByValue<const TDesC>_L("."));
|
|
376 |
logLine.Append(anEntry);
|
|
377 |
}
|
|
378 |
}
|
|
379 |
else
|
|
380 |
{
|
|
381 |
anEntry.Format(TRefByValue<const TDesC>_L(" "));
|
|
382 |
logLine.Append(anEntry);
|
|
383 |
}
|
|
384 |
}
|
|
385 |
logLine.Zero();
|
|
386 |
|
|
387 |
// Advance to next byte segment (1 seg= cols)
|
|
388 |
pos += cols;
|
|
389 |
}
|
|
390 |
}
|
|
391 |
|
|
392 |
void CHttpEventHandler::SetSilent(TBool aSilent)
|
|
393 |
{
|
|
394 |
iSilent = aSilent;
|
|
395 |
}
|
|
396 |
|
|
397 |
void CHttpEventHandler::CloseSaveFile()
|
|
398 |
{
|
|
399 |
if(iRespBody != NULL)
|
|
400 |
{
|
|
401 |
if(iRespBodyFile.SubSessionHandle() != 0)
|
|
402 |
{
|
|
403 |
TInt size;
|
|
404 |
iRespBodyFile.Size(size);
|
|
405 |
DP2("Closing file at size %d, bytes downloaded %d", size, iBytesDownloaded);
|
|
406 |
iRespBodyFile.Close();
|
|
407 |
}
|
|
408 |
}
|
|
409 |
}
|
|
410 |
|