-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
NetworkManager.cs
458 lines (394 loc) · 16.9 KB
/
NetworkManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Helpers.Json;
using PuppeteerSharp.Messaging;
namespace PuppeteerSharp
{
internal class NetworkManager
{
#region Private members
private readonly CDPSession _client;
private readonly ConcurrentDictionary<string, Request> _requestIdToRequest =
new ConcurrentDictionary<string, Request>();
private readonly ConcurrentDictionary<string, RequestWillBeSentPayload> _requestIdToRequestWillBeSentEvent =
new ConcurrentDictionary<string, RequestWillBeSentPayload>();
private readonly ConcurrentDictionary<string, FetchRequestPausedResponse> _requestIdToRequestPausedEvent =
new ConcurrentDictionary<string, FetchRequestPausedResponse>();
private readonly ILogger _logger;
private Dictionary<string, string> _extraHTTPHeaders;
private Credentials _credentials;
private readonly ConcurrentSet<string> _attemptedAuthentications = new ConcurrentSet<string>();
private bool _userRequestInterceptionEnabled;
private bool _protocolRequestInterceptionEnabled;
private readonly bool _ignoreHTTPSErrors;
private bool _userCacheDisabled;
private readonly InternalNetworkConditions _emulatedNetworkConditions = new InternalNetworkConditions
{
Offline = false,
Upload = -1,
Download = -1,
Latency = 0,
};
#endregion
internal NetworkManager(CDPSession client, bool ignoreHTTPSErrors, FrameManager frameManager)
{
FrameManager = frameManager;
_client = client;
_ignoreHTTPSErrors = ignoreHTTPSErrors;
_client.MessageReceived += Client_MessageReceived;
_logger = _client.Connection.LoggerFactory.CreateLogger<NetworkManager>();
}
#region Public Properties
internal Dictionary<string, string> ExtraHTTPHeaders => _extraHTTPHeaders?.Clone();
internal event EventHandler<ResponseCreatedEventArgs> Response;
internal event EventHandler<RequestEventArgs> Request;
internal event EventHandler<RequestEventArgs> RequestFinished;
internal event EventHandler<RequestEventArgs> RequestFailed;
internal FrameManager FrameManager { get; set; }
#endregion
#region Public Methods
internal async Task InitializeAsync()
{
await _client.SendAsync("Network.enable").ConfigureAwait(false);
if (_ignoreHTTPSErrors)
{
await _client.SendAsync("Security.setIgnoreCertificateErrors", new SecuritySetIgnoreCertificateErrorsRequest
{
Ignore = true
}).ConfigureAwait(false);
}
}
internal Task AuthenticateAsync(Credentials credentials)
{
_credentials = credentials;
return UpdateProtocolRequestInterceptionAsync();
}
internal Task SetExtraHTTPHeadersAsync(Dictionary<string, string> extraHTTPHeaders)
{
_extraHTTPHeaders = new Dictionary<string, string>();
foreach (var item in extraHTTPHeaders)
{
_extraHTTPHeaders[item.Key.ToLower(CultureInfo.CurrentCulture)] = item.Value;
}
return _client.SendAsync("Network.setExtraHTTPHeaders", new NetworkSetExtraHTTPHeadersRequest
{
Headers = _extraHTTPHeaders
});
}
internal async Task SetOfflineModeAsync(bool value)
{
_emulatedNetworkConditions.Offline = value;
await UpdateNetworkConditionsAsync().ConfigureAwait(false);
}
internal async Task EmulateNetworkConditionsAsync(NetworkConditions networkConditions)
{
_emulatedNetworkConditions.Upload = networkConditions?.Upload ?? -1;
_emulatedNetworkConditions.Download = networkConditions?.Download ?? -1;
_emulatedNetworkConditions.Latency = networkConditions?.Latency ?? 0;
await UpdateNetworkConditionsAsync().ConfigureAwait(false);
}
private Task UpdateNetworkConditionsAsync()
=> _client.SendAsync("Network.emulateNetworkConditions", new NetworkEmulateNetworkConditionsRequest
{
Offline = _emulatedNetworkConditions.Offline,
Latency = _emulatedNetworkConditions.Latency,
UploadThroughput = _emulatedNetworkConditions.Upload,
DownloadThroughput = _emulatedNetworkConditions.Download,
});
internal Task SetUserAgentAsync(string userAgent)
=> _client.SendAsync("Network.setUserAgentOverride", new NetworkSetUserAgentOverrideRequest
{
UserAgent = userAgent
});
internal Task SetCacheEnabledAsync(bool enabled)
{
_userCacheDisabled = !enabled;
return UpdateProtocolCacheDisabledAsync();
}
internal Task SetRequestInterceptionAsync(bool value)
{
_userRequestInterceptionEnabled = value;
return UpdateProtocolRequestInterceptionAsync();
}
#endregion
#region Private Methods
private Task UpdateProtocolCacheDisabledAsync()
=> _client.SendAsync("Network.setCacheDisabled", new NetworkSetCacheDisabledRequest
{
CacheDisabled = _userCacheDisabled
});
private async void Client_MessageReceived(object sender, MessageEventArgs e)
{
try
{
switch (e.MessageID)
{
case "Fetch.requestPaused":
await OnRequestPausedAsync(e.MessageData.ToObject<FetchRequestPausedResponse>(true)).ConfigureAwait(false);
break;
case "Fetch.authRequired":
await OnAuthRequiredAsync(e.MessageData.ToObject<FetchAuthRequiredResponse>(true)).ConfigureAwait(false);
break;
case "Network.requestWillBeSent":
await OnRequestWillBeSentAsync(e.MessageData.ToObject<RequestWillBeSentPayload>(true)).ConfigureAwait(false);
break;
case "Network.requestServedFromCache":
OnRequestServedFromCache(e.MessageData.ToObject<RequestServedFromCacheResponse>(true));
break;
case "Network.responseReceived":
OnResponseReceived(e.MessageData.ToObject<ResponseReceivedResponse>(true));
break;
case "Network.loadingFinished":
OnLoadingFinished(e.MessageData.ToObject<LoadingFinishedResponse>(true));
break;
case "Network.loadingFailed":
OnLoadingFailed(e.MessageData.ToObject<LoadingFailedResponse>(true));
break;
}
}
catch (Exception ex)
{
var message = $"NetworkManager failed to process {e.MessageID}. {ex.Message}. {ex.StackTrace}";
_logger.LogError(ex, message);
_client.Close(message);
}
}
private void OnLoadingFailed(LoadingFailedResponse e)
{
// For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469
if (_requestIdToRequest.TryGetValue(e.RequestId, out var request))
{
request.Failure = e.ErrorText;
request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);
ForgetRequest(request, true);
RequestFailed?.Invoke(this, new RequestEventArgs
{
Request = request
});
}
}
private void OnLoadingFinished(LoadingFinishedResponse e)
{
// For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469
if (_requestIdToRequest.TryGetValue(e.RequestId, out var request))
{
request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);
ForgetRequest(request, true);
RequestFinished?.Invoke(this, new RequestEventArgs
{
Request = request
});
}
}
private void ForgetRequest(Request request, bool events)
{
_requestIdToRequest.TryRemove(request.RequestId, out _);
if (request.InterceptionId != null)
{
_attemptedAuthentications.Remove(request.InterceptionId);
}
if (events)
{
_requestIdToRequestWillBeSentEvent.TryRemove(request.RequestId, out _);
_requestIdToRequestPausedEvent.TryRemove(request.RequestId, out _);
}
}
private void OnResponseReceived(ResponseReceivedResponse e)
{
// FileUpload sends a response without a matching request.
if (_requestIdToRequest.TryGetValue(e.RequestId, out var request))
{
var response = new Response(
_client,
request,
e.Response);
request.Response = response;
Response?.Invoke(this, new ResponseCreatedEventArgs
{
Response = response
});
}
}
private async Task OnAuthRequiredAsync(FetchAuthRequiredResponse e)
{
var response = "Default";
if (_attemptedAuthentications.Contains(e.RequestId))
{
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
{
RequestId = e.RequestId,
AuthChallengeResponse = new ContinueWithAuthRequestChallengeResponse
{
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("Fetch.continueRequest", new FetchContinueRequestRequest
{
RequestId = e.RequestId
}).ConfigureAwait(false);
}
catch (PuppeteerException ex)
{
_logger.LogError(ex.ToString());
}
}
var requestId = e.NetworkId;
var interceptionId = e.RequestId;
if (string.IsNullOrEmpty(requestId))
{
return;
}
var hasRequestWillBeSentEvent = _requestIdToRequestWillBeSentEvent.TryGetValue(requestId, out var requestWillBeSentEvent);
// redirect requests have the same `requestId`
if (hasRequestWillBeSentEvent &&
(requestWillBeSentEvent.Request.Url != e.Request.Url ||
requestWillBeSentEvent.Request.Method != e.Request.Method))
{
_requestIdToRequestWillBeSentEvent.TryRemove(requestId, out _);
hasRequestWillBeSentEvent = false;
}
if (hasRequestWillBeSentEvent)
{
await OnRequestAsync(requestWillBeSentEvent, interceptionId).ConfigureAwait(false);
_requestIdToRequestWillBeSentEvent.TryRemove(requestId, out _);
}
else
{
_requestIdToRequestPausedEvent[requestId] = e;
}
}
private async Task OnRequestAsync(RequestWillBeSentPayload e, string interceptionId)
{
Request request;
var redirectChain = new List<Request>();
if (e.RedirectResponse != null)
{
_requestIdToRequest.TryGetValue(e.RequestId, out request);
// If we connect late to the target, we could have missed the requestWillBeSent event.
if (request != null)
{
HandleRequestRedirect(request, e.RedirectResponse);
redirectChain = request.RedirectChainList;
}
}
if (!_requestIdToRequest.TryGetValue(e.RequestId, out var currentRequest) ||
currentRequest.Frame == null)
{
var frame = !string.IsNullOrEmpty(e.FrameId) ? await FrameManager.TryGetFrameAsync(e.FrameId).ConfigureAwait(false) : null;
request = new Request(
_client,
frame,
interceptionId,
_userRequestInterceptionEnabled,
e,
redirectChain);
_requestIdToRequest[e.RequestId] = request;
Request?.Invoke(this, new RequestEventArgs
{
Request = request
});
}
}
private void OnRequestServedFromCache(RequestServedFromCacheResponse response)
{
if (_requestIdToRequest.TryGetValue(response.RequestId, out var request))
{
request.FromMemoryCache = true;
}
}
private void HandleRequestRedirect(Request request, ResponsePayload responseMessage)
{
var response = new Response(
_client,
request,
responseMessage);
request.Response = response;
request.RedirectChainList.Add(request);
response.BodyLoadedTaskWrapper.TrySetException(
new PuppeteerException("Response body is unavailable for redirect responses"));
ForgetRequest(request, false);
Response?.Invoke(this, new ResponseCreatedEventArgs
{
Response = response
});
RequestFinished?.Invoke(this, new RequestEventArgs
{
Request = request
});
}
private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
{
// Request interception doesn't happen for data URLs with Network Service.
if (_userRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase))
{
var hasRequestPausedEvent = _requestIdToRequestPausedEvent.TryGetValue(e.RequestId, out var requestPausedEvent);
_requestIdToRequestWillBeSentEvent.TryAdd(e.RequestId, e);
if (hasRequestPausedEvent)
{
var interceptionId = requestPausedEvent.RequestId;
await OnRequestAsync(e, interceptionId).ConfigureAwait(false);
_requestIdToRequestPausedEvent.TryRemove(e.RequestId, out _);
}
return;
}
await OnRequestAsync(e, null).ConfigureAwait(false);
}
private async Task UpdateProtocolRequestInterceptionAsync()
{
var enabled = _userRequestInterceptionEnabled || _credentials != null;
if (enabled == _protocolRequestInterceptionEnabled)
{
return;
}
_protocolRequestInterceptionEnabled = enabled;
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
}
}