Skip to content

Commit 6f3dc09

Browse files
Meir017kblok
authored andcommitted
Migrate onto Fetch domain (#1098)
1 parent 6e3b164 commit 6f3dc09

11 files changed

+173
-90
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Collections.Generic;
22
namespace PuppeteerSharp.Messaging
33
{
4-
internal class NetworkContinueInterceptedRequestRequest
4+
internal class ContinueWithAuthRequest
55
{
6-
public string InterceptionId { get; set; }
7-
public NetworkContinueInterceptedRequestChallengeResponse AuthChallengeResponse { get; set; }
6+
public string RequestId { get; set; }
7+
public ContinueWithAuthRequestChallengeResponse AuthChallengeResponse { get; set; }
88
public string RawResponse { get; set; }
99
public string ErrorReason { get; set; }
1010
public string Url { get; set; }
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace PuppeteerSharp.Messaging
2-
{
3-
internal class NetworkContinueInterceptedRequestChallengeResponse
2+
{
3+
internal class ContinueWithAuthRequestChallengeResponse
44
{
55
public string Response { get; set; }
66
public string Username { get; set; }
77
public string Password { get; set; }
88
}
9-
}
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.Net;
3+
4+
namespace PuppeteerSharp.Messaging
5+
{
6+
internal class FetchAuthRequiredResponse
7+
{
8+
public string RequestId { get; set; }
9+
public Payload Request { get; set; }
10+
public string FrameId { get; set; }
11+
public ResourceType ResourceType { get; set; }
12+
public bool IsNavigationRequest { get; set; }
13+
public Dictionary<string, object> ResponseHeaders { get; set; }
14+
public HttpStatusCode ResponseStatusCode { get; set; }
15+
public string RedirectUrl { get; set; }
16+
public AuthChallengeData AuthChallenge { get; set; }
17+
18+
internal class AuthChallengeData
19+
{
20+
public string Source { get; set; }
21+
public string Origin { get; set; }
22+
public string Scheme { get; set; }
23+
public string Realm { get; set; }
24+
}
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace PuppeteerSharp.Messaging
2+
{
3+
internal class FetchContinueRequestRequest
4+
{
5+
public string RequestId { get; set; }
6+
public string Url { get; set; }
7+
public string Method { get; set; }
8+
public string PostData { get; set; }
9+
public Header[] Headers { get; set; }
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace PuppeteerSharp.Messaging
2+
{
3+
internal class FetchEnableRequest
4+
{
5+
public bool HandleAuthRequests { get; set; }
6+
public Pattern[] Patterns { get; set; }
7+
8+
internal class Pattern
9+
{
10+
public string UrlPattern { get; set; }
11+
}
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
namespace PuppeteerSharp.Messaging
3+
{
4+
internal class FetchFailRequest
5+
{
6+
public string RequestId { get; set; }
7+
public string ErrorReason { get; set; }
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
namespace PuppeteerSharp.Messaging
3+
{
4+
internal class FetchFulfillRequest
5+
{
6+
public string RequestId { get; set; }
7+
public int ResponseCode { get; set; }
8+
public Header[] ResponseHeaders { get; set; }
9+
public string Body { get; set; }
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PuppeteerSharp.Messaging
2+
{
3+
internal class FetchRequestPausedResponse
4+
{
5+
public string RequestId { get; set; }
6+
public string NetworkId { get; set; }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PuppeteerSharp.Messaging
2+
{
3+
internal class Header
4+
{
5+
public string Name { get; set; }
6+
public string Value { get; set; }
7+
}
8+
}

lib/PuppeteerSharp/NetworkManager.cs

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,15 @@ private async void Client_MessageReceived(object sender, MessageEventArgs e)
131131
{
132132
switch (e.MessageID)
133133
{
134+
case "Fetch.requestPaused":
135+
await OnRequestPausedAsync(e.MessageData.ToObject<FetchRequestPausedResponse>(true));
136+
break;
137+
case "Fetch.authRequired":
138+
await OnAuthRequiredAsync(e.MessageData.ToObject<FetchAuthRequiredResponse>(true));
139+
break;
134140
case "Network.requestWillBeSent":
135141
await OnRequestWillBeSentAsync(e.MessageData.ToObject<RequestWillBeSentPayload>(true));
136142
break;
137-
case "Network.requestIntercepted":
138-
await OnRequestInterceptedAsync(e.MessageData.ToObject<RequestInterceptedResponse>(true)).ConfigureAwait(false);
139-
break;
140143
case "Network.requestServedFromCache":
141144
OnRequestServedFromCache(e.MessageData.ToObject<RequestServedFromCacheResponse>(true));
142145
break;
@@ -221,47 +224,47 @@ private void OnResponseReceived(ResponseReceivedResponse e)
221224
}
222225
}
223226

224-
private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
227+
private async Task OnAuthRequiredAsync(FetchAuthRequiredResponse e)
225228
{
226-
if (e.AuthChallenge != null)
229+
var response = "Default";
230+
if (_attemptedAuthentications.Contains(e.RequestId))
227231
{
228-
var response = "Default";
229-
if (_attemptedAuthentications.Contains(e.InterceptionId))
230-
{
231-
response = "CancelAuth";
232-
}
233-
else if (_credentials != null)
234-
{
235-
response = "ProvideCredentials";
236-
_attemptedAuthentications.Add(e.InterceptionId);
237-
}
238-
var credentials = _credentials ?? new Credentials();
239-
try
232+
response = "CancelAuth";
233+
}
234+
else if (_credentials != null)
235+
{
236+
response = "ProvideCredentials";
237+
_attemptedAuthentications.Add(e.RequestId);
238+
}
239+
var credentials = _credentials ?? new Credentials();
240+
try
241+
{
242+
await _client.SendAsync("Fetch.continueWithAuth", new ContinueWithAuthRequest
240243
{
241-
await _client.SendAsync("Network.continueInterceptedRequest", new NetworkContinueInterceptedRequestRequest
244+
RequestId = e.RequestId,
245+
AuthChallengeResponse = new ContinueWithAuthRequestChallengeResponse
242246
{
243-
InterceptionId = e.InterceptionId,
244-
AuthChallengeResponse = new NetworkContinueInterceptedRequestChallengeResponse
245-
{
246-
Response = response,
247-
Username = credentials.Username,
248-
Password = credentials.Password
249-
}
250-
}).ConfigureAwait(false);
251-
}
252-
catch (PuppeteerException ex)
253-
{
254-
_logger.LogError(ex.ToString());
255-
}
256-
return;
247+
Response = response,
248+
Username = credentials.Username,
249+
Password = credentials.Password
250+
}
251+
}).ConfigureAwait(false);
252+
}
253+
catch (PuppeteerException ex)
254+
{
255+
_logger.LogError(ex.ToString());
257256
}
257+
}
258+
259+
private async Task OnRequestPausedAsync(FetchRequestPausedResponse e)
260+
{
258261
if (!_userRequestInterceptionEnabled && _protocolRequestInterceptionEnabled)
259262
{
260263
try
261264
{
262-
await _client.SendAsync("Network.continueInterceptedRequest", new NetworkContinueInterceptedRequestRequest
265+
await _client.SendAsync("Fetch.continueRequest", new FetchContinueRequestRequest
263266
{
264-
InterceptionId = e.InterceptionId
267+
RequestId = e.RequestId
265268
}).ConfigureAwait(false);
266269
}
267270
catch (PuppeteerException ex)
@@ -270,13 +273,15 @@ private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
270273
}
271274
}
272275

273-
if (e.RequestId != null && _requestIdToRequestWillBeSentEvent.TryRemove(e.RequestId, out var requestWillBeSentEvent))
276+
var requestId = e.NetworkId;
277+
var interceptionId = e.RequestId;
278+
if (!string.IsNullOrEmpty(requestId) && _requestIdToRequestWillBeSentEvent.TryRemove(requestId, out var requestWillBeSentEvent))
274279
{
275-
await OnRequestAsync(requestWillBeSentEvent, e.InterceptionId);
280+
await OnRequestAsync(requestWillBeSentEvent, interceptionId).ConfigureAwait(false);
276281
}
277282
else
278283
{
279-
_requestIdToInterceptionId[e.RequestId] = e.InterceptionId;
284+
_requestIdToInterceptionId[requestId] = interceptionId;
280285
}
281286
}
282287

@@ -364,7 +369,7 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
364369
{
365370
if (_requestIdToInterceptionId.TryRemove(e.RequestId, out var interceptionId))
366371
{
367-
await OnRequestAsync(e, interceptionId);
372+
await OnRequestAsync(e, interceptionId).ConfigureAwait(false);
368373
}
369374
else
370375
{
@@ -373,7 +378,7 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
373378
}
374379
return;
375380
}
376-
await OnRequestAsync(e, null);
381+
await OnRequestAsync(e, null).ConfigureAwait(false);
377382
}
378383

379384
private async Task UpdateProtocolRequestInterceptionAsync()
@@ -384,20 +389,27 @@ private async Task UpdateProtocolRequestInterceptionAsync()
384389
{
385390
return;
386391
}
387-
388392
_protocolRequestInterceptionEnabled = enabled;
389-
var patterns = enabled ?
390-
new object[] { new KeyValuePair<string, string>("urlPattern", "*") } :
391-
Array.Empty<object>();
392-
393-
await Task.WhenAll(
394-
UpdateProtocolCacheDisabledAsync(),
395-
_client.SendAsync("Network.setRequestInterception", new NetworkSetRequestInterceptionRequest
396-
{
397-
Patterns = patterns
398-
})
399-
).ConfigureAwait(false);
393+
if (enabled)
394+
{
395+
await Task.WhenAll(
396+
UpdateProtocolCacheDisabledAsync(),
397+
_client.SendAsync("Fetch.enable", new FetchEnableRequest
398+
{
399+
HandleAuthRequests = true,
400+
Patterns = new[] { new FetchEnableRequest.Pattern { UrlPattern = "*" } }
401+
})
402+
).ConfigureAwait(false);
403+
}
404+
else
405+
{
406+
await Task.WhenAll(
407+
UpdateProtocolCacheDisabledAsync(),
408+
_client.SendAsync("Fetch.disable")
409+
).ConfigureAwait(false);
410+
}
400411
}
412+
401413
#endregion
402414
}
403415
}

0 commit comments

Comments
 (0)