netwerk/cookie/nsCookie.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
parent 0
6474c204b198
child 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsCookie.h"
     7 #include "nsUTF8ConverterService.h"
     8 #include <stdlib.h>
    10 /******************************************************************************
    11  * nsCookie:
    12  * string helper impl
    13  ******************************************************************************/
    15 // copy aSource strings into contiguous storage provided in aDest1,
    16 // providing terminating nulls for each destination string.
    17 static inline void
    18 StrBlockCopy(const nsACString &aSource1,
    19              const nsACString &aSource2,
    20              const nsACString &aSource3,
    21              const nsACString &aSource4,
    22              const nsACString &aSource5,
    23              char             *&aDest1,
    24              char             *&aDest2,
    25              char             *&aDest3,
    26              char             *&aDest4,
    27              char             *&aDest5,
    28              char             *&aDestEnd)
    29 {
    30   char *toBegin = aDest1;
    31   nsACString::const_iterator fromBegin, fromEnd;
    33   *copy_string(aSource1.BeginReading(fromBegin), aSource1.EndReading(fromEnd), toBegin) = char(0);
    34   aDest2 = ++toBegin;
    35   *copy_string(aSource2.BeginReading(fromBegin), aSource2.EndReading(fromEnd), toBegin) = char(0);
    36   aDest3 = ++toBegin;
    37   *copy_string(aSource3.BeginReading(fromBegin), aSource3.EndReading(fromEnd), toBegin) = char(0);
    38   aDest4 = ++toBegin;
    39   *copy_string(aSource4.BeginReading(fromBegin), aSource4.EndReading(fromEnd), toBegin) = char(0);
    40   aDest5 = ++toBegin;
    41   *copy_string(aSource5.BeginReading(fromBegin), aSource5.EndReading(fromEnd), toBegin) = char(0);
    42   aDestEnd = toBegin;
    43 }
    45 /******************************************************************************
    46  * nsCookie:
    47  * creation helper
    48  ******************************************************************************/
    50 // This is a counter that keeps track of the last used creation time, each time
    51 // we create a new nsCookie. This is nominally the time (in microseconds) the
    52 // cookie was created, but is guaranteed to be monotonically increasing for
    53 // cookies added at runtime after the database has been read in. This is
    54 // necessary to enforce ordering among cookies whose creation times would
    55 // otherwise overlap, since it's possible two cookies may be created at the same
    56 // time, or that the system clock isn't monotonic.
    57 static int64_t gLastCreationTime;
    59 int64_t
    60 nsCookie::GenerateUniqueCreationTime(int64_t aCreationTime)
    61 {
    62   // Check if the creation time given to us is greater than the running maximum
    63   // (it should always be monotonically increasing).
    64   if (aCreationTime > gLastCreationTime) {
    65     gLastCreationTime = aCreationTime;
    66     return aCreationTime;
    67   }
    69   // Make up our own.
    70   return ++gLastCreationTime;
    71 }
    73 nsCookie *
    74 nsCookie::Create(const nsACString &aName,
    75                  const nsACString &aValue,
    76                  const nsACString &aHost,
    77                  const nsACString &aOrigin,
    78                  const nsACString &aPath,
    79                  int64_t           aExpiry,
    80                  int64_t           aLastAccessed,
    81                  int64_t           aCreationTime,
    82                  bool              aIsSession,
    83                  bool              aIsSecure,
    84                  bool              aIsHttpOnly)
    85 {
    86   // Ensure mValue contains a valid UTF-8 sequence. Otherwise XPConnect will
    87   // truncate the string after the first invalid octet.
    88   nsUTF8ConverterService converter;
    89   nsAutoCString aUTF8Value;
    90   converter.ConvertStringToUTF8(aValue, "UTF-8", false, true, 1, aUTF8Value);
    92   // find the required string buffer size, accommodating terminating nulls
    93   const uint32_t stringLength = aName.Length() + aUTF8Value.Length() +
    94                                 aHost.Length() + aOrigin.Length() +
    95                                 aPath.Length() + 5;
    97   // allocate contiguous space for the nsCookie and its strings -
    98   // we store the strings in-line with the nsCookie to save allocations
    99   void *place = ::operator new(sizeof(nsCookie) + stringLength);
   100   if (!place)
   101     return nullptr;
   103   // assign string members
   104   char *name, *value, *host, *origin, *path, *end;
   105   name = static_cast<char *>(place) + sizeof(nsCookie);
   106   StrBlockCopy(aName, aUTF8Value, aHost, aOrigin, aPath,
   107                name, value, host, origin, path, end);
   109   // If the creationTime given to us is higher than the running maximum, update
   110   // our maximum.
   111   if (aCreationTime > gLastCreationTime)
   112     gLastCreationTime = aCreationTime;
   114   // construct the cookie. placement new, oh yeah!
   115   return new (place) nsCookie(name, value, host, origin, path, end,
   116                               aExpiry, aLastAccessed, aCreationTime,
   117                               aIsSession, aIsSecure, aIsHttpOnly);
   118 }
   120 size_t
   121 nsCookie::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
   122 {
   123     // There is no need to measure the sizes of the individual string
   124     // members, since the strings are stored in-line with the nsCookie.
   125     return aMallocSizeOf(this);
   126 }
   128 /******************************************************************************
   129  * nsCookie:
   130  * xpcom impl
   131  ******************************************************************************/
   133 // xpcom getters
   134 NS_IMETHODIMP nsCookie::GetName(nsACString &aName)         { aName = Name();            return NS_OK; }
   135 NS_IMETHODIMP nsCookie::GetValue(nsACString &aValue)       { aValue = Value();          return NS_OK; }
   136 NS_IMETHODIMP nsCookie::GetOrigin(nsACString &aOrigin)     { aOrigin = Origin();    return NS_OK; }
   137 NS_IMETHODIMP nsCookie::GetHost(nsACString &aHost)         { aHost = Host();            return NS_OK; }
   138 NS_IMETHODIMP nsCookie::GetRawHost(nsACString &aHost)      { aHost = RawHost();         return NS_OK; }
   139 NS_IMETHODIMP nsCookie::GetPath(nsACString &aPath)         { aPath = Path();            return NS_OK; }
   140 NS_IMETHODIMP nsCookie::GetExpiry(int64_t *aExpiry)        { *aExpiry = Expiry();       return NS_OK; }
   141 NS_IMETHODIMP nsCookie::GetIsSession(bool *aIsSession)   { *aIsSession = IsSession(); return NS_OK; }
   142 NS_IMETHODIMP nsCookie::GetIsDomain(bool *aIsDomain)     { *aIsDomain = IsDomain();   return NS_OK; }
   143 NS_IMETHODIMP nsCookie::GetIsSecure(bool *aIsSecure)     { *aIsSecure = IsSecure();   return NS_OK; }
   144 NS_IMETHODIMP nsCookie::GetIsHttpOnly(bool *aHttpOnly)   { *aHttpOnly = IsHttpOnly(); return NS_OK; }
   145 NS_IMETHODIMP nsCookie::GetStatus(nsCookieStatus *aStatus) { *aStatus = 0;              return NS_OK; }
   146 NS_IMETHODIMP nsCookie::GetPolicy(nsCookiePolicy *aPolicy) { *aPolicy = 0;              return NS_OK; }
   147 NS_IMETHODIMP nsCookie::GetCreationTime(int64_t *aCreation){ *aCreation = CreationTime(); return NS_OK; }
   148 NS_IMETHODIMP nsCookie::GetLastAccessed(int64_t *aTime)    { *aTime = LastAccessed();   return NS_OK; }
   150 // compatibility method, for use with the legacy nsICookie interface.
   151 // here, expires == 0 denotes a session cookie.
   152 NS_IMETHODIMP
   153 nsCookie::GetExpires(uint64_t *aExpires)
   154 {
   155   if (IsSession()) {
   156     *aExpires = 0;
   157   } else {
   158     *aExpires = Expiry() > 0 ? Expiry() : 1;
   159   }
   160   return NS_OK;
   161 }
   163 NS_IMPL_ISUPPORTS(nsCookie, nsICookie2, nsICookie)

mercurial