private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
        {
            if (e.AuthChallenge != null)
            {
                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
                {
                    await _client.SendAsync("Network.continueInterceptedRequest", new NetworkContinueInterceptedRequestRequest
                    {
                        InterceptionId        = e.InterceptionId,
                        AuthChallengeResponse = new NetworkContinueInterceptedRequestChallengeResponse
                        {
                            Response = response,
                            Username = credentials.Username,
                            Password = credentials.Password
                        }
                    }).ConfigureAwait(false);
                }
                catch (PuppeteerException ex)
                {
                    _logger.LogError(ex.ToString());
                }
                return;
            }
            if (!_userRequestInterceptionEnabled && _protocolRequestInterceptionEnabled)
            {
                try
                {
                    await _client.SendAsync("Network.continueInterceptedRequest", new NetworkContinueInterceptedRequestRequest
                    {
                        InterceptionId = e.InterceptionId
                    }).ConfigureAwait(false);
                }
                catch (PuppeteerException ex)
                {
                    _logger.LogError(ex.ToString());
                }
            }

            if (e.RequestId != null && _requestIdToRequestWillBeSentEvent.TryRemove(e.RequestId, out var requestWillBeSentEvent))
            {
                await OnRequestAsync(requestWillBeSentEvent, e.InterceptionId);
            }
            else
            {
                _requestIdToInterceptionId[e.RequestId] = e.InterceptionId;
            }
        }
Esempio n. 2
0
        private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
        {
            if (e.AuthChallenge != null)
            {
                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
                {
                    await _client.SendAsync("Network.continueInterceptedRequest", new Dictionary <string, object>
                    {
                        { "interceptionId", e.InterceptionId },
                        { "authChallengeResponse", new
                          {
                              response,
                              username = credentials.Username,
                              password = credentials.Password
                          } }
                    });
                }
                catch (PuppeteerException ex)
                {
                    _logger.LogError(ex.ToString());
                }
                return;
            }
            if (!_userRequestInterceptionEnabled && _protocolRequestInterceptionEnabled)
            {
                try
                {
                    await _client.SendAsync("Network.continueInterceptedRequest", new Dictionary <string, object>
                    {
                        { "interceptionId", e.InterceptionId }
                    });
                }
                catch (PuppeteerException ex)
                {
                    _logger.LogError(ex.ToString());
                }
            }

            if (!string.IsNullOrEmpty(e.RedirectUrl))
            {
                var request = _interceptionIdToRequest[e.InterceptionId];

                HandleRequestRedirect(request, e.ResponseStatusCode, e.ResponseHeaders, false, false);
                HandleRequestStart(request.RequestId, e.InterceptionId, e.RedirectUrl, e.ResourceType, e.Request, e.FrameId);
                return;
            }

            string requestHash = e.Request.Hash;
            var    requestId   = _requestHashToRequestIds.FirstValue(requestHash);

            if (requestId != null)
            {
                _requestHashToRequestIds.Delete(requestHash, requestId);
                HandleRequestStart(requestId, e.InterceptionId, e.Request.Url, e.ResourceType, e.Request, e.FrameId);
            }
            else
            {
                _requestHashToInterceptionIds.Add(requestHash, e.InterceptionId);
                HandleRequestStart(null, e.InterceptionId, e.Request.Url, e.ResourceType, e.Request, e.FrameId);
            }
        }
        private async Task OnRequestInterceptedAsync(RequestInterceptedResponse e)
        {
            if (e.AuthChallenge != null)
            {
                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
                {
                    await _client.SendAsync("Network.continueInterceptedRequest", new Dictionary <string, object>
                    {
                        { MessageKeys.InterceptionId, e.InterceptionId },
                        { MessageKeys.AuthChallengeResponse, new
                          {
                              response,
                              username = credentials.Username,
                              password = credentials.Password
                          } }
                    }).ConfigureAwait(false);
                }
                catch (PuppeteerException ex)
                {
                    _logger.LogError(ex.ToString());
                }
                return;
            }
            if (!_userRequestInterceptionEnabled && _protocolRequestInterceptionEnabled)
            {
                try
                {
                    await _client.SendAsync("Network.continueInterceptedRequest", new Dictionary <string, object>
                    {
                        { MessageKeys.InterceptionId, e.InterceptionId }
                    }).ConfigureAwait(false);
                }
                catch (PuppeteerException ex)
                {
                    _logger.LogError(ex.ToString());
                }
            }

            var requestHash = e.Request.Hash;
            var requestId   = _requestHashToRequestIds.FirstValue(requestHash);

            if (requestId != null)
            {
                _requestIdToRequestWillBeSentEvent.TryGetValue(requestId, out var requestWillBeSentEvent);

                if (requestWillBeSentEvent != null)
                {
                    OnRequest(requestWillBeSentEvent, e.InterceptionId);
                    _requestHashToRequestIds.Delete(requestHash, requestId);
                    _requestIdToRequestWillBeSentEvent.Remove(requestId);
                }
            }
            else
            {
                _requestHashToInterceptionIds.Add(requestHash, e.InterceptionId);
            }
        }