-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
V122Network.cs
349 lines (316 loc) · 15 KB
/
V122Network.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
// <copyright file="V122Network.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.DevTools.V122.Fetch;
using OpenQA.Selenium.DevTools.V122.Network;
namespace OpenQA.Selenium.DevTools.V122
{
/// <summary>
/// Class providing functionality for manipulating network calls using version 122 of the DevTools Protocol
/// </summary>
public class V122Network : DevTools.Network
{
private FetchAdapter fetch;
private NetworkAdapter network;
/// <summary>
/// Initializes a new instance of the <see cref="V122Network"/> class.
/// </summary>
/// <param name="network">The adapter for the Network domain.</param>
/// <param name="fetch">The adapter for the Fetch domain.</param>
public V122Network(NetworkAdapter network, FetchAdapter fetch)
{
this.network = network;
this.fetch = fetch;
fetch.AuthRequired += OnFetchAuthRequired;
fetch.RequestPaused += OnFetchRequestPaused;
}
/// <summary>
/// Asynchronously disables network caching.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DisableNetworkCaching()
{
await network.SetCacheDisabled(new SetCacheDisabledCommandSettings() { CacheDisabled = true }).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously enables network caching.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task EnableNetworkCaching()
{
await network.SetCacheDisabled(new SetCacheDisabledCommandSettings() { CacheDisabled = false }).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously enables the Network domain..
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task EnableNetwork()
{
await network.Enable(new Network.EnableCommandSettings()).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously disables the Network domain..
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DisableNetwork()
{
await network.Disable().ConfigureAwait(false);
}
/// <summary>
/// Asynchronously enables the fetch domain for all URL patterns.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task EnableFetchForAllPatterns()
{
await fetch.Enable(new Fetch.EnableCommandSettings()
{
Patterns = new Fetch.RequestPattern[]
{
new Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Request },
new Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Response }
},
HandleAuthRequests = true
}).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously diables the fetch domain.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DisableFetch()
{
await fetch.Disable().ConfigureAwait(false);
}
/// <summary>
/// Asynchronously sets the override of the user agent settings.
/// </summary>
/// <param name="userAgent">A <see cref="UserAgent"/> object containing the user agent values to override.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task SetUserAgentOverride(UserAgent userAgent)
{
await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
{
UserAgent = userAgent.UserAgentString,
AcceptLanguage = userAgent.AcceptLanguage,
Platform = userAgent.Platform
}).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously continues an intercepted network request.
/// </summary>
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ContinueRequest(HttpRequestData requestData)
{
var commandSettings = new ContinueRequestCommandSettings()
{
RequestId = requestData.RequestId,
Method = requestData.Method,
Url = requestData.Url,
};
if (requestData.Headers.Count > 0)
{
List<HeaderEntry> headers = new List<HeaderEntry>();
foreach (KeyValuePair<string, string> headerPair in requestData.Headers)
{
headers.Add(new HeaderEntry() { Name = headerPair.Key, Value = headerPair.Value });
}
commandSettings.Headers = headers.ToArray();
}
if (!string.IsNullOrEmpty(requestData.PostData))
{
commandSettings.PostData = Convert.ToBase64String(Encoding.UTF8.GetBytes(requestData.PostData));
}
await fetch.ContinueRequest(commandSettings).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously continues an intercepted network request.
/// </summary>
/// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
/// <param name="responseData">The <see cref="HttpResponseData"/> with which to respond to the request</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ContinueRequestWithResponse(HttpRequestData requestData, HttpResponseData responseData)
{
var commandSettings = new FulfillRequestCommandSettings()
{
RequestId = requestData.RequestId,
ResponseCode = responseData.StatusCode,
};
if (responseData.Headers.Count > 0 || responseData.CookieHeaders.Count > 0)
{
List<HeaderEntry> headers = new List<HeaderEntry>();
foreach (KeyValuePair<string, string> headerPair in responseData.Headers)
{
headers.Add(new HeaderEntry() { Name = headerPair.Key, Value = headerPair.Value });
}
foreach (string cookieHeader in responseData.CookieHeaders)
{
headers.Add(new HeaderEntry() { Name = "Set-Cookie", Value = cookieHeader });
}
commandSettings.ResponseHeaders = headers.ToArray();
}
if (!string.IsNullOrEmpty(responseData.Body))
{
commandSettings.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(responseData.Body));
}
await fetch.FulfillRequest(commandSettings).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously contines an intercepted network call without modification.
/// </summary>
/// <param name="requestData">The <see cref="HttpRequestData"/> of the network call.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ContinueRequestWithoutModification(HttpRequestData requestData)
{
await fetch.ContinueRequest(new ContinueRequestCommandSettings() { RequestId = requestData.RequestId }).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously continues an intercepted network call using authentication.
/// </summary>
/// <param name="requestId">The ID of the network request for which to continue with authentication.</param>
/// <param name="userName">The user name with which to authenticate.</param>
/// <param name="password">The password with which to authenticate.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ContinueWithAuth(string requestId, string userName, string password)
{
await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
{
RequestId = requestId,
AuthChallengeResponse = new V122.Fetch.AuthChallengeResponse()
{
Response = V122.Fetch.AuthChallengeResponseResponseValues.ProvideCredentials,
Username = userName,
Password = password
}
}).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously cancels authorization of an intercepted network request.
/// </summary>
/// <param name="requestId">The ID of the network request for which to cancel authentication.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task CancelAuth(string requestId)
{
await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
{
RequestId = requestId,
AuthChallengeResponse = new OpenQA.Selenium.DevTools.V122.Fetch.AuthChallengeResponse()
{
Response = V122.Fetch.AuthChallengeResponseResponseValues.CancelAuth
}
}).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously adds the response body to the provided <see cref="HttpResponseData"/> object.
/// </summary>
/// <param name="responseData">The <see cref="HttpResponseData"/> object to which to add the response body.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task AddResponseBody(HttpResponseData responseData)
{
// If the response is a redirect, retrieving the body will throw an error in CDP.
if (responseData.StatusCode < 300 || responseData.StatusCode > 399)
{
var bodyResponse = await fetch.GetResponseBody(new Fetch.GetResponseBodyCommandSettings() { RequestId = responseData.RequestId }).ConfigureAwait(false);
if (bodyResponse != null)
{
if (bodyResponse.Base64Encoded)
{
responseData.Content = new HttpResponseContent(Convert.FromBase64String(bodyResponse.Body));
}
else
{
responseData.Content = new HttpResponseContent(bodyResponse.Body);
}
}
}
}
/// <summary>
/// Asynchronously continues an intercepted network response without modification.
/// </summary>
/// <param name="responseData">The <see cref="HttpResponseData"/> of the network response.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ContinueResponseWithoutModification(HttpResponseData responseData)
{
await fetch.ContinueResponse(new ContinueResponseCommandSettings() { RequestId = responseData.RequestId }).ConfigureAwait(false);
}
private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
{
AuthRequiredEventArgs wrapped = new AuthRequiredEventArgs()
{
RequestId = e.RequestId,
Uri = e.Request.Url
};
this.OnAuthRequired(wrapped);
}
private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
{
if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
{
RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
wrapped.RequestData = new HttpRequestData()
{
RequestId = e.RequestId,
Method = e.Request.Method,
Url = e.Request.Url,
PostData = e.Request.PostData,
Headers = new Dictionary<string, string>(e.Request.Headers)
};
this.OnRequestPaused(wrapped);
}
else
{
ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs();
wrappedResponse.ResponseData = new HttpResponseData()
{
RequestId = e.RequestId,
Url = e.Request.Url,
ResourceType = e.ResourceType.ToString()
};
if (e.ResponseStatusCode.HasValue)
{
wrappedResponse.ResponseData.StatusCode = e.ResponseStatusCode.Value;
}
if (e.ResponseHeaders != null)
{
foreach (var header in e.ResponseHeaders)
{
if (header.Name.ToLowerInvariant() == "set-cookie")
{
wrappedResponse.ResponseData.CookieHeaders.Add(header.Value);
}
else
{
if (wrappedResponse.ResponseData.Headers.ContainsKey(header.Name))
{
string currentHeaderValue = wrappedResponse.ResponseData.Headers[header.Name];
wrappedResponse.ResponseData.Headers[header.Name] = currentHeaderValue + ", " + header.Value;
}
else
{
wrappedResponse.ResponseData.Headers.Add(header.Name, header.Value);
}
}
}
}
this.OnResponsePaused(wrappedResponse);
}
}
}
}