blob: d4f66334146320ebe0173644e827ceb120c0ffe9 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
Tim van der Lippe0ed1d2b2020-02-04 13:45:1331import * as Common from '../common/common.js';
Tim van der Lippeec69c212020-03-12 16:40:4732import * as CookieTable from '../cookie_table/cookie_table.js'; // eslint-disable-line no-unused-vars
Tim van der Lippe0ed1d2b2020-02-04 13:45:1333import * as SDK from '../sdk/sdk.js';
34import * as UI from '../ui/ui.js';
35
36export class RequestCookiesView extends UI.Widget.Widget {
Blink Reformat4c46d092018-04-07 15:32:3737 /**
Tim van der Lippe0ed1d2b2020-02-04 13:45:1338 * @param {!SDK.NetworkRequest.NetworkRequest} request
Blink Reformat4c46d092018-04-07 15:32:3739 */
40 constructor(request) {
41 super();
Jack Franklin71519f82020-11-03 12:08:5942 this.registerRequiredCSS('network/requestCookiesView.css', {enableLegacyPatching: true});
Blink Reformat4c46d092018-04-07 15:32:3743 this.element.classList.add('request-cookies-view');
44
Tim van der Lippe0ed1d2b2020-02-04 13:45:1345 /** @type {!SDK.NetworkRequest.NetworkRequest} */
Blink Reformat4c46d092018-04-07 15:32:3746 this._request = request;
Paul Lewis2d7d65c2020-03-16 17:26:3047 this._showFilteredOutCookiesSetting = Common.Settings.Settings.instance().createSetting(
48 'show-filtered-out-request-cookies', /* defaultValue */ false);
Joey Arhard4139c82019-09-07 03:54:3949
Tim van der Lippe0ed1d2b2020-02-04 13:45:1350 this._emptyWidget = new UI.EmptyWidget.EmptyWidget(Common.UIString.UIString('This request has no cookies.'));
Joey Arhard4139c82019-09-07 03:54:3951 this._emptyWidget.show(this.element);
52
53 this._requestCookiesTitle = this.element.createChild('div');
54 const titleText = this._requestCookiesTitle.createChild('span', 'request-cookies-title');
55 titleText.textContent = ls`Request Cookies`;
56 titleText.title = ls`Cookies that were sent to the server in the 'cookie' header of the request`;
57
Simon Zündf3899c22020-10-08 10:59:5958 const requestCookiesCheckbox = /** @type {!UI.UIUtils.CheckboxLabel} */ (UI.SettingsUI.createSettingCheckbox(
Joey Arhard4139c82019-09-07 03:54:3959 ls`show filtered out request cookies`, this._showFilteredOutCookiesSetting,
Simon Zündf3899c22020-10-08 10:59:5960 /* omitParagraphElement */ true));
Joey Arhard4139c82019-09-07 03:54:3961 requestCookiesCheckbox.checkboxElement.addEventListener('change', () => {
62 this._refreshRequestCookiesView();
63 });
64 this._requestCookiesTitle.appendChild(requestCookiesCheckbox);
65
Joey Arhar1c4df4f2019-09-26 18:59:1366 this._requestCookiesEmpty = this.element.createChild('div', 'cookies-panel-item');
67 this._requestCookiesEmpty.textContent = ls`No request cookies were sent.`;
68
Tim van der Lippeec69c212020-03-12 16:40:4769 this._requestCookiesTable = new CookieTable.CookiesTable.CookiesTable(/* renderInline */ true);
Joey Arhar1c4df4f2019-09-26 18:59:1370 this._requestCookiesTable.contentElement.classList.add('cookie-table', 'cookies-panel-item');
Joey Arhard4139c82019-09-07 03:54:3971 this._requestCookiesTable.show(this.element);
72
73 this._responseCookiesTitle = this.element.createChild('div', 'request-cookies-title');
74 this._responseCookiesTitle.textContent = ls`Response Cookies`;
75 this._responseCookiesTitle.title =
76 ls`Cookies that were received from the server in the 'set-cookie' header of the response`;
77
Tim van der Lippeec69c212020-03-12 16:40:4778 this._responseCookiesTable = new CookieTable.CookiesTable.CookiesTable(/* renderInline */ true);
Joey Arhar1c4df4f2019-09-26 18:59:1379 this._responseCookiesTable.contentElement.classList.add('cookie-table', 'cookies-panel-item');
Joey Arhard4139c82019-09-07 03:54:3980 this._responseCookiesTable.show(this.element);
81
82 this._malformedResponseCookiesTitle = this.element.createChild('div', 'request-cookies-title');
83 this._malformedResponseCookiesTitle.textContent = ls`Malformed Response Cookies`;
84 this._malformedResponseCookiesTitle.title = ls
85 `Cookies that were received from the server in the 'set-cookie' header of the response but were malformed`;
86
87 this._malformedResponseCookiesList = this.element.createChild('div');
88 }
89
90 /**
Tim van der Lippeec69c212020-03-12 16:40:4791 * @return {!{requestCookies: !Array<!SDK.Cookie.Cookie>, requestCookieToBlockedReasons: !Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>}}
Joey Arhard4139c82019-09-07 03:54:3992 */
93 _getRequestCookies() {
Tim van der Lippeec69c212020-03-12 16:40:4794 /** @type {!Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>} */
Joey Arhard4139c82019-09-07 03:54:3995 const requestCookieToBlockedReasons = new Map();
Sigurd Schneider0e88b912020-05-08 08:28:2396 const requestCookies = this._request.includedRequestCookies().slice();
Joey Arhard4139c82019-09-07 03:54:3997
Joey Arhar1c4df4f2019-09-26 18:59:1398 if (this._showFilteredOutCookiesSetting.get()) {
99 for (const blockedCookie of this._request.blockedRequestCookies()) {
100 requestCookieToBlockedReasons.set(blockedCookie.cookie, blockedCookie.blockedReasons.map(blockedReason => {
101 return {
102 attribute: SDK.NetworkRequest.cookieBlockedReasonToAttribute(blockedReason),
103 uiString: SDK.NetworkRequest.cookieBlockedReasonToUiString(blockedReason)
104 };
105 }));
106 requestCookies.push(blockedCookie.cookie);
107 }
108 }
109
Joey Arhard4139c82019-09-07 03:54:39110 return {requestCookies, requestCookieToBlockedReasons};
111 }
112
113 /**
Tim van der Lippeec69c212020-03-12 16:40:47114 * @return {!{responseCookies: !Array<!SDK.Cookie.Cookie>, responseCookieToBlockedReasons: !Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>, malformedResponseCookies: !Array<!SDK.NetworkRequest.BlockedSetCookieWithReason>}}
Joey Arhard4139c82019-09-07 03:54:39115 */
116 _getResponseCookies() {
Tim van der Lippe0ed1d2b2020-02-04 13:45:13117 /** @type {!Array<!SDK.Cookie.Cookie>} */
Joey Arhard4139c82019-09-07 03:54:39118 let responseCookies = [];
Tim van der Lippeec69c212020-03-12 16:40:47119 /** @type {!Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>} */
Joey Arhard4139c82019-09-07 03:54:39120 const responseCookieToBlockedReasons = new Map();
121 /** @type {!Array<!SDK.NetworkRequest.BlockedSetCookieWithReason>} */
122 const malformedResponseCookies = [];
123
Jan Scheffler341eea52019-12-12 09:08:41124 if (this._request.responseCookies.length) {
Simon Zündf3899c22020-10-08 10:59:59125 /** @type {!Array<?string>} */
Joey Arhard4139c82019-09-07 03:54:39126 const blockedCookieLines = this._request.blockedResponseCookies().map(blockedCookie => blockedCookie.cookieLine);
127 responseCookies = this._request.responseCookies.filter(cookie => {
128 // remove the regular cookies that would overlap with blocked cookies
Simon Zündd5ae9bf2020-03-03 09:48:43129 const index = blockedCookieLines.indexOf(cookie.getCookieLine());
130 if (index !== -1) {
131 blockedCookieLines[index] = null;
Joey Arhard4139c82019-09-07 03:54:39132 return false;
133 }
134 return true;
135 });
136
137 for (const blockedCookie of this._request.blockedResponseCookies()) {
Tim van der Lippe0ed1d2b2020-02-04 13:45:13138 const parsedCookies = SDK.CookieParser.CookieParser.parseSetCookie(blockedCookie.cookieLine);
Simon Zündf3899c22020-10-08 10:59:59139 if (parsedCookies && !parsedCookies.length ||
Joey Arhar41a5fad2019-09-13 22:18:45140 blockedCookie.blockedReasons.includes(Protocol.Network.SetCookieBlockedReason.SyntaxError)) {
Joey Arhard4139c82019-09-07 03:54:39141 malformedResponseCookies.push(blockedCookie);
142 continue;
143 }
144
Simon Zündf3899c22020-10-08 10:59:59145 let cookie = blockedCookie.cookie;
146 if (!cookie && parsedCookies) {
147 cookie = parsedCookies[0];
148 }
Jan Scheffler341eea52019-12-12 09:08:41149 if (cookie) {
150 responseCookieToBlockedReasons.set(cookie, blockedCookie.blockedReasons.map(blockedReason => {
151 return {
152 attribute: SDK.NetworkRequest.setCookieBlockedReasonToAttribute(blockedReason),
153 uiString: SDK.NetworkRequest.setCookieBlockedReasonToUiString(blockedReason)
154 };
155 }));
156 responseCookies.push(cookie);
157 }
Joey Arhard4139c82019-09-07 03:54:39158 }
159 }
160
161 return {responseCookies, responseCookieToBlockedReasons, malformedResponseCookies};
162 }
163
164 _refreshRequestCookiesView() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34165 if (!this.isShowing()) {
Joey Arhard4139c82019-09-07 03:54:39166 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34167 }
Joey Arhard4139c82019-09-07 03:54:39168
Sigurd Schneider0e88b912020-05-08 08:28:23169 const gotCookies = this._request.hasRequestCookies() || this._request.responseCookies.length;
Joey Arhar8b090572020-01-09 18:05:59170 if (gotCookies) {
171 this._emptyWidget.hideWidget();
172 } else {
173 this._emptyWidget.showWidget();
174 }
175
Joey Arhard4139c82019-09-07 03:54:39176 const {requestCookies, requestCookieToBlockedReasons} = this._getRequestCookies();
177 const {responseCookies, responseCookieToBlockedReasons, malformedResponseCookies} = this._getResponseCookies();
178
179 if (requestCookies.length) {
180 this._requestCookiesTitle.classList.remove('hidden');
Joey Arhar1c4df4f2019-09-26 18:59:13181 this._requestCookiesEmpty.classList.add('hidden');
Joey Arhard4139c82019-09-07 03:54:39182 this._requestCookiesTable.showWidget();
183 this._requestCookiesTable.setCookies(requestCookies, requestCookieToBlockedReasons);
Joey Arhar1c4df4f2019-09-26 18:59:13184
185 } else if (this._request.blockedRequestCookies().length) {
186 this._requestCookiesTitle.classList.remove('hidden');
187 this._requestCookiesEmpty.classList.remove('hidden');
188 this._requestCookiesTable.hideWidget();
189
Joey Arhard4139c82019-09-07 03:54:39190 } else {
191 this._requestCookiesTitle.classList.add('hidden');
Joey Arhar1c4df4f2019-09-26 18:59:13192 this._requestCookiesEmpty.classList.add('hidden');
Joey Arhard4139c82019-09-07 03:54:39193 this._requestCookiesTable.hideWidget();
194 }
195
196 if (responseCookies.length) {
197 this._responseCookiesTitle.classList.remove('hidden');
198 this._responseCookiesTable.showWidget();
199 this._responseCookiesTable.setCookies(responseCookies, responseCookieToBlockedReasons);
200 } else {
201 this._responseCookiesTitle.classList.add('hidden');
202 this._responseCookiesTable.hideWidget();
203 }
204
205 if (malformedResponseCookies.length) {
206 this._malformedResponseCookiesTitle.classList.remove('hidden');
207 this._malformedResponseCookiesList.classList.remove('hidden');
208
209 this._malformedResponseCookiesList.removeChildren();
210 for (const malformedCookie of malformedResponseCookies) {
211 const listItem = this._malformedResponseCookiesList.createChild('span', 'cookie-line source-code');
Tim van der Lippe0ed1d2b2020-02-04 13:45:13212 const icon = UI.Icon.Icon.create('smallicon-error', 'cookie-warning-icon');
Joey Arhard4139c82019-09-07 03:54:39213 listItem.appendChild(icon);
Sigurd Schneider23c52972020-10-13 09:31:14214 UI.UIUtils.createTextChild(listItem, malformedCookie.cookieLine);
Joey Arhar41a5fad2019-09-13 22:18:45215 listItem.title =
216 SDK.NetworkRequest.setCookieBlockedReasonToUiString(Protocol.Network.SetCookieBlockedReason.SyntaxError);
Joey Arhard4139c82019-09-07 03:54:39217 }
218 } else {
219 this._malformedResponseCookiesTitle.classList.add('hidden');
220 this._malformedResponseCookiesList.classList.add('hidden');
221 }
Blink Reformat4c46d092018-04-07 15:32:37222 }
223
224 /**
225 * @override
226 */
227 wasShown() {
Joey Arhar8b090572020-01-09 18:05:59228 this._request.addEventListener(
229 SDK.NetworkRequest.Events.RequestHeadersChanged, this._refreshRequestCookiesView, this);
230 this._request.addEventListener(
231 SDK.NetworkRequest.Events.ResponseHeadersChanged, this._refreshRequestCookiesView, this);
Blink Reformat4c46d092018-04-07 15:32:37232
Joey Arhar8b090572020-01-09 18:05:59233 this._refreshRequestCookiesView();
Blink Reformat4c46d092018-04-07 15:32:37234 }
235
236 /**
237 * @override
238 */
239 willHide() {
Joey Arhar8b090572020-01-09 18:05:59240 this._request.removeEventListener(
241 SDK.NetworkRequest.Events.RequestHeadersChanged, this._refreshRequestCookiesView, this);
242 this._request.removeEventListener(
243 SDK.NetworkRequest.Events.ResponseHeadersChanged, this._refreshRequestCookiesView, this);
Blink Reformat4c46d092018-04-07 15:32:37244 }
Paul Lewis56509652019-12-06 12:51:58245}