Back to index

lightning-sunbird  0.9+nobinonly
nsCookie.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * Daniel Witte.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Daniel Witte (dwitte@stanford.edu)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCookie.h"
00040 #include <stdlib.h>
00041 
00042 /******************************************************************************
00043  * nsCookie:
00044  * string helper impl
00045  ******************************************************************************/
00046 
00047 // copy aSource strings into contiguous storage provided in aDest1,
00048 // providing terminating nulls for each destination string.
00049 static inline void
00050 StrBlockCopy(const nsACString &aSource1,
00051              const nsACString &aSource2,
00052              const nsACString &aSource3,
00053              const nsACString &aSource4,
00054              char             *&aDest1,
00055              char             *&aDest2,
00056              char             *&aDest3,
00057              char             *&aDest4,
00058              char             *&aDestEnd)
00059 {
00060   char *toBegin = aDest1;
00061   nsACString::const_iterator fromBegin, fromEnd;
00062 
00063   *copy_string(aSource1.BeginReading(fromBegin), aSource1.EndReading(fromEnd), toBegin) = char(0);
00064   aDest2 = ++toBegin;
00065   *copy_string(aSource2.BeginReading(fromBegin), aSource2.EndReading(fromEnd), toBegin) = char(0);
00066   aDest3 = ++toBegin;
00067   *copy_string(aSource3.BeginReading(fromBegin), aSource3.EndReading(fromEnd), toBegin) = char(0);
00068   aDest4 = ++toBegin;
00069   *copy_string(aSource4.BeginReading(fromBegin), aSource4.EndReading(fromEnd), toBegin) = char(0);
00070   aDestEnd = toBegin;
00071 }
00072 
00073 /******************************************************************************
00074  * nsCookie:
00075  * creation helper
00076  ******************************************************************************/
00077 
00078 // This is a counter that is incremented each time we allocate a new nsCookie.
00079 // The value of the counter is stored with each nsCookie so that we can sort
00080 // cookies by creation time (within the current browser session).
00081 static PRUint32 gLastCreationTime;
00082 
00083 nsCookie *
00084 nsCookie::Create(const nsACString &aName,
00085                  const nsACString &aValue,
00086                  const nsACString &aHost,
00087                  const nsACString &aPath,
00088                  nsInt64          aExpiry,
00089                  nsInt64          aLastAccessed,
00090                  PRBool           aIsSession,
00091                  PRBool           aIsSecure,
00092                  PRBool           aIsHttpOnly,
00093                  nsCookieStatus   aStatus,
00094                  nsCookiePolicy   aPolicy)
00095 {
00096   // find the required string buffer size, adding 4 for the terminating nulls
00097   const PRUint32 stringLength = aName.Length() + aValue.Length() +
00098                                 aHost.Length() + aPath.Length() + 4;
00099 
00100   // allocate contiguous space for the nsCookie and its strings -
00101   // we store the strings in-line with the nsCookie to save allocations
00102   void *place = ::operator new(sizeof(nsCookie) + stringLength);
00103   if (!place)
00104     return nsnull;
00105 
00106   // assign string members
00107   char *name, *value, *host, *path, *end;
00108   name = NS_STATIC_CAST(char *, place) + sizeof(nsCookie);
00109   StrBlockCopy(aName, aValue, aHost, aPath,
00110                name, value, host, path, end);
00111 
00112   // construct the cookie. placement new, oh yeah!
00113   return new (place) nsCookie(name, value, host, path, end,
00114                               aExpiry, aLastAccessed, ++gLastCreationTime,
00115                               aIsSession, aIsSecure, aIsHttpOnly,
00116                               aStatus, aPolicy);
00117 }
00118 
00119 /******************************************************************************
00120  * nsCookie:
00121  * xpcom impl
00122  ******************************************************************************/
00123 
00124 // xpcom getters
00125 NS_IMETHODIMP nsCookie::GetName(nsACString &aName)         { aName = Name();            return NS_OK; }
00126 NS_IMETHODIMP nsCookie::GetValue(nsACString &aValue)       { aValue = Value();          return NS_OK; }
00127 NS_IMETHODIMP nsCookie::GetHost(nsACString &aHost)         { aHost = Host();            return NS_OK; }
00128 NS_IMETHODIMP nsCookie::GetRawHost(nsACString &aHost)      { aHost = RawHost();         return NS_OK; }
00129 NS_IMETHODIMP nsCookie::GetPath(nsACString &aPath)         { aPath = Path();            return NS_OK; }
00130 NS_IMETHODIMP nsCookie::GetExpiry(PRInt64 *aExpiry)        { *aExpiry = Expiry();       return NS_OK; }
00131 NS_IMETHODIMP nsCookie::GetIsSession(PRBool *aIsSession)   { *aIsSession = IsSession(); return NS_OK; }
00132 NS_IMETHODIMP nsCookie::GetIsDomain(PRBool *aIsDomain)     { *aIsDomain = IsDomain();   return NS_OK; }
00133 NS_IMETHODIMP nsCookie::GetIsSecure(PRBool *aIsSecure)     { *aIsSecure = IsSecure();   return NS_OK; }
00134 NS_IMETHODIMP nsCookie::GetIsHttpOnly(PRBool *aHttpOnly)   { *aHttpOnly = IsHttpOnly(); return NS_OK; }
00135 NS_IMETHODIMP nsCookie::GetStatus(nsCookieStatus *aStatus) { *aStatus = Status();       return NS_OK; }
00136 NS_IMETHODIMP nsCookie::GetPolicy(nsCookiePolicy *aPolicy) { *aPolicy = Policy();       return NS_OK; }
00137 
00138 // compatibility method, for use with the legacy nsICookie interface.
00139 // here, expires == 0 denotes a session cookie.
00140 NS_IMETHODIMP
00141 nsCookie::GetExpires(PRUint64 *aExpires)
00142 {
00143   if (IsSession()) {
00144     *aExpires = 0;
00145   } else {
00146     *aExpires = Expiry() > nsInt64(0) ? PRInt64(Expiry()) : 1;
00147   }
00148   return NS_OK;
00149 }
00150 
00151 NS_IMPL_ISUPPORTS3(nsCookie, nsICookie2, nsICookie, nsICookie2_MOZILLA_1_8_BRANCH)