|
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 <inetprottextutils.h> |
|
17 #include <httperr.h> |
|
18 #include <httpstringconstants.h> |
|
19 |
|
20 #include "cupnprequestparser.h" |
|
21 #include "tupnpmessagepanic.h" |
|
22 |
|
23 _LIT8(KHeaderSeparator, "\n"); |
|
24 |
|
25 __FLOG_STMT(_LIT8(KSubsys,"UPnPReqParser");) |
|
26 __FLOG_STMT(_LIT8(KComponent,"UPnPMessage");) |
|
27 |
|
28 |
|
29 EXPORT_C CUpnpRequestParser* CUpnpRequestParser::NewL(MParserObserver& aObserver) |
|
30 { |
|
31 CUpnpRequestParser* self = new(ELeave) CUpnpRequestParser(aObserver); |
|
32 CleanupStack::PushL(self); |
|
33 self->ConstructL(); |
|
34 CleanupStack::Pop(self); |
|
35 return self; |
|
36 } |
|
37 |
|
38 CUpnpRequestParser::CUpnpRequestParser(MParserObserver& aObserver) |
|
39 :iObserver(aObserver) |
|
40 { |
|
41 } |
|
42 |
|
43 EXPORT_C CUpnpRequestParser::~CUpnpRequestParser() |
|
44 { |
|
45 iMessageParser.Close(); |
|
46 iBodyParts.Close(); |
|
47 if(!iMsgBuf.IsEmpty()) |
|
48 { |
|
49 iMsgBuf.Free(); |
|
50 } |
|
51 iRawDataArray.Close(); |
|
52 __FLOG(_L8("-> Request parser destroyed")); |
|
53 __FLOG_CLOSE; |
|
54 } |
|
55 |
|
56 void CUpnpRequestParser::ConstructL() |
|
57 { |
|
58 iMessageParser.OpenL(*this); |
|
59 __FLOG_OPEN(KSubsys, KComponent); |
|
60 __FLOG(_L8("-> Request parser created")); |
|
61 } |
|
62 |
|
63 TBool CUpnpRequestParser::GetNextDataPart(TPtrC8& aDataPart) |
|
64 { |
|
65 __FLOG(_L8("-> Supplying request body part")); |
|
66 |
|
67 TInt bodyPartsCount = iBodyParts.Count(); |
|
68 __ASSERT_ALWAYS((bodyPartsCount > 0), TUPnPMessagePanic::Panic(TUPnPMessagePanic::ENoBodyPartInDataSupplier)); |
|
69 |
|
70 // Provide the first chunk. |
|
71 aDataPart.Set(iBodyParts[0]); |
|
72 __FLOG_1(_L8("%S"), &aDataPart); |
|
73 |
|
74 return (BodyComplete() && bodyPartsCount == 1); |
|
75 } |
|
76 |
|
77 void CUpnpRequestParser::ReleaseData() |
|
78 { |
|
79 __FLOG(_L8("-> Releasing request body part")); |
|
80 |
|
81 // Remove the oldest chunk. |
|
82 if( iBodyParts.Count () > 0 ) |
|
83 { |
|
84 iBodyParts.Remove ( 0 ); |
|
85 } |
|
86 |
|
87 // Are there any more chunks? |
|
88 if( iBodyParts.Count () > 0 ) |
|
89 { |
|
90 // Notify the sender about the presence of the body. He can then call |
|
91 // MHTTPDataSupplier::GetNextDataPart() to get the body data. |
|
92 iObserver.GotBodyData(); |
|
93 } |
|
94 |
|
95 else |
|
96 { |
|
97 // Does this data packet need to be released? |
|
98 if( NotifyReleaseData() ) |
|
99 { |
|
100 // Notify sender that the current data is no longer needed. |
|
101 // Can release as there are no body chunks waiting to be passed to the |
|
102 // client. |
|
103 iObserver.DataParsed(); |
|
104 iFlags &= ~ENotifyReleaseData; |
|
105 } |
|
106 } |
|
107 } |
|
108 |
|
109 TInt CUpnpRequestParser::OverallDataSize() |
|
110 { |
|
111 return (iOverallDataSize < 0) ? KErrNotFound : iOverallDataSize; |
|
112 } |
|
113 |
|
114 TInt CUpnpRequestParser::Reset() |
|
115 { |
|
116 return KErrNotSupported; |
|
117 } |
|
118 |
|
119 void CUpnpRequestParser::GetDataPacket(TPtrC8& aData) |
|
120 { |
|
121 __FLOG(_L8("-> Supplying request data to the HTTP message parser")); |
|
122 aData.Set(iRawDataArray[0]); |
|
123 iRawDataArray.Remove(0); |
|
124 } |
|
125 |
|
126 void CUpnpRequestParser::ReleaseDataPacket() |
|
127 { |
|
128 __FLOG(_L8("-> Releasing request data")); |
|
129 if ( iBodyParts.Count () == 0 ) |
|
130 { |
|
131 if(iRawDataArray.Count() > 0 && !IsExcessData()) |
|
132 { |
|
133 iMessageParser.ReceivedMessageData(); |
|
134 } |
|
135 else |
|
136 { |
|
137 // Notify sender that the current data is no longer needed. |
|
138 // Can release as there are no body chunks waiting to be passed to the |
|
139 // client. |
|
140 iObserver.DataParsed(); |
|
141 } |
|
142 } |
|
143 else |
|
144 { |
|
145 // Flag that the data needs to be released |
|
146 iFlags |= ENotifyReleaseData; |
|
147 } |
|
148 } |
|
149 |
|
150 void CUpnpRequestParser::StartLineL(const TDesC8& aStartLine) |
|
151 { |
|
152 // From RFC 2616 Sec 5.1 Request-Line = Method SP Request-URI SP HTTP-Version CRLF |
|
153 // Request-Line == Start-Line |
|
154 __FLOG(_L8("-> Got the request start line")); |
|
155 __FLOG_1(_L8("%S"), &aStartLine); |
|
156 |
|
157 TPtrC8 startLinePtr ( aStartLine ); |
|
158 // Find the first space. |
|
159 TInt pos = startLinePtr.Locate ( ' ' ); |
|
160 User::LeaveIfError ( pos ); |
|
161 |
|
162 // Extract the method |
|
163 TPtrC8 method ( startLinePtr.Left ( pos ) ); |
|
164 InetProtTextUtils::RemoveWhiteSpace ( method, InetProtTextUtils::ERemoveBoth ); |
|
165 |
|
166 // Now move past the space that we located |
|
167 startLinePtr.Set ( startLinePtr.Mid ( pos + 1 ) ); |
|
168 |
|
169 // Locate the next space. |
|
170 pos = startLinePtr.Locate ( ' ' ); |
|
171 User::LeaveIfError ( pos ); |
|
172 |
|
173 // Extract the request URI |
|
174 TPtrC8 requestUri ( startLinePtr.Left ( pos ) ); |
|
175 InetProtTextUtils::RemoveWhiteSpace ( requestUri, InetProtTextUtils::ERemoveBoth ); |
|
176 |
|
177 TUriParser8 uriParser; |
|
178 User::LeaveIfError ( uriParser.Parse ( requestUri ) ); |
|
179 |
|
180 // Now move past the space that we located |
|
181 startLinePtr.Set ( startLinePtr.Mid ( pos + 1 ) ); |
|
182 |
|
183 // Extract the version |
|
184 TPtrC8 versionPtr ( startLinePtr ); |
|
185 if ( versionPtr.Length () == 0 ) |
|
186 User::Leave ( KErrCorrupt ); |
|
187 |
|
188 InetProtTextUtils::RemoveWhiteSpace ( requestUri, InetProtTextUtils::ERemoveBoth ); |
|
189 |
|
190 |
|
191 RStringPool sp = iRequest->StringPool(); |
|
192 |
|
193 pos = versionPtr.Locate('/'); |
|
194 if( pos == KErrNotFound ) |
|
195 User::Leave(KErrCorrupt); |
|
196 |
|
197 versionPtr.Set(versionPtr.Mid(pos + 1)); |
|
198 |
|
199 // Extract the major version number |
|
200 TInt major; |
|
201 pos = InetProtTextUtils::ConvertDescriptorToInt(versionPtr, major); |
|
202 User::LeaveIfError(pos); |
|
203 |
|
204 // Skip past major version number and the "." |
|
205 versionPtr.Set(versionPtr.Mid(pos + 1)); |
|
206 |
|
207 // Extract the minor version number |
|
208 TInt minor; |
|
209 pos = InetProtTextUtils::ConvertDescriptorToInt(versionPtr, minor); |
|
210 User::LeaveIfError(pos); |
|
211 |
|
212 RStringF methodStr = sp.OpenFStringL ( method ); |
|
213 CleanupClosePushL(methodStr); |
|
214 |
|
215 RRequest request = iRequest->Handle (); |
|
216 request.SetMethod ( methodStr ); |
|
217 request.SetURIL ( uriParser ); |
|
218 |
|
219 TVersion version ( major, minor, 0); |
|
220 request.SetVersion ( version ); |
|
221 |
|
222 CleanupStack::PopAndDestroy(&methodStr); |
|
223 } |
|
224 |
|
225 void CUpnpRequestParser::HeaderL(const TDesC8& aFieldName, TDesC8& aFieldValue) |
|
226 { |
|
227 __FLOG(_L8("-> Got header value pair")); |
|
228 __FLOG_2(_L8("%S: %S"), &aFieldName, &aFieldValue); |
|
229 |
|
230 RStringF name = iRequest->StringPool().OpenFStringL(aFieldName); |
|
231 CleanupClosePushL(name); |
|
232 |
|
233 iRequest->Handle().GetHeaderCollection().SetRawFieldL(name, aFieldValue, KHeaderSeparator); |
|
234 |
|
235 CleanupStack::PopAndDestroy(&name); |
|
236 } |
|
237 |
|
238 TInt CUpnpRequestParser::BodySizeL() |
|
239 { |
|
240 // Notify the sender that all the request headers have been parsed. |
|
241 iObserver.GotHeaders(); |
|
242 |
|
243 //First check for a Transfer-Encoding header field. |
|
244 iRequest->Handle().SetBody(*this); |
|
245 THTTPHdrVal value; |
|
246 RStringF name = iRequest->StringPool().StringF(HTTP::ETransferEncoding, THTTPTable::Table()); |
|
247 |
|
248 if( iRequest->Handle().GetHeaderCollection().GetField(name, 0, value) == KErrNone ) |
|
249 { |
|
250 // It exists - what's the value? |
|
251 if( value.Type() == THTTPHdrVal::KStrFVal && |
|
252 value.StrF().Index(THTTPTable::Table()) == HTTP::EChunked ) |
|
253 { |
|
254 // The Transfer-Encoding header is Chunked and as the chunked |
|
255 // encoding is removed, we remove the header. |
|
256 iRequest->Handle().GetHeaderCollection().RemoveField(name); |
|
257 |
|
258 __FLOG(_L8("-> Request has chunked body")); |
|
259 // As the entity body is chunked the overall data size is unknown. |
|
260 iOverallDataSize = MHttpMessageParserObserver::EChunked; |
|
261 } |
|
262 } |
|
263 |
|
264 else |
|
265 { |
|
266 // Transfer-Encoding header was not present - now check for a |
|
267 // Content-Length header. |
|
268 name = iRequest->StringPool().StringF(HTTP::EContentLength, THTTPTable::Table()); |
|
269 TInt err = iRequest->Handle().GetHeaderCollection().GetField(name, 0, value); |
|
270 |
|
271 if( err != KErrNone && err != KErrNotFound ) |
|
272 User::Leave(err); |
|
273 |
|
274 if( err == KErrNone && value.Type() == THTTPHdrVal::KTIntVal ) |
|
275 { |
|
276 // Content-Length header value specified the length of entity in bytes. |
|
277 iOverallDataSize = value.Int(); |
|
278 __FLOG_1(_L8("-> Request body length = %d"), iOverallDataSize); |
|
279 } |
|
280 } |
|
281 |
|
282 return iOverallDataSize; |
|
283 } |
|
284 |
|
285 void CUpnpRequestParser::BodyChunkL(const TDesC8& aData) |
|
286 { |
|
287 __FLOG(_L8("-> Got request body chunk")); |
|
288 __FLOG_1(_L8("%S"), &aData); |
|
289 iFlags |= EBodyPresent; |
|
290 |
|
291 if (aData.Length() > 0) |
|
292 { |
|
293 iBodyParts.Append(aData); |
|
294 } |
|
295 |
|
296 // Only notify the client that there is a body part if there is one. |
|
297 if (iBodyParts.Count() > 0) |
|
298 { |
|
299 // Notify the sender about the presence of the body. He can then call |
|
300 // MHTTPDataSupplier::GetNextDataPart() to get the body data. |
|
301 iObserver.GotBodyData(); |
|
302 } |
|
303 } |
|
304 |
|
305 void CUpnpRequestParser::BodyCompleteL() |
|
306 { |
|
307 __FLOG(_L8("-> Request body complete")); |
|
308 iFlags |= EBodyComplete; |
|
309 } |
|
310 |
|
311 void CUpnpRequestParser::MessageCompleteL(const TPtrC8& aExcessData) |
|
312 { |
|
313 __FLOG(_L8("-> Message complete")); |
|
314 |
|
315 if ( aExcessData.Length() > 0 || iRawDataArray.Count() > 0 ) |
|
316 { |
|
317 __FLOG(_L8("->Excess Data:")); |
|
318 __FLOG_1(_L8("%S"), &aExcessData); |
|
319 |
|
320 iFlags |= EExcessData; |
|
321 } |
|
322 else |
|
323 { |
|
324 __FLOG(_L8("-> No excess data")); |
|
325 } |
|
326 |
|
327 // Post a message to the sender with aExcessData as the payload |
|
328 TInt excessDataLen = aExcessData.Length(); |
|
329 TInt count = iRawDataArray.Count(); |
|
330 while(count > 0) |
|
331 { |
|
332 excessDataLen += iRawDataArray[count-1].Length(); |
|
333 count--; |
|
334 } |
|
335 |
|
336 TInt lenToTrim = iMsgBuf.Length() - excessDataLen; |
|
337 iMsgBuf.TrimStart(lenToTrim); |
|
338 iObserver.ParsingComplete(iMsgBuf); |
|
339 } |
|
340 |
|
341 TInt CUpnpRequestParser::HandleParserError(TInt aError) |
|
342 { |
|
343 __FLOG_1(_L8("-> HTTP message parser received error: %d"), aError); |
|
344 iRawDataArray.Reset(); |
|
345 iBodyParts.Reset(); |
|
346 iObserver.ParserError(aError); |
|
347 return KErrNone; |
|
348 } |
|
349 |
|
350 EXPORT_C void CUpnpRequestParser::ParseRequest(RMemChunk& aMessage, CRequest* aRequest) |
|
351 { |
|
352 __FLOG(_L8("-> Parsing request")); |
|
353 __ASSERT_DEBUG(aRequest, TUPnPMessagePanic::Panic(TUPnPMessagePanic::EMissingRequest)); |
|
354 __ASSERT_DEBUG(!aMessage.IsEmpty(), TUPnPMessagePanic::Panic(TUPnPMessagePanic::ENoMessage)); |
|
355 |
|
356 iRequest = aRequest; |
|
357 if(!iMsgBuf.IsEmpty()) |
|
358 { |
|
359 iMsgBuf.Init(); |
|
360 } |
|
361 iMsgBuf.Assign(aMessage); |
|
362 TPtrC8 rMBufPtr; |
|
363 RMemCell* rMBuf; |
|
364 TMemCellIterator mBufIter(iMsgBuf); |
|
365 TInt err = KErrNone; |
|
366 while((rMBuf = mBufIter++) != NULL) |
|
367 { |
|
368 rMBufPtr.Set(rMBuf->Ptr(), rMBuf->Length()); |
|
369 err = iRawDataArray.Append(rMBufPtr); |
|
370 if(err != KErrNone) |
|
371 { |
|
372 HandleParserError(err); |
|
373 break; |
|
374 } |
|
375 } |
|
376 |
|
377 if(err == KErrNone) |
|
378 { |
|
379 // Initiate the message parser. |
|
380 iMessageParser.ReceivedMessageData(); |
|
381 } |
|
382 } |
|
383 |
|
384 EXPORT_C void CUpnpRequestParser::ResetParser() |
|
385 { |
|
386 __FLOG(_L8("-> Request parser reset")); |
|
387 iMsgBuf.Init(); |
|
388 iMessageParser.Reset(); |
|
389 iRawDataArray.Reset(); |
|
390 iBodyParts.Reset(); |
|
391 iOverallDataSize = 0; |
|
392 iFlags &= ~EExcessData; |
|
393 } |