New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(requestinterception): fix font loading issue #7060
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49a150c
test(requestinterception): add failing test for fonts issue
Androbin 71d4c4a
refactor(requestinterception): refactor NetworkManager.ts
Androbin 5f183e6
fix(requestinterception): fix fonts loading issue
Androbin 4ada9b4
docs(requestinterception): document the expected order of events for …
Androbin 82cf65b
Merge branch 'main' into requestinterception
Androbin 129c3bb
Disable flaky test on firefox (#7182)
jschfflr 1bafe5e
Merge branch 'main' into requestinterception
jschfflr adea595
Merge branch 'main' into requestinterception
OrKoN File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,19 +69,54 @@ export class NetworkManager extends EventEmitter { | |
_client: CDPSession; | ||
_ignoreHTTPSErrors: boolean; | ||
_frameManager: FrameManager; | ||
_requestIdToRequest = new Map<string, HTTPRequest>(); | ||
|
||
/* | ||
* There are four possible orders of events: | ||
* A. `_onRequestWillBeSent` | ||
* B. `_onRequestWillBeSent`, `_onRequestPaused` | ||
* C. `_onRequestPaused`, `_onRequestWillBeSent` | ||
* D. `_onRequestPaused`, `_onRequestWillBeSent`, `_onRequestPaused` | ||
* (see crbug.com/1196004) | ||
* | ||
* For `_onRequest` we need the event from `_onRequestWillBeSent` and | ||
* optionally the `interceptionId` from `_onRequestPaused`. | ||
* | ||
* If request interception is disabled, call `_onRequest` once per call to | ||
* `_onRequestWillBeSent`. | ||
* If request interception is enabled, call `_onRequest` once per call to | ||
* `_onRequestPaused` (once per `interceptionId`). | ||
* | ||
* Events are stored to allow for subsequent events to call `_onRequest`. | ||
* | ||
* Note that (chains of) redirect requests have the same `requestId` (!) as | ||
* the original request. We have to anticipate series of events like these: | ||
* A. `_onRequestWillBeSent`, | ||
* `_onRequestWillBeSent`, ... | ||
* B. `_onRequestWillBeSent`, `_onRequestPaused`, | ||
* `_onRequestWillBeSent`, `_onRequestPaused`, ... | ||
* C. `_onRequestWillBeSent`, `_onRequestPaused`, | ||
* `_onRequestPaused`, `_onRequestWillBeSent`, ... | ||
* D. `_onRequestPaused`, `_onRequestWillBeSent`, | ||
* `_onRequestPaused`, `_onRequestWillBeSent`, `_onRequestPaused`, ... | ||
* (see crbug.com/1196004) | ||
*/ | ||
_requestIdToRequestWillBeSentEvent = new Map< | ||
string, | ||
Protocol.Network.RequestWillBeSentEvent | ||
>(); | ||
_requestIdToRequestPausedEvent = new Map< | ||
Androbin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string, | ||
Protocol.Fetch.RequestPausedEvent | ||
>(); | ||
_requestIdToRequest = new Map<string, HTTPRequest>(); | ||
|
||
_extraHTTPHeaders: Record<string, string> = {}; | ||
_credentials?: Credentials = null; | ||
_attemptedAuthentications = new Set<string>(); | ||
_userRequestInterceptionEnabled = false; | ||
_userRequestInterceptionCacheSafe = false; | ||
_protocolRequestInterceptionEnabled = false; | ||
_userCacheDisabled = false; | ||
_requestIdToInterceptionId = new Map<string, string>(); | ||
_emulatedNetworkConditions: InternalNetworkConditions = { | ||
offline: false, | ||
upload: -1, | ||
|
@@ -222,12 +257,17 @@ export class NetworkManager extends EventEmitter { | |
} | ||
} | ||
|
||
_cacheDisabled(): boolean { | ||
return ( | ||
this._userCacheDisabled || | ||
(this._userRequestInterceptionEnabled && | ||
!this._userRequestInterceptionCacheSafe) | ||
); | ||
} | ||
|
||
async _updateProtocolCacheDisabled(): Promise<void> { | ||
await this._client.send('Network.setCacheDisabled', { | ||
cacheDisabled: | ||
this._userCacheDisabled || | ||
(this._userRequestInterceptionEnabled && | ||
!this._userRequestInterceptionCacheSafe), | ||
cacheDisabled: this._cacheDisabled(), | ||
}); | ||
} | ||
|
||
|
@@ -238,13 +278,18 @@ export class NetworkManager extends EventEmitter { | |
!event.request.url.startsWith('data:') | ||
) { | ||
const requestId = event.requestId; | ||
const interceptionId = this._requestIdToInterceptionId.get(requestId); | ||
if (interceptionId) { | ||
const requestPausedEvent = this._requestIdToRequestPausedEvent.get( | ||
OrKoN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
requestId | ||
); | ||
|
||
this._requestIdToRequestWillBeSentEvent.set(requestId, event); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory, the condition for this line could be stricter: |
||
|
||
if (requestPausedEvent) { | ||
const interceptionId = requestPausedEvent.requestId; | ||
this._onRequest(event, interceptionId); | ||
this._requestIdToInterceptionId.delete(requestId); | ||
} else { | ||
this._requestIdToRequestWillBeSentEvent.set(event.requestId, event); | ||
this._requestIdToRequestPausedEvent.delete(requestId); | ||
Androbin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return; | ||
} | ||
this._onRequest(event, null); | ||
|
@@ -288,14 +333,30 @@ export class NetworkManager extends EventEmitter { | |
|
||
const requestId = event.networkId; | ||
const interceptionId = event.requestId; | ||
if (requestId && this._requestIdToRequestWillBeSentEvent.has(requestId)) { | ||
const requestWillBeSentEvent = this._requestIdToRequestWillBeSentEvent.get( | ||
requestId | ||
); | ||
|
||
if (!requestId) { | ||
return; | ||
} | ||
|
||
let requestWillBeSentEvent = this._requestIdToRequestWillBeSentEvent.get( | ||
requestId | ||
); | ||
|
||
// redirect requests have the same `requestId`, | ||
if ( | ||
requestWillBeSentEvent && | ||
(requestWillBeSentEvent.request.url !== event.request.url || | ||
requestWillBeSentEvent.request.method !== event.request.method) | ||
) { | ||
this._requestIdToRequestWillBeSentEvent.delete(requestId); | ||
requestWillBeSentEvent = null; | ||
} | ||
|
||
if (requestWillBeSentEvent) { | ||
this._onRequest(requestWillBeSentEvent, interceptionId); | ||
this._requestIdToRequestWillBeSentEvent.delete(requestId); | ||
} else { | ||
this._requestIdToInterceptionId.set(requestId, interceptionId); | ||
this._requestIdToRequestPausedEvent.set(requestId, event); | ||
} | ||
} | ||
|
||
|
@@ -346,8 +407,7 @@ export class NetworkManager extends EventEmitter { | |
response._resolveBody( | ||
new Error('Response body is unavailable for redirect responses') | ||
); | ||
this._requestIdToRequest.delete(request._requestId); | ||
this._attemptedAuthentications.delete(request._interceptionId); | ||
this._forgetRequest(request, false); | ||
this.emit(NetworkManagerEmittedEvents.Response, response); | ||
this.emit(NetworkManagerEmittedEvents.RequestFinished, request); | ||
} | ||
|
@@ -361,6 +421,19 @@ export class NetworkManager extends EventEmitter { | |
this.emit(NetworkManagerEmittedEvents.Response, response); | ||
} | ||
|
||
_forgetRequest(request: HTTPRequest, events: boolean): void { | ||
const requestId = request._requestId; | ||
const interceptionId = request._interceptionId; | ||
|
||
this._requestIdToRequest.delete(requestId); | ||
this._attemptedAuthentications.delete(interceptionId); | ||
|
||
if (events) { | ||
this._requestIdToRequestWillBeSentEvent.delete(requestId); | ||
this._requestIdToRequestPausedEvent.delete(requestId); | ||
} | ||
} | ||
|
||
_onLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void { | ||
const request = this._requestIdToRequest.get(event.requestId); | ||
// For certain requestIds we never receive requestWillBeSent event. | ||
|
@@ -370,8 +443,7 @@ export class NetworkManager extends EventEmitter { | |
// Under certain conditions we never get the Network.responseReceived | ||
// event from protocol. @see https://crbug.com/883475 | ||
if (request.response()) request.response()._resolveBody(null); | ||
this._requestIdToRequest.delete(request._requestId); | ||
this._attemptedAuthentications.delete(request._interceptionId); | ||
this._forgetRequest(request, true); | ||
this.emit(NetworkManagerEmittedEvents.RequestFinished, request); | ||
} | ||
|
||
|
@@ -383,8 +455,7 @@ export class NetworkManager extends EventEmitter { | |
request._failureText = event.errorText; | ||
const response = request.response(); | ||
if (response) response._resolveBody(null); | ||
this._requestIdToRequest.delete(request._requestId); | ||
this._attemptedAuthentications.delete(request._interceptionId); | ||
this._forgetRequest(request, true); | ||
this.emit(NetworkManagerEmittedEvents.RequestFailed, request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@font-face { | ||
font-family: 'one-style'; | ||
src: url('./one-style.woff') format('woff'); | ||
} | ||
|
||
body { | ||
background-color: pink; | ||
font-family: 'one-style', sans-serif; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<link rel='stylesheet' href='./one-style-font.css'> | ||
<div>hello, world!</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when does the C sequence happen? So A is when there is no interception and B is the normal interception flow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is correct. The difference between B and C is undocumented.
Seems to depend on whether the cache is enabled and whether the cache is hit or missed: #7038 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module has always been explicitly anticipating both B and C but it previously failed for D.
D is trivially fixed by preserving the entry in
_requestIdToRequestWillBeSentEvent
until_onLoadingFinished
(and adding a guard clause for redirect requests as they have the samerequestId
): 5f183e6#r613342144