@@ -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