Bug 1730324 - [web-share] implement validate share and non-fully active check r=saschanaz
[gecko.git] / netwerk / cookie / CookieLogging.cpp
blob5724a070835eacc57176b1c06796c35a88053747
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "CookieLogging.h"
7 #include "Cookie.h"
9 constexpr auto TIME_STRING_LENGTH = 40;
11 namespace mozilla {
12 namespace net {
14 LazyLogModule gCookieLog("cookie");
16 static const char* SameSiteToString(uint32_t aSameSite) {
17 switch (aSameSite) {
18 case nsICookie::SAMESITE_NONE:
19 return "none";
20 case nsICookie::SAMESITE_LAX:
21 return "lax";
22 case nsICookie::SAMESITE_STRICT:
23 return "strict";
24 default:
25 MOZ_CRASH("Invalid nsICookie sameSite value");
26 return "";
30 // static
31 void CookieLogging::LogSuccess(bool aSetCookie, nsIURI* aHostURI,
32 const nsACString& aCookieString, Cookie* aCookie,
33 bool aReplacing) {
34 // if logging isn't enabled, return now to save cycles
35 if (!MOZ_LOG_TEST(gCookieLog, LogLevel::Debug)) {
36 return;
39 nsAutoCString spec;
40 if (aHostURI) {
41 aHostURI->GetAsciiSpec(spec);
44 MOZ_LOG(gCookieLog, LogLevel::Debug,
45 ("===== %s =====\n", aSetCookie ? "COOKIE ACCEPTED" : "COOKIE SENT"));
46 MOZ_LOG(gCookieLog, LogLevel::Debug, ("request URL: %s\n", spec.get()));
47 MOZ_LOG(gCookieLog, LogLevel::Debug,
48 ("cookie string: %s\n", aCookieString.BeginReading()));
49 if (aSetCookie) {
50 MOZ_LOG(gCookieLog, LogLevel::Debug,
51 ("replaces existing cookie: %s\n", aReplacing ? "true" : "false"));
54 LogCookie(aCookie);
56 MOZ_LOG(gCookieLog, LogLevel::Debug, ("\n"));
59 // static
60 void CookieLogging::LogFailure(bool aSetCookie, nsIURI* aHostURI,
61 const nsACString& aCookieString,
62 const char* aReason) {
63 // if logging isn't enabled, return now to save cycles
64 if (!MOZ_LOG_TEST(gCookieLog, LogLevel::Warning)) {
65 return;
68 nsAutoCString spec;
69 if (aHostURI) {
70 aHostURI->GetAsciiSpec(spec);
73 MOZ_LOG(gCookieLog, LogLevel::Warning,
74 ("===== %s =====\n",
75 aSetCookie ? "COOKIE NOT ACCEPTED" : "COOKIE NOT SENT"));
76 MOZ_LOG(gCookieLog, LogLevel::Warning, ("request URL: %s\n", spec.get()));
77 if (aSetCookie) {
78 MOZ_LOG(gCookieLog, LogLevel::Warning,
79 ("cookie string: %s\n", aCookieString.BeginReading()));
82 PRExplodedTime explodedTime;
83 PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime);
84 char timeString[TIME_STRING_LENGTH];
85 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
86 &explodedTime);
88 MOZ_LOG(gCookieLog, LogLevel::Warning, ("current time: %s", timeString));
89 MOZ_LOG(gCookieLog, LogLevel::Warning, ("rejected because %s\n", aReason));
90 MOZ_LOG(gCookieLog, LogLevel::Warning, ("\n"));
93 // static
94 void CookieLogging::LogCookie(Cookie* aCookie) {
95 PRExplodedTime explodedTime;
96 PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime);
97 char timeString[TIME_STRING_LENGTH];
98 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
99 &explodedTime);
101 MOZ_LOG(gCookieLog, LogLevel::Debug, ("current time: %s", timeString));
103 if (aCookie) {
104 MOZ_LOG(gCookieLog, LogLevel::Debug, ("----------------\n"));
105 MOZ_LOG(gCookieLog, LogLevel::Debug, ("name: %s\n", aCookie->Name().get()));
106 MOZ_LOG(gCookieLog, LogLevel::Debug,
107 ("value: %s\n", aCookie->Value().get()));
108 MOZ_LOG(gCookieLog, LogLevel::Debug,
109 ("%s: %s\n", aCookie->IsDomain() ? "domain" : "host",
110 aCookie->Host().get()));
111 MOZ_LOG(gCookieLog, LogLevel::Debug, ("path: %s\n", aCookie->Path().get()));
113 PR_ExplodeTime(aCookie->Expiry() * int64_t(PR_USEC_PER_SEC),
114 PR_GMTParameters, &explodedTime);
115 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
116 &explodedTime);
117 MOZ_LOG(gCookieLog, LogLevel::Debug,
118 ("expires: %s%s", timeString,
119 aCookie->IsSession() ? " (at end of session)" : ""));
121 PR_ExplodeTime(aCookie->CreationTime(), PR_GMTParameters, &explodedTime);
122 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT",
123 &explodedTime);
124 MOZ_LOG(gCookieLog, LogLevel::Debug, ("created: %s", timeString));
126 MOZ_LOG(gCookieLog, LogLevel::Debug,
127 ("is secure: %s\n", aCookie->IsSecure() ? "true" : "false"));
128 MOZ_LOG(gCookieLog, LogLevel::Debug,
129 ("is httpOnly: %s\n", aCookie->IsHttpOnly() ? "true" : "false"));
130 MOZ_LOG(gCookieLog, LogLevel::Debug,
131 ("sameSite: %s - rawSameSite: %s\n",
132 SameSiteToString(aCookie->SameSite()),
133 SameSiteToString(aCookie->RawSameSite())));
134 MOZ_LOG(
135 gCookieLog, LogLevel::Debug,
136 ("schemeMap %d (http: %s | https: %s | file: %s)\n",
137 aCookie->SchemeMap(),
138 (aCookie->SchemeMap() & nsICookie::SCHEME_HTTP ? "true" : "false"),
139 (aCookie->SchemeMap() & nsICookie::SCHEME_HTTPS ? "true" : "false"),
140 (aCookie->SchemeMap() & nsICookie::SCHEME_FILE ? "true" : "false")));
142 nsAutoCString suffix;
143 aCookie->OriginAttributesRef().CreateSuffix(suffix);
144 MOZ_LOG(gCookieLog, LogLevel::Debug,
145 ("origin attributes: %s\n",
146 suffix.IsEmpty() ? "{empty}" : suffix.get()));
150 // static
151 void CookieLogging::LogEvicted(Cookie* aCookie, const char* details) {
152 MOZ_LOG(gCookieLog, LogLevel::Debug, ("===== COOKIE EVICTED =====\n"));
153 MOZ_LOG(gCookieLog, LogLevel::Debug, ("%s\n", details));
155 LogCookie(aCookie);
157 MOZ_LOG(gCookieLog, LogLevel::Debug, ("\n"));
160 // static
161 void CookieLogging::LogMessageToConsole(nsIConsoleReportCollector* aCRC,
162 nsIURI* aURI, uint32_t aErrorFlags,
163 const nsACString& aCategory,
164 const nsACString& aMsg,
165 const nsTArray<nsString>& aParams) {
166 if (!aCRC) {
167 return;
170 nsAutoCString uri;
171 if (aURI) {
172 nsresult rv = aURI->GetSpec(uri);
173 if (NS_WARN_IF(NS_FAILED(rv))) {
174 return;
178 aCRC->AddConsoleReport(aErrorFlags, aCategory,
179 nsContentUtils::eNECKO_PROPERTIES, uri, 0, 0, aMsg,
180 aParams);
183 } // namespace net
184 } // namespace mozilla