1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef nsUrlClassifierUtils_h_ 6 #define nsUrlClassifierUtils_h_ 7 8 #include "nsAutoPtr.h" 9 #include "nsIUrlClassifierUtils.h" 10 #include "nsClassHashtable.h" 11 #include "nsIObserver.h" 12 13 class nsUrlClassifierUtils final : public nsIUrlClassifierUtils, 14 public nsIObserver 15 { 16 public: 17 typedef nsClassHashtable<nsCStringHashKey, nsCString> ProviderDictType; 18 19 private: 20 /** 21 * A fast, bit-vector map for ascii characters. 22 * 23 * Internally stores 256 bits in an array of 8 ints. 24 * Does quick bit-flicking to lookup needed characters. 25 */ 26 class Charmap 27 { 28 public: Charmap(uint32_t b0,uint32_t b1,uint32_t b2,uint32_t b3,uint32_t b4,uint32_t b5,uint32_t b6,uint32_t b7)29 Charmap(uint32_t b0, uint32_t b1, uint32_t b2, uint32_t b3, 30 uint32_t b4, uint32_t b5, uint32_t b6, uint32_t b7) 31 { 32 mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3; 33 mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7; 34 } 35 36 /** 37 * Do a quick lookup to see if the letter is in the map. 38 */ Contains(unsigned char c)39 bool Contains(unsigned char c) const 40 { 41 return mMap[c >> 5] & (1 << (c & 31)); 42 } 43 44 private: 45 // Store the 256 bits in an 8 byte array. 46 uint32_t mMap[8]; 47 }; 48 49 50 public: 51 nsUrlClassifierUtils(); 52 53 NS_DECL_THREADSAFE_ISUPPORTS 54 NS_DECL_NSIURLCLASSIFIERUTILS 55 NS_DECL_NSIOBSERVER 56 57 nsresult Init(); 58 59 nsresult CanonicalizeHostname(const nsACString & hostname, 60 nsACString & _retval); 61 nsresult CanonicalizePath(const nsACString & url, nsACString & _retval); 62 63 // This function will encode all "special" characters in typical url encoding, 64 // that is %hh where h is a valid hex digit. The characters which are encoded 65 // by this function are any ascii characters under 32(control characters and 66 // space), 37(%), and anything 127 or above (special characters). Url is the 67 // string to encode, ret is the encoded string. Function returns true if 68 // ret != url. 69 bool SpecialEncode(const nsACString & url, 70 bool foldSlashes, 71 nsACString & _retval); 72 73 void ParseIPAddress(const nsACString & host, nsACString & _retval); 74 void CanonicalNum(const nsACString & num, 75 uint32_t bytes, 76 bool allowOctal, 77 nsACString & _retval); 78 79 private: ~nsUrlClassifierUtils()80 ~nsUrlClassifierUtils() {} 81 82 // Disallow copy constructor 83 nsUrlClassifierUtils(const nsUrlClassifierUtils&); 84 85 // Function to tell if we should encode a character. 86 bool ShouldURLEscape(const unsigned char c) const; 87 88 void CleanupHostname(const nsACString & host, nsACString & _retval); 89 90 nsresult ReadProvidersFromPrefs(ProviderDictType& aDict); 91 92 nsAutoPtr<Charmap> mEscapeCharmap; 93 94 // The provider lookup table and its mutex. 95 ProviderDictType mProviderDict; 96 mozilla::Mutex mProviderDictLock; 97 }; 98 99 #endif // nsUrlClassifierUtils_h_ 100