2
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2006 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 the License "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:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#ifndef WEBCLIENTENGINE_H
|
|
20 |
#define WEBCLIENTENGINE_H
|
|
21 |
|
|
22 |
// INCLUDES
|
|
23 |
#include <coecntrl.h>
|
|
24 |
#include <http.h>
|
|
25 |
#include <http/mhttpauthenticationcallback.h>
|
|
26 |
#include <es_sock.h>
|
|
27 |
|
|
28 |
// CONSTANTS
|
|
29 |
const TInt KMaxHeaderNameLength = 32;
|
|
30 |
const TInt KMaxHeaderValueLength = 128;
|
|
31 |
const TInt KMaxAuthTypeLength = 128;
|
|
32 |
const TInt KMaxDateTimeStringLength = 40;
|
|
33 |
const TInt KMaxStatusTextLength = 32;
|
|
34 |
const TInt KMaxUserNameLength = 128;
|
|
35 |
const TInt KMaxPasswordLength = 128;
|
|
36 |
|
|
37 |
// Used user agent for requests
|
|
38 |
_LIT8( KUserAgent, "WebClient 1.0" );
|
|
39 |
|
|
40 |
// This client accepts all content types.
|
|
41 |
// (change to e.g. "text/plain" for plain text only)
|
|
42 |
_LIT8( KAccept, "*/*" );
|
|
43 |
|
|
44 |
// Format for output of data/time values
|
|
45 |
_LIT( KDateFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3" );
|
|
46 |
|
|
47 |
// Some texts for header output
|
|
48 |
_LIT( KColon, ": " );
|
|
49 |
_LIT( Krealm, "Realm: " );
|
|
50 |
|
|
51 |
class CWebClientEngine;
|
|
52 |
|
|
53 |
// CLASS DECLARATION
|
|
54 |
|
|
55 |
/**
|
|
56 |
* MWebClientObserver
|
|
57 |
* CWebClientEngine passes events and responses body data with this interface.
|
|
58 |
* An instance of this class must be provided for construction of CWebClientEngine.
|
|
59 |
*/
|
|
60 |
class MWebClientObserver
|
|
61 |
{
|
|
62 |
public:
|
|
63 |
/**
|
|
64 |
* ClientOpenSessionFailedL( CWebClientEngine& anEngine )
|
|
65 |
* An error occurred opening the HTTP session. The calling code
|
|
66 |
* will leave after this method returns.
|
|
67 |
* @param anError: the error code
|
|
68 |
* Most common error; no access point configured, and session creation
|
|
69 |
* leaves with KErrNotFound.
|
|
70 |
*/
|
|
71 |
virtual void ClientOpenSessionFailedL( CWebClientEngine& anEngine, TInt anError ) = 0;
|
|
72 |
/**
|
|
73 |
* ClientConnectingL()
|
|
74 |
* Called to notify that a connection was initiated
|
|
75 |
*/
|
|
76 |
virtual void ClientConnectingL( CWebClientEngine& anEngine ) = 0;
|
|
77 |
|
|
78 |
/**
|
|
79 |
* ClientHeaderReceivedL()
|
|
80 |
* Called when HTTP header is received.
|
|
81 |
* @param aHeaderData: Header field name and value
|
|
82 |
*/
|
|
83 |
virtual void ClientHeaderReceivedL( CWebClientEngine& anEngine, const TDesC& aHeaderData ) = 0;
|
|
84 |
|
|
85 |
/**
|
|
86 |
* ClientBodyReceivedL()
|
|
87 |
* Called when a part of the HTTP body is received.
|
|
88 |
* @param aBodyData: Part of the body data received. (e.g. part of
|
|
89 |
* the received HTML page)
|
|
90 |
*/
|
|
91 |
virtual void ClientBodyReceivedL( CWebClientEngine& anEngine, const TDesC8& aBodyData ) = 0;
|
|
92 |
|
|
93 |
/**
|
|
94 |
* ClientConnectionCanceledL()
|
|
95 |
* Called to notify that a connection attempt has been canceled
|
|
96 |
*/
|
|
97 |
virtual void ClientConnectionCanceledL( CWebClientEngine& anEngine ) = 0;
|
|
98 |
|
|
99 |
/**
|
|
100 |
* ClientResponseCompleteL
|
|
101 |
* Called to notify that a transaction's response is complete.
|
|
102 |
* See TTransactionEvent::EResponseComplete
|
|
103 |
*/
|
|
104 |
virtual void ClientResponseCompleteL( CWebClientEngine& anEngine ) = 0;
|
|
105 |
|
|
106 |
/**
|
|
107 |
* ClientTransactionSucceeded()
|
|
108 |
* Called to notify that a transaction completed successfully
|
|
109 |
* See TTransactionEvent::ESucceeded
|
|
110 |
*/
|
|
111 |
virtual void ClientTransactionSucceededL( CWebClientEngine& anEngine ) = 0;
|
|
112 |
|
|
113 |
/**
|
|
114 |
* ClientTransactionFailed()
|
|
115 |
* Catch-all for failure.
|
|
116 |
* See TTransactionEvent::EFailed
|
|
117 |
*/
|
|
118 |
virtual void ClientTransactionFailedL( CWebClientEngine& anEngine ) = 0;
|
|
119 |
|
|
120 |
/**
|
|
121 |
* ClientUnknownEventL()
|
|
122 |
* Called to notify that an unknown HTTP event has
|
|
123 |
* been received.
|
|
124 |
* @param aStatus: the iStatus field of the event
|
|
125 |
* See THTTPEvent::iStatus
|
|
126 |
*/
|
|
127 |
virtual void ClientUnknownEventL( CWebClientEngine& anEngine , TInt aStatus ) = 0;
|
|
128 |
|
|
129 |
/**
|
|
130 |
* ClientRunErrorL()
|
|
131 |
* Called when a error occurs in the handling of a transaction event.
|
|
132 |
* @param anError: the error code
|
|
133 |
*/
|
|
134 |
virtual void ClientRunErrorL( CWebClientEngine& anEngine , TInt anError) = 0;
|
|
135 |
|
|
136 |
/**
|
|
137 |
* ClientGetCredentialsL()
|
|
138 |
* Called when authentication has been requested by the server.
|
|
139 |
* Return EFalse for no authentication or e.g. the user cancels
|
|
140 |
* an input dialog. Otherwise return the user name and password
|
|
141 |
* as out parameters along with an ETrue result.
|
|
142 |
* @param aUri: the current URI
|
|
143 |
* @param aRealm: the realm associated with the request
|
|
144 |
* @param aUserName: the returned user name
|
|
145 |
* @param aPassword: the returned password
|
|
146 |
*/
|
|
147 |
virtual TBool ClientGetCredentialsL( CWebClientEngine& anEngine, const TUriC8& aUri,
|
|
148 |
const TDesC8& aRealm,
|
|
149 |
TDes& aUsername,
|
|
150 |
TDes& aPassword ) = 0;
|
|
151 |
|
|
152 |
};
|
|
153 |
|
|
154 |
/**
|
|
155 |
* CWebClientEngine
|
|
156 |
* Provides simple interface to HTTP Client API.
|
|
157 |
*/
|
|
158 |
class CWebClientEngine : public CBase,
|
|
159 |
public MHTTPTransactionCallback,
|
|
160 |
public MHTTPAuthenticationCallback
|
|
161 |
{
|
|
162 |
public:
|
|
163 |
/**
|
|
164 |
* NewL()
|
|
165 |
* Create a CWebClientEngine object.
|
|
166 |
* @param iObserver:
|
|
167 |
* @return A pointer to the created instance of CWebClientEngine
|
|
168 |
*/
|
|
169 |
static CWebClientEngine* NewL( MWebClientObserver& aObserver );
|
|
170 |
|
|
171 |
/**
|
|
172 |
* NewLC()
|
|
173 |
* Create a CWebClientEngine object.
|
|
174 |
* @param iObserver:
|
|
175 |
* @return A pointer to the created instance of CWebClientEngine
|
|
176 |
*/
|
|
177 |
static CWebClientEngine* NewLC( MWebClientObserver& aObserver );
|
|
178 |
|
|
179 |
/**
|
|
180 |
* ~CWebClientEngine()
|
|
181 |
* Destroy the object
|
|
182 |
*/
|
|
183 |
~CWebClientEngine();
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Opens the HTTP session. Automatically called when a session is initiated,
|
|
187 |
* or can be called separately.
|
|
188 |
*/
|
|
189 |
void OpenSessionL();
|
|
190 |
|
|
191 |
/**
|
|
192 |
* IssueHTTPGetL()
|
|
193 |
* Starts a new HTTP GET transaction.
|
|
194 |
* @param aUri: URI to get. (e.g. http://host.org")
|
|
195 |
*/
|
|
196 |
void IssueHTTPGetL( const TDesC8& aUri );
|
|
197 |
|
|
198 |
/**
|
|
199 |
* CancelTransactionL()
|
|
200 |
* Closes currently running transaction and frees resources related to it.
|
|
201 |
*/
|
|
202 |
void CancelTransactionL();
|
|
203 |
|
|
204 |
/**
|
|
205 |
* IsRunning()
|
|
206 |
* Checks if the transaction is running.
|
|
207 |
* @return ETrue, if transaction is currently running.
|
|
208 |
*/
|
|
209 |
inline TBool IsRunning() { return iRunning; };
|
|
210 |
|
|
211 |
private:
|
|
212 |
/**
|
|
213 |
* ConstructL()
|
|
214 |
* Perform the second phase construction of a CWebClientEngine object.
|
|
215 |
*/
|
|
216 |
void ConstructL();
|
|
217 |
|
|
218 |
/**
|
|
219 |
* CWebClientEngine()
|
|
220 |
* Perform the first phase of two phase construction.
|
|
221 |
* @param iObserver:
|
|
222 |
*/
|
|
223 |
CWebClientEngine( MWebClientObserver& iObserver );
|
|
224 |
|
|
225 |
/**
|
|
226 |
* SetHeaderL()
|
|
227 |
* Sets header value of an HTTP request.
|
|
228 |
* @param aHeaders: Headers of the HTTP request
|
|
229 |
* @param aHdrField: Enumerated HTTP header field, e.g. HTTP::EUserAgent
|
|
230 |
* @param aHdrValue: New value for header field
|
|
231 |
*/
|
|
232 |
void SetHeaderL( RHTTPHeaders aHeaders, TInt aHdrField,
|
|
233 |
const TDesC8& aHdrValue );
|
|
234 |
|
|
235 |
/**
|
|
236 |
* DumpRespHeadersL()
|
|
237 |
* Called when HTTP header is received.
|
|
238 |
* Displays HTTP header field names and values
|
|
239 |
* @param aTransaction: The transaction that is processed.
|
|
240 |
*/
|
|
241 |
void DumpRespHeadersL( RHTTPTransaction& aTransantion );
|
|
242 |
|
|
243 |
/**
|
|
244 |
* HandleRunErrorL()
|
|
245 |
* Called from MHFRunError() when *leave* occurs in handling of transaction event.
|
|
246 |
* @param aError: The leave code that occured.
|
|
247 |
*/
|
|
248 |
void HandleRunErrorL( TInt aError );
|
|
249 |
|
|
250 |
/**
|
|
251 |
* From MHTTPSessionEventCallback
|
|
252 |
*/
|
|
253 |
private:
|
|
254 |
/**
|
|
255 |
* MHFRunL()
|
|
256 |
* Called by framework to notify about transaction events.
|
|
257 |
* @param aTransaction: Transaction, where the event occured.
|
|
258 |
* @param aEvent: Occured event.
|
|
259 |
*/
|
|
260 |
void MHFRunL( RHTTPTransaction aTransaction, const THTTPEvent& aEvent );
|
|
261 |
|
|
262 |
/**
|
|
263 |
* MHFRunError()
|
|
264 |
* Called by framework when *leave* occurs in handling of transaction event.
|
|
265 |
* @param aError: The leave code that occured.
|
|
266 |
* @param aTransaction: The transaction that was being processed when leave occured.
|
|
267 |
* @param aEvent: The event that was being processed when leave occured.
|
|
268 |
* @return KErrNone, if the error was handled. Otherwise the value of aError, or
|
|
269 |
* some other error value. Returning error value causes causes
|
|
270 |
* HTTP-CORE 6 panic.
|
|
271 |
*/
|
|
272 |
TInt MHFRunError( TInt aError,
|
|
273 |
RHTTPTransaction aTransaction,
|
|
274 |
const THTTPEvent& aEvent );
|
|
275 |
|
|
276 |
/**
|
|
277 |
* From MHTTPAuthenticationCallback (needed for HTTP authentication)
|
|
278 |
*/
|
|
279 |
private:
|
|
280 |
/**
|
|
281 |
* GetCredentialsL()
|
|
282 |
* Called by framework when username and password for requested URI is
|
|
283 |
* needed.
|
|
284 |
* @param aURI: The URI being requested (e.g. "http://host.org")
|
|
285 |
* @param aRealm: The realm being requested (e.g. "user@host.org")
|
|
286 |
* @param aAuthenticationType: Authentication type. (e.g. "Basic")
|
|
287 |
* @param aUsername: Given user name.
|
|
288 |
* @param aPassword: Given password.
|
|
289 |
* @return A pointer to the created document
|
|
290 |
*/
|
|
291 |
TBool GetCredentialsL( const TUriC8& aUri,
|
|
292 |
RString aRealm,
|
|
293 |
RStringF aAuthenticationType,
|
|
294 |
RString& aUsername,
|
|
295 |
RString& aPassword );
|
|
296 |
|
|
297 |
private: // Data
|
|
298 |
RHTTPSession iSession;
|
|
299 |
RHTTPTransaction iTransaction;
|
|
300 |
RSocketServ iSocketServ;
|
|
301 |
RConnection iConnection;
|
|
302 |
|
|
303 |
MWebClientObserver& iObserver; // Used for passing body data and
|
|
304 |
// events to UI.
|
|
305 |
TBool iSessionOpened; // ETrue, if session successfully opened
|
|
306 |
TBool iRunning; // ETrue, if transaction running
|
|
307 |
};
|
|
308 |
|
|
309 |
#endif // WEBCLIENTENGINE_H
|