netwerk/cookie/nsCookie.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
child 4
fc2d59ddac77
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial