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 |
// Determine if the body will be saved to disk
|
|
75 |
iSavingResponseBody = ETrue;
|
|
76 |
TBool cancelling = EFalse;
|
|
77 |
if (resp.HasBody() && (iLastStatusCode >= 200) && (iLastStatusCode < 300) && (iLastStatusCode != 204))
|
|
78 |
{
|
|
79 |
//iBytesDownloaded = 0;
|
|
80 |
TInt dataSize = resp.Body()->OverallDataSize();
|
|
81 |
if (dataSize >= 0) {
|
|
82 |
DP1("Response body size is %d", dataSize);
|
|
83 |
iBytesTotal = dataSize;
|
|
84 |
} else {
|
|
85 |
DP("Response body size is unknown");
|
|
86 |
iBytesTotal = -1;
|
|
87 |
}
|
|
88 |
iCallbacks.DownloadInfo(iHttpClient, dataSize);
|
|
89 |
|
|
90 |
cancelling = EFalse;
|
|
91 |
}
|
|
92 |
|
|
93 |
// If we're cancelling, must do it now..
|
|
94 |
if (cancelling)
|
|
95 |
{
|
|
96 |
DP("Transaction Cancelled");
|
|
97 |
aTransaction.Close();
|
|
98 |
iHttpClient->ClientRequestCompleteL(KErrCancel);
|
|
99 |
}
|
|
100 |
else if (iSavingResponseBody) // If we're saving, then open a file handle for the new file
|
|
101 |
{
|
|
102 |
iFileServ.Parse(iFileName, iParsedFileName);
|
|
103 |
TInt valid = iFileServ.IsValidName(iFileName);
|
60
|
104 |
|
2
|
105 |
if (!valid)
|
|
106 |
{
|
|
107 |
DP("The specified filename is not valid!.");
|
|
108 |
iSavingResponseBody = EFalse;
|
60
|
109 |
iHttpClient->ClientRequestCompleteL(KErrBadName);
|
2
|
110 |
}
|
|
111 |
else
|
|
112 |
{
|
|
113 |
if (iContinue) {
|
|
114 |
TInt err = iRespBodyFile.Open(iFileServ, iParsedFileName.FullName(),EFileWrite);
|
|
115 |
if (err)
|
|
116 |
{
|
60
|
117 |
DP2("There was an error opening file '%S', err=%d", &iParsedFileName.FullName(), err);
|
2
|
118 |
iSavingResponseBody = EFalse;
|
60
|
119 |
iHttpClient->ClientRequestCompleteL(KErrInUse);
|
2
|
120 |
User::Leave(err);
|
60
|
121 |
}
|
|
122 |
else
|
|
123 |
{
|
2
|
124 |
int pos = -KByteOverlap;
|
60
|
125 |
if((err=iRespBodyFile.Seek(ESeekEnd, pos)) != KErrNone)
|
|
126 |
{
|
2
|
127 |
DP("Failed to set position!");
|
60
|
128 |
iHttpClient->ClientRequestCompleteL(KErrWrite);
|
2
|
129 |
User::Leave(err);
|
60
|
130 |
}
|
|
131 |
iBytesDownloaded = (pos > 0) ? pos : 0;
|
|
132 |
iBytesTotal += iBytesDownloaded;
|
|
133 |
DP1("Total bytes is now %u", iBytesTotal);
|
|
134 |
DP1("Seeking end: %d", pos);
|
2
|
135 |
}
|
60
|
136 |
}
|
|
137 |
else
|
|
138 |
{
|
2
|
139 |
TInt err = iRespBodyFile.Replace(iFileServ,
|
|
140 |
iParsedFileName.FullName(),
|
|
141 |
EFileWrite);
|
|
142 |
if (err)
|
|
143 |
{
|
|
144 |
DP("There was an error replacing file");
|
|
145 |
iSavingResponseBody = EFalse;
|
|
146 |
User::Leave(err);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
} break;
|
|
153 |
case THTTPEvent::EGotResponseBodyData:
|
|
154 |
{
|
|
155 |
// Get the body data supplier
|
|
156 |
iRespBody = aTransaction.Response().Body();
|
|
157 |
|
|
158 |
// Some (more) body data has been received (in the HTTP response)
|
|
159 |
//DumpRespBody(aTransaction);
|
|
160 |
//DP1("Saving: %d", iSavingResponseBody);
|
|
161 |
// Append to the output file if we're saving responses
|
|
162 |
if (iSavingResponseBody)
|
|
163 |
{
|
|
164 |
TPtrC8 bodyData;
|
|
165 |
iRespBody->GetNextDataPart(bodyData);
|
|
166 |
iBytesDownloaded += bodyData.Length();
|
|
167 |
TInt error = iRespBodyFile.Write(bodyData);
|
60
|
168 |
|
2
|
169 |
// on writing error we close connection
|
|
170 |
if (error != KErrNone) {
|
60
|
171 |
iRespBodyFile.Close();
|
2
|
172 |
iCallbacks.FileError(error);
|
|
173 |
iHttpClient->ClientRequestCompleteL(error);
|
|
174 |
return;
|
|
175 |
}
|
|
176 |
|
|
177 |
if (!iSilent) {
|
|
178 |
iCallbacks.Progress(iHttpClient, iBytesDownloaded, iBytesTotal);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
// Done with that bit of body data
|
|
183 |
iRespBody->ReleaseData();
|
|
184 |
} break;
|
|
185 |
case THTTPEvent::EResponseComplete:
|
|
186 |
{
|
|
187 |
// The transaction's response is complete
|
|
188 |
|
|
189 |
DP("Transaction Complete");
|
|
190 |
DP("Closing file");
|
|
191 |
iRespBodyFile.Close();
|
|
192 |
} break;
|
|
193 |
case THTTPEvent::ESucceeded:
|
|
194 |
{
|
|
195 |
DP("Transaction Successful");
|
|
196 |
aTransaction.Close();
|
|
197 |
iHttpClient->ClientRequestCompleteL(KErrNone);
|
|
198 |
} break;
|
|
199 |
case THTTPEvent::EFailed:
|
|
200 |
{
|
|
201 |
DP("Transaction Failed");
|
|
202 |
aTransaction.Close();
|
|
203 |
|
|
204 |
if(iLastStatusCode == HTTPStatus::EOk || iLastStatusCode == HTTPStatus::ECreated || iLastStatusCode == HTTPStatus::EAccepted)
|
|
205 |
{
|
|
206 |
iLastStatusCode = KErrNone;
|
|
207 |
}
|
|
208 |
|
|
209 |
iHttpClient->ClientRequestCompleteL(iLastStatusCode);
|
|
210 |
} break;
|
|
211 |
case THTTPEvent::ERedirectedPermanently:
|
|
212 |
{
|
|
213 |
DP("Permanent Redirection");
|
|
214 |
} break;
|
|
215 |
case THTTPEvent::ERedirectedTemporarily:
|
|
216 |
{
|
|
217 |
DP("Temporary Redirection");
|
|
218 |
} break;
|
|
219 |
default:
|
|
220 |
{
|
|
221 |
DP1("<unrecognised event: %d>", aEvent.iStatus);
|
|
222 |
// close off the transaction if it's an error
|
|
223 |
if (aEvent.iStatus < 0)
|
|
224 |
{
|
|
225 |
aTransaction.Close();
|
|
226 |
iHttpClient->ClientRequestCompleteL(aEvent.iStatus);
|
|
227 |
}
|
|
228 |
} break;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
60
|
232 |
TInt CHttpEventHandler::MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& /*aEvent*/)
|
2
|
233 |
{
|
|
234 |
DP1("MHFRunError fired with error code %d", aError);
|
60
|
235 |
aTransaction.Close();
|
|
236 |
TRAP_IGNORE(iHttpClient->ClientRequestCompleteL(aError));
|
2
|
237 |
return KErrNone;
|
|
238 |
}
|
|
239 |
|
|
240 |
void CHttpEventHandler::SetSaveFileName(const TDesC &fName, TBool aContinue)
|
|
241 |
{
|
|
242 |
iFileName.Copy(fName);
|
|
243 |
iContinue = aContinue;
|
|
244 |
}
|
|
245 |
|
|
246 |
void CHttpEventHandler::DumpRespHeadersL(RHTTPTransaction& aTrans)
|
|
247 |
{
|
|
248 |
RHTTPResponse resp = aTrans.Response();
|
|
249 |
RStringPool strP = aTrans.Session().StringPool();
|
|
250 |
RHTTPHeaders hdr = resp.GetHeaderCollection();
|
|
251 |
THTTPHdrFieldIter it = hdr.Fields();
|
|
252 |
|
|
253 |
TBuf<KMaxHeaderNameLen> fieldName16;
|
|
254 |
TBuf<KMaxHeaderValueLen> fieldVal16;
|
|
255 |
|
|
256 |
while (it.AtEnd() == EFalse)
|
|
257 |
{
|
|
258 |
RStringTokenF fieldName = it();
|
|
259 |
RStringF fieldNameStr = strP.StringF(fieldName);
|
|
260 |
THTTPHdrVal fieldVal;
|
|
261 |
if (hdr.GetField(fieldNameStr,0,fieldVal) == KErrNone)
|
|
262 |
{
|
|
263 |
const TDesC8& fieldNameDesC = fieldNameStr.DesC();
|
|
264 |
fieldName16.Copy(fieldNameDesC.Left(KMaxHeaderNameLen));
|
|
265 |
switch (fieldVal.Type())
|
|
266 |
{
|
|
267 |
case THTTPHdrVal::KTIntVal:
|
|
268 |
DP2("%S: %d", &fieldName16, fieldVal.Int());
|
|
269 |
break;
|
|
270 |
case THTTPHdrVal::KStrFVal:
|
|
271 |
{
|
|
272 |
RStringF fieldValStr = strP.StringF(fieldVal.StrF());
|
|
273 |
const TDesC8& fieldValDesC = fieldValStr.DesC();
|
|
274 |
fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen));
|
|
275 |
DP2("%S: %S", &fieldName16, &fieldVal16);
|
|
276 |
}
|
|
277 |
break;
|
|
278 |
case THTTPHdrVal::KStrVal:
|
|
279 |
{
|
|
280 |
RString fieldValStr = strP.String(fieldVal.Str());
|
|
281 |
const TDesC8& fieldValDesC = fieldValStr.DesC();
|
|
282 |
fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen));
|
|
283 |
DP2("%S: %S", &fieldName16, &fieldVal16);
|
|
284 |
}
|
|
285 |
break;
|
|
286 |
case THTTPHdrVal::KDateVal:
|
|
287 |
{
|
|
288 |
TDateTime date = fieldVal.DateTime();
|
|
289 |
}
|
|
290 |
break;
|
|
291 |
default:
|
|
292 |
DP1("%S: <unrecognised value type>", &fieldName16);
|
|
293 |
break;
|
|
294 |
}
|
|
295 |
|
|
296 |
// Display realm for WWW-Authenticate header
|
|
297 |
RStringF wwwAuth = strP.StringF(HTTP::EWWWAuthenticate,RHTTPSession::GetTable());
|
|
298 |
if (fieldNameStr == wwwAuth)
|
|
299 |
{
|
|
300 |
// check the auth scheme is 'basic'
|
|
301 |
RStringF basic = strP.StringF(HTTP::EBasic,RHTTPSession::GetTable());
|
|
302 |
RStringF realm = strP.StringF(HTTP::ERealm,RHTTPSession::GetTable());
|
|
303 |
THTTPHdrVal realmVal;
|
|
304 |
if ((fieldVal.StrF() == basic) &&
|
|
305 |
(!hdr.GetParam(wwwAuth, realm, realmVal)))
|
|
306 |
{
|
|
307 |
RStringF realmValStr = strP.StringF(realmVal.StrF());
|
|
308 |
fieldVal16.Copy(realmValStr.DesC());
|
|
309 |
DP1("Realm is: %S", &fieldVal16);
|
|
310 |
}
|
|
311 |
}
|
|
312 |
}
|
|
313 |
++it;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
void CHttpEventHandler::DumpRespBody(RHTTPTransaction& aTrans)
|
|
318 |
{
|
|
319 |
MHTTPDataSupplier* body = aTrans.Response().Body();
|
|
320 |
TPtrC8 dataChunk;
|
|
321 |
TBool isLast = body->GetNextDataPart(dataChunk);
|
|
322 |
DumpIt(dataChunk);
|
|
323 |
if (isLast)
|
|
324 |
DP("Got last data chunk.");
|
|
325 |
}
|
|
326 |
|
|
327 |
|
|
328 |
void CHttpEventHandler::DumpIt(const TDesC8& aData)
|
|
329 |
//Do a formatted dump of binary data
|
|
330 |
{
|
|
331 |
// Iterate the supplied block of data in blocks of cols=80 bytes
|
|
332 |
const TInt cols=16;
|
|
333 |
TInt pos = 0;
|
|
334 |
TBuf<KMaxFileName - 2> logLine;
|
|
335 |
TBuf<KMaxFileName - 2> anEntry;
|
|
336 |
const TInt dataLength = aData.Length();
|
|
337 |
|
|
338 |
while (pos < dataLength)
|
|
339 |
{
|
|
340 |
//start-line hexadecimal( a 4 digit number)
|
|
341 |
anEntry.Format(TRefByValue<const TDesC>_L("%04x : "), pos);
|
|
342 |
logLine.Append(anEntry);
|
|
343 |
|
|
344 |
// Hex output
|
|
345 |
TInt offset;
|
|
346 |
for (offset = 0; offset < cols; ++offset)
|
|
347 |
{
|
|
348 |
if (pos + offset < aData.Length())
|
|
349 |
{
|
|
350 |
TInt nextByte = aData[pos + offset];
|
|
351 |
anEntry.Format(TRefByValue<const TDesC>_L("%02x "), nextByte);
|
|
352 |
logLine.Append(anEntry);
|
|
353 |
}
|
|
354 |
else
|
|
355 |
{
|
|
356 |
//fill the remaining spaces with blanks untill the cols-th Hex number
|
|
357 |
anEntry.Format(TRefByValue<const TDesC>_L(" "));
|
|
358 |
logLine.Append(anEntry);
|
|
359 |
}
|
|
360 |
}
|
|
361 |
anEntry.Format(TRefByValue<const TDesC>_L(": "));
|
|
362 |
logLine.Append(anEntry);
|
|
363 |
|
|
364 |
// Char output
|
|
365 |
for (offset = 0; offset < cols; ++offset)
|
|
366 |
{
|
|
367 |
if (pos + offset < aData.Length())
|
|
368 |
{
|
|
369 |
TInt nextByte = aData[pos + offset];
|
|
370 |
if ((nextByte >= ' ') && (nextByte <= '~'))
|
|
371 |
{
|
|
372 |
anEntry.Format(TRefByValue<const TDesC>_L("%c"), nextByte);
|
|
373 |
logLine.Append(anEntry);
|
|
374 |
}
|
|
375 |
else
|
|
376 |
{
|
|
377 |
anEntry.Format(TRefByValue<const TDesC>_L("."));
|
|
378 |
logLine.Append(anEntry);
|
|
379 |
}
|
|
380 |
}
|
|
381 |
else
|
|
382 |
{
|
|
383 |
anEntry.Format(TRefByValue<const TDesC>_L(" "));
|
|
384 |
logLine.Append(anEntry);
|
|
385 |
}
|
|
386 |
}
|
|
387 |
logLine.Zero();
|
|
388 |
|
|
389 |
// Advance to next byte segment (1 seg= cols)
|
|
390 |
pos += cols;
|
|
391 |
}
|
|
392 |
}
|
|
393 |
|
|
394 |
void CHttpEventHandler::SetSilent(TBool aSilent)
|
|
395 |
{
|
|
396 |
iSilent = aSilent;
|
|
397 |
}
|
|
398 |
|
|
399 |
void CHttpEventHandler::CloseSaveFile()
|
|
400 |
{
|
|
401 |
if(iRespBody != NULL)
|
|
402 |
{
|
|
403 |
if(iRespBodyFile.SubSessionHandle() != 0)
|
|
404 |
{
|
|
405 |
TInt size;
|
|
406 |
iRespBodyFile.Size(size);
|
|
407 |
DP2("Closing file at size %d, bytes downloaded %d", size, iBytesDownloaded);
|
|
408 |
iRespBodyFile.Close();
|
|
409 |
}
|
|
410 |
}
|
|
411 |
}
|
|
412 |
|