|
1 // Copyright (c) 2000-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 // Purpose: Definition of CUrl class - Url processor based on RFC2396. |
|
15 // CUrl encapsulates a url and provides access to its components. On creation, the contents of |
|
16 // the url is in unescaped mode - excluded characters (as defined by RFC2396) have not been coded into their |
|
17 // escape triples. Two NewL() functions are provided. The first takes any descriptor and encapsulates it into |
|
18 // the CUrl object, and leaves if the the url begins with a ':' (EWapErrCorruptUrl) or if the scheme is |
|
19 // |
|
20 // |
|
21 |
|
22 // corrupted (EWapErrCorruptScheme). The second NewL() creates a url with a file scheme (file://) from a |
|
23 // TParseBase argument, and will leave if the url is invalid (EWapErrCorruptUrl). Two static functions have |
|
24 // been provided that each take a descriptor argument which is then escape encoded/decoded. |
|
25 // |
|
26 // NOTE - maintainer needs detailed knowledge of url parsing (RFC2396) |
|
27 // |
|
28 |
|
29 #if !defined(__URLBASE_H__) |
|
30 #define __URLBASE_H__ |
|
31 |
|
32 // System includes |
|
33 // |
|
34 #include <e32base.h> |
|
35 #include <f32file.h> |
|
36 #include <wapengstd.h> |
|
37 |
|
38 |
|
39 // Definition of CUrl |
|
40 // |
|
41 class CUrl : public CBase |
|
42 { |
|
43 public: // Type definitions |
|
44 |
|
45 // Enum of component parts of the Url. |
|
46 // |
|
47 enum TComponent |
|
48 { |
|
49 EUrlScheme = 0x01, |
|
50 EUrlLocation = 0x02, |
|
51 EUrlPath = 0x04, |
|
52 EUrlQuery = 0x08, |
|
53 EUrlFragment = 0x10, |
|
54 EUrlFileName = 0x20, // the end of the path |
|
55 EUrlUsername = 0x40, |
|
56 EUrlPassword = 0x80, |
|
57 EUrlAuthority = EUrlLocation | EUrlUsername | EUrlPassword, |
|
58 EUrlNoCredentials = EUrlScheme | EUrlLocation | EUrlPath | EUrlQuery | |
|
59 EUrlFragment, |
|
60 EUrlGenericCompare = EUrlScheme | EUrlAuthority | EUrlPath | EUrlQuery |
|
61 }; |
|
62 |
|
63 public: // Methods |
|
64 |
|
65 // Static factory c'tor. Creates a CUrl object from a |
|
66 // descriptor. This can hold any kind of Url - local file or http, |
|
67 // full url with scheme, or a relative url. Leaves if scheme is |
|
68 // corrupted or url is invalid. |
|
69 // |
|
70 // In: |
|
71 // aUrl - descriptor of URL |
|
72 // |
|
73 // Rtn: pointer to created CUrl object |
|
74 // |
|
75 IMPORT_C static CUrl* NewL(const TDesC& aUrl); |
|
76 |
|
77 // Static factory c'tor. Creates a CUrl objects from a TParseBase |
|
78 // that refers to a file in the local file system. This function |
|
79 // will prefix path with file scheme - file://. Leaves if url is |
|
80 // invalid. |
|
81 // |
|
82 // In: |
|
83 // aFileName - path for a resource in local file system |
|
84 // |
|
85 // Rtn: pointer to created CUrl object |
|
86 // |
|
87 IMPORT_C static CUrl* NewL(const TParseBase& aFileName); |
|
88 |
|
89 // Resolves aRelativeUrl against aBaseUrl based on relative |
|
90 // parsing algorithm in RFC2396. Resolved url is returned Note - |
|
91 // if aBaseUrl is empty, the relative url aRelativeUrl is |
|
92 // returned. |
|
93 // |
|
94 // In: |
|
95 // aBaseUrl - base url to resolve against |
|
96 // aRelativeUrl - relative url |
|
97 // |
|
98 // Rtn: resolved url - owner is transfered to caller |
|
99 // |
|
100 IMPORT_C static CUrl* ResolveL(CUrl& aBaseUrl, CUrl& aRelativeUrl); |
|
101 |
|
102 // Sets current url to aUrl. If leave occurs, this object is unchanged. |
|
103 // |
|
104 // In: |
|
105 // aUrl - the new url |
|
106 // |
|
107 IMPORT_C void SetL(CUrl& aUrl); |
|
108 |
|
109 // Returns a copy of itself. |
|
110 // |
|
111 // Rtn: copy of itself - ownership transfered to caller. |
|
112 // |
|
113 IMPORT_C CUrl* AllocL() const; |
|
114 |
|
115 // Returns a copy of one of its components.Currently any defined value of component |
|
116 // is supported, but the result may well not be a valid URL. |
|
117 // |
|
118 // In: |
|
119 // aComponent - the desired component of the url |
|
120 // |
|
121 // Rtn: copy of the desired url component - ownership is transfered to caller. |
|
122 // |
|
123 IMPORT_C CUrl* AllocL(TComponent aComponent) const; |
|
124 |
|
125 // Returns a TPtrC to the specified component part of tbe Url. |
|
126 // |
|
127 // In: |
|
128 // aType - the desired component of the url. |
|
129 // |
|
130 // Rtn: pointer to the component in the url |
|
131 // |
|
132 IMPORT_C virtual const TPtrC Component(TComponent aType) const; |
|
133 |
|
134 // Sets the specified component to the specified value. This function is not completely |
|
135 // implemented - currently it will work in the following circumstances; |
|
136 // 1 - the desired component already exists in the URL |
|
137 // 2 - it's the username or password and the location exists. |
|
138 // In other cases the function will leave with KErrNotSupported. |
|
139 // |
|
140 // In: |
|
141 // aComponent - the component to be set. |
|
142 // aValue - the new value. NB must already be escaped. |
|
143 // |
|
144 IMPORT_C void SetComponentL(TComponent aComponent, const TDesC& aValue); |
|
145 |
|
146 // Overloaded equality operator. |
|
147 // |
|
148 // In: |
|
149 // aUrl - the Url to be compared against. |
|
150 // |
|
151 // Rtn: ETrue is returned if aUrl is the same as this url. |
|
152 // |
|
153 IMPORT_C TBool operator==(CUrl& aUrl) const; |
|
154 |
|
155 // Compares the specified component part of two urls. |
|
156 // |
|
157 // In: |
|
158 // aUrl - the url to be compared against. |
|
159 // aCompareComps - the component of the urls to be compared. Defaults to components defined by EUrlGenericCompare. |
|
160 // |
|
161 // Rtn: zero is returned for an exact match. |
|
162 // |
|
163 IMPORT_C TInt Compare(CUrl& aUrl, TInt aCompareComps = EUrlGenericCompare) const; |
|
164 |
|
165 // Returns TDesC to descriptor of entire url. |
|
166 // |
|
167 // Rtn: the descriptor to the url. |
|
168 // |
|
169 inline const TDesC& UrlDes() const; |
|
170 |
|
171 // THIS SHOULD NOT BE USED AS CAUSES PANIC - WILL BE REMOVED. |
|
172 // |
|
173 IMPORT_C CUrl* UrlEscapedL() const; |
|
174 |
|
175 // THIS SHOULD NOT BE USED AS CAUSES - WILL BE REMOVED. |
|
176 // |
|
177 IMPORT_C CUrl* UrlUnescapedL() const; |
|
178 |
|
179 // D'tor |
|
180 // |
|
181 IMPORT_C ~CUrl(); |
|
182 |
|
183 // Encodes any excluded characters in input string as escape triples. Uses the overloaded |
|
184 // function. |
|
185 // |
|
186 // THIS SHOULD NOT BE USED - WILL BE REMOVED. USE NEW OVERLOAD FUNCTION. |
|
187 // |
|
188 // In: |
|
189 // aString - descriptor with string to be encoded. |
|
190 // |
|
191 // Rtn: version of string with excluded characters converted to |
|
192 // escape triples - ownership transfered to caller. |
|
193 // |
|
194 IMPORT_C static HBufC* EscapeEncodeL(const TDesC& aString); |
|
195 |
|
196 // Encodes any excluded characters in input string as escape triples. The excluded characters |
|
197 // are set by the value or aEscapeMode. Control characters, space (ASCII 0x20) and characters |
|
198 // above 127 are always encoded as escape triples. |
|
199 // |
|
200 // In: |
|
201 // aString - descriptor with string to be encoded. |
|
202 // aEscapeMode - governs which chars are to excluded (and escaped) |
|
203 // |
|
204 // Rtn: version of string with excluded characters converted to |
|
205 // escape triples - ownership transfered to caller. |
|
206 // |
|
207 IMPORT_C static HBufC* EscapeEncodeL(const TDesC& aString, TInt aEscapeMode); |
|
208 |
|
209 // Decodes any escape triples in input string into original |
|
210 // excluded characters. |
|
211 // |
|
212 // In: |
|
213 // aString - descriptor with string to be decoded. |
|
214 // |
|
215 // Rtn: version of string with escape triples converted back to |
|
216 // excluded characters - ownership transfered to caller. |
|
217 // |
|
218 IMPORT_C static HBufC* EscapeDecodeL(const TDesC& aString); |
|
219 |
|
220 // Convert a Unicode string into UTF8 format. |
|
221 // |
|
222 // In: |
|
223 // aString - the input Unicode string. |
|
224 // |
|
225 // Rtn: the converted string (UTF8 format) - ownership transfered to caller. |
|
226 // |
|
227 IMPORT_C static HBufC8* ConvertFromUnicodeToUtf8L(const TDesC& aString); |
|
228 |
|
229 // Convert a UTF8 string into Unicode format. |
|
230 // |
|
231 // In: |
|
232 // aString - the input UTF8 string. |
|
233 // |
|
234 // Rtn: the converted string (Unicode format) - ownership transfered to caller. |
|
235 // |
|
236 IMPORT_C static HBufC* ConvertToUnicodeFromUtf8L(const TDesC8& aString); |
|
237 |
|
238 private: // Type definitions |
|
239 |
|
240 // Helper class used to resolve a relative path against a base path. |
|
241 // |
|
242 class TRelativePaths; |
|
243 |
|
244 // Enumeration of panic codes for this class |
|
245 // |
|
246 enum TPanicCode |
|
247 { |
|
248 EInvalidUrl |
|
249 }; |
|
250 |
|
251 private: // Methods |
|
252 |
|
253 // Normal c'tor - non-allocating creation of this class |
|
254 // |
|
255 CUrl(); |
|
256 |
|
257 // Second-phase c'tor for general url - any allocation takes place here. |
|
258 // |
|
259 // In: |
|
260 // aUrl - descriptor containing url. |
|
261 // |
|
262 void ConstructL(const TDesC& aUrl); |
|
263 |
|
264 // Second-phase c'tor for url with file scheme - any allocation |
|
265 // takes place here. |
|
266 // |
|
267 // In: |
|
268 // aFileName - path to local file system resource. |
|
269 // |
|
270 void ConstructL(const TParseBase& aFileName); |
|
271 |
|
272 // Panic handler for ths class - kills the process in the event of |
|
273 // a panic |
|
274 // |
|
275 // In: |
|
276 // aPanicCode - code enumerating the panic that has occured. |
|
277 // |
|
278 void Panic(TPanicCode aPanicCode) const; |
|
279 |
|
280 // Checks the scheme if present to ensure it is valid |
|
281 // Leaves with EWapErrCorruptScheme if not valid |
|
282 // |
|
283 // In: |
|
284 // aScheme - the scheme to be checked |
|
285 // |
|
286 void CheckSchemeValidL(const TDesC& aScheme) const; |
|
287 |
|
288 // Parses a url to find the start and end of a specified component |
|
289 // in the url. |
|
290 // |
|
291 // In: |
|
292 // aComponent - the component sought. |
|
293 // aUrl - descriptor of url to be parsed |
|
294 // |
|
295 // Out: |
|
296 // aStartPos - index of start of component; is KCUrlInvalidCharPos |
|
297 // if component not found. |
|
298 // aEndPos - index of end of component; is KCUrlInvalidCharPos if |
|
299 // component not found. |
|
300 // |
|
301 void Part(TComponent aComponent, const TDesC& aUrl, TInt& aStartPos, |
|
302 TInt& aEndPos) const; |
|
303 |
|
304 // Parses an identified authority to extract the location, |
|
305 // username or password. |
|
306 // |
|
307 // In: |
|
308 // aComponent - the component sought. (EUrlLocation, EUrlUsername |
|
309 // or EUrlPassword) |
|
310 // aUrl - descriptor of url to be parsed |
|
311 // |
|
312 // In/Out: |
|
313 // aStartPos - index of start of component; is KCUrlInvalidCharPos |
|
314 // if component not found. Is the start of the authority on inupt. |
|
315 // aEndPos - index of end of component; is KCUrlInvalidCharPos if |
|
316 // component not found. End of authority on input. |
|
317 void PartOfAuthority(TComponent aComponent, const TDesC& aUrl, |
|
318 TInt& aStartPos, TInt& aEndPos) const; |
|
319 |
|
320 private: // Attributes |
|
321 |
|
322 // Pointer to buffer containing url. Owned by this class |
|
323 // |
|
324 HBufC* iUrlDes; |
|
325 }; |
|
326 |
|
327 inline const TDesC& CUrl::UrlDes() const |
|
328 // |
|
329 // Return descriptor to url |
|
330 { |
|
331 return *iUrlDes; |
|
332 } |
|
333 |
|
334 class CUrl::TRelativePaths |
|
335 // |
|
336 // Helper class used to resolve a relative path against a base path |
|
337 { |
|
338 public: // Methods |
|
339 |
|
340 // Normal c'tor |
|
341 // |
|
342 // In: |
|
343 // aBasePath - base path to resolve against |
|
344 // aRelativePath - relative path |
|
345 // aResolvedPath - result of resolving relative path against base path |
|
346 // |
|
347 TRelativePaths(TPtrC aBasePath, TPtrC aRelativePath, TPtr aResolvedPath); |
|
348 |
|
349 // Does the path resolution |
|
350 // |
|
351 void ResolveRelativePaths(); |
|
352 |
|
353 // Cleans resolved path, removing structures such as '/../' |
|
354 // |
|
355 void CleanResolvedPath(); |
|
356 |
|
357 public: // Attributes |
|
358 |
|
359 // Descriptor for base path |
|
360 // |
|
361 TPtrC iBasePath; |
|
362 |
|
363 // Descriptor for relative path |
|
364 // |
|
365 TPtrC iRelativePath; |
|
366 |
|
367 // Modifiable descriptor to resolved path |
|
368 // |
|
369 TPtr iResolvedPath; |
|
370 }; |
|
371 |
|
372 #endif // __URLBASE_H__ |