|
1 // Copyright (c) 2003-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 // defines test classes: CTestMessageParser |
|
15 // |
|
16 // |
|
17 |
|
18 #include "ctestmessageparser.h" |
|
19 #include <inetprottextutils.h> |
|
20 #include <httpstringconstants.h> |
|
21 #include <http/rhttpheaders.h> |
|
22 |
|
23 _LIT8(KUri, "http://www.connecting.nokia.com"); |
|
24 _LIT8(KHeaderSeparator, "\n"); |
|
25 |
|
26 //Various http response messages which has to be parsed |
|
27 _LIT8(KMessageResponseData1, "HTTP/1.1 200 OK\r\nDate: Tue, 04 Aug 2009 10:49:50 GMT\r\nServer: Apache/2.2.11 (Unix)\r\nLast-Modified: Thu, 16 Jul 2009 12:06:00 GMT\r\nETag: 4c0f49-329-46ed17eb73a00 \r\nAccept-Ranges: bytes\r\n\r\n "); |
|
28 _LIT8(KMessageResponseData2, "HTTP/1.1 200 OK\r\nDate: Tue, 04 Aug 2009 10:49:59 GMT\r\nServer: Apache/2.2.11 (Unix)\r\nLast-Modified: Thu, 16 Jul 2009 12:06:00 GMT\r\nETag: 4c134b-cbc-46ed17eb73a00 \r\nAccept-Ranges: bytes\r\nContent-Length: 3260\r\nKeep-Alive: timeout=5, max=88\r\nConnection: Keep-Alive\r\nContent-Type: image/png\r\n\r\n "); |
|
29 _LIT8(KMessageResponseData3, "HTTP/1.1 200 OK\r\nDate: Tue, 04 Aug 2009 10:49:59 GMT\r\nServer: Apache\r\nConnection: Close\r\nTransfer-Encoding: chunked\r\nContent-Type: text/html\r\n\r\n "); |
|
30 _LIT8(KMessageResponseData4, "HTTP/1.1 200 OK\r\nDate: Tue, Date: Tue, 22 Sep 2009 04:27:56 GMT\r\nServer: Apache\r\nContent-Encoding: gzip\r\nContent-Length: 6355\r\nContent-Type: text/html\r\nExpires: Tue, 22 Sep 2009 04:27:56 GMT\r\nCache-Control: max-age=0, no-cache\r\nPragma: no-cache\r\nConnection: close\r\nVary: Accept-Encoding\r\n\r\n "); |
|
31 |
|
32 CTestMessageParser* CTestMessageParser::NewL(MTestObserver& aObserver) |
|
33 { |
|
34 CTestMessageParser* self = new(ELeave) CTestMessageParser(aObserver); |
|
35 CleanupStack::PushL(self); |
|
36 self->ConstructL(); |
|
37 CleanupStack::Pop(self); |
|
38 return self; |
|
39 } |
|
40 |
|
41 CTestMessageParser::~CTestMessageParser() |
|
42 { |
|
43 delete iBuffer; |
|
44 iBuffer = NULL; |
|
45 iTransaction.Close(); |
|
46 iSession.Close(); |
|
47 iMessageParser.Close(); |
|
48 Cancel(); |
|
49 } |
|
50 |
|
51 CTestMessageParser::CTestMessageParser(MTestObserver& aObserver) |
|
52 : CActive(CActive::EPriorityStandard), iObserver(aObserver) |
|
53 { |
|
54 CActiveScheduler::Add(this); |
|
55 } |
|
56 |
|
57 void CTestMessageParser::ConstructL() |
|
58 { |
|
59 iBuffer = NULL; |
|
60 iSession.OpenL(); |
|
61 iStringPool = iSession.StringPool(); |
|
62 iMessageParser.OpenL(*this); |
|
63 TUriParser8 up; |
|
64 up.Parse(KUri); |
|
65 iTransaction = iSession.OpenTransactionL(up, *this, iSession.StringPool().StringF(HTTP::EGET,RHTTPSession::GetTable())); |
|
66 } |
|
67 void CTestMessageParser::StartRequest(TInt aMessageType) |
|
68 { |
|
69 iMessageType = aMessageType; |
|
70 // Self-complete to kick-off the test. |
|
71 CompleteSelf(); |
|
72 } |
|
73 |
|
74 void CTestMessageParser::CompleteSelf() |
|
75 { |
|
76 TRequestStatus* pStat = &iStatus; |
|
77 User::RequestComplete(pStat, KErrNone); |
|
78 SetActive(); |
|
79 } |
|
80 |
|
81 void CTestMessageParser::DoReset() |
|
82 { |
|
83 // Reset the parser and update the reset state. |
|
84 } |
|
85 |
|
86 /* |
|
87 * Methods from CActive |
|
88 */ |
|
89 |
|
90 void CTestMessageParser::RunL() |
|
91 { |
|
92 // Reset the test case data |
|
93 |
|
94 // Start the parser... |
|
95 iMessageParser.ReceivedMessageData(); |
|
96 } |
|
97 |
|
98 void CTestMessageParser::DoCancel() |
|
99 { |
|
100 // Do nothing... |
|
101 } |
|
102 |
|
103 TInt CTestMessageParser::RunError(TInt /*aError*/) |
|
104 { |
|
105 // Inform the observer of the error |
|
106 return KErrNone; |
|
107 } |
|
108 |
|
109 /* |
|
110 * Methods from MHttpMessageParserObserver |
|
111 */ |
|
112 |
|
113 void CTestMessageParser::GetDataPacket(TPtrC8& aData) |
|
114 { |
|
115 //_LIT8(KMessageResponseData, "HTTP/1.1 200 OK\r\n Date: Tue, 04 Aug 2009 10:49:50 GMT\r\n Server: Apache/2.2.11 (Unix)\r\n Last-Modified: Thu, 16 Jul 2009 12:06:00 GMT\r\n ETag: 4c0f49-329-46ed17eb73a00 \r\n Accept-Ranges: bytes\r\n \r\n "); |
|
116 switch(iMessageType) |
|
117 { |
|
118 case 1: |
|
119 iBuffer = KMessageResponseData1().AllocL(); |
|
120 break; |
|
121 case 2: |
|
122 iBuffer = KMessageResponseData2().AllocL(); |
|
123 break; |
|
124 case 3: |
|
125 iBuffer = KMessageResponseData3().AllocL(); |
|
126 break; |
|
127 case 4: |
|
128 iBuffer = KMessageResponseData4().AllocL(); |
|
129 break; |
|
130 default: |
|
131 break; |
|
132 } |
|
133 aData.Set(*iBuffer); |
|
134 } |
|
135 |
|
136 void CTestMessageParser::ReleaseDataPacket() |
|
137 { |
|
138 |
|
139 } |
|
140 |
|
141 void CTestMessageParser::StartLineL(const TDesC8& aStartLine) |
|
142 { |
|
143 // The RFC2616 defines the Status-Line as follows - |
|
144 // |
|
145 // Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF |
|
146 // |
|
147 // First extract the HTTP-Version |
|
148 TPtrC8 data = aStartLine; |
|
149 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft)); |
|
150 // HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT |
|
151 TInt consumed = data.Locate('/'); |
|
152 User::LeaveIfError(consumed); |
|
153 RStringPool stringPool = iTransaction.Session().StringPool(); |
|
154 TPtrC8 http = data.Left(consumed); |
|
155 if( http.CompareF(stringPool.StringF(HTTP::EHTTP, RHTTPSession::GetTable()).DesC()) != 0 ) |
|
156 User::Leave(KErrCorrupt); |
|
157 // Skip past the HTTP and "/" |
|
158 data.Set(data.Mid(consumed + 1)); |
|
159 // Extract the major version number |
|
160 TInt major; |
|
161 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, major); |
|
162 User::LeaveIfError(consumed); |
|
163 // Skip past major version number and the "." |
|
164 data.Set(data.Mid(consumed + 1)); |
|
165 // Extract the minor version number |
|
166 TInt minor; |
|
167 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, minor); |
|
168 User::LeaveIfError(consumed); |
|
169 // Skip past minor version number and the SP |
|
170 data.Set(data.Mid(consumed + 1)); |
|
171 // Clear any extra surrounding whitespace |
|
172 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveBoth)); |
|
173 // Extract the status code |
|
174 TInt status; |
|
175 consumed = InetProtTextUtils::ConvertDescriptorToInt(data, status); |
|
176 User::LeaveIfError(consumed); |
|
177 if( data.Length() > consumed ) |
|
178 { |
|
179 // Skip past status code and the SP |
|
180 data.Set(data.Mid(consumed + 1)); |
|
181 |
|
182 // Remaining data is the status reason - trim any leading whitespace as right side already trimmed |
|
183 User::LeaveIfError(InetProtTextUtils::RemoveWhiteSpace(data, InetProtTextUtils::ERemoveLeft)); |
|
184 } |
|
185 else |
|
186 { |
|
187 // No reason phrase following the status code |
|
188 data.Set(KNullDesC8()); |
|
189 } |
|
190 // Populate transaction with Status-Line info |
|
191 RStringF reason = stringPool.OpenFStringL(data); |
|
192 RHTTPResponse response = iTransaction.Response(); |
|
193 TVersion version(major, minor, 0); |
|
194 response.SetVersion(version); |
|
195 response.SetStatusCode(status); |
|
196 response.SetStatusText(reason); |
|
197 reason.Close(); |
|
198 } |
|
199 |
|
200 void CTestMessageParser::HeaderL(const TDesC8& aFieldName, TDesC8& aFieldValue) |
|
201 { |
|
202 RStringPool stringPool = iTransaction.Session().StringPool(); |
|
203 RStringF name = stringPool.OpenFStringL(aFieldName); |
|
204 CleanupClosePushL(name); |
|
205 iHTTPHeaders = iTransaction.Response().GetHeaderCollection(); |
|
206 iHTTPHeaders.SetRawFieldL(name, aFieldValue, KHeaderSeparator); |
|
207 CleanupStack::PopAndDestroy(&name); |
|
208 } |
|
209 |
|
210 TInt CTestMessageParser::BodySizeL() |
|
211 { |
|
212 return ENoBody; |
|
213 } |
|
214 |
|
215 void CTestMessageParser::BodyChunkL(const TDesC8& /*aData*/) |
|
216 { |
|
217 } |
|
218 |
|
219 void CTestMessageParser::BodyCompleteL() |
|
220 { |
|
221 } |
|
222 |
|
223 void CTestMessageParser::MessageCompleteL(const TPtrC8& /*aExcessData*/) |
|
224 { |
|
225 iObserver.NotifyComplete(); |
|
226 } |
|
227 |
|
228 TInt CTestMessageParser::HandleParserError(TInt /*aError*/) |
|
229 { |
|
230 return KErrNone; |
|
231 } |
|
232 |
|
233 void CTestMessageParser::Reserved_MHttpMessageParserObserver() |
|
234 { |
|
235 User::Invariant(); |
|
236 } |
|
237 |
|
238 TInt CTestMessageParser::MHFRunError(TInt /*aError*/, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/) |
|
239 { |
|
240 return KErrNone; |
|
241 } |
|
242 |
|
243 void CTestMessageParser::MHFRunL(RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/) |
|
244 { |
|
245 } |