Skip to content

Commit d05695d

Browse files
authored
Use new requestId for interception request matching (#1100)
* Use new requestId for interception request matching * ops
1 parent 2b2f1d2 commit d05695d

File tree

4 files changed

+7
-56
lines changed

4 files changed

+7
-56
lines changed

lib/PuppeteerSharp.Tests/NetworkTests/SetRequestInterceptionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ await Task.WhenAll(
6666
);
6767
}
6868

69-
[Fact(Skip = "see https://github.com/GoogleChrome/puppeteer/issues/3973")]
69+
[Fact]
7070
public async Task ShouldWorkWhenHeaderManipulationHeadersWithRedirect()
7171
{
7272
Server.SetRedirect("/rredirect", "/empty.html");

lib/PuppeteerSharp/Messaging/RequestInterceptedResponse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class RequestInterceptedResponse
1414
public HttpStatusCode ResponseStatusCode { get; set; }
1515
public string RedirectUrl { get; set; }
1616
public AuthChallengeData AuthChallenge { get; set; }
17+
public string RequestId { get; set; }
1718

1819
internal class AuthChallengeData
1920
{

lib/PuppeteerSharp/NetworkManager.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ internal class NetworkManager
1717
private readonly ConcurrentDictionary<string, Request> _requestIdToRequest = new ConcurrentDictionary<string, Request>();
1818
private readonly ConcurrentDictionary<string, RequestWillBeSentPayload> _requestIdToRequestWillBeSentEvent =
1919
new ConcurrentDictionary<string, RequestWillBeSentPayload>();
20-
private readonly MultiMap<string, string> _requestHashToRequestIds = new MultiMap<string, string>();
21-
private readonly MultiMap<string, string> _requestHashToInterceptionIds = new MultiMap<string, string>();
20+
private readonly ConcurrentDictionary<string, string> _requestIdToInterceptionId = new ConcurrentDictionary<string, string>();
2221
private readonly ILogger _logger;
2322
private Dictionary<string, string> _extraHTTPHeaders;
2423
private bool _offine;
@@ -245,19 +244,13 @@ private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
245244
}
246245
}
247246

248-
var requestHash = e.Request.Hash;
249-
var requestId = _requestHashToRequestIds.FirstValue(requestHash);
250-
if (requestId != null)
247+
if (e.RequestId != null && _requestIdToRequestWillBeSentEvent.TryRemove(e.RequestId, out var requestWillBeSentEvent))
251248
{
252-
if (_requestIdToRequestWillBeSentEvent.TryRemove(requestId, out var requestWillBeSentEvent))
253-
{
254-
await OnRequestAsync(requestWillBeSentEvent, e.InterceptionId);
255-
_requestHashToRequestIds.Delete(requestHash, requestId);
256-
}
249+
await OnRequestAsync(requestWillBeSentEvent, e.InterceptionId);
257250
}
258251
else
259252
{
260-
_requestHashToInterceptionIds.Add(requestHash, e.InterceptionId);
253+
_requestIdToInterceptionId[e.RequestId] = e.InterceptionId;
261254
}
262255
}
263256

@@ -343,16 +336,12 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
343336
// Request interception doesn't happen for data URLs with Network Service.
344337
if (_protocolRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase))
345338
{
346-
var requestHash = e.Request.Hash;
347-
var interceptionId = _requestHashToInterceptionIds.FirstValue(requestHash);
348-
if (interceptionId != null)
339+
if (_requestIdToInterceptionId.TryRemove(e.RequestId, out var interceptionId))
349340
{
350341
await OnRequestAsync(e, interceptionId);
351-
_requestHashToInterceptionIds.Delete(requestHash, interceptionId);
352342
}
353343
else
354344
{
355-
_requestHashToRequestIds.Add(requestHash, e.RequestId);
356345
// Under load, we may get to this section more than once
357346
_requestIdToRequestWillBeSentEvent.TryAdd(e.RequestId, e);
358347
}

lib/PuppeteerSharp/Payload.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,5 @@ public class Payload
4545
/// </summary>
4646
/// <value>The URL.</value>
4747
public string Url { get; set; }
48-
49-
internal string Hash
50-
{
51-
get
52-
{
53-
var normalizedUrl = Url;
54-
55-
try
56-
{
57-
// Decoding is necessary to normalize URLs.
58-
// The method will throw if the URL is malformed. In this case,
59-
// consider URL to be normalized as-is.
60-
normalizedUrl = HttpUtility.UrlDecode(Url);
61-
}
62-
catch
63-
{
64-
}
65-
66-
var hash = new Payload
67-
{
68-
Url = Url,
69-
Method = Method,
70-
PostData = PostData
71-
};
72-
73-
if (!normalizedUrl.StartsWith("data:", StringComparison.Ordinal))
74-
{
75-
foreach (var item in Headers.OrderBy(kv => kv.Key))
76-
{
77-
if (IgnoredHeaders.Contains(item.Key))
78-
{
79-
continue;
80-
}
81-
hash.Headers[item.Key] = item.Value;
82-
}
83-
}
84-
return JsonConvert.SerializeObject(hash, JsonHelper.DefaultJsonSerializerSettings);
85-
}
86-
}
8748
}
8849
}

0 commit comments

Comments
 (0)