|
1 // Copyright (c) 2008-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 #include <httpstringconstants.h> |
|
17 #include <inetprottextutils.h> |
|
18 #include <httperr.h> |
|
19 #include <rmemcell.h> |
|
20 |
|
21 #include "cupnpresponseparser.h" |
|
22 #include "tupnpmessagepanic.h" |
|
23 |
|
24 _LIT8(KHeaderSeparator, "\n"); |
|
25 |
|
26 __FLOG_STMT(_LIT8(KSubsys,"UPnPResParser");) |
|
27 __FLOG_STMT(_LIT8(KComponent,"UPnPMessage");) |
|
28 |
|
29 _LIT8(KHTTP, "HTTP"); |
|
30 |
|
31 EXPORT_C CUpnpResponseParser* CUpnpResponseParser::NewL(MParserObserver& aObserver) |
|
32 { |
|
33 CUpnpResponseParser* self = new(ELeave) CUpnpResponseParser(aObserver); |
|
34 CleanupStack::PushL(self); |
|
35 self->ConstructL(); |
|
36 CleanupStack::Pop(self); |
|
37 return self; |
|
38 } |
|
39 |
|
40 CUpnpResponseParser::CUpnpResponseParser(MParserObserver& aObserver) |
|
41 :iObserver(aObserver) |
|
42 { |
|
43 } |
|
44 |
|
45 EXPORT_C CUpnpResponseParser::~CUpnpResponseParser() |
|
46 { |
|
47 iMessageParser.Close(); |
|
48 iBodyParts.Close(); |
|
49 if(!iMsgBuf.IsEmpty()) |
|
50 { |
|
51 iMsgBuf.Free(); |
|
52 } |
|
53 iRawDataArray.Close(); |
|
54 __FLOG(_L8("-> Response parser destroyed")); |
|
55 __FLOG_CLOSE; |
|
56 } |
|
57 |
|
58 void CUpnpResponseParser::ConstructL() |
|
59 { |
|
60 iMessageParser.OpenL(*this); |
|
61 __FLOG_OPEN(KSubsys, KComponent); |
|
62 __FLOG(_L8("-> Response parser created")); |
|
63 } |
|
64 |
|
65 void CUpnpResponseParser::ResetData() |
|
66 { |
|
67 iMessageParser.Reset(); |
|
68 iBodyParts.Reset(); |
|
69 iOverallDataSize = 0; |
|
70 iFlags = 0; |
|
71 } |
|
72 |
|
73 TBool CUpnpResponseParser::GetNextDataPart(TPtrC8& aDataPart) |
|
74 { |
|
75 __FLOG(_L8("-> Supplying response body part")); |
|
76 |
|
77 TInt bodyPartsCount = iBodyParts.Count(); |
|
78 __ASSERT_ALWAYS((bodyPartsCount > 0), TUPnPMessagePanic::Panic(TUPnPMessagePanic::ENoBodyPartInDataSupplier)); |
|
79 |
|
80 // Provide the first chunk. |
|
81 aDataPart.Set(iBodyParts[0]); |
|
82 __FLOG_1(_L8("%S"), &aDataPart); |
|
83 |
|
84 return (BodyComplete() && bodyPartsCount == 1); |
|
85 } |
|
86 |
|
87 void CUpnpResponseParser::ReleaseData() |
|
88 { |
|
89 __FLOG(_L8("-> Releasing response body part")); |
|
90 |
|
91 // Remove the oldest chunk. |
|
92 if( iBodyParts.Count() > 0 ) |
|
93 { |
|
94 iBodyParts.Remove(0); |
|
95 } |
|
96 |
|
97 // Are there any more chunks? |
|
98 if(iBodyParts.Count() > 0) |
|
99 { |
|
100 // Notify the sender about the presence of the body. He can then call |
|
101 // MHTTPDataSupplier::GetNextDataPart() to get the body data. |
|
102 iObserver.GotBodyData(); |
|
103 } |
|
104 |
|
105 else |
|
106 { |
|
107 // Does this data packet need to be released? |
|
108 if( NotifyReleaseData() ) |
|
109 { |
|
110 // Notify sender that the current data is no longer needed. |
|
111 // Can release as there are no body chunks waiting to be passed to the |
|
112 // client. |
|
113 iObserver.DataParsed(); |
|
114 iFlags &= ~ENotifyReleaseData; |
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 TInt CUpnpResponseParser::OverallDataSize() |
|
120 { |
|
121 return (iOverallDataSize < 0) ? KErrNotFound : iOverallDataSize; |
|
122 } |
|
123 |
|
124 TInt CUpnpResponseParser::Reset() |
|
125 { |
|
126 return KErrNotSupported; |
|
127 } |
|
128 |
|
129 void CUpnpResponseParser::GetDataPacket(TPtrC8& aData) |
|
130 { |
|
131 __FLOG(_L8("-> Supplying response data to the HTTP message parser")); |
|
132 aData.Set(iRawDataArray[0]); |
|
133 iRawDataArray.Remove(0); |
|
134 } |
|
135 |
|
136 void CUpnpResponseParser::ReleaseDataPacket() |
|
137 { |
|
138 __FLOG(_L8("-> Releasing response data")); |
|
139 |
|
140 if( ConsumingResponse() && MessageComplete() ) |
|
141 { |
|
142 // Ok, the response was a 1xx message which has been consumed. Reset the |
|
143 // this object and continue parsing the next part of the response. |
|
144 ResetData(); |
|
145 iMessageParser.ReceivedMessageData(); |
|
146 } |
|
147 else if(iBodyParts.Count() == 0) |
|
148 { |
|
149 if(iRawDataArray.Count() > 0 && !IsExcessData()) |
|
150 { |
|
151 iMessageParser.ReceivedMessageData(); |
|
152 } |
|
153 else |
|
154 { |
|
155 // Notify sender that the current data is no longer needed. |
|
156 // Can release as there are no body chunks waiting to be passed to the |
|
157 // client. |
|
158 iObserver.DataParsed(); |
|
159 } |
|
160 } |
|
161 else |
|
162 { |
|
163 // Flag that the data needs to be released |
|
164 iFlags |= ENotifyReleaseData; |
|
165 } |
|
166 } |
|
167 |
|
168 void CUpnpResponseParser::StartLineL(const TDesC8& aStartLine) |
|
169 { |
|
170 // The RFC2616 defines the Status-Line as follows - |
|
171 // |
|
172 // Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF |
|
173 // |
|
174 // First extract the HTTP-Version |
|
175 __FLOG(_L8("-> Got the reponse start line")); |
|
176 __FLOG_1(_L8("%S"), &aStartLine); |
|
177 |
|
178 TPtrC8 data = aStartLine; |
|
179 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft)); |
|
180 |
|
181 // HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT |
|
182 TInt consumed = data.Locate('/'); |
|
183 User::LeaveIfError(consumed); |
|
184 |
|
185 if((consumed > KHTTP().Length()) || (consumed < KHTTP().Length())) |
|
186 User::Leave(KErrCorrupt); |
|
187 |
|
188 TPtrC8 http = data.Left(consumed); |
|
189 if( http.CompareF(iResponse->StringPool().StringF(HTTP::EHTTP, THTTPTable::Table()).DesC()) != 0 ) |
|
190 User::Leave(KErrCorrupt); |
|
191 |
|
192 // Skip past the HTTP and "/" |
|
193 data.Set(data.Mid(consumed + 1)); |
|
194 |
|
195 // Extract the major version number |
|
196 TInt major; |
|
197 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, major); |
|
198 User::LeaveIfError(consumed); |
|
199 |
|
200 // Skip past major version number and the "." |
|
201 data.Set(data.Mid(consumed + 1)); |
|
202 |
|
203 // Extract the minor version number |
|
204 TInt minor; |
|
205 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, minor); |
|
206 User::LeaveIfError(consumed); |
|
207 |
|
208 // Skip past minor version number and the SP |
|
209 data.Set(data.Mid(consumed + 1)); |
|
210 |
|
211 // Clear any extra surrounding whitespace |
|
212 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveBoth)); |
|
213 |
|
214 // Extract the status code |
|
215 TInt status; |
|
216 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, status); |
|
217 User::LeaveIfError(consumed); |
|
218 |
|
219 if( data.Length() > consumed ) |
|
220 { |
|
221 // Skip past status code and the SP |
|
222 data.Set(data.Mid(consumed + 1)); |
|
223 |
|
224 // Remaining data is the status reason - trim any leading whitespace as right side already trimmed |
|
225 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft)); |
|
226 } |
|
227 else |
|
228 { |
|
229 // No reason phrase following the status code |
|
230 data.Set(KNullDesC8()); |
|
231 } |
|
232 // Populate the Status-Line info |
|
233 RStringF reason = iResponse->StringPool().OpenFStringL(data); |
|
234 |
|
235 RResponse response = iResponse->Handle(); |
|
236 |
|
237 TVersion version(major, minor, 0); |
|
238 response.SetVersion(version); |
|
239 response.SetStatusCode(status); |
|
240 response.SetStatusText(reason); |
|
241 reason.Close(); |
|
242 |
|
243 // Check to see if a body is expected |
|
244 if( HTTPStatus::IsInformational(status) ) |
|
245 { |
|
246 // 1xx status - no body and need to consume this response |
|
247 iFlags |= EConsumingResponse; |
|
248 } |
|
249 } |
|
250 |
|
251 void CUpnpResponseParser::HeaderL(const TDesC8& aFieldName, TDesC8& aFieldValue) |
|
252 { |
|
253 // Only set the headers if we are not consuming the response. |
|
254 __FLOG(_L8("-> Got header value pair")); |
|
255 __FLOG_2(_L8("%S: %S"), &aFieldName, &aFieldValue); |
|
256 |
|
257 if( !ConsumingResponse() ) |
|
258 { |
|
259 RStringF name = iResponse->StringPool().OpenFStringL(aFieldName); |
|
260 CleanupClosePushL(name); |
|
261 |
|
262 iResponse->Handle().GetHeaderCollection().SetRawFieldL(name, aFieldValue, KHeaderSeparator); |
|
263 |
|
264 CleanupStack::PopAndDestroy(&name); |
|
265 } |
|
266 } |
|
267 |
|
268 TInt CUpnpResponseParser::BodySizeL() |
|
269 { |
|
270 if( !ConsumingResponse() ) |
|
271 { |
|
272 // Notify the sender that all the response headers have been parsed. |
|
273 iObserver.GotHeaders(); |
|
274 } |
|
275 |
|
276 // Check for a body... |
|
277 if( ConsumingResponse() || // this implies that the status code was 1xx - no body |
|
278 iResponse->Handle().StatusCode() == 204 || |
|
279 iResponse->Handle().StatusCode() == 304 && |
|
280 // if 2xx response... |
|
281 ( HTTPStatus::IsSuccessful(iResponse->Handle().StatusCode()) ) ) |
|
282 { |
|
283 // No entity body is expected as specified in RFC2616 section 4.4. |
|
284 iOverallDataSize = MHttpMessageParserObserver::ENoBody; |
|
285 iFlags |= EBodyComplete; |
|
286 __FLOG(_L8("-> Response has no body")); |
|
287 return iOverallDataSize; |
|
288 } |
|
289 |
|
290 // A body is expected - find the length. First check for a Transfer-Encoding |
|
291 // header field. |
|
292 iResponse->Handle().SetBody(*this); |
|
293 THTTPHdrVal value; |
|
294 RStringF name = iResponse->StringPool().StringF(HTTP::ETransferEncoding, THTTPTable::Table()); |
|
295 |
|
296 TInt err = iResponse->Handle().GetHeaderCollection().GetField(name, 0, value); |
|
297 if( err != KErrNone && err != KErrNotFound ) |
|
298 User::Leave(err); |
|
299 |
|
300 // It exists - what's the value? |
|
301 if( err == KErrNone && value.Type() == THTTPHdrVal::KStrFVal && |
|
302 value.StrF().Index(THTTPTable::Table()) == HTTP::EChunked ) |
|
303 { |
|
304 // The Transfer-Encoding header is Chunked and as the chunked |
|
305 // encoding is removed, we remove the header. |
|
306 iResponse->Handle().GetHeaderCollection().RemoveField(name); |
|
307 |
|
308 // As the entity body is chunked the overall data size is unknown. |
|
309 iOverallDataSize = MHttpMessageParserObserver::EChunked; |
|
310 __FLOG(_L8("-> Response has chunked body")); |
|
311 return iOverallDataSize; |
|
312 } |
|
313 |
|
314 // Either no Transfer-Encoding header was present - now check for a |
|
315 // Content-Length header. |
|
316 err = KErrNone; |
|
317 name = iResponse->StringPool().StringF(HTTP::EContentLength, THTTPTable::Table()); |
|
318 err = iResponse->Handle().GetHeaderCollection().GetField(name, 0, value); |
|
319 |
|
320 if( err != KErrNone && err != KErrNotFound ) |
|
321 User::Leave(err); |
|
322 |
|
323 if( err == KErrNone && value.Type() == THTTPHdrVal::KTIntVal ) |
|
324 { |
|
325 // Content-Length header value specified the length of entity in bytes. |
|
326 iOverallDataSize = value.Int(); |
|
327 __FLOG_1(_L8("-> Response body length = %d"), iOverallDataSize); |
|
328 return iOverallDataSize; |
|
329 } |
|
330 |
|
331 // There was no Content-Length header either, so the server will signal the |
|
332 // end of the message by closing the connection - overall data size unknown. |
|
333 iOverallDataSize = MHttpMessageParserObserver::EUnknown; |
|
334 __FLOG(_L8("-> Response body size is unknown")); |
|
335 return iOverallDataSize; |
|
336 } |
|
337 |
|
338 void CUpnpResponseParser::BodyChunkL(const TDesC8& aData) |
|
339 { |
|
340 __FLOG(_L8("-> Got response body chunk")); |
|
341 __FLOG_1(_L8("%S"), &aData); |
|
342 |
|
343 iFlags |= EBodyPresent; |
|
344 if (aData.Length() > 0) |
|
345 { |
|
346 iBodyParts.Append(aData); |
|
347 } |
|
348 |
|
349 // Only notify the client that there is a body part if there is one. |
|
350 if(iBodyParts.Count() > 0) |
|
351 { |
|
352 // Notify the sender about the presence of the body. He can then call |
|
353 // MHTTPDataSupplier::GetNextDataPart() to get the body data. |
|
354 iObserver.GotBodyData(); |
|
355 } |
|
356 } |
|
357 |
|
358 void CUpnpResponseParser::BodyCompleteL() |
|
359 { |
|
360 __FLOG(_L8("-> Response body complete")); |
|
361 iFlags |= EBodyComplete; |
|
362 } |
|
363 |
|
364 void CUpnpResponseParser::MessageCompleteL(const TPtrC8& aExcessData) |
|
365 { |
|
366 __FLOG(_L8("-> Message complete")); |
|
367 |
|
368 iFlags |= EMessageComplete; |
|
369 |
|
370 if ( aExcessData.Length() > 0 || iRawDataArray.Count() > 0 ) |
|
371 { |
|
372 __FLOG(_L8("->Excess Data:")); |
|
373 __FLOG_1(_L8("%S"), &aExcessData); |
|
374 |
|
375 iFlags |= EExcessData; |
|
376 } |
|
377 else |
|
378 { |
|
379 __FLOG(_L8("-> No excess data")); |
|
380 } |
|
381 |
|
382 TInt excessDataLen = aExcessData.Length(); |
|
383 TInt count = iRawDataArray.Count(); |
|
384 while(count > 0) |
|
385 { |
|
386 excessDataLen += iRawDataArray[count-1].Length(); |
|
387 count--; |
|
388 } |
|
389 TInt lenToTrim = iMsgBuf.Length() - excessDataLen; |
|
390 iMsgBuf.TrimStart(lenToTrim); |
|
391 |
|
392 if( !ConsumingResponse() ) |
|
393 { |
|
394 iObserver.ParsingComplete(iMsgBuf); |
|
395 } |
|
396 else |
|
397 { |
|
398 // There could be excess data - this data is for this response and needs |
|
399 // to be parsed. Set it as the raw data. |
|
400 iRawDataArray.Reset(); |
|
401 TPtrC8 rMBufPtr; |
|
402 RMemCell* rMBuf; |
|
403 TMemCellIterator mBufIter(iMsgBuf); |
|
404 while((rMBuf = mBufIter++) != NULL) |
|
405 { |
|
406 rMBufPtr.Set(rMBuf->Ptr(), rMBuf->Length()); |
|
407 iRawDataArray.AppendL(rMBufPtr); |
|
408 } |
|
409 } |
|
410 } |
|
411 |
|
412 TInt CUpnpResponseParser::HandleParserError(TInt aError) |
|
413 { |
|
414 __FLOG_1(_L8("-> HTTP message parser received error: %d"), aError); |
|
415 iRawDataArray.Reset(); |
|
416 iBodyParts.Reset(); |
|
417 iObserver.ParserError(aError); |
|
418 return KErrNone; |
|
419 } |
|
420 |
|
421 EXPORT_C void CUpnpResponseParser::ParseResponse(RMemChunk& aMessage, CResponse* aResponse) |
|
422 { |
|
423 __FLOG(_L8("-> Parsing response")); |
|
424 __ASSERT_DEBUG(aResponse, TUPnPMessagePanic::Panic(TUPnPMessagePanic::EMissingResponse)); |
|
425 __ASSERT_DEBUG(!aMessage.IsEmpty(), TUPnPMessagePanic::Panic(TUPnPMessagePanic::ENoMessage)); |
|
426 |
|
427 iResponse = aResponse; |
|
428 if(!iMsgBuf.IsEmpty()) |
|
429 { |
|
430 iMsgBuf.Free(); |
|
431 } |
|
432 iMsgBuf.Assign(aMessage); |
|
433 TPtrC8 rMBufPtr; |
|
434 RMemCell* rMBuf; |
|
435 TMemCellIterator mBufIter(iMsgBuf); |
|
436 TInt err = KErrNone; |
|
437 while((rMBuf = mBufIter++) != NULL) |
|
438 { |
|
439 rMBufPtr.Set(rMBuf->Ptr(), rMBuf->Length()); |
|
440 err = iRawDataArray.Append(rMBufPtr); |
|
441 if(err != KErrNone) |
|
442 { |
|
443 HandleParserError(err); |
|
444 break; |
|
445 } |
|
446 } |
|
447 |
|
448 if(err == KErrNone) |
|
449 { |
|
450 // Initiate the message parser |
|
451 iMessageParser.ReceivedMessageData(); |
|
452 } |
|
453 } |
|
454 |
|
455 EXPORT_C void CUpnpResponseParser::ResetParser() |
|
456 { |
|
457 __FLOG(_L8("-> Response parser reset")); |
|
458 iMessageParser.Reset(); |
|
459 iRawDataArray.Reset(); |
|
460 iBodyParts.Reset(); |
|
461 iOverallDataSize = 0; |
|
462 iFlags &= ~EExcessData; |
|
463 } |