Skip to content

Commit 0112e13

Browse files
committed
Fix request interception with complex PostData
1 parent 174fe31 commit 0112e13

14 files changed

+76
-23
lines changed

lib/PuppeteerSharp/BrowserData/JsonUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace PuppeteerSharp.BrowserData
77
{
8-
internal class JsonUtils
8+
internal static class JsonUtils
99
{
1010
public static async Task<T> GetAsync<T>(string url)
1111
{

lib/PuppeteerSharp/CDPSession.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public abstract class CDPSession : ICDPSession
3030
/// <inheritdoc/>
3131
public string Id { get; init; }
3232

33+
/// <inheritdoc/>
34+
public string CloseReason { get; protected set; }
35+
3336
/// <inheritdoc/>
3437
public ILoggerFactory LoggerFactory => Connection.LoggerFactory;
3538

lib/PuppeteerSharp/Cdp/CdpCDPSession.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class CdpCDPSession : CDPSession
4040
private readonly ConcurrentDictionary<int, MessageTask> _callbacks = new();
4141
private readonly string _parentSessionId;
4242
private readonly TargetType _targetType;
43-
private string _closeReason;
4443
private int _lastId;
4544

4645
internal CdpCDPSession(Connection connection, TargetType targetType, string sessionId, string parentSessionId)
@@ -79,8 +78,8 @@ public override Task DetachAsync()
7978
throw new TargetClosedException(
8079
$"Protocol error ({method}): Session closed. " +
8180
$"Most likely the {_targetType} has been closed." +
82-
$"Close reason: {_closeReason}",
83-
_closeReason);
81+
$"Close reason: {CloseReason}",
82+
CloseReason);
8483
}
8584

8685
var id = GetMessageId();
@@ -147,7 +146,7 @@ internal override void Close(string closeReason)
147146
return;
148147
}
149148

150-
_closeReason = closeReason;
149+
CloseReason = closeReason;
151150
IsClosed = true;
152151

153152
foreach (var callback in _callbacks.Values.ToArray())

lib/PuppeteerSharp/Cdp/CdpHttpRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal CdpHttpRequest(
2929
IFrame frame,
3030
string interceptionId,
3131
bool allowInterception,
32-
RequestWillBeSentPayload data,
32+
RequestWillBeSentResponse data,
3333
List<IRequest> redirectChain,
3434
ILoggerFactory loggerFactory)
3535
{

lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ private void OnFrameDetached(object sender, EventArgs e)
110110
var frame = sender as Frame;
111111
if (_frame == frame)
112112
{
113-
Terminate(new PuppeteerException("Navigating frame was detached"));
113+
var message = "Navigating frame was detached";
114+
115+
if (!string.IsNullOrEmpty(frame?.Client?.CloseReason))
116+
{
117+
message += $": {frame.Client.CloseReason}";
118+
}
119+
120+
Terminate(new PuppeteerException(message));
114121
return;
115122
}
116123

lib/PuppeteerSharp/Cdp/Messaging/FetchRequestPausedResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace PuppeteerSharp.Cdp.Messaging
22
{
3-
internal class FetchRequestPausedResponse : RequestWillBeSentPayload
3+
internal class FetchRequestPausedResponse : RequestWillBeSentResponse
44
{
55
public ResourceType? ResourceType { get; set; }
66
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// * MIT License
2+
// *
3+
// * Copyright (c) Darío Kondratiuk
4+
// *
5+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// * of this software and associated documentation files (the "Software"), to deal
7+
// * in the Software without restriction, including without limitation the rights
8+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// * copies of the Software, and to permit persons to whom the Software is
10+
// * furnished to do so, subject to the following conditions:
11+
// *
12+
// * The above copyright notice and this permission notice shall be included in all
13+
// * copies or substantial portions of the Software.
14+
// *
15+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// * SOFTWARE.
22+
23+
namespace PuppeteerSharp.Cdp.Messaging;
24+
25+
using System.Collections.Generic;
26+
using System.Net.Http;
27+
28+
internal class Request
29+
{
30+
public HttpMethod Method { get; set; }
31+
32+
public object PostData { get; set; }
33+
34+
public Dictionary<string, string> Headers { get; set; } = [];
35+
36+
public string Url { get; set; }
37+
38+
public bool? HasPostData { get; set; }
39+
}

lib/PuppeteerSharp/Cdp/Messaging/RequestWillBeSentPayload.cs renamed to lib/PuppeteerSharp/Cdp/Messaging/RequestWillBeSentResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace PuppeteerSharp.Cdp.Messaging
22
{
3-
internal class RequestWillBeSentPayload
3+
internal class RequestWillBeSentResponse
44
{
55
public string RequestId { get; set; }
66

77
public string LoaderId { get; set; }
88

9-
public Payload Request { get; set; }
9+
public Request Request { get; set; }
1010

1111
public ResponsePayload RedirectResponse { get; set; }
1212

lib/PuppeteerSharp/Cdp/NetworkEventManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace PuppeteerSharp.Cdp
77
{
88
internal class NetworkEventManager
99
{
10-
private readonly ConcurrentDictionary<string, RequestWillBeSentPayload> _requestWillBeSentMap = new();
10+
private readonly ConcurrentDictionary<string, RequestWillBeSentResponse> _requestWillBeSentMap = new();
1111
private readonly ConcurrentDictionary<string, FetchRequestPausedResponse> _requestPausedMap = new();
1212
private readonly ConcurrentDictionary<string, CdpHttpRequest> _httpRequestsMap = new();
1313
private readonly ConcurrentDictionary<string, QueuedEventGroup> _queuedEventGroupMap = new();
@@ -59,10 +59,10 @@ internal ResponseReceivedExtraInfoResponse ShiftResponseExtraInfo(string network
5959
return result;
6060
}
6161

62-
internal void StoreRequestWillBeSent(string networkRequestId, RequestWillBeSentPayload e)
62+
internal void StoreRequestWillBeSent(string networkRequestId, RequestWillBeSentResponse e)
6363
=> _requestWillBeSentMap.AddOrUpdate(networkRequestId, e, (_, _) => e);
6464

65-
internal RequestWillBeSentPayload GetRequestWillBeSent(string networkRequestId)
65+
internal RequestWillBeSentResponse GetRequestWillBeSent(string networkRequestId)
6666
{
6767
_requestWillBeSentMap.TryGetValue(networkRequestId, out var result);
6868
return result;

lib/PuppeteerSharp/Cdp/NetworkManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private async void Client_MessageReceived(object sender, MessageEventArgs e)
173173
await OnAuthRequiredAsync(client, e.MessageData.ToObject<FetchAuthRequiredResponse>()).ConfigureAwait(false);
174174
break;
175175
case "Network.requestWillBeSent":
176-
await OnRequestWillBeSentAsync(client, e.MessageData.ToObject<RequestWillBeSentPayload>()).ConfigureAwait(false);
176+
await OnRequestWillBeSentAsync(client, e.MessageData.ToObject<RequestWillBeSentResponse>()).ConfigureAwait(false);
177177
break;
178178
case "Network.requestServedFromCache":
179179
OnRequestServedFromCache(e.MessageData.ToObject<RequestServedFromCacheResponse>());
@@ -462,7 +462,7 @@ private async void OnRequestWithoutNetworkInstrumentationAsync(CDPSession client
462462
_ = request.FinalizeInterceptionsAsync();
463463
}
464464

465-
private async Task OnRequestAsync(CDPSession client, RequestWillBeSentPayload e, string fetchRequestId)
465+
private async Task OnRequestAsync(CDPSession client, RequestWillBeSentResponse e, string fetchRequestId)
466466
{
467467
CdpHttpRequest request;
468468
var redirectChain = new List<IRequest>();
@@ -549,7 +549,7 @@ private void HandleRequestRedirect(CDPSession client, CdpHttpRequest request, Re
549549
RequestFinished?.Invoke(this, new RequestEventArgs(request));
550550
}
551551

552-
private async Task OnRequestWillBeSentAsync(CDPSession client, RequestWillBeSentPayload e)
552+
private async Task OnRequestWillBeSentAsync(CDPSession client, RequestWillBeSentResponse e)
553553
{
554554
// Request interception doesn't happen for data URLs with Network Service.
555555
if (_userRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase))
@@ -571,7 +571,7 @@ private async Task OnRequestWillBeSentAsync(CDPSession client, RequestWillBeSent
571571
await OnRequestAsync(client, e, null).ConfigureAwait(false);
572572
}
573573

574-
private void PatchRequestEventHeaders(RequestWillBeSentPayload requestWillBeSentEvent, FetchRequestPausedResponse requestPausedEvent)
574+
private void PatchRequestEventHeaders(RequestWillBeSentResponse requestWillBeSentEvent, FetchRequestPausedResponse requestPausedEvent)
575575
{
576576
foreach (var kv in requestPausedEvent.Request.Headers)
577577
{

0 commit comments

Comments
 (0)