toolkit/components/url-classifier/nsUrlClassifierUtils.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 #ifndef nsUrlClassifierUtils_h_
     6 #define nsUrlClassifierUtils_h_
     8 #include "nsAutoPtr.h"
     9 #include "nsIUrlClassifierUtils.h"
    10 #include "nsTArray.h"
    11 #include "nsDataHashtable.h"
    12 #include "mozilla/Attributes.h"
    14 class nsUrlClassifierUtils MOZ_FINAL : public nsIUrlClassifierUtils
    15 {
    16 private:
    17   /**
    18    * A fast, bit-vector map for ascii characters.
    19    *
    20    * Internally stores 256 bits in an array of 8 ints.
    21    * Does quick bit-flicking to lookup needed characters.
    22    */
    23   class Charmap
    24   {
    25   public:
    26     Charmap(uint32_t b0, uint32_t b1, uint32_t b2, uint32_t b3,
    27             uint32_t b4, uint32_t b5, uint32_t b6, uint32_t b7)
    28     {
    29       mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3;
    30       mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7;
    31     }
    33     /**
    34      * Do a quick lookup to see if the letter is in the map.
    35      */
    36     bool Contains(unsigned char c) const
    37     {
    38       return mMap[c >> 5] & (1 << (c & 31));
    39     }
    41   private:
    42     // Store the 256 bits in an 8 byte array.
    43     uint32_t mMap[8];
    44   };
    47 public:
    48   nsUrlClassifierUtils();
    49   ~nsUrlClassifierUtils() {}
    51   NS_DECL_ISUPPORTS
    52   NS_DECL_NSIURLCLASSIFIERUTILS
    54   nsresult Init();
    56   nsresult CanonicalizeHostname(const nsACString & hostname,
    57                                 nsACString & _retval);
    58   nsresult CanonicalizePath(const nsACString & url, nsACString & _retval);
    60   // This function will encode all "special" characters in typical url encoding,
    61   // that is %hh where h is a valid hex digit.  The characters which are encoded
    62   // by this function are any ascii characters under 32(control characters and
    63   // space), 37(%), and anything 127 or above (special characters).  Url is the
    64   // string to encode, ret is the encoded string.  Function returns true if
    65   // ret != url.
    66   bool SpecialEncode(const nsACString & url,
    67                        bool foldSlashes,
    68                        nsACString & _retval);
    70   void ParseIPAddress(const nsACString & host, nsACString & _retval);
    71   void CanonicalNum(const nsACString & num,
    72                     uint32_t bytes,
    73                     bool allowOctal,
    74                     nsACString & _retval);
    76 private:
    77   // Disallow copy constructor
    78   nsUrlClassifierUtils(const nsUrlClassifierUtils&);
    80   // Function to tell if we should encode a character.
    81   bool ShouldURLEscape(const unsigned char c) const;
    83   void CleanupHostname(const nsACString & host, nsACString & _retval);
    85   nsAutoPtr<Charmap> mEscapeCharmap;
    86 };
    88 #endif // nsUrlClassifierUtils_h_

mercurial