|
1 // Copyright (c) 2006-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 "cimapheaderfieldsparser.h" |
|
17 #include "cimapheaderfields.h" |
|
18 #include "cimapsessionconsts.h" |
|
19 #include "cimaplogger.h" |
|
20 #include "cimapcommand.h" |
|
21 |
|
22 CImapHeaderFieldsParser::CImapHeaderFieldsParser(TInt aLogId) : |
|
23 iState(EWaitLine), |
|
24 iLogId(aLogId) |
|
25 { |
|
26 } |
|
27 |
|
28 void CImapHeaderFieldsParser::Construct(CImapHeaderFields* aHeaderFields) |
|
29 { |
|
30 iHeaderFields = aHeaderFields; |
|
31 iHeaderFieldsOwned = ETrue; |
|
32 } |
|
33 |
|
34 CImapHeaderFieldsParser::~CImapHeaderFieldsParser() |
|
35 { |
|
36 if (iHeaderFieldsOwned) |
|
37 { |
|
38 delete iHeaderFields; |
|
39 } |
|
40 } |
|
41 |
|
42 TBool CImapHeaderFieldsParser::ProcessBlockL(const TDesC8& aData) |
|
43 { |
|
44 TBool wantMore = ETrue; |
|
45 switch (iState) |
|
46 { |
|
47 case EWaitLine: |
|
48 { |
|
49 if ((aData.Length() >= 3) && (aData[aData.Length() - 1] == '}')) |
|
50 { |
|
51 iState = EWaitLiteral; |
|
52 } |
|
53 else |
|
54 { |
|
55 // Header Fields ALWAYS delivered as a literal |
|
56 CImapCommand::CorruptDataL(iLogId); |
|
57 } |
|
58 } |
|
59 break; |
|
60 case EWaitLiteral: |
|
61 { |
|
62 ParseHeaderFieldsBlockL(aData); |
|
63 iState = EParseComplete; |
|
64 wantMore = EFalse; |
|
65 } |
|
66 break; |
|
67 case EParseComplete: |
|
68 default: |
|
69 { |
|
70 ASSERT(EFalse); |
|
71 wantMore = EFalse; |
|
72 } |
|
73 break; |
|
74 } |
|
75 |
|
76 return wantMore; |
|
77 } |
|
78 |
|
79 |
|
80 TPtrC8 CImapHeaderFieldsParser::UnparsedData() |
|
81 { |
|
82 return iData; |
|
83 } |
|
84 |
|
85 void CImapHeaderFieldsParser::ParseHeaderFieldsBlockL(const TDesC8& aData) |
|
86 { |
|
87 // Only expecting one header fields block. |
|
88 // Check that we own the header fields at this point. |
|
89 ASSERT(iHeaderFieldsOwned); |
|
90 |
|
91 iData.Set(aData); |
|
92 |
|
93 ParseFieldsL(); |
|
94 |
|
95 // The block has been parsed so transfer ownership of iHeaderFields |
|
96 HeaderFieldsComplete(iHeaderFields); |
|
97 iHeaderFieldsOwned = EFalse; |
|
98 } |
|
99 |
|
100 void CImapHeaderFieldsParser::ParseFieldsL() |
|
101 { |
|
102 RArray<TPtrC8> parts; |
|
103 CleanupClosePushL(parts); |
|
104 TInt length(0); |
|
105 |
|
106 // Parse one line at a time |
|
107 TInt posCrlf = iData.Find(KImapTxtCrlf); |
|
108 |
|
109 while (posCrlf >= 0) |
|
110 { |
|
111 // Fetch the line and consume it from iData |
|
112 TPtrC8 line = iData.Left(posCrlf); |
|
113 |
|
114 if (line.Length() > 0) |
|
115 { |
|
116 parts.AppendL(line); |
|
117 length += line.Length(); |
|
118 } |
|
119 else |
|
120 { |
|
121 // Two instances of CRLF in a row. This signifies the end of the previous line. |
|
122 ParsePartsL(parts, length); |
|
123 } |
|
124 |
|
125 if (iData.Length() > posCrlf + KImapTxtCrlf.iTypeLength) |
|
126 { |
|
127 iData.Set(iData.Mid(posCrlf + KImapTxtCrlf.iTypeLength)); |
|
128 |
|
129 if (iData[0] > ' ') |
|
130 { |
|
131 // Next line does not contain whitespace as its first character. This |
|
132 // signifies the end of the previous line. |
|
133 ParsePartsL(parts, length); |
|
134 } |
|
135 } |
|
136 else |
|
137 { |
|
138 iData.Set(KNullDesC8()); |
|
139 } |
|
140 |
|
141 posCrlf = iData.Find(KImapTxtCrlf); |
|
142 } |
|
143 |
|
144 // Data block is now complete. Process any oustanding parts. |
|
145 ParsePartsL(parts, length); |
|
146 |
|
147 CleanupStack::PopAndDestroy(&parts); |
|
148 } |
|
149 |
|
150 void CImapHeaderFieldsParser::ParsePartsL(RArray<TPtrC8>& aParts, TInt& aLength) |
|
151 { |
|
152 if (aParts.Count() > 0) |
|
153 { |
|
154 // Search for colon in the first part |
|
155 TInt posColon = aParts[0].Locate(':'); |
|
156 |
|
157 // Ignore line if it has no colon (or if it begins with a colon) |
|
158 if (posColon > 0) |
|
159 { |
|
160 TPtrC8 fieldName(aParts[0].Left(posColon)); |
|
161 |
|
162 // Move past the colon |
|
163 ++posColon; |
|
164 |
|
165 // Check that there is some data still left to process after the colon. |
|
166 if (aLength > posColon) |
|
167 { |
|
168 // Update the first part to remove the portion before the colon |
|
169 if (aParts[0].Length() > posColon) |
|
170 { |
|
171 aParts[0].Set(aParts[0].Mid(posColon)); |
|
172 } |
|
173 else |
|
174 { |
|
175 aParts[0].Set(KNullDesC8()); |
|
176 } |
|
177 |
|
178 aLength -= posColon; |
|
179 |
|
180 // Strip all whitespace up to the start of the field data |
|
181 TInt part(0); |
|
182 for (; part < aParts.Count(); ++part) |
|
183 { |
|
184 while ((aParts[part].Length() > 0) && (aParts[part][0] <= ' ')) |
|
185 { |
|
186 aParts[part].Set(aParts[part].Mid(1)); |
|
187 aLength--; |
|
188 } |
|
189 |
|
190 // Either we have found a non whitespace character, or the current part |
|
191 // is empty. If we have found a non whitespace character, we can exit the |
|
192 // loop now. |
|
193 if (aParts[part].Length() > 0) |
|
194 { |
|
195 break; |
|
196 } |
|
197 } |
|
198 |
|
199 if (aLength > 0) |
|
200 { |
|
201 HBufC8* wholeLine = HBufC8::NewLC(aLength); |
|
202 |
|
203 for (part = 0; part < aParts.Count(); ++part) |
|
204 { |
|
205 wholeLine->Des().Append(aParts[part]); |
|
206 } |
|
207 |
|
208 // Pop whole line as ownership is about to be transferred |
|
209 CleanupStack::Pop(wholeLine); |
|
210 __LOG_FORMAT((iLogId, "Add header field - %S: %S", &fieldName, &(*wholeLine))); |
|
211 iHeaderFields->SetFieldL(fieldName, wholeLine); |
|
212 } |
|
213 } |
|
214 } |
|
215 |
|
216 aParts.Reset(); |
|
217 aLength = 0; |
|
218 } |
|
219 } |