|
1 /* |
|
2 * Copyright (c) 2005 - 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: utility class for handling multipart/related HTTP responses |
|
15 * in ROAP |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 #include <e32base.h> |
|
23 #include <hash.h> |
|
24 #include "Base64.h" |
|
25 #include "MultipartHandler.h" |
|
26 |
|
27 #ifdef _DEBUG |
|
28 |
|
29 #include <e32debug.h> |
|
30 #define MULTIPART_DEBUGLIT( a ) RDebug::Print( _L ( a ) ); |
|
31 |
|
32 #else // DEBUG |
|
33 |
|
34 #define MULTIPART_DEBUGLIT( a ) |
|
35 |
|
36 #endif // DEBUG |
|
37 |
|
38 // CONSTANTS |
|
39 |
|
40 _LIT8( KBoundaryPrefix, "--" ); |
|
41 _LIT8( KCrLf, "\r\n" ); |
|
42 _LIT8( KCrLfCrLf, "\r\n\r\n" ); |
|
43 |
|
44 |
|
45 // ============================ MEMBER FUNCTIONS =============================== |
|
46 |
|
47 // --------------------------------------------------------------------------- |
|
48 // TMultipartHandler::TMultipartHandler |
|
49 // --------------------------------------------------------------------------- |
|
50 // |
|
51 TMultipartHandler::TMultipartHandler(): |
|
52 iState( EPreamble ) |
|
53 { |
|
54 MULTIPART_DEBUGLIT( "TMultipartHandler::TMultipartHandler -->" ) |
|
55 iBuffer.SetLength( 0 ); |
|
56 MULTIPART_DEBUGLIT( "--> TMultipartHandler::TMultipartHandler" ) |
|
57 } |
|
58 |
|
59 // --------------------------------------------------------------------------- |
|
60 // TMultipartHandler::WriteL |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 void TMultipartHandler::WriteL( |
|
64 const TDesC8& aData ) |
|
65 { |
|
66 MULTIPART_DEBUGLIT( "TMultipartHandler::WriteL -->" ) |
|
67 switch ( iState ) |
|
68 { |
|
69 case EPreamble: |
|
70 MULTIPART_DEBUGLIT( "EPreamble" ) |
|
71 HandlePreambleL( aData ); |
|
72 break; |
|
73 case EBodyPartHeaders: |
|
74 MULTIPART_DEBUGLIT( "EBodyPartHeaders" ) |
|
75 HandleBodyPartHeadersL( aData ); |
|
76 break; |
|
77 case EBodyPart: |
|
78 MULTIPART_DEBUGLIT( "EBodyPart" ) |
|
79 HandleBodyPartL( aData ); |
|
80 break; |
|
81 case EEpilog: |
|
82 MULTIPART_DEBUGLIT( "EEpilog" ) |
|
83 break; |
|
84 } |
|
85 MULTIPART_DEBUGLIT( "--> TMultipartHandler::WriteL" ) |
|
86 } |
|
87 |
|
88 // --------------------------------------------------------------------------- |
|
89 // TMultipartHandler::FindDashBoundary |
|
90 // --------------------------------------------------------------------------- |
|
91 // |
|
92 TBool TMultipartHandler::FindDashBoundary( |
|
93 const TDesC8& aBuffer, |
|
94 TInt& aStart, |
|
95 TInt& aEnd ) |
|
96 { |
|
97 MULTIPART_DEBUGLIT( "TMultipartHandler::FindDashBoundary -->" ) |
|
98 TBool r( EFalse ); |
|
99 const TInt KBoundaryPrefixLength( KBoundaryPrefix().Length() ); |
|
100 const TInt KCrLfLength( KCrLf().Length() ); |
|
101 aStart = aBuffer.Find( KBoundaryPrefix ); |
|
102 if ( aStart >= 0 ) |
|
103 { |
|
104 MULTIPART_DEBUGLIT( "TMultipartHandler::FindDashBoundary aStart nonnegative" ) |
|
105 aEnd = aBuffer.Mid( aStart ).Find( KCrLf ); |
|
106 aEnd += aStart; |
|
107 if ( aEnd > aStart + KBoundaryPrefixLength ) |
|
108 { |
|
109 MULTIPART_DEBUGLIT( "TMultipartHandler::FindDashBoundary aEnd > aStart + (boundary prefix length)" ) |
|
110 const TInt delimiterLength( aEnd - aStart + KCrLfLength ); |
|
111 __ASSERT_ALWAYS( KMaxBoundaryLength >= delimiterLength, User::Invariant() ); |
|
112 iDelimiter.Copy( KCrLf ); |
|
113 iDelimiter.Append( KBoundaryPrefix ); |
|
114 iDelimiter.Append( aBuffer.Mid( |
|
115 aStart + KBoundaryPrefixLength, |
|
116 aEnd - aStart - KBoundaryPrefixLength ) ); |
|
117 r = ETrue; |
|
118 MULTIPART_DEBUGLIT( "TMultipartHandler::FindDashBoundary found dash boundary" ) |
|
119 } |
|
120 } |
|
121 MULTIPART_DEBUGLIT( "--> TMultipartHandler::FindDashBoundary" ) |
|
122 return r; |
|
123 } |
|
124 |
|
125 // --------------------------------------------------------------------------- |
|
126 // TMultipartHandler::FindHeaderEnd |
|
127 // --------------------------------------------------------------------------- |
|
128 // |
|
129 TBool TMultipartHandler::FindHeaderEnd( |
|
130 const TDesC8& aBuffer, |
|
131 TInt& aStart, |
|
132 TInt& aEnd ) |
|
133 { |
|
134 MULTIPART_DEBUGLIT( "TMultipartHandler::FindHeaderEnd -->" ) |
|
135 TBool r( EFalse ); |
|
136 |
|
137 aStart = aBuffer.Find( KCrLfCrLf ); |
|
138 if ( aStart >= 0 ) |
|
139 { |
|
140 aEnd = aStart + KCrLfCrLf().Length(); |
|
141 r = ETrue; |
|
142 } |
|
143 MULTIPART_DEBUGLIT( "--> TMultipartHandler::FindHeaderEnd" ) |
|
144 return r; |
|
145 } |
|
146 |
|
147 // --------------------------------------------------------------------------- |
|
148 // TMultipartHandler::FindDelimiter |
|
149 // --------------------------------------------------------------------------- |
|
150 // |
|
151 TBool TMultipartHandler::FindDelimiter( |
|
152 const TDesC8& aBuffer, |
|
153 TInt& aStart, |
|
154 TInt& aEnd ) |
|
155 { |
|
156 MULTIPART_DEBUGLIT( "TMultipartHandler::FindDelimiter -->" ) |
|
157 TBool r( EFalse ); |
|
158 |
|
159 aStart = aBuffer.Find( iDelimiter ); |
|
160 if ( aStart >= 0 ) |
|
161 { |
|
162 aEnd = aStart + iDelimiter.Length(); |
|
163 r = ETrue; |
|
164 } |
|
165 MULTIPART_DEBUGLIT( "--> TMultipartHandler::FindDelimiter" ) |
|
166 return r; |
|
167 } |
|
168 |
|
169 // --------------------------------------------------------------------------- |
|
170 // TMultipartHandler::HandlePreambleL |
|
171 // --------------------------------------------------------------------------- |
|
172 // |
|
173 void TMultipartHandler::HandlePreambleL( |
|
174 const TDesC8& aData ) |
|
175 { |
|
176 MULTIPART_DEBUGLIT( "TMultipartHandler::HandlePreambleL -->" ) |
|
177 TInt bStart( 0 ); |
|
178 TInt bEnd( 0 ); |
|
179 HBufC8* buffer( NULL ); |
|
180 |
|
181 buffer = HBufC8::NewLC( iBuffer.Length() + aData.Length() ); |
|
182 buffer->Des().Copy( iBuffer ); |
|
183 buffer->Des().Append( aData ); |
|
184 if ( !FindDashBoundary( *buffer, bStart, bEnd ) ) |
|
185 { |
|
186 iBuffer.Copy( buffer->Right( Min( |
|
187 KMaxBoundaryLength, buffer->Length() ) ) ); |
|
188 } |
|
189 else |
|
190 { |
|
191 iBuffer.SetLength( 0 ); |
|
192 iState = EBodyPartHeaders; |
|
193 StartBodyPartHeadersL(); |
|
194 HandleBodyPartHeadersL( buffer->Mid( bEnd + KCrLf().Length() ) ); |
|
195 } |
|
196 CleanupStack::PopAndDestroy( buffer ); |
|
197 MULTIPART_DEBUGLIT( "--> TMultipartHandler::HandlePreambleL" ) |
|
198 } |
|
199 |
|
200 // --------------------------------------------------------------------------- |
|
201 // TMultipartHandler::HandleBodyPartHeadersL |
|
202 // --------------------------------------------------------------------------- |
|
203 // |
|
204 void TMultipartHandler::HandleBodyPartHeadersL( |
|
205 const TDesC8& aData ) |
|
206 { |
|
207 MULTIPART_DEBUGLIT( "TMultipartHandler::HandleBodyPartHeadersL -->" ) |
|
208 TInt bStart( 0 ); |
|
209 TInt bEnd( 0 ); |
|
210 HBufC8* buffer( HBufC8::NewLC( iBuffer.Length() + aData.Length() ) ); |
|
211 buffer->Des().Copy( iBuffer ); |
|
212 buffer->Des().Append( aData ); |
|
213 iBuffer.Copy( buffer->Right( Min( |
|
214 KMaxBoundaryLength, buffer->Length() ) ) ); |
|
215 if ( !FindHeaderEnd( *buffer, bStart, bEnd ) ) |
|
216 { |
|
217 const TInt lengthNoBoundary( buffer->Length() - KMaxBoundaryLength ); |
|
218 if ( lengthNoBoundary > 0 ) |
|
219 { |
|
220 HandleBodyHeaderDataL( buffer->Left( lengthNoBoundary ) ); |
|
221 } |
|
222 } |
|
223 else |
|
224 { |
|
225 HandleBodyHeaderDataL( buffer->Left( bStart ) ); |
|
226 iBuffer.SetLength( 0 ); |
|
227 iState = EBodyPart; |
|
228 StartBodyPartL(); |
|
229 HandleBodyPartL( buffer->Mid( bEnd ) ); |
|
230 } |
|
231 CleanupStack::PopAndDestroy( buffer ); |
|
232 MULTIPART_DEBUGLIT( "--> TMultipartHandler::HandleBodyPartHeadersL" ) |
|
233 } |
|
234 |
|
235 // --------------------------------------------------------------------------- |
|
236 // TMultipartHandler::HandleBodyPartL |
|
237 // --------------------------------------------------------------------------- |
|
238 // |
|
239 void TMultipartHandler::HandleBodyPartL( |
|
240 const TDesC8& aData ) |
|
241 { |
|
242 MULTIPART_DEBUGLIT( "TMultipartHandler::HandleBodyPartL -->" ) |
|
243 TInt bStart( 0 ); |
|
244 TInt bEnd( 0 ); |
|
245 |
|
246 HBufC8* buffer( HBufC8::NewLC( iBuffer.Length() + aData.Length() ) ); |
|
247 buffer->Des().Copy( iBuffer ); |
|
248 buffer->Des().Append( aData ); |
|
249 |
|
250 iBuffer.Copy( buffer->Right( Min( |
|
251 KMaxBoundaryLength, buffer->Length() ) ) ); |
|
252 |
|
253 if ( !FindDelimiter( *buffer, bStart, bEnd ) ) |
|
254 { |
|
255 const TInt lengthNoBoundary( buffer->Length() - KMaxBoundaryLength ); |
|
256 if ( lengthNoBoundary > 0 ) |
|
257 { |
|
258 HandleBodyDataL( buffer->Left( lengthNoBoundary ) ); |
|
259 } |
|
260 } |
|
261 else |
|
262 { |
|
263 HandleBodyDataL( buffer->Left( bStart ) ); |
|
264 iBuffer.SetLength( 0 ); |
|
265 if ( ( *buffer )[ bEnd ] == '-' && ( *buffer )[ bEnd + 1 ] == '-' ) |
|
266 { |
|
267 EndBodyPartL(); |
|
268 iState = EEpilog; |
|
269 } |
|
270 else |
|
271 { |
|
272 iState = EBodyPartHeaders; |
|
273 StartBodyPartHeadersL(); |
|
274 HandleBodyPartHeadersL( buffer->Mid( bEnd + KCrLf().Length() ) ); |
|
275 } |
|
276 } |
|
277 CleanupStack::PopAndDestroy( buffer ); |
|
278 MULTIPART_DEBUGLIT( "--> TMultipartHandler::HandleBodyPartL" ) |
|
279 } |
|
280 |
|
281 // --------------------------------------------------------------------------- |
|
282 // TMultipartHandler::StartBodyPartHeadersL |
|
283 // --------------------------------------------------------------------------- |
|
284 // |
|
285 void TMultipartHandler::StartBodyPartHeadersL() |
|
286 { |
|
287 MULTIPART_DEBUGLIT( "--> TMultipartHandler::StartBodyPartHeadersL -->" ) |
|
288 } |
|
289 |
|
290 // --------------------------------------------------------------------------- |
|
291 // TMultipartHandler::EndBodyPartHeadersL |
|
292 // --------------------------------------------------------------------------- |
|
293 // |
|
294 void TMultipartHandler::EndBodyPartHeadersL() |
|
295 { |
|
296 MULTIPART_DEBUGLIT( "--> TMultipartHandler::EndBodyPartHeadersL -->" ) |
|
297 } |
|
298 |
|
299 // --------------------------------------------------------------------------- |
|
300 // TMultipartHandler::HandleBodyHeaderDataL |
|
301 // --------------------------------------------------------------------------- |
|
302 // |
|
303 void TMultipartHandler::HandleBodyHeaderDataL( |
|
304 const TDesC8& /*aData*/ ) |
|
305 { |
|
306 MULTIPART_DEBUGLIT( "--> TMultipartHandler::HandleBodyHeaderDataL -->" ) |
|
307 } |
|
308 |
|
309 // --------------------------------------------------------------------------- |
|
310 // TMultipartHandler::StartBodyPartL |
|
311 // --------------------------------------------------------------------------- |
|
312 // |
|
313 void TMultipartHandler::StartBodyPartL() |
|
314 { |
|
315 MULTIPART_DEBUGLIT( "--> TMultipartHandler::StartBodyPartL -->" ) |
|
316 } |
|
317 |
|
318 // --------------------------------------------------------------------------- |
|
319 // TMultipartHandler::EndBodyPartL |
|
320 // --------------------------------------------------------------------------- |
|
321 // |
|
322 void TMultipartHandler::EndBodyPartL() |
|
323 { |
|
324 MULTIPART_DEBUGLIT( "--> TMultipartHandler::EndBodyPartL -->" ) |
|
325 } |
|
326 |
|
327 // --------------------------------------------------------------------------- |
|
328 // TMultipartHandler::HandleBodyDataL |
|
329 // --------------------------------------------------------------------------- |
|
330 // |
|
331 void TMultipartHandler::HandleBodyDataL( |
|
332 const TDesC8& /*aData*/ ) |
|
333 { |
|
334 MULTIPART_DEBUGLIT( "--> TMultipartHandler::HandleBodyDataL -->" ) |
|
335 } |