1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Url Classifier code
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Google Inc.
18 : * Portions created by the Initial Developer are Copyright (C) 2007
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #ifndef nsUrlClassifierUtils_h_
38 : #define nsUrlClassifierUtils_h_
39 :
40 : #include "nsAutoPtr.h"
41 : #include "nsIUrlClassifierUtils.h"
42 : #include "nsTArray.h"
43 : #include "nsDataHashtable.h"
44 :
45 : class nsUrlClassifierUtils : public nsIUrlClassifierUtils
46 : {
47 : private:
48 : /**
49 : * A fast, bit-vector map for ascii characters.
50 : *
51 : * Internally stores 256 bits in an array of 8 ints.
52 : * Does quick bit-flicking to lookup needed characters.
53 : */
54 : class Charmap
55 : {
56 : public:
57 5 : Charmap(PRUint32 b0, PRUint32 b1, PRUint32 b2, PRUint32 b3,
58 : PRUint32 b4, PRUint32 b5, PRUint32 b6, PRUint32 b7)
59 : {
60 5 : mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3;
61 5 : mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7;
62 5 : }
63 :
64 : /**
65 : * Do a quick lookup to see if the letter is in the map.
66 : */
67 : bool Contains(unsigned char c) const
68 : {
69 : return mMap[c >> 5] & (1 << (c & 31));
70 : }
71 :
72 : private:
73 : // Store the 256 bits in an 8 byte array.
74 : PRUint32 mMap[8];
75 : };
76 :
77 :
78 : public:
79 : nsUrlClassifierUtils();
80 5 : ~nsUrlClassifierUtils() {}
81 :
82 : NS_DECL_ISUPPORTS
83 : NS_DECL_NSIURLCLASSIFIERUTILS
84 :
85 : nsresult Init();
86 :
87 : nsresult CanonicalizeHostname(const nsACString & hostname,
88 : nsACString & _retval);
89 : nsresult CanonicalizePath(const nsACString & url, nsACString & _retval);
90 :
91 : // This function will encode all "special" characters in typical url encoding,
92 : // that is %hh where h is a valid hex digit. The characters which are encoded
93 : // by this function are any ascii characters under 32(control characters and
94 : // space), 37(%), and anything 127 or above (special characters). Url is the
95 : // string to encode, ret is the encoded string. Function returns true if
96 : // ret != url.
97 : bool SpecialEncode(const nsACString & url,
98 : bool foldSlashes,
99 : nsACString & _retval);
100 :
101 : void ParseIPAddress(const nsACString & host, nsACString & _retval);
102 : void CanonicalNum(const nsACString & num,
103 : PRUint32 bytes,
104 : bool allowOctal,
105 : nsACString & _retval);
106 :
107 : // Convert an urlsafe base64 string to a normal base64 string.
108 : // This method will leave an already-normal base64 string alone.
109 : static void UnUrlsafeBase64(nsACString & str);
110 :
111 : // Takes an urlsafe-base64 encoded client key and gives back binary
112 : // key data
113 : static nsresult DecodeClientKey(const nsACString & clientKey,
114 : nsACString & _retval);
115 : private:
116 : // Disallow copy constructor
117 : nsUrlClassifierUtils(const nsUrlClassifierUtils&);
118 :
119 : // Function to tell if we should encode a character.
120 : bool ShouldURLEscape(const unsigned char c) const;
121 :
122 : void CleanupHostname(const nsACString & host, nsACString & _retval);
123 :
124 : nsAutoPtr<Charmap> mEscapeCharmap;
125 : };
126 :
127 : #endif // nsUrlClassifierUtils_h_
|