Skip to content
Permalink
Browse files

Migrate onto Fetch domain (#1098)

  • Loading branch information...
Meir017 authored and kblok committed May 15, 2019
1 parent 6e3b164 commit 6f3dc0908857270d04afb9e905bb3624b9c94c6c
@@ -1,10 +1,10 @@
using System.Collections.Generic;
namespace PuppeteerSharp.Messaging
{
internal class NetworkContinueInterceptedRequestRequest
internal class ContinueWithAuthRequest
{
public string InterceptionId { get; set; }
public NetworkContinueInterceptedRequestChallengeResponse AuthChallengeResponse { get; set; }
public string RequestId { get; set; }
public ContinueWithAuthRequestChallengeResponse AuthChallengeResponse { get; set; }
public string RawResponse { get; set; }
public string ErrorReason { get; set; }
public string Url { get; set; }
@@ -1,9 +1,9 @@
namespace PuppeteerSharp.Messaging
{
internal class NetworkContinueInterceptedRequestChallengeResponse
{
internal class ContinueWithAuthRequestChallengeResponse
{
public string Response { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
}
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Net;

namespace PuppeteerSharp.Messaging
{
internal class FetchAuthRequiredResponse
{
public string RequestId { get; set; }
public Payload Request { get; set; }
public string FrameId { get; set; }
public ResourceType ResourceType { get; set; }
public bool IsNavigationRequest { get; set; }
public Dictionary<string, object> ResponseHeaders { get; set; }
public HttpStatusCode ResponseStatusCode { get; set; }
public string RedirectUrl { get; set; }
public AuthChallengeData AuthChallenge { get; set; }

internal class AuthChallengeData
{
public string Source { get; set; }
public string Origin { get; set; }
public string Scheme { get; set; }
public string Realm { get; set; }
}
}
}
@@ -0,0 +1,11 @@
namespace PuppeteerSharp.Messaging
{
internal class FetchContinueRequestRequest
{
public string RequestId { get; set; }
public string Url { get; set; }
public string Method { get; set; }
public string PostData { get; set; }
public Header[] Headers { get; set; }
}
}
@@ -0,0 +1,13 @@
namespace PuppeteerSharp.Messaging
{
internal class FetchEnableRequest
{
public bool HandleAuthRequests { get; set; }
public Pattern[] Patterns { get; set; }

internal class Pattern
{
public string UrlPattern { get; set; }
}
}
}
@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace PuppeteerSharp.Messaging
{
internal class FetchFailRequest
{
public string RequestId { get; set; }
public string ErrorReason { get; set; }
}
}
@@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace PuppeteerSharp.Messaging
{
internal class FetchFulfillRequest
{
public string RequestId { get; set; }
public int ResponseCode { get; set; }
public Header[] ResponseHeaders { get; set; }
public string Body { get; set; }
}
}
@@ -0,0 +1,8 @@
namespace PuppeteerSharp.Messaging
{
internal class FetchRequestPausedResponse
{
public string RequestId { get; set; }
public string NetworkId { get; set; }
}
}
@@ -0,0 +1,8 @@
namespace PuppeteerSharp.Messaging
{
internal class Header
{
public string Name { get; set; }
public string Value { get; set; }
}
}
@@ -131,12 +131,15 @@ private async void Client_MessageReceived(object sender, MessageEventArgs e)
{
switch (e.MessageID)
{
case "Fetch.requestPaused":
await OnRequestPausedAsync(e.MessageData.ToObject<FetchRequestPausedResponse>(true));
break;
case "Fetch.authRequired":
await OnAuthRequiredAsync(e.MessageData.ToObject<FetchAuthRequiredResponse>(true));
break;
case "Network.requestWillBeSent":
await OnRequestWillBeSentAsync(e.MessageData.ToObject<RequestWillBeSentPayload>(true));
break;
case "Network.requestIntercepted":
await OnRequestInterceptedAsync(e.MessageData.ToObject<RequestInterceptedResponse>(true)).ConfigureAwait(false);
break;
case "Network.requestServedFromCache":
OnRequestServedFromCache(e.MessageData.ToObject<RequestServedFromCacheResponse>(true));
break;
@@ -221,47 +224,47 @@ private void OnResponseReceived(ResponseReceivedResponse e)
}
}

private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
private async Task OnAuthRequiredAsync(FetchAuthRequiredResponse e)
{
if (e.AuthChallenge != null)
var response = "Default";
if (_attemptedAuthentications.Contains(e.RequestId))
{
var response = "Default";
if (_attemptedAuthentications.Contains(e.InterceptionId))
{
response = "CancelAuth";
}
else if (_credentials != null)
{
response = "ProvideCredentials";
_attemptedAuthentications.Add(e.InterceptionId);
}
var credentials = _credentials ?? new Credentials();
try
response = "CancelAuth";
}
else if (_credentials != null)
{
response = "ProvideCredentials";
_attemptedAuthentications.Add(e.RequestId);
}
var credentials = _credentials ?? new Credentials();
try
{
await _client.SendAsync("Fetch.continueWithAuth", new ContinueWithAuthRequest
{
await _client.SendAsync("Network.continueInterceptedRequest", new NetworkContinueInterceptedRequestRequest
RequestId = e.RequestId,
AuthChallengeResponse = new ContinueWithAuthRequestChallengeResponse
{
InterceptionId = e.InterceptionId,
AuthChallengeResponse = new NetworkContinueInterceptedRequestChallengeResponse
{
Response = response,
Username = credentials.Username,
Password = credentials.Password
}
}).ConfigureAwait(false);
}
catch (PuppeteerException ex)
{
_logger.LogError(ex.ToString());
}
return;
Response = response,
Username = credentials.Username,
Password = credentials.Password
}
}).ConfigureAwait(false);
}
catch (PuppeteerException ex)
{
_logger.LogError(ex.ToString());
}
}

private async Task OnRequestPausedAsync(FetchRequestPausedResponse e)
{
if (!_userRequestInterceptionEnabled && _protocolRequestInterceptionEnabled)
{
try
{
await _client.SendAsync("Network.continueInterceptedRequest", new NetworkContinueInterceptedRequestRequest
await _client.SendAsync("Fetch.continueRequest", new FetchContinueRequestRequest
{
InterceptionId = e.InterceptionId
RequestId = e.RequestId
}).ConfigureAwait(false);
}
catch (PuppeteerException ex)
@@ -270,13 +273,15 @@ private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
}
}

if (e.RequestId != null && _requestIdToRequestWillBeSentEvent.TryRemove(e.RequestId, out var requestWillBeSentEvent))
var requestId = e.NetworkId;
var interceptionId = e.RequestId;
if (!string.IsNullOrEmpty(requestId) && _requestIdToRequestWillBeSentEvent.TryRemove(requestId, out var requestWillBeSentEvent))
{
await OnRequestAsync(requestWillBeSentEvent, e.InterceptionId);
await OnRequestAsync(requestWillBeSentEvent, interceptionId).ConfigureAwait(false);
}
else
{
_requestIdToInterceptionId[e.RequestId] = e.InterceptionId;
_requestIdToInterceptionId[requestId] = interceptionId;
}
}

@@ -364,7 +369,7 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
{
if (_requestIdToInterceptionId.TryRemove(e.RequestId, out var interceptionId))
{
await OnRequestAsync(e, interceptionId);
await OnRequestAsync(e, interceptionId).ConfigureAwait(false);
}
else
{
@@ -373,7 +378,7 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
}
return;
}
await OnRequestAsync(e, null);
await OnRequestAsync(e, null).ConfigureAwait(false);
}

private async Task UpdateProtocolRequestInterceptionAsync()
@@ -384,20 +389,27 @@ private async Task UpdateProtocolRequestInterceptionAsync()
{
return;
}

_protocolRequestInterceptionEnabled = enabled;
var patterns = enabled ?
new object[] { new KeyValuePair<string, string>("urlPattern", "*") } :
Array.Empty<object>();

await Task.WhenAll(
UpdateProtocolCacheDisabledAsync(),
_client.SendAsync("Network.setRequestInterception", new NetworkSetRequestInterceptionRequest
{
Patterns = patterns
})
).ConfigureAwait(false);
if (enabled)
{
await Task.WhenAll(
UpdateProtocolCacheDisabledAsync(),
_client.SendAsync("Fetch.enable", new FetchEnableRequest
{
HandleAuthRequests = true,
Patterns = new[] { new FetchEnableRequest.Pattern { UrlPattern = "*" } }
})
).ConfigureAwait(false);
}
else
{
await Task.WhenAll(
UpdateProtocolCacheDisabledAsync(),
_client.SendAsync("Fetch.disable")
).ConfigureAwait(false);
}
}

#endregion
}
}

0 comments on commit 6f3dc09

Please sign in to comment.
You can’t perform that action at this time.