LCOV - code coverage report
Current view: directory - netwerk/protocol/http - nsHttpAuthCache.h (source / functions) Found Hit Coverage
Test: app.info Lines: 27 27 100.0 %
Date: 2012-06-02 Functions: 17 17 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Gagan Saksena <gagan@netscape.com> (original author)
      24                 :  *   Darin Fisher <darin@netscape.com>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef nsHttpAuthCache_h__
      41                 : #define nsHttpAuthCache_h__
      42                 : 
      43                 : #include "nsHttp.h"
      44                 : #include "nsError.h"
      45                 : #include "nsTArray.h"
      46                 : #include "nsAutoPtr.h"
      47                 : #include "nsAString.h"
      48                 : #include "nsString.h"
      49                 : #include "nsCOMPtr.h"
      50                 : #include "plhash.h"
      51                 : #include "nsCRT.h"
      52                 : 
      53                 : 
      54                 : struct nsHttpAuthPath {
      55                 :     struct nsHttpAuthPath *mNext;
      56                 :     char                   mPath[1];
      57                 : };
      58                 : 
      59                 : //-----------------------------------------------------------------------------
      60                 : // nsHttpAuthIdentity
      61                 : //-----------------------------------------------------------------------------
      62                 : 
      63                 : class nsHttpAuthIdentity
      64                 : {
      65                 : public:
      66            7055 :     nsHttpAuthIdentity()
      67                 :         : mUser(nsnull)
      68                 :         , mPass(nsnull)
      69            7055 :         , mDomain(nsnull)
      70                 :     {
      71            7055 :     }
      72               4 :     nsHttpAuthIdentity(const PRUnichar *domain,
      73                 :                        const PRUnichar *user,
      74                 :                        const PRUnichar *password)
      75               4 :         : mUser(nsnull)
      76                 :     {
      77               4 :         Set(domain, user, password);
      78               4 :     }
      79            6511 :    ~nsHttpAuthIdentity()
      80                 :     {
      81            6511 :         Clear();
      82            6511 :     }
      83                 : 
      84              35 :     const PRUnichar *Domain()   const { return mDomain; }
      85              42 :     const PRUnichar *User()     const { return mUser; }
      86              28 :     const PRUnichar *Password() const { return mPass; }
      87                 : 
      88                 :     nsresult Set(const PRUnichar *domain,
      89                 :                  const PRUnichar *user,
      90                 :                  const PRUnichar *password);
      91              34 :     nsresult Set(const nsHttpAuthIdentity &other) { return Set(other.mDomain, other.mUser, other.mPass); }
      92                 :     void Clear();
      93                 : 
      94                 :     bool Equals(const nsHttpAuthIdentity &other) const;
      95             106 :     bool IsEmpty() const { return !mUser; }
      96                 : 
      97                 : private:
      98                 :     // allocated as one contiguous blob, starting at mUser.
      99                 :     PRUnichar *mUser;
     100                 :     PRUnichar *mPass;
     101                 :     PRUnichar *mDomain;
     102                 : };
     103                 : 
     104                 : //-----------------------------------------------------------------------------
     105                 : // nsHttpAuthEntry
     106                 : //-----------------------------------------------------------------------------
     107                 : 
     108                 : class nsHttpAuthEntry
     109                 : {
     110                 : public:
     111              11 :     const char *Realm()       const { return mRealm; }
     112               7 :     const char *Creds()       const { return mCreds; }
     113               7 :     const char *Challenge()   const { return mChallenge; }
     114              11 :     const PRUnichar *Domain() const { return mIdent.Domain(); }
     115              11 :     const PRUnichar *User()   const { return mIdent.User(); }
     116               4 :     const PRUnichar *Pass()   const { return mIdent.Password(); }
     117               7 :     nsHttpAuthPath *RootPath()      { return mRoot; }
     118                 : 
     119              14 :     const nsHttpAuthIdentity &Identity() const { return mIdent; }
     120                 :             
     121                 :     nsresult AddPath(const char *aPath);
     122                 :             
     123                 :     nsCOMPtr<nsISupports> mMetaData;
     124                 : 
     125                 : private:
     126              27 :     nsHttpAuthEntry(const char *path,
     127                 :                     const char *realm,
     128                 :                     const char *creds,
     129                 :                     const char *challenge,
     130                 :                     const nsHttpAuthIdentity *ident,
     131                 :                     nsISupports *metadata)
     132                 :         : mRoot(nsnull)
     133                 :         , mTail(nsnull)
     134              27 :         , mRealm(nsnull)
     135                 :     {
     136              27 :         Set(path, realm, creds, challenge, ident, metadata);
     137              27 :     }
     138                 :    ~nsHttpAuthEntry();
     139                 : 
     140                 :     nsresult Set(const char *path,
     141                 :                  const char *realm,
     142                 :                  const char *creds,
     143                 :                  const char *challenge,
     144                 :                  const nsHttpAuthIdentity *ident,
     145                 :                  nsISupports *metadata);
     146                 : 
     147                 :     nsHttpAuthIdentity mIdent;
     148                 : 
     149                 :     nsHttpAuthPath *mRoot; //root pointer
     150                 :     nsHttpAuthPath *mTail; //tail pointer
     151                 : 
     152                 :     // allocated together in one blob, starting with mRealm.
     153                 :     char *mRealm;
     154                 :     char *mCreds;
     155                 :     char *mChallenge;
     156                 : 
     157                 :     friend class nsHttpAuthNode;
     158                 :     friend class nsHttpAuthCache;
     159                 :     friend class nsAutoPtr<nsHttpAuthEntry>; // needs to call the destructor
     160                 : };
     161                 : 
     162                 : //-----------------------------------------------------------------------------
     163                 : // nsHttpAuthNode
     164                 : //-----------------------------------------------------------------------------
     165                 : 
     166                 : class nsHttpAuthNode
     167                 : {
     168                 : private:
     169                 :     nsHttpAuthNode();
     170                 :    ~nsHttpAuthNode();
     171                 : 
     172                 :     // path can be null, in which case we'll search for an entry
     173                 :     // with a null path.
     174                 :     nsHttpAuthEntry *LookupEntryByPath(const char *path);
     175                 : 
     176                 :     // realm must not be null
     177                 :     nsHttpAuthEntry *LookupEntryByRealm(const char *realm);
     178                 : 
     179                 :     // if a matching entry is found, then credentials will be changed.
     180                 :     nsresult SetAuthEntry(const char *path,
     181                 :                           const char *realm,
     182                 :                           const char *credentials,
     183                 :                           const char *challenge,
     184                 :                           const nsHttpAuthIdentity *ident,
     185                 :                           nsISupports *metadata);
     186                 : 
     187                 :     void ClearAuthEntry(const char *realm);
     188                 : 
     189                 :     PRUint32 EntryCount() { return mList.Length(); }
     190                 : 
     191                 : private:
     192                 :     nsTArray<nsAutoPtr<nsHttpAuthEntry> > mList;
     193                 : 
     194                 :     friend class nsHttpAuthCache;
     195                 : };
     196                 : 
     197                 : //-----------------------------------------------------------------------------
     198                 : // nsHttpAuthCache
     199                 : //  (holds a hash table from host:port to nsHttpAuthNode)
     200                 : //-----------------------------------------------------------------------------
     201                 : 
     202                 : class nsHttpAuthCache
     203                 : {
     204                 : public:
     205                 :     nsHttpAuthCache();
     206                 :    ~nsHttpAuthCache();
     207                 : 
     208                 :     nsresult Init();
     209                 : 
     210                 :     // |scheme|, |host|, and |port| are required
     211                 :     // |path| can be null
     212                 :     // |entry| is either null or a weak reference
     213                 :     nsresult GetAuthEntryForPath(const char *scheme,
     214                 :                                  const char *host,
     215                 :                                  PRInt32     port,
     216                 :                                  const char *path,
     217                 :                                  nsHttpAuthEntry **entry);
     218                 : 
     219                 :     // |scheme|, |host|, and |port| are required
     220                 :     // |realm| must not be null
     221                 :     // |entry| is either null or a weak reference
     222                 :     nsresult GetAuthEntryForDomain(const char *scheme,
     223                 :                                    const char *host,
     224                 :                                    PRInt32     port,
     225                 :                                    const char *realm,
     226                 :                                    nsHttpAuthEntry **entry);
     227                 : 
     228                 :     // |scheme|, |host|, and |port| are required
     229                 :     // |path| can be null
     230                 :     // |realm| must not be null
     231                 :     // if |credentials|, |user|, |pass|, and |challenge| are each
     232                 :     // null, then the entry is deleted.
     233                 :     nsresult SetAuthEntry(const char *scheme,
     234                 :                           const char *host,
     235                 :                           PRInt32     port,
     236                 :                           const char *directory,
     237                 :                           const char *realm,
     238                 :                           const char *credentials,
     239                 :                           const char *challenge,
     240                 :                           const nsHttpAuthIdentity *ident,
     241                 :                           nsISupports *metadata);
     242                 : 
     243                 :     void ClearAuthEntry(const char *scheme,
     244                 :                         const char *host,
     245                 :                         PRInt32     port,
     246                 :                         const char *realm);
     247                 : 
     248                 :     // expire all existing auth list entries including proxy auths. 
     249                 :     nsresult ClearAll();
     250                 : 
     251                 : private:
     252                 :     nsHttpAuthNode *LookupAuthNode(const char *scheme,
     253                 :                                    const char *host,
     254                 :                                    PRInt32     port,
     255                 :                                    nsCString  &key);
     256                 : 
     257                 :     // hash table allocation functions
     258                 :     static void*        AllocTable(void *, PRSize size);
     259                 :     static void         FreeTable(void *, void *item);
     260                 :     static PLHashEntry* AllocEntry(void *, const void *key);
     261                 :     static void         FreeEntry(void *, PLHashEntry *he, PRUintn flag);
     262                 : 
     263                 :     static PLHashAllocOps gHashAllocOps;
     264                 :     
     265                 : private:
     266                 :     PLHashTable *mDB; // "host:port" --> nsHttpAuthNode
     267                 : };
     268                 : 
     269                 : #endif // nsHttpAuthCache_h__

Generated by: LCOV version 1.7