Back to index

lightning-sunbird  0.9+nobinonly
nsHttpAuthCache.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Gagan Saksena <gagan@netscape.com> (original author)
00024  *   Darin Fisher <darin@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsHttpAuthCache_h__
00041 #define nsHttpAuthCache_h__
00042 
00043 #include "nsHttp.h"
00044 #include "nsError.h"
00045 #include "nsVoidArray.h"
00046 #include "nsAString.h"
00047 #include "nsString.h"
00048 #include "nsCOMPtr.h"
00049 #include "plhash.h"
00050 #include "nsCRT.h"
00051 
00052 
00053 struct nsHttpAuthPath {
00054     struct nsHttpAuthPath *mNext;
00055     char                   mPath[1];
00056 };
00057 
00058 //-----------------------------------------------------------------------------
00059 // nsHttpAuthIdentity
00060 //-----------------------------------------------------------------------------
00061 
00062 class nsHttpAuthIdentity
00063 {
00064 public:
00065     nsHttpAuthIdentity()
00066         : mUser(nsnull)
00067         , mPass(nsnull)
00068         , mDomain(nsnull)
00069     {
00070     }
00071     nsHttpAuthIdentity(const PRUnichar *domain,
00072                        const PRUnichar *user,
00073                        const PRUnichar *password)
00074         : mUser(nsnull)
00075     {
00076         Set(domain, user, password);
00077     }
00078    ~nsHttpAuthIdentity()
00079     {
00080         Clear();
00081     }
00082 
00083     const PRUnichar *Domain()   const { return mDomain; }
00084     const PRUnichar *User()     const { return mUser; }
00085     const PRUnichar *Password() const { return mPass; }
00086 
00087     nsresult Set(const PRUnichar *domain,
00088                  const PRUnichar *user,
00089                  const PRUnichar *password);
00090     nsresult Set(const nsHttpAuthIdentity &other) { return Set(other.mDomain, other.mUser, other.mPass); }
00091     void Clear();
00092 
00093     PRBool Equals(const nsHttpAuthIdentity &other) const;
00094     PRBool IsEmpty() const { return !mUser; }
00095 
00096 private:
00097     // allocated as one contiguous blob, starting at mUser.
00098     PRUnichar *mUser;
00099     PRUnichar *mPass;
00100     PRUnichar *mDomain;
00101 };
00102 
00103 //-----------------------------------------------------------------------------
00104 // nsHttpAuthEntry
00105 //-----------------------------------------------------------------------------
00106 
00107 class nsHttpAuthEntry
00108 {
00109 public:
00110     const char *Realm()       const { return mRealm; }
00111     const char *Creds()       const { return mCreds; }
00112     const char *Challenge()   const { return mChallenge; }
00113     const PRUnichar *Domain() const { return mIdent.Domain(); }
00114     const PRUnichar *User()   const { return mIdent.User(); }
00115     const PRUnichar *Pass()   const { return mIdent.Password(); }
00116     nsHttpAuthPath *RootPath()      { return mRoot; }
00117 
00118     const nsHttpAuthIdentity &Identity() const { return mIdent; }
00119             
00120     nsresult AddPath(const char *aPath);
00121             
00122     nsCOMPtr<nsISupports> mMetaData;
00123 
00124 private:
00125     nsHttpAuthEntry(const char *path,
00126                     const char *realm,
00127                     const char *creds,
00128                     const char *challenge,
00129                     const nsHttpAuthIdentity &ident,
00130                     nsISupports *metadata)
00131         : mRoot(nsnull)
00132         , mTail(nsnull)
00133         , mRealm(nsnull)
00134     {
00135         Set(path, realm, creds, challenge, ident, metadata);
00136     }
00137    ~nsHttpAuthEntry();
00138 
00139     nsresult Set(const char *path,
00140                  const char *realm,
00141                  const char *creds,
00142                  const char *challenge,
00143                  const nsHttpAuthIdentity &ident,
00144                  nsISupports *metadata);
00145 
00146     nsHttpAuthIdentity mIdent;
00147 
00148     nsHttpAuthPath *mRoot; //root pointer
00149     nsHttpAuthPath *mTail; //tail pointer
00150 
00151     // allocated together in one blob, starting with mRealm.
00152     char *mRealm;
00153     char *mCreds;
00154     char *mChallenge;
00155 
00156     friend class nsHttpAuthNode;
00157     friend class nsHttpAuthCache;
00158 };
00159 
00160 //-----------------------------------------------------------------------------
00161 // nsHttpAuthNode
00162 //-----------------------------------------------------------------------------
00163 
00164 class nsHttpAuthNode
00165 {
00166 private:
00167     nsHttpAuthNode();
00168    ~nsHttpAuthNode();
00169 
00170     // path can be null, in which case we'll search for an entry
00171     // with a null path.
00172     nsHttpAuthEntry *LookupEntryByPath(const char *path);
00173 
00174     // realm must not be null
00175     nsHttpAuthEntry *LookupEntryByRealm(const char *realm);
00176 
00177     // if a matching entry is found, then credentials will be changed.
00178     nsresult SetAuthEntry(const char *path,
00179                           const char *realm,
00180                           const char *credentials,
00181                           const char *challenge,
00182                           const nsHttpAuthIdentity &ident,
00183                           nsISupports *metadata);
00184 
00185     void ClearAuthEntry(const char *realm);
00186 
00187     PRUint32 EntryCount() { return (PRUint32) mList.Count(); }
00188 
00189 private:
00190     nsVoidArray mList; // list of nsHttpAuthEntry objects
00191 
00192     friend class nsHttpAuthCache;
00193 };
00194 
00195 //-----------------------------------------------------------------------------
00196 // nsHttpAuthCache
00197 //  (holds a hash table from host:port to nsHttpAuthNode)
00198 //-----------------------------------------------------------------------------
00199 
00200 class nsHttpAuthCache
00201 {
00202 public:
00203     nsHttpAuthCache();
00204    ~nsHttpAuthCache();
00205 
00206     nsresult Init();
00207 
00208     // |scheme|, |host|, and |port| are required
00209     // |path| can be null
00210     // |entry| is either null or a weak reference
00211     nsresult GetAuthEntryForPath(const char *scheme,
00212                                  const char *host,
00213                                  PRInt32     port,
00214                                  const char *path,
00215                                  nsHttpAuthEntry **entry);
00216 
00217     // |scheme|, |host|, and |port| are required
00218     // |realm| must not be null
00219     // |entry| is either null or a weak reference
00220     nsresult GetAuthEntryForDomain(const char *scheme,
00221                                    const char *host,
00222                                    PRInt32     port,
00223                                    const char *realm,
00224                                    nsHttpAuthEntry **entry);
00225 
00226     // |scheme|, |host|, and |port| are required
00227     // |path| can be null
00228     // |realm| must not be null
00229     // if |credentials|, |user|, |pass|, and |challenge| are each
00230     // null, then the entry is deleted.
00231     nsresult SetAuthEntry(const char *scheme,
00232                           const char *host,
00233                           PRInt32     port,
00234                           const char *directory,
00235                           const char *realm,
00236                           const char *credentials,
00237                           const char *challenge,
00238                           const nsHttpAuthIdentity &ident,
00239                           nsISupports *metadata);
00240 
00241     void ClearAuthEntry(const char *scheme,
00242                         const char *host,
00243                         PRInt32     port,
00244                         const char *realm);
00245 
00246     // expire all existing auth list entries including proxy auths. 
00247     nsresult ClearAll();
00248 
00249 private:
00250     nsHttpAuthNode *LookupAuthNode(const char *scheme,
00251                                    const char *host,
00252                                    PRInt32     port,
00253                                    nsCString  &key);
00254 
00255     // hash table allocation functions
00256     static void*        PR_CALLBACK AllocTable(void *, PRSize size);
00257     static void         PR_CALLBACK FreeTable(void *, void *item);
00258     static PLHashEntry* PR_CALLBACK AllocEntry(void *, const void *key);
00259     static void         PR_CALLBACK FreeEntry(void *, PLHashEntry *he, PRUintn flag);
00260 
00261     static PLHashAllocOps gHashAllocOps;
00262     
00263 private:
00264     PLHashTable *mDB; // "host:port" --> nsHttpAuthNode
00265 };
00266 
00267 #endif // nsHttpAuthCache_h__