Skip to content

Commit b287219

Browse files
authored
fix(requestinterception): fix font loading issue (#1765)
* refactor(requestinterception): refactor NetworkManager.ts * fix(requestinterception): fix fonts loading issue * feat(requestinterception): don't disable cache by default * Fix TestServer caching behavior
1 parent f7193bc commit b287219

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

lib/PuppeteerSharp.TestServer/SimpleServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public SimpleServer(int port, string contentRoot, bool isHttps)
6565
{
6666
fileResponseContext.Context.Response.Headers["Content-Security-Policy"] = csp;
6767
}
68+
69+
if (!fileResponseContext.Context.Request.Path.Value.StartsWith("/cached/"))
70+
{
71+
fileResponseContext.Context.Response.Headers["Cache-Control"] = "no-cache, no-store";
72+
fileResponseContext.Context.Response.Headers["Expires"] = "-1";
73+
}
6874
}
6975
}))
7076
.UseKestrel(options =>

lib/PuppeteerSharp/Messaging/FetchRequestPausedResponse.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ internal class FetchRequestPausedResponse
44
{
55
public string RequestId { get; set; }
66

7+
public Payload Request { get; set; }
8+
79
public string NetworkId { get; set; }
810
}
911
}

lib/PuppeteerSharp/NetworkManager.cs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ internal class NetworkManager
1515
#region Private members
1616

1717
private readonly CDPSession _client;
18-
private readonly ConcurrentDictionary<string, Request> _requestIdToRequest = new ConcurrentDictionary<string, Request>();
18+
19+
private readonly ConcurrentDictionary<string, Request> _requestIdToRequest =
20+
new ConcurrentDictionary<string, Request>();
21+
1922
private readonly ConcurrentDictionary<string, RequestWillBeSentPayload> _requestIdToRequestWillBeSentEvent =
2023
new ConcurrentDictionary<string, RequestWillBeSentPayload>();
2124

22-
private readonly ConcurrentDictionary<string, string> _requestIdToInterceptionId = new ConcurrentDictionary<string, string>();
25+
private readonly ConcurrentDictionary<string, FetchRequestPausedResponse> _requestIdToRequestPausedEvent =
26+
new ConcurrentDictionary<string, FetchRequestPausedResponse>();
27+
2328
private readonly ILogger _logger;
2429
private Dictionary<string, string> _extraHTTPHeaders;
2530
private Credentials _credentials;
@@ -142,7 +147,7 @@ internal Task SetRequestInterceptionAsync(bool value)
142147
private Task UpdateProtocolCacheDisabledAsync()
143148
=> _client.SendAsync("Network.setCacheDisabled", new NetworkSetCacheDisabledRequest
144149
{
145-
CacheDisabled = _userCacheDisabled || _userRequestInterceptionEnabled
150+
CacheDisabled = _userCacheDisabled
146151
});
147152

148153
private async void Client_MessageReceived(object sender, MessageEventArgs e)
@@ -190,12 +195,8 @@ private void OnLoadingFailed(LoadingFailedResponse e)
190195
{
191196
request.Failure = e.ErrorText;
192197
request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);
193-
_requestIdToRequest.TryRemove(request.RequestId, out _);
194198

195-
if (request.InterceptionId != null)
196-
{
197-
_attemptedAuthentications.Remove(request.InterceptionId);
198-
}
199+
ForgetRequest(request, true);
199200

200201
RequestFailed?.Invoke(this, new RequestEventArgs
201202
{
@@ -211,12 +212,8 @@ private void OnLoadingFinished(LoadingFinishedResponse e)
211212
if (_requestIdToRequest.TryGetValue(e.RequestId, out var request))
212213
{
213214
request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);
214-
_requestIdToRequest.TryRemove(request.RequestId, out _);
215215

216-
if (request.InterceptionId != null)
217-
{
218-
_attemptedAuthentications.Remove(request.InterceptionId);
219-
}
216+
ForgetRequest(request, true);
220217

221218
RequestFinished?.Invoke(this, new RequestEventArgs
222219
{
@@ -225,6 +222,22 @@ private void OnLoadingFinished(LoadingFinishedResponse e)
225222
}
226223
}
227224

225+
private void ForgetRequest(Request request, bool events)
226+
{
227+
_requestIdToRequest.TryRemove(request.RequestId, out _);
228+
229+
if (request.InterceptionId != null)
230+
{
231+
_attemptedAuthentications.Remove(request.InterceptionId);
232+
}
233+
234+
if (events)
235+
{
236+
_requestIdToRequestWillBeSentEvent.TryRemove(request.RequestId, out _);
237+
_requestIdToRequestPausedEvent.TryRemove(request.RequestId, out _);
238+
}
239+
}
240+
228241
private void OnResponseReceived(ResponseReceivedResponse e)
229242
{
230243
// FileUpload sends a response without a matching request.
@@ -296,16 +309,31 @@ private async Task OnRequestPausedAsync(FetchRequestPausedResponse e)
296309
var requestId = e.NetworkId;
297310
var interceptionId = e.RequestId;
298311

299-
if (!string.IsNullOrEmpty(requestId))
312+
if (string.IsNullOrEmpty(requestId))
300313
{
301-
if (_requestIdToRequestWillBeSentEvent.TryRemove(requestId, out var requestWillBeSentEvent))
302-
{
303-
await OnRequestAsync(requestWillBeSentEvent, interceptionId).ConfigureAwait(false);
304-
}
305-
else
306-
{
307-
_requestIdToInterceptionId[requestId] = interceptionId;
308-
}
314+
return;
315+
}
316+
317+
var hasRequestWillBeSentEvent = _requestIdToRequestWillBeSentEvent.TryGetValue(requestId, out var requestWillBeSentEvent);
318+
319+
// redirect requests have the same `requestId`
320+
321+
if (hasRequestWillBeSentEvent &&
322+
(requestWillBeSentEvent.Request.Url != e.Request.Url ||
323+
requestWillBeSentEvent.Request.Method != e.Request.Method))
324+
{
325+
_requestIdToRequestWillBeSentEvent.TryRemove(requestId, out _);
326+
hasRequestWillBeSentEvent = false;
327+
}
328+
329+
if (hasRequestWillBeSentEvent)
330+
{
331+
await OnRequestAsync(requestWillBeSentEvent, interceptionId).ConfigureAwait(false);
332+
_requestIdToRequestWillBeSentEvent.TryRemove(requestId, out _);
333+
}
334+
else
335+
{
336+
_requestIdToRequestPausedEvent[requestId] = e;
309337
}
310338
}
311339

@@ -365,15 +393,7 @@ private void HandleRequestRedirect(Request request, ResponsePayload responseMess
365393
response.BodyLoadedTaskWrapper.TrySetException(
366394
new PuppeteerException("Response body is unavailable for redirect responses"));
367395

368-
if (request.RequestId != null)
369-
{
370-
_requestIdToRequest.TryRemove(request.RequestId, out _);
371-
}
372-
373-
if (request.InterceptionId != null)
374-
{
375-
_attemptedAuthentications.Remove(request.InterceptionId);
376-
}
396+
ForgetRequest(request, false);
377397

378398
Response?.Invoke(this, new ResponseCreatedEventArgs
379399
{
@@ -391,15 +411,16 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
391411
// Request interception doesn't happen for data URLs with Network Service.
392412
if (_userRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase))
393413
{
394-
if (_requestIdToInterceptionId.TryRemove(e.RequestId, out var interceptionId))
414+
var hasRequestPausedEvent = _requestIdToRequestPausedEvent.TryGetValue(e.RequestId, out var requestPausedEvent);
415+
_requestIdToRequestWillBeSentEvent[e.RequestId] = e;
416+
417+
if (hasRequestPausedEvent)
395418
{
419+
var interceptionId = requestPausedEvent.RequestId;
396420
await OnRequestAsync(e, interceptionId).ConfigureAwait(false);
421+
_requestIdToRequestPausedEvent.TryRemove(e.RequestId, out _);
397422
}
398-
else
399-
{
400-
// Under load, we may get to this section more than once
401-
_requestIdToRequestWillBeSentEvent.TryAdd(e.RequestId, e);
402-
}
423+
403424
return;
404425
}
405426
await OnRequestAsync(e, null).ConfigureAwait(false);

0 commit comments

Comments
 (0)