|
1 // Copyright (c) 2000-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 // System Include |
|
17 #include <push/pushmessage.h> |
|
18 #include "wapdecoder.h" |
|
19 |
|
20 // User Include |
|
21 #include "chttpresponse.h" |
|
22 |
|
23 /** |
|
24 Allocates and constructs a new WAP Push message object. |
|
25 |
|
26 CPushMessage takes ownership of aHeader and aBody. aHeader and aBody must be put on |
|
27 the cleanup stack before calling CPushMessage::NewL(), and the arguments must be poped |
|
28 off the cleanupstack after CPushMessage has been constructed. For example: |
|
29 @code |
|
30 CleanupStack::PushL(header); |
|
31 CleanupStack::PushL(body); |
|
32 CPushMessagemessage = CPushMessage::NewL(header, body); |
|
33 CleanupStack::Pop(2); //header, body |
|
34 CleanupStack::PushL(message); |
|
35 message->FunctionL(); |
|
36 @endcode |
|
37 |
|
38 If the arguments are data members, than they must be set to null after CPushMessage |
|
39 has been constructed. For example: |
|
40 @code |
|
41 iMessage = CPushMessage::NewL(iHeader, iBody); |
|
42 iHeader = NULL; |
|
43 iBody = NULL; |
|
44 @endcode |
|
45 |
|
46 CPushMessage::NewLC can not be implemented as the arguments must be poped of the cleanupstack |
|
47 or set to NULL before pushing CPushMessage object on the cleanupstack. |
|
48 |
|
49 @param aHeader |
|
50 Pointer to message header. Ownership passes to CPushMessage. |
|
51 |
|
52 @param aBody |
|
53 Pointer to message body. Ownership passes to CPushMessage. |
|
54 |
|
55 @return |
|
56 New WAP Push message object. |
|
57 */ |
|
58 EXPORT_C CPushMessage* CPushMessage::NewL(HBufC8* aHeader, HBufC8* aBody) |
|
59 { |
|
60 __ASSERT_DEBUG(aHeader!=NULL,User::Invariant()); |
|
61 CPushMessage* self = new (ELeave) CPushMessage(aHeader); |
|
62 CleanupStack::PushL(self); |
|
63 self->ConstructL(aHeader, aBody, NULL); |
|
64 CleanupStack::Pop(); |
|
65 return self; |
|
66 } |
|
67 |
|
68 |
|
69 /** |
|
70 Allocates and constructs a new WAP Push message object. |
|
71 |
|
72 It takes ownership of aHeader, aBody and aServerAddress. These must be put |
|
73 on the cleanup stack before calling NewL(), and popped off after the call |
|
74 returns. |
|
75 |
|
76 @param aHeader |
|
77 Pointer to message header. Ownership passes to CPushMessage. |
|
78 |
|
79 @param aBody |
|
80 Pointer to message body. Ownership passes to CPushMessage. |
|
81 |
|
82 @param aPushID |
|
83 Message push ID. |
|
84 |
|
85 @param aRemoteAddress |
|
86 Pointer to remote server address. Ownership passes to CPushMessage. |
|
87 |
|
88 @return |
|
89 New WAP Push message object. |
|
90 */ |
|
91 EXPORT_C CPushMessage* CPushMessage::NewL(HBufC8* aHeader, HBufC8* aBody, |
|
92 TUint8 aPushID, HBufC8* aRemoteAddress) |
|
93 { |
|
94 __ASSERT_DEBUG(aHeader!=NULL,User::Invariant()); |
|
95 CPushMessage* self = new (ELeave) CPushMessage(aHeader, aPushID); |
|
96 CleanupStack::PushL(self); |
|
97 self->ConstructL(aHeader, aBody, aRemoteAddress); |
|
98 CleanupStack::Pop(); |
|
99 return self; |
|
100 } |
|
101 |
|
102 |
|
103 /** |
|
104 Constructor. |
|
105 |
|
106 @param aHeader |
|
107 Pointer to the push message header. |
|
108 |
|
109 @param aPushID |
|
110 Push Message ID |
|
111 */ |
|
112 CPushMessage::CPushMessage(HBufC8* aHeader, TUint8 aPushID) : iPushID(aPushID) |
|
113 { |
|
114 iPushHeader.Set(*aHeader); |
|
115 } |
|
116 |
|
117 |
|
118 /** |
|
119 Constructor. |
|
120 Ownership of aHeader is passed to the CHTTPResponse member. |
|
121 |
|
122 @param aHeader |
|
123 Pointer to 8 bit descriptor with the header data. |
|
124 |
|
125 @param aBody |
|
126 Pointer to 8 bit body data. |
|
127 |
|
128 @param aRemoteAddress |
|
129 Pointer to 8 bit remote server address. |
|
130 */ |
|
131 void CPushMessage::ConstructL(HBufC8* aHeader, HBufC8* aBody, HBufC8* aRemoteAddress) |
|
132 { |
|
133 iHttpResponse = CHTTPResponse::NewL(); |
|
134 iHttpResponse->AddResponse(aHeader);// Passes over ownership to iResponse |
|
135 iPushBody = aBody;// It is now safe to take ownership of aBody. CPushMessage::NewL can't fail after this point. |
|
136 iServerAddress = aRemoteAddress; |
|
137 } |
|
138 |
|
139 |
|
140 /** |
|
141 Destructor. |
|
142 */ |
|
143 EXPORT_C CPushMessage::~CPushMessage() |
|
144 { |
|
145 delete iHttpResponse; |
|
146 delete iPushBody; |
|
147 delete iServerAddress; |
|
148 delete iContentType; |
|
149 } |
|
150 |
|
151 |
|
152 /** |
|
153 Gets the X-Wap-Application-Id field in the WSP header. |
|
154 |
|
155 As the App Id can be either an integer or a URI string, the function handles |
|
156 both of these data types. |
|
157 |
|
158 @param aAppURI |
|
159 On return, App Id URI (if present). |
|
160 |
|
161 @param aAppID |
|
162 On return, App Id integer (if present). |
|
163 |
|
164 @param aIsAnInt |
|
165 On return, ETrue if the App ID is an integer, EFalse if it's a URI. |
|
166 |
|
167 @return |
|
168 True if the field was found, false if not. |
|
169 |
|
170 @deprecated |
|
171 Use the new CPushMessage::GetAppIdL function. |
|
172 */ |
|
173 EXPORT_C TBool CPushMessage::GetAppID(TPtrC8& aAppURI, TInt& aAppID, TBool& aIsAnInt) |
|
174 { |
|
175 TInt index = iHttpResponse->LocateField(EHttpXWapApplicationID); |
|
176 |
|
177 if (index == KErrNotFound) //Field isn't present in the header |
|
178 return EFalse; |
|
179 |
|
180 // Use our friendship to take a peek at the first byte |
|
181 // of the next field - is it a text character? |
|
182 TInt nextByte = iHttpResponse->iResponse->Des()[index]; |
|
183 |
|
184 if (nextByte > 0x1F && nextByte < 0x80) // call FindField as it's a string |
|
185 { |
|
186 iHttpResponse->FindField(EHttpXWapApplicationID, aAppURI); |
|
187 aIsAnInt = EFalse; |
|
188 } |
|
189 else |
|
190 { |
|
191 if (nextByte >=0x80) // Bit 7 set - encoded short integer value <=127 |
|
192 aAppID = (nextByte & 0x7F); // AND away Bit7 to get the integer value |
|
193 else |
|
194 { |
|
195 TPtrC8 sourceBuffer = iHttpResponse->iResponse->Des().Mid(index); |
|
196 TWapBinCodex::ExtractMultiOctetInteger(sourceBuffer, aAppID); |
|
197 } |
|
198 aIsAnInt = ETrue; |
|
199 } |
|
200 |
|
201 return ETrue; |
|
202 } |
|
203 |
|
204 /** |
|
205 Gets the X-Wap-Application-Id field in the WSP header. |
|
206 |
|
207 The App Id can be in either an integer or a URI string format - this function |
|
208 handles both of these formats. |
|
209 |
|
210 @param aAppURI |
|
211 An output argument with the App ID, URI string format. Only valid if aIsInt is |
|
212 true and the function returns a value of true. |
|
213 |
|
214 @param aAppID |
|
215 An output argument with the App ID, integer format. Only valid if aIsInt is true |
|
216 and the function returns a value of true. |
|
217 |
|
218 @param aIsInt |
|
219 An output argument indicating if the App ID is in integer format. Only valid if |
|
220 the function returns a value of true. |
|
221 |
|
222 @return |
|
223 A value of true if the X-Wap-Application-Id field was found, false if not. |
|
224 |
|
225 @leave KErrCorrupt |
|
226 The App ID could not be parsed as the binary data was corrupted. |
|
227 */ |
|
228 EXPORT_C TBool CPushMessage::GetAppIdL(TPtrC8& aAppURI, TInt& aAppID, TBool& aIsInt) |
|
229 { |
|
230 TInt index = iHttpResponse->LocateField(EHttpXWapApplicationID); |
|
231 |
|
232 if (index == KErrNotFound) // X-Wap-Application-Id field isn't present |
|
233 return EFalse; |
|
234 |
|
235 User::LeaveIfError(index); |
|
236 |
|
237 // Use our friendship to take a peek at the first byte |
|
238 // of the next field - is it a text character? |
|
239 TInt nextByte = iHttpResponse->iResponse->Des()[index]; |
|
240 |
|
241 if (nextByte > 0x1F && nextByte < 0x80) |
|
242 { |
|
243 // The AppId is given in Uri-value format - a string. Use FindField |
|
244 // to get the AppId. |
|
245 iHttpResponse->FindField(EHttpXWapApplicationID, aAppURI); |
|
246 aIsInt = EFalse; |
|
247 } |
|
248 else |
|
249 { |
|
250 // The AppId is in either Short-integer or Long-integer format... |
|
251 if (nextByte >=0x80) |
|
252 { |
|
253 // The AppId is in Short-integer format - |
|
254 // Short-integer = OCTET |
|
255 // ; encoded as 7-nit data - 1xxx xxxx |
|
256 // |
|
257 // Using AND function to mask 7th bit and get the AppId. |
|
258 aAppID = (nextByte & 0x7F); |
|
259 } |
|
260 else |
|
261 { |
|
262 // The AppId is in Long-integer format. |
|
263 TPtrC8 sourceBuffer = iHttpResponse->iResponse->Des().Mid(index); |
|
264 User::LeaveIfError(TWapBinCodex::ParseMultiOctetInteger(sourceBuffer, aAppID)); |
|
265 } |
|
266 aIsInt = ETrue; |
|
267 } |
|
268 |
|
269 return ETrue; |
|
270 } |
|
271 |
|
272 |
|
273 /** |
|
274 Gets the Content-Type header. |
|
275 |
|
276 If the content type is not found or not recognised, a default of "application/octet-stream" |
|
277 is returned. |
|
278 |
|
279 @param aContentType |
|
280 On return, Content-Type header value. An empty descriptor (zero-length) indicates an |
|
281 allocation error occured. |
|
282 */ |
|
283 EXPORT_C void CPushMessage::GetContentType(TPtrC& aContentType) const |
|
284 { |
|
285 if (iPushHeader.Length()) |
|
286 { |
|
287 // Get content type from HTTPResponse |
|
288 TPtrC8 type; |
|
289 iHttpResponse->ContentType(type); |
|
290 |
|
291 // Copy content-type to local unicode buffer |
|
292 delete iContentType; |
|
293 iContentType = NULL; |
|
294 iContentType = HBufC::New(type.Length()); |
|
295 if(iContentType) |
|
296 { |
|
297 TPtr typeBuffer(iContentType->Des()); |
|
298 typeBuffer.Copy(type); |
|
299 aContentType.Set(*iContentType); |
|
300 } |
|
301 else // Buffer could not be created so set to null descriptor |
|
302 aContentType.Set(KNullDesC()); |
|
303 } |
|
304 } |
|
305 |
|
306 |
|
307 /** |
|
308 Gets a specified header field. |
|
309 |
|
310 @param aField |
|
311 Header field identifier. |
|
312 |
|
313 @param aFieldValue |
|
314 On return, the header field text. |
|
315 |
|
316 @return |
|
317 True if the field was found, false if not. |
|
318 */ |
|
319 EXPORT_C TBool CPushMessage::GetHeaderField(THttpHeaderField aField, TPtrC8& aFieldValue) const |
|
320 { |
|
321 return iHttpResponse->FindField(aField, aFieldValue); |
|
322 } |
|
323 |
|
324 |
|
325 /** |
|
326 Gets a specified header field as an 8-bit octet data. |
|
327 |
|
328 @param aField |
|
329 Header field identifier. |
|
330 |
|
331 @param aFieldValue |
|
332 On return, the header field value in binary form. |
|
333 |
|
334 @return |
|
335 True if the field was found, false if not. |
|
336 */ |
|
337 EXPORT_C TBool CPushMessage::GetBinaryHeaderField(THttpHeaderField aField, TPtrC8& aFieldValue) const |
|
338 { |
|
339 return iHttpResponse->FindBinaryDescField(aField, aFieldValue); |
|
340 } |
|
341 |
|
342 |
|
343 /** |
|
344 Gets a specified header field as a date/time value. |
|
345 |
|
346 @param aField |
|
347 Header field identifier. |
|
348 |
|
349 @param aTime |
|
350 On return, the header field value in date/time form. |
|
351 |
|
352 @return |
|
353 True if the field was found, false if not. |
|
354 */ |
|
355 EXPORT_C TBool CPushMessage::GetHeaderField(THttpHeaderField aField, TTime& aTime) const |
|
356 { |
|
357 return iHttpResponse->FindField(aField, aTime); |
|
358 } |
|
359 |
|
360 |
|
361 /** |
|
362 Gets the message body. |
|
363 |
|
364 @param aMessageBodyPtr |
|
365 On return, the message body. KNullDesC8 if no body was found. |
|
366 |
|
367 @return |
|
368 True if a message body was found, false if it was empty. |
|
369 */ |
|
370 EXPORT_C TBool CPushMessage::GetMessageBody(TPtrC8& aMessageBodyPtr) const |
|
371 { |
|
372 if (iPushBody == NULL) |
|
373 { |
|
374 aMessageBodyPtr.Set(KNullDesC8); |
|
375 return EFalse; |
|
376 } |
|
377 else |
|
378 { |
|
379 aMessageBodyPtr.Set(*iPushBody); |
|
380 return ETrue; |
|
381 } |
|
382 } |
|
383 |
|
384 |
|
385 /** |
|
386 Gets the complete message header. |
|
387 |
|
388 @param aMessageHeaderPtr |
|
389 On return, the complete message header. KNullDesC8 if no header was found. |
|
390 |
|
391 @return |
|
392 True if a message header was found, false if it was empty. |
|
393 */ |
|
394 EXPORT_C TBool CPushMessage::GetHeader(TPtrC8& aMessageHeaderPtr) const |
|
395 { |
|
396 aMessageHeaderPtr.Set(iPushHeader); |
|
397 return ETrue; |
|
398 } |
|
399 |
|
400 |
|
401 /** |
|
402 Gets the remote server address. |
|
403 |
|
404 @param aServerAddress |
|
405 On return, the remote server address (if present). |
|
406 |
|
407 @return |
|
408 True if the remote server address was found, false if not. |
|
409 */ |
|
410 EXPORT_C TBool CPushMessage::GetServerAddress(TPtrC8& aServerAddress) const |
|
411 { |
|
412 if (iServerAddress) |
|
413 { |
|
414 aServerAddress.Set(*iServerAddress); |
|
415 return ETrue; |
|
416 } |
|
417 return EFalse; |
|
418 } |
|
419 |
|
420 |
|
421 /** |
|
422 Gets the Push ID. |
|
423 |
|
424 @param aPushID |
|
425 On return, the ID value. |
|
426 |
|
427 @return |
|
428 Indicates if the ID has been returned: EFalse if the ID was not set. |
|
429 */ |
|
430 EXPORT_C TBool CPushMessage::GetPushId(TUint8& aPushID) const |
|
431 { |
|
432 if (iPushID) |
|
433 { |
|
434 aPushID=iPushID; |
|
435 return ETrue; |
|
436 } |
|
437 return EFalse; |
|
438 } |
|
439 |
|
440 |
|
441 /** |
|
442 Sets the PDU source allowed flag. |
|
443 |
|
444 @param aAllowed ETrue - push PDU source address is present in whitelist. |
|
445 EFalse - push PDU is from an unknown origin. |
|
446 */ |
|
447 EXPORT_C void CPushMessage::SetMessageAllowed(TBool aAllowed) |
|
448 { |
|
449 iAllowed = aAllowed; |
|
450 } |
|
451 |
|
452 |
|
453 /** |
|
454 Gets the PDU source allowed flag. |
|
455 |
|
456 @return ETrue - push PDU source address is present in whitelist. |
|
457 EFalse - push PDU is from an unknown origin. |
|
458 */ |
|
459 EXPORT_C TBool CPushMessage::MessageAllowed() const |
|
460 { |
|
461 return iAllowed; |
|
462 } |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |