michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsUrlClassifierUtils_h_ michael@0: #define nsUrlClassifierUtils_h_ michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIUrlClassifierUtils.h" michael@0: #include "nsTArray.h" michael@0: #include "nsDataHashtable.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: class nsUrlClassifierUtils MOZ_FINAL : public nsIUrlClassifierUtils michael@0: { michael@0: private: michael@0: /** michael@0: * A fast, bit-vector map for ascii characters. michael@0: * michael@0: * Internally stores 256 bits in an array of 8 ints. michael@0: * Does quick bit-flicking to lookup needed characters. michael@0: */ michael@0: class Charmap michael@0: { michael@0: public: michael@0: Charmap(uint32_t b0, uint32_t b1, uint32_t b2, uint32_t b3, michael@0: uint32_t b4, uint32_t b5, uint32_t b6, uint32_t b7) michael@0: { michael@0: mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3; michael@0: mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7; michael@0: } michael@0: michael@0: /** michael@0: * Do a quick lookup to see if the letter is in the map. michael@0: */ michael@0: bool Contains(unsigned char c) const michael@0: { michael@0: return mMap[c >> 5] & (1 << (c & 31)); michael@0: } michael@0: michael@0: private: michael@0: // Store the 256 bits in an 8 byte array. michael@0: uint32_t mMap[8]; michael@0: }; michael@0: michael@0: michael@0: public: michael@0: nsUrlClassifierUtils(); michael@0: ~nsUrlClassifierUtils() {} michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIURLCLASSIFIERUTILS michael@0: michael@0: nsresult Init(); michael@0: michael@0: nsresult CanonicalizeHostname(const nsACString & hostname, michael@0: nsACString & _retval); michael@0: nsresult CanonicalizePath(const nsACString & url, nsACString & _retval); michael@0: michael@0: // This function will encode all "special" characters in typical url encoding, michael@0: // that is %hh where h is a valid hex digit. The characters which are encoded michael@0: // by this function are any ascii characters under 32(control characters and michael@0: // space), 37(%), and anything 127 or above (special characters). Url is the michael@0: // string to encode, ret is the encoded string. Function returns true if michael@0: // ret != url. michael@0: bool SpecialEncode(const nsACString & url, michael@0: bool foldSlashes, michael@0: nsACString & _retval); michael@0: michael@0: void ParseIPAddress(const nsACString & host, nsACString & _retval); michael@0: void CanonicalNum(const nsACString & num, michael@0: uint32_t bytes, michael@0: bool allowOctal, michael@0: nsACString & _retval); michael@0: michael@0: private: michael@0: // Disallow copy constructor michael@0: nsUrlClassifierUtils(const nsUrlClassifierUtils&); michael@0: michael@0: // Function to tell if we should encode a character. michael@0: bool ShouldURLEscape(const unsigned char c) const; michael@0: michael@0: void CleanupHostname(const nsACString & host, nsACString & _retval); michael@0: michael@0: nsAutoPtr mEscapeCharmap; michael@0: }; michael@0: michael@0: #endif // nsUrlClassifierUtils_h_