1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 : * Daniel Witte.
19 : * Portions created by the Initial Developer are Copyright (C) 2003
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Daniel Witte (dwitte@stanford.edu)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsCookie.h"
40 : #include <stdlib.h>
41 :
42 : /******************************************************************************
43 : * nsCookie:
44 : * string helper impl
45 : ******************************************************************************/
46 :
47 : // copy aSource strings into contiguous storage provided in aDest1,
48 : // providing terminating nulls for each destination string.
49 : static inline void
50 41965 : StrBlockCopy(const nsACString &aSource1,
51 : const nsACString &aSource2,
52 : const nsACString &aSource3,
53 : const nsACString &aSource4,
54 : char *&aDest1,
55 : char *&aDest2,
56 : char *&aDest3,
57 : char *&aDest4,
58 : char *&aDestEnd)
59 : {
60 41965 : char *toBegin = aDest1;
61 41965 : nsACString::const_iterator fromBegin, fromEnd;
62 :
63 41965 : *copy_string(aSource1.BeginReading(fromBegin), aSource1.EndReading(fromEnd), toBegin) = char(0);
64 41965 : aDest2 = ++toBegin;
65 41965 : *copy_string(aSource2.BeginReading(fromBegin), aSource2.EndReading(fromEnd), toBegin) = char(0);
66 41965 : aDest3 = ++toBegin;
67 41965 : *copy_string(aSource3.BeginReading(fromBegin), aSource3.EndReading(fromEnd), toBegin) = char(0);
68 41965 : aDest4 = ++toBegin;
69 41965 : *copy_string(aSource4.BeginReading(fromBegin), aSource4.EndReading(fromEnd), toBegin) = char(0);
70 41965 : aDestEnd = toBegin;
71 41965 : }
72 :
73 : /******************************************************************************
74 : * nsCookie:
75 : * creation helper
76 : ******************************************************************************/
77 :
78 : // This is a counter that keeps track of the last used creation time, each time
79 : // we create a new nsCookie. This is nominally the time (in microseconds) the
80 : // cookie was created, but is guaranteed to be monotonically increasing for
81 : // cookies added at runtime after the database has been read in. This is
82 : // necessary to enforce ordering among cookies whose creation times would
83 : // otherwise overlap, since it's possible two cookies may be created at the same
84 : // time, or that the system clock isn't monotonic.
85 : static PRInt64 gLastCreationTime;
86 :
87 : PRInt64
88 16232 : nsCookie::GenerateUniqueCreationTime(PRInt64 aCreationTime)
89 : {
90 : // Check if the creation time given to us is greater than the running maximum
91 : // (it should always be monotonically increasing).
92 16232 : if (aCreationTime > gLastCreationTime) {
93 14277 : gLastCreationTime = aCreationTime;
94 14277 : return aCreationTime;
95 : }
96 :
97 : // Make up our own.
98 1955 : return ++gLastCreationTime;
99 : }
100 :
101 : nsCookie *
102 41965 : nsCookie::Create(const nsACString &aName,
103 : const nsACString &aValue,
104 : const nsACString &aHost,
105 : const nsACString &aPath,
106 : PRInt64 aExpiry,
107 : PRInt64 aLastAccessed,
108 : PRInt64 aCreationTime,
109 : bool aIsSession,
110 : bool aIsSecure,
111 : bool aIsHttpOnly)
112 : {
113 : // find the required string buffer size, adding 4 for the terminating nulls
114 41965 : const PRUint32 stringLength = aName.Length() + aValue.Length() +
115 41965 : aHost.Length() + aPath.Length() + 4;
116 :
117 : // allocate contiguous space for the nsCookie and its strings -
118 : // we store the strings in-line with the nsCookie to save allocations
119 83930 : void *place = ::operator new(sizeof(nsCookie) + stringLength);
120 41965 : if (!place)
121 0 : return nsnull;
122 :
123 : // assign string members
124 : char *name, *value, *host, *path, *end;
125 41965 : name = static_cast<char *>(place) + sizeof(nsCookie);
126 : StrBlockCopy(aName, aValue, aHost, aPath,
127 41965 : name, value, host, path, end);
128 :
129 : // If the creationTime given to us is higher than the running maximum, update
130 : // our maximum.
131 41965 : if (aCreationTime > gLastCreationTime)
132 82 : gLastCreationTime = aCreationTime;
133 :
134 : // construct the cookie. placement new, oh yeah!
135 : return new (place) nsCookie(name, value, host, path, end,
136 : aExpiry, aLastAccessed, aCreationTime,
137 41965 : aIsSession, aIsSecure, aIsHttpOnly);
138 : }
139 :
140 : /******************************************************************************
141 : * nsCookie:
142 : * xpcom impl
143 : ******************************************************************************/
144 :
145 : // xpcom getters
146 598 : NS_IMETHODIMP nsCookie::GetName(nsACString &aName) { aName = Name(); return NS_OK; }
147 4 : NS_IMETHODIMP nsCookie::GetValue(nsACString &aValue) { aValue = Value(); return NS_OK; }
148 4251 : NS_IMETHODIMP nsCookie::GetHost(nsACString &aHost) { aHost = Host(); return NS_OK; }
149 400 : NS_IMETHODIMP nsCookie::GetRawHost(nsACString &aHost) { aHost = RawHost(); return NS_OK; }
150 14 : NS_IMETHODIMP nsCookie::GetPath(nsACString &aPath) { aPath = Path(); return NS_OK; }
151 52 : NS_IMETHODIMP nsCookie::GetExpiry(PRInt64 *aExpiry) { *aExpiry = Expiry(); return NS_OK; }
152 0 : NS_IMETHODIMP nsCookie::GetIsSession(bool *aIsSession) { *aIsSession = IsSession(); return NS_OK; }
153 0 : NS_IMETHODIMP nsCookie::GetIsDomain(bool *aIsDomain) { *aIsDomain = IsDomain(); return NS_OK; }
154 3 : NS_IMETHODIMP nsCookie::GetIsSecure(bool *aIsSecure) { *aIsSecure = IsSecure(); return NS_OK; }
155 0 : NS_IMETHODIMP nsCookie::GetIsHttpOnly(bool *aHttpOnly) { *aHttpOnly = IsHttpOnly(); return NS_OK; }
156 0 : NS_IMETHODIMP nsCookie::GetStatus(nsCookieStatus *aStatus) { *aStatus = 0; return NS_OK; }
157 0 : NS_IMETHODIMP nsCookie::GetPolicy(nsCookiePolicy *aPolicy) { *aPolicy = 0; return NS_OK; }
158 26 : NS_IMETHODIMP nsCookie::GetCreationTime(PRInt64 *aCreation){ *aCreation = CreationTime(); return NS_OK; }
159 0 : NS_IMETHODIMP nsCookie::GetLastAccessed(PRInt64 *aTime) { *aTime = LastAccessed(); return NS_OK; }
160 :
161 : // compatibility method, for use with the legacy nsICookie interface.
162 : // here, expires == 0 denotes a session cookie.
163 : NS_IMETHODIMP
164 3 : nsCookie::GetExpires(PRUint64 *aExpires)
165 : {
166 3 : if (IsSession()) {
167 0 : *aExpires = 0;
168 : } else {
169 3 : *aExpires = Expiry() > 0 ? Expiry() : 1;
170 : }
171 3 : return NS_OK;
172 : }
173 :
174 418738 : NS_IMPL_ISUPPORTS2(nsCookie, nsICookie2, nsICookie)
|