1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is Url Classifier code
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
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.
35 * ***** END LICENSE BLOCK ***** */
37 #ifndef nsUrlClassifierUtils_h_
38 #define nsUrlClassifierUtils_h_
40 #include "nsAutoPtr.h"
41 #include "nsIUrlClassifierUtils.h"
43 #include "nsDataHashtable.h"
45 class nsUrlClassifierUtils
: public nsIUrlClassifierUtils
49 * A fast, bit-vector map for ascii characters.
51 * Internally stores 256 bits in an array of 8 ints.
52 * Does quick bit-flicking to lookup needed characters.
57 Charmap(PRUint32 b0
, PRUint32 b1
, PRUint32 b2
, PRUint32 b3
,
58 PRUint32 b4
, PRUint32 b5
, PRUint32 b6
, PRUint32 b7
)
60 mMap
[0] = b0
; mMap
[1] = b1
; mMap
[2] = b2
; mMap
[3] = b3
;
61 mMap
[4] = b4
; mMap
[5] = b5
; mMap
[6] = b6
; mMap
[7] = b7
;
65 * Do a quick lookup to see if the letter is in the map.
67 PRBool
Contains(unsigned char c
) const
69 return mMap
[c
>> 5] & (1 << (c
& 31));
73 // Store the 256 bits in an 8 byte array.
79 nsUrlClassifierUtils();
80 ~nsUrlClassifierUtils() {}
83 NS_DECL_NSIURLCLASSIFIERUTILS
87 nsresult
CanonicalizeHostname(const nsACString
& hostname
,
88 nsACString
& _retval
);
89 nsresult
CanonicalizePath(const nsACString
& url
, nsACString
& _retval
);
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
97 PRBool
SpecialEncode(const nsACString
& url
,
99 nsACString
& _retval
);
101 void ParseIPAddress(const nsACString
& host
, nsACString
& _retval
);
102 void CanonicalNum(const nsACString
& num
,
105 nsACString
& _retval
);
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
);
111 // Takes an urlsafe-base64 encoded client key and gives back binary
113 static nsresult
DecodeClientKey(const nsACString
& clientKey
,
114 nsACString
& _retval
);
116 // Disallow copy constructor
117 nsUrlClassifierUtils(const nsUrlClassifierUtils
&);
119 // Function to tell if we should encode a character.
120 PRBool
ShouldURLEscape(const unsigned char c
) const;
122 void CleanupHostname(const nsACString
& host
, nsACString
& _retval
);
124 nsAutoPtr
<Charmap
> mEscapeCharmap
;
127 // An MRU list of fragments. This is used by the DB service to
128 // keep a set of known-clean fragments that don't need a database
130 class nsUrlClassifierFragmentSet
133 nsUrlClassifierFragmentSet() : mFirst(nsnull
), mLast(nsnull
), mCapacity(16) {}
135 PRBool
Init(PRUint32 maxEntries
) {
136 mCapacity
= maxEntries
;
137 if (!mEntryStorage
.SetCapacity(mCapacity
))
140 if (!mEntries
.Init())
146 PRBool
Put(const nsACString
&fragment
) {
148 if (mEntries
.Get(fragment
, &entry
)) {
149 // Remove this entry from the list, we'll add it back
153 if (mEntryStorage
.Length() < mEntryStorage
.Capacity()) {
154 entry
= mEntryStorage
.AppendElement();
158 // Reuse the oldest entry.
161 mEntries
.Remove(entry
->mFragment
);
163 entry
->mFragment
= fragment
;
164 mEntries
.Put(fragment
, entry
);
167 // Add the entry to the front of the list
168 entry
->mPrev
= nsnull
;
169 entry
->mNext
= mFirst
;
178 PRBool
Has(const nsACString
&fragment
) {
179 return mEntries
.Get(fragment
, nsnull
);
183 mFirst
= mLast
= nsnull
;
185 mEntryStorage
.Clear();
186 mEntryStorage
.SetCapacity(mCapacity
);
190 // One entry in the set. We maintain a doubly-linked list, with
191 // the most recently used entry at the front.
194 Entry() : mNext(nsnull
), mPrev(nsnull
) {};
202 void UnlinkEntry(Entry
*entry
)
205 entry
->mPrev
->mNext
= entry
->mNext
;
207 mFirst
= entry
->mNext
;
210 entry
->mNext
->mPrev
= entry
->mPrev
;
212 mLast
= entry
->mPrev
;
214 entry
->mPrev
= entry
->mNext
= nsnull
;
217 // The newest entry in the cache.
219 // The oldest entry in the cache.
222 // Max entries in the cache.
225 // Storage for the entries in this set.
226 nsTArray
<Entry
> mEntryStorage
;
228 // Entry lookup by fragment.
229 nsDataHashtable
<nsCStringHashKey
, Entry
*> mEntries
;
232 #endif // nsUrlClassifierUtils_h_