Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 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 Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 31 | import * as Common from '../common/common.js'; |
Tim van der Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 32 | import * as CookieTable from '../cookie_table/cookie_table.js'; // eslint-disable-line no-unused-vars |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 33 | import * as SDK from '../sdk/sdk.js'; |
| 34 | import * as UI from '../ui/ui.js'; |
| 35 | |
| 36 | export class RequestCookiesView extends UI.Widget.Widget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | /** |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 38 | * @param {!SDK.NetworkRequest.NetworkRequest} request |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | */ |
| 40 | constructor(request) { |
| 41 | super(); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 42 | this.registerRequiredCSS('network/requestCookiesView.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 43 | this.element.classList.add('request-cookies-view'); |
| 44 | |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 45 | /** @type {!SDK.NetworkRequest.NetworkRequest} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 46 | this._request = request; |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 47 | this._showFilteredOutCookiesSetting = Common.Settings.Settings.instance().createSetting( |
| 48 | 'show-filtered-out-request-cookies', /* defaultValue */ false); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 49 | |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 50 | this._emptyWidget = new UI.EmptyWidget.EmptyWidget(Common.UIString.UIString('This request has no cookies.')); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 51 | 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ünd | f3899c2 | 2020-10-08 10:59:59 | [diff] [blame] | 58 | const requestCookiesCheckbox = /** @type {!UI.UIUtils.CheckboxLabel} */ (UI.SettingsUI.createSettingCheckbox( |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 59 | ls`show filtered out request cookies`, this._showFilteredOutCookiesSetting, |
Simon Zünd | f3899c2 | 2020-10-08 10:59:59 | [diff] [blame] | 60 | /* omitParagraphElement */ true)); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 61 | requestCookiesCheckbox.checkboxElement.addEventListener('change', () => { |
| 62 | this._refreshRequestCookiesView(); |
| 63 | }); |
| 64 | this._requestCookiesTitle.appendChild(requestCookiesCheckbox); |
| 65 | |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 66 | this._requestCookiesEmpty = this.element.createChild('div', 'cookies-panel-item'); |
| 67 | this._requestCookiesEmpty.textContent = ls`No request cookies were sent.`; |
| 68 | |
Tim van der Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 69 | this._requestCookiesTable = new CookieTable.CookiesTable.CookiesTable(/* renderInline */ true); |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 70 | this._requestCookiesTable.contentElement.classList.add('cookie-table', 'cookies-panel-item'); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 71 | 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 Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 78 | this._responseCookiesTable = new CookieTable.CookiesTable.CookiesTable(/* renderInline */ true); |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 79 | this._responseCookiesTable.contentElement.classList.add('cookie-table', 'cookies-panel-item'); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 80 | 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 Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 91 | * @return {!{requestCookies: !Array<!SDK.Cookie.Cookie>, requestCookieToBlockedReasons: !Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>}} |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 92 | */ |
| 93 | _getRequestCookies() { |
Tim van der Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 94 | /** @type {!Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>} */ |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 95 | const requestCookieToBlockedReasons = new Map(); |
Sigurd Schneider | 0e88b91 | 2020-05-08 08:28:23 | [diff] [blame] | 96 | const requestCookies = this._request.includedRequestCookies().slice(); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 97 | |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 98 | 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 Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 110 | return {requestCookies, requestCookieToBlockedReasons}; |
| 111 | } |
| 112 | |
| 113 | /** |
Tim van der Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 114 | * @return {!{responseCookies: !Array<!SDK.Cookie.Cookie>, responseCookieToBlockedReasons: !Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>, malformedResponseCookies: !Array<!SDK.NetworkRequest.BlockedSetCookieWithReason>}} |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 115 | */ |
| 116 | _getResponseCookies() { |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 117 | /** @type {!Array<!SDK.Cookie.Cookie>} */ |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 118 | let responseCookies = []; |
Tim van der Lippe | ec69c21 | 2020-03-12 16:40:47 | [diff] [blame] | 119 | /** @type {!Map<!SDK.Cookie.Cookie, !Array<!SDK.CookieModel.BlockedReason>>} */ |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 120 | const responseCookieToBlockedReasons = new Map(); |
| 121 | /** @type {!Array<!SDK.NetworkRequest.BlockedSetCookieWithReason>} */ |
| 122 | const malformedResponseCookies = []; |
| 123 | |
Jan Scheffler | 341eea5 | 2019-12-12 09:08:41 | [diff] [blame] | 124 | if (this._request.responseCookies.length) { |
Simon Zünd | f3899c2 | 2020-10-08 10:59:59 | [diff] [blame] | 125 | /** @type {!Array<?string>} */ |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 126 | 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ünd | d5ae9bf | 2020-03-03 09:48:43 | [diff] [blame] | 129 | const index = blockedCookieLines.indexOf(cookie.getCookieLine()); |
| 130 | if (index !== -1) { |
| 131 | blockedCookieLines[index] = null; |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | }); |
| 136 | |
| 137 | for (const blockedCookie of this._request.blockedResponseCookies()) { |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 138 | const parsedCookies = SDK.CookieParser.CookieParser.parseSetCookie(blockedCookie.cookieLine); |
Simon Zünd | f3899c2 | 2020-10-08 10:59:59 | [diff] [blame] | 139 | if (parsedCookies && !parsedCookies.length || |
Joey Arhar | 41a5fad | 2019-09-13 22:18:45 | [diff] [blame] | 140 | blockedCookie.blockedReasons.includes(Protocol.Network.SetCookieBlockedReason.SyntaxError)) { |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 141 | malformedResponseCookies.push(blockedCookie); |
| 142 | continue; |
| 143 | } |
| 144 | |
Simon Zünd | f3899c2 | 2020-10-08 10:59:59 | [diff] [blame] | 145 | let cookie = blockedCookie.cookie; |
| 146 | if (!cookie && parsedCookies) { |
| 147 | cookie = parsedCookies[0]; |
| 148 | } |
Jan Scheffler | 341eea5 | 2019-12-12 09:08:41 | [diff] [blame] | 149 | 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 Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | return {responseCookies, responseCookieToBlockedReasons, malformedResponseCookies}; |
| 162 | } |
| 163 | |
| 164 | _refreshRequestCookiesView() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 165 | if (!this.isShowing()) { |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 166 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 167 | } |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 168 | |
Sigurd Schneider | 0e88b91 | 2020-05-08 08:28:23 | [diff] [blame] | 169 | const gotCookies = this._request.hasRequestCookies() || this._request.responseCookies.length; |
Joey Arhar | 8b09057 | 2020-01-09 18:05:59 | [diff] [blame] | 170 | if (gotCookies) { |
| 171 | this._emptyWidget.hideWidget(); |
| 172 | } else { |
| 173 | this._emptyWidget.showWidget(); |
| 174 | } |
| 175 | |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 176 | const {requestCookies, requestCookieToBlockedReasons} = this._getRequestCookies(); |
| 177 | const {responseCookies, responseCookieToBlockedReasons, malformedResponseCookies} = this._getResponseCookies(); |
| 178 | |
| 179 | if (requestCookies.length) { |
| 180 | this._requestCookiesTitle.classList.remove('hidden'); |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 181 | this._requestCookiesEmpty.classList.add('hidden'); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 182 | this._requestCookiesTable.showWidget(); |
| 183 | this._requestCookiesTable.setCookies(requestCookies, requestCookieToBlockedReasons); |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 184 | |
| 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 Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 190 | } else { |
| 191 | this._requestCookiesTitle.classList.add('hidden'); |
Joey Arhar | 1c4df4f | 2019-09-26 18:59:13 | [diff] [blame] | 192 | this._requestCookiesEmpty.classList.add('hidden'); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 193 | 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 Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 212 | const icon = UI.Icon.Icon.create('smallicon-error', 'cookie-warning-icon'); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 213 | listItem.appendChild(icon); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 214 | UI.UIUtils.createTextChild(listItem, malformedCookie.cookieLine); |
Joey Arhar | 41a5fad | 2019-09-13 22:18:45 | [diff] [blame] | 215 | listItem.title = |
| 216 | SDK.NetworkRequest.setCookieBlockedReasonToUiString(Protocol.Network.SetCookieBlockedReason.SyntaxError); |
Joey Arhar | d4139c8 | 2019-09-07 03:54:39 | [diff] [blame] | 217 | } |
| 218 | } else { |
| 219 | this._malformedResponseCookiesTitle.classList.add('hidden'); |
| 220 | this._malformedResponseCookiesList.classList.add('hidden'); |
| 221 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @override |
| 226 | */ |
| 227 | wasShown() { |
Joey Arhar | 8b09057 | 2020-01-09 18:05:59 | [diff] [blame] | 228 | this._request.addEventListener( |
| 229 | SDK.NetworkRequest.Events.RequestHeadersChanged, this._refreshRequestCookiesView, this); |
| 230 | this._request.addEventListener( |
| 231 | SDK.NetworkRequest.Events.ResponseHeadersChanged, this._refreshRequestCookiesView, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 232 | |
Joey Arhar | 8b09057 | 2020-01-09 18:05:59 | [diff] [blame] | 233 | this._refreshRequestCookiesView(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @override |
| 238 | */ |
| 239 | willHide() { |
Joey Arhar | 8b09057 | 2020-01-09 18:05:59 | [diff] [blame] | 240 | this._request.removeEventListener( |
| 241 | SDK.NetworkRequest.Events.RequestHeadersChanged, this._refreshRequestCookiesView, this); |
| 242 | this._request.removeEventListener( |
| 243 | SDK.NetworkRequest.Events.ResponseHeadersChanged, this._refreshRequestCookiesView, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 244 | } |
Paul Lewis | 5650965 | 2019-12-06 12:51:58 | [diff] [blame] | 245 | } |