"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/src/webdriver/DevTools/v108/V108Network.cs" (17 Feb 2023, 14568 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "V108Network.cs" see the
Fossies "Dox" file reference documentation.
1 // <copyright file="V108Network.cs" company="WebDriver Committers">
2 // Licensed to the Software Freedom Conservancy (SFC) under one
3 // or more contributor license agreements. See the NOTICE file
4 // distributed with this work for additional information
5 // regarding copyright ownership. The SFC licenses this file
6 // to you under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // </copyright>
18
19 using System;
20 using System.Collections.Generic;
21 using System.Text;
22 using System.Threading.Tasks;
23 using OpenQA.Selenium.DevTools.V108.Fetch;
24 using OpenQA.Selenium.DevTools.V108.Network;
25
26 namespace OpenQA.Selenium.DevTools.V108
27 {
28 /// <summary>
29 /// Class providing functionality for manipulating network calls using version 108 of the DevTools Protocol
30 /// </summary>
31 public class V108Network : DevTools.Network
32 {
33 private FetchAdapter fetch;
34 private NetworkAdapter network;
35
36 /// <summary>
37 /// Initializes a new instance of the <see cref="V108Network"/> class.
38 /// </summary>
39 /// <param name="network">The adapter for the Network domain.</param>
40 /// <param name="fetch">The adapter for the Fetch domain.</param>
41 public V108Network(NetworkAdapter network, FetchAdapter fetch)
42 {
43 this.network = network;
44 this.fetch = fetch;
45 fetch.AuthRequired += OnFetchAuthRequired;
46 fetch.RequestPaused += OnFetchRequestPaused;
47 }
48
49 /// <summary>
50 /// Asynchronously disables network caching.
51 /// </summary>
52 /// <returns>A task that represents the asynchronous operation.</returns>
53 public override async Task DisableNetworkCaching()
54 {
55 await network.SetCacheDisabled(new SetCacheDisabledCommandSettings() { CacheDisabled = true });
56 }
57
58 /// <summary>
59 /// Asynchronously enables network caching.
60 /// </summary>
61 /// <returns>A task that represents the asynchronous operation.</returns>
62 public override async Task EnableNetworkCaching()
63 {
64 await network.SetCacheDisabled(new SetCacheDisabledCommandSettings() { CacheDisabled = false });
65 }
66
67 public override async Task EnableNetwork()
68 {
69 await network.Enable(new Network.EnableCommandSettings());
70 }
71
72 public override async Task DisableNetwork()
73 {
74 await network.Disable();
75 }
76
77 /// <summary>
78 /// Asynchronously enables the fetch domain for all URL patterns.
79 /// </summary>
80 /// <returns>A task that represents the asynchronous operation.</returns>
81 public override async Task EnableFetchForAllPatterns()
82 {
83 await fetch.Enable(new OpenQA.Selenium.DevTools.V108.Fetch.EnableCommandSettings()
84 {
85 Patterns = new OpenQA.Selenium.DevTools.V108.Fetch.RequestPattern[]
86 {
87 new OpenQA.Selenium.DevTools.V108.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Request },
88 new OpenQA.Selenium.DevTools.V108.Fetch.RequestPattern() { UrlPattern = "*", RequestStage = RequestStage.Response }
89 },
90 HandleAuthRequests = true
91 });
92 }
93
94 /// <summary>
95 /// Asynchronously diables the fetch domain.
96 /// </summary>
97 /// <returns>A task that represents the asynchronous operation.</returns>
98 public override async Task DisableFetch()
99 {
100 await fetch.Disable();
101 }
102
103 /// <summary>
104 /// Asynchronously sets the override of the user agent settings.
105 /// </summary>
106 /// <param name="userAgent">A <see cref="UserAgent"/> object containing the user agent values to override.</param>
107 /// <returns>A task that represents the asynchronous operation.</returns>
108 public override async Task SetUserAgentOverride(UserAgent userAgent)
109 {
110 await network.SetUserAgentOverride(new SetUserAgentOverrideCommandSettings()
111 {
112 UserAgent = userAgent.UserAgentString,
113 AcceptLanguage = userAgent.AcceptLanguage,
114 Platform = userAgent.Platform
115 });
116 }
117
118 /// <summary>
119 /// Asynchronously continues an intercepted network request.
120 /// </summary>
121 /// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
122 /// <returns>A task that represents the asynchronous operation.</returns>
123 public override async Task ContinueRequest(HttpRequestData requestData)
124 {
125 var commandSettings = new ContinueRequestCommandSettings()
126 {
127 RequestId = requestData.RequestId,
128 Method = requestData.Method,
129 Url = requestData.Url,
130 };
131
132 if (requestData.Headers.Count > 0)
133 {
134 List<HeaderEntry> headers = new List<HeaderEntry>();
135 foreach (KeyValuePair<string, string> headerPair in requestData.Headers)
136 {
137 headers.Add(new HeaderEntry() { Name = headerPair.Key, Value = headerPair.Value });
138 }
139
140 commandSettings.Headers = headers.ToArray();
141 }
142
143 if (!string.IsNullOrEmpty(requestData.PostData))
144 {
145 commandSettings.PostData = requestData.PostData;
146 }
147
148 await fetch.ContinueRequest(commandSettings);
149 }
150
151 /// <summary>
152 /// Asynchronously continues an intercepted network request.
153 /// </summary>
154 /// <param name="requestData">The <see cref="HttpRequestData"/> of the request.</param>
155 /// <param name="responseData">The <see cref="HttpResponseData"/> with which to respond to the request</param>
156 /// <returns>A task that represents the asynchronous operation.</returns>
157 public override async Task ContinueRequestWithResponse(HttpRequestData requestData, HttpResponseData responseData)
158 {
159 var commandSettings = new FulfillRequestCommandSettings()
160 {
161 RequestId = requestData.RequestId,
162 ResponseCode = responseData.StatusCode,
163 };
164
165 if (responseData.Headers.Count > 0 || responseData.CookieHeaders.Count > 0)
166 {
167 List<HeaderEntry> headers = new List<HeaderEntry>();
168 foreach (KeyValuePair<string, string> headerPair in responseData.Headers)
169 {
170 headers.Add(new HeaderEntry() { Name = headerPair.Key, Value = headerPair.Value });
171 }
172
173 foreach (string cookieHeader in responseData.CookieHeaders)
174 {
175 headers.Add(new HeaderEntry() { Name = "Set-Cookie", Value = cookieHeader });
176 }
177
178 commandSettings.ResponseHeaders = headers.ToArray();
179 }
180
181 if (!string.IsNullOrEmpty(responseData.Body))
182 {
183 commandSettings.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(responseData.Body));
184 }
185
186 await fetch.FulfillRequest(commandSettings);
187 }
188
189 /// <summary>
190 /// Asynchronously contines an intercepted network call without modification.
191 /// </summary>
192 /// <param name="requestData">The <see cref="HttpRequestData"/> of the network call.</param>
193 /// <returns>A task that represents the asynchronous operation.</returns>
194 public override async Task ContinueRequestWithoutModification(HttpRequestData requestData)
195 {
196 await fetch.ContinueRequest(new ContinueRequestCommandSettings() { RequestId = requestData.RequestId });
197 }
198
199 /// <summary>
200 /// Asynchronously continues an intercepted network call using authentication.
201 /// </summary>
202 /// <param name="requestId">The ID of the network request for which to continue with authentication.</param>
203 /// <param name="userName">The user name with which to authenticate.</param>
204 /// <param name="password">The password with which to authenticate.</param>
205 /// <returns>A task that represents the asynchronous operation.</returns>
206 public override async Task ContinueWithAuth(string requestId, string userName, string password)
207 {
208 await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
209 {
210 RequestId = requestId,
211 AuthChallengeResponse = new V108.Fetch.AuthChallengeResponse()
212 {
213 Response = V108.Fetch.AuthChallengeResponseResponseValues.ProvideCredentials,
214 Username = userName,
215 Password = password
216 }
217 });
218 }
219
220 /// <summary>
221 /// Asynchronously cancels authorization of an intercepted network request.
222 /// </summary>
223 /// <param name="requestId">The ID of the network request for which to cancel authentication.</param>
224 /// <returns>A task that represents the asynchronous operation.</returns>
225 public override async Task CancelAuth(string requestId)
226 {
227 await fetch.ContinueWithAuth(new ContinueWithAuthCommandSettings()
228 {
229 RequestId = requestId,
230 AuthChallengeResponse = new OpenQA.Selenium.DevTools.V108.Fetch.AuthChallengeResponse()
231 {
232 Response = V108.Fetch.AuthChallengeResponseResponseValues.CancelAuth
233 }
234 });
235 }
236
237 /// <summary>
238 /// Asynchronously adds the response body to the provided <see cref="HttpResponseData"/> object.
239 /// </summary>
240 /// <param name="responseData">The <see cref="HttpResponseData"/> object to which to add the response body.</param>
241 /// <returns>A task that represents the asynchronous operation.</returns>
242 public override async Task AddResponseBody(HttpResponseData responseData)
243 {
244 // If the response is a redirect, retrieving the body will throw an error in CDP.
245 if (responseData.StatusCode < 300 || responseData.StatusCode > 399)
246 {
247 var bodyResponse = await fetch.GetResponseBody(new Fetch.GetResponseBodyCommandSettings() { RequestId = responseData.RequestId });
248 if (bodyResponse.Base64Encoded)
249 {
250 responseData.Body = Encoding.UTF8.GetString(Convert.FromBase64String(bodyResponse.Body));
251 }
252 else
253 {
254 responseData.Body = bodyResponse.Body;
255 }
256 }
257 }
258
259 /// <summary>
260 /// Asynchronously contines an intercepted network response without modification.
261 /// </summary>
262 /// <param name="responseData">The <see cref="HttpResponseData"/> of the network response.</param>
263 /// <returns>A task that represents the asynchronous operation.</returns>
264 public override async Task ContinueResponseWithoutModification(HttpResponseData responseData)
265 {
266 await fetch.ContinueRequest(new ContinueRequestCommandSettings() { RequestId = responseData.RequestId });
267 }
268
269 private void OnFetchAuthRequired(object sender, Fetch.AuthRequiredEventArgs e)
270 {
271 AuthRequiredEventArgs wrapped = new AuthRequiredEventArgs()
272 {
273 RequestId = e.RequestId,
274 Uri = e.Request.Url
275 };
276
277 this.OnAuthRequired(wrapped);
278 }
279
280 private void OnFetchRequestPaused(object sender, Fetch.RequestPausedEventArgs e)
281 {
282 if (e.ResponseErrorReason == null && e.ResponseStatusCode == null)
283 {
284 RequestPausedEventArgs wrapped = new RequestPausedEventArgs();
285 wrapped.RequestData = new HttpRequestData()
286 {
287 RequestId = e.RequestId,
288 Method = e.Request.Method,
289 Url = e.Request.Url,
290 PostData = e.Request.PostData,
291 Headers = new Dictionary<string, string>(e.Request.Headers)
292 };
293
294 this.OnRequestPaused(wrapped);
295 }
296 else
297 {
298 ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs();
299 wrappedResponse.ResponseData = new HttpResponseData()
300 {
301 RequestId = e.RequestId,
302 Url = e.Request.Url,
303 ResourceType = e.ResourceType.ToString()
304 };
305
306 if (e.ResponseStatusCode.HasValue)
307 {
308 wrappedResponse.ResponseData.StatusCode = e.ResponseStatusCode.Value;
309 }
310
311 if (e.ResponseHeaders != null)
312 {
313 foreach (var header in e.ResponseHeaders)
314 {
315 if (header.Name.ToLowerInvariant() == "set-cookie")
316 {
317 wrappedResponse.ResponseData.CookieHeaders.Add(header.Value);
318 }
319 else
320 {
321 if (wrappedResponse.ResponseData.Headers.ContainsKey(header.Name))
322 {
323 string currentHeaderValue = wrappedResponse.ResponseData.Headers[header.Name];
324 wrappedResponse.ResponseData.Headers[header.Name] = currentHeaderValue + ", " + header.Value;
325 }
326 else
327 {
328 wrappedResponse.ResponseData.Headers.Add(header.Name, header.Value);
329 }
330 }
331 }
332 }
333
334 this.OnResponsePaused(wrappedResponse);
335 }
336 }
337 }
338 }