forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Add RecordHarAsync extension method for BiDi Network module with automatic request/response body recording and memory optimization #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
12
commits into
trunk
Choose a base branch
from
copilot/add-har-file-capture
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,382
−0
Draft
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
91247f4
Initial plan
Copilot de7e82a
Add HAR capture extension for BiDi Network module
Copilot a8e0e28
Fix BytesValue conversion and add documentation
Copilot 9d58d56
Add HAR files to gitignore
Copilot 9e4f5ee
Add request/response body collection support
Copilot 0af3052
Make body collection unconditional and enabled by default
Copilot 8a9e201
Optimize memory usage by flushing entries to temp file
Copilot 627a1af
Rename CaptureNetworkTrafficAsync to CaptureHarAsync
Copilot e0cf3f2
Extract IHarRecorder interface and remove GetHar method
Copilot a26d88e
Rename from Capture to Record terminology for consistency
Copilot 413276e
Move HarRecorder to separate file
Copilot 4ed692e
Use dedicated file per network response for better concurrency
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
387 changes: 387 additions & 0 deletions
387
dotnet/src/webdriver/BiDi/Network/Har/BiDi.HarExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,387 @@ | ||
| // <copyright file="BiDi.HarExtensions.cs" company="Selenium 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.IO; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace OpenQA.Selenium.BiDi.Network.Har; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for BiDi class to capture network traffic as HAR. | ||
| /// </summary> | ||
| public static class BiDiHarExtensions | ||
| { | ||
| /// <summary> | ||
| /// Captures network traffic and returns a HAR recorder that can be used to save the captured traffic. | ||
| /// </summary> | ||
| /// <param name="bidi">The BiDi instance.</param> | ||
| /// <param name="options">Optional configuration options.</param> | ||
| /// <returns>A task that represents the asynchronous operation and returns a HarRecorder.</returns> | ||
| public static async Task<HarRecorder> CaptureNetworkTrafficAsync(this BiDi bidi, HarCaptureOptions? options = null) | ||
| { | ||
| if (bidi is null) throw new ArgumentNullException(nameof(bidi)); | ||
|
|
||
| var recorder = new HarRecorder(bidi, options ?? new HarCaptureOptions()); | ||
| await recorder.StartAsync().ConfigureAwait(false); | ||
| return recorder; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Options for HAR capture. | ||
| /// </summary> | ||
| public sealed class HarCaptureOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the browser name to include in the HAR file. | ||
| /// </summary> | ||
| public string? BrowserName { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the browser version to include in the HAR file. | ||
| /// </summary> | ||
| public string? BrowserVersion { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Records network traffic and provides methods to save it as HAR format. | ||
| /// </summary> | ||
| public sealed class HarRecorder : IAsyncDisposable | ||
nvborisenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
nvborisenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| private readonly BiDi _bidi; | ||
| private readonly HarCaptureOptions _options; | ||
| private readonly HarFile _harFile; | ||
| private readonly Dictionary<string, HarEntry> _pendingRequests; | ||
| private Subscription? _beforeRequestSubscription; | ||
| private Subscription? _responseStartedSubscription; | ||
| private Subscription? _responseCompletedSubscription; | ||
| private Collector? _dataCollector; | ||
|
|
||
| internal HarRecorder(BiDi bidi, HarCaptureOptions options) | ||
| { | ||
| _bidi = bidi ?? throw new ArgumentNullException(nameof(bidi)); | ||
| _options = options ?? throw new ArgumentNullException(nameof(options)); | ||
| _harFile = new HarFile(); | ||
| _pendingRequests = new Dictionary<string, HarEntry>(); | ||
|
|
||
| if (!string.IsNullOrEmpty(options.BrowserName)) | ||
| { | ||
| _harFile.Log.Browser = new HarBrowser | ||
| { | ||
| Name = options.BrowserName, | ||
| Version = options.BrowserVersion ?? string.Empty | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| internal async Task StartAsync() | ||
| { | ||
| // Always create data collector for capturing request and response bodies | ||
| _dataCollector = await _bidi.Network.AddDataCollectorAsync([DataType.Request, DataType.Response], 200000000).ConfigureAwait(false); | ||
|
|
||
| _beforeRequestSubscription = await _bidi.Network.OnBeforeRequestSentAsync(OnBeforeRequestSent).ConfigureAwait(false); | ||
| _responseStartedSubscription = await _bidi.Network.OnResponseStartedAsync(OnResponseStarted).ConfigureAwait(false); | ||
| _responseCompletedSubscription = await _bidi.Network.OnResponseCompletedAsync(OnResponseCompleted).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private void OnBeforeRequestSent(BeforeRequestSentEventArgs args) | ||
| { | ||
| var entry = new HarEntry | ||
| { | ||
| StartedDateTime = args.Timestamp.ToString("o"), | ||
| Request = ConvertRequest(args.Request), | ||
| Timings = ConvertTimings(args.Request.Timings), | ||
| Time = 0 | ||
| }; | ||
|
|
||
| if (args.Context != null) | ||
| { | ||
| entry.Pageref = args.Context.Id; | ||
| } | ||
|
|
||
| lock (_pendingRequests) | ||
| { | ||
| _pendingRequests[args.Request.Request.Id] = entry; | ||
| } | ||
| } | ||
|
|
||
| private void OnResponseStarted(ResponseStartedEventArgs args) | ||
| { | ||
| lock (_pendingRequests) | ||
| { | ||
| if (_pendingRequests.TryGetValue(args.Request.Request.Id, out var entry)) | ||
| { | ||
| entry.Response = ConvertResponse(args.Response); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async void OnResponseCompleted(ResponseCompletedEventArgs args) | ||
| { | ||
| HarEntry? entry = null; | ||
|
|
||
| lock (_pendingRequests) | ||
| { | ||
| if (_pendingRequests.TryGetValue(args.Request.Request.Id, out entry)) | ||
| { | ||
| entry.Response = ConvertResponse(args.Response); | ||
|
|
||
| // Calculate total time | ||
| var timings = args.Request.Timings; | ||
| entry.Time = CalculateTotalTime(timings); | ||
| } | ||
| } | ||
|
|
||
| if (entry != null) | ||
| { | ||
| // Retrieve request and response bodies | ||
| if (_dataCollector != null) | ||
| { | ||
| try | ||
| { | ||
| // Get request body | ||
| var requestBody = await _bidi.Network.GetDataAsync(DataType.Request, args.Request.Request).ConfigureAwait(false); | ||
| if (requestBody != null) | ||
| { | ||
| entry.Request.PostData = new HarPostData | ||
| { | ||
| MimeType = GetContentType(entry.Request.Headers), | ||
| Text = (string)requestBody | ||
| }; | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Request body may not be available for all requests (e.g., GET requests) | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // Get response body | ||
| var responseBody = await _bidi.Network.GetDataAsync(DataType.Response, args.Request.Request).ConfigureAwait(false); | ||
| if (responseBody != null) | ||
| { | ||
| var bodyText = (string)responseBody; | ||
| entry.Response.Content.Text = bodyText; | ||
| entry.Response.Content.Size = System.Text.Encoding.UTF8.GetByteCount(bodyText); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Response body may not be available for all responses | ||
| } | ||
| } | ||
|
|
||
| lock (_pendingRequests) | ||
| { | ||
| _harFile.Log.Entries.Add(entry); | ||
nvborisenko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _pendingRequests.Remove(args.Request.Request.Id); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private string GetContentType(List<HarHeader> headers) | ||
| { | ||
| var contentTypeHeader = headers.FirstOrDefault(h => h.Name.Equals("Content-Type", StringComparison.OrdinalIgnoreCase)); | ||
| return contentTypeHeader?.Value ?? "application/octet-stream"; | ||
| } | ||
|
|
||
| private HarRequest ConvertRequest(RequestData request) | ||
| { | ||
| var harRequest = new HarRequest | ||
| { | ||
| Method = request.Method, | ||
| Url = request.Url, | ||
| HttpVersion = "HTTP/1.1", | ||
| HeadersSize = request.HeadersSize ?? -1, | ||
| BodySize = request.BodySize ?? -1 | ||
| }; | ||
|
|
||
| foreach (var header in request.Headers) | ||
| { | ||
| harRequest.Headers.Add(new HarHeader | ||
| { | ||
| Name = header.Name, | ||
| Value = (string)header.Value | ||
| }); | ||
| } | ||
|
|
||
| foreach (var cookie in request.Cookies) | ||
| { | ||
| harRequest.Cookies.Add(new HarCookie | ||
| { | ||
| Name = cookie.Name, | ||
| Value = (string)cookie.Value, | ||
| Domain = cookie.Domain, | ||
| Path = cookie.Path, | ||
| HttpOnly = cookie.HttpOnly, | ||
| Secure = cookie.Secure, | ||
| Expires = cookie.Expiry?.ToString("o") | ||
| }); | ||
| } | ||
|
|
||
| // Parse query string from URL | ||
| var uri = new Uri(request.Url); | ||
| if (!string.IsNullOrEmpty(uri.Query)) | ||
| { | ||
| var queryString = uri.Query.TrimStart('?'); | ||
| var queryParams = queryString.Split('&'); | ||
| foreach (var param in queryParams) | ||
| { | ||
| var parts = param.Split('=', 2); | ||
| harRequest.QueryString.Add(new HarQueryParam | ||
| { | ||
| Name = Uri.UnescapeDataString(parts[0]), | ||
| Value = parts.Length > 1 ? Uri.UnescapeDataString(parts[1]) : string.Empty | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return harRequest; | ||
| } | ||
|
|
||
| private HarResponse ConvertResponse(ResponseData response) | ||
| { | ||
| var harResponse = new HarResponse | ||
| { | ||
| Status = response.Status, | ||
| StatusText = response.StatusText, | ||
| HttpVersion = response.Protocol, | ||
| HeadersSize = response.HeadersSize ?? -1, | ||
| BodySize = response.BodySize ?? -1, | ||
| Content = new HarContent | ||
| { | ||
| Size = response.BodySize ?? 0, | ||
| MimeType = response.MimeType | ||
| } | ||
| }; | ||
|
|
||
| foreach (var header in response.Headers) | ||
| { | ||
| var headerValue = (string)header.Value; | ||
| harResponse.Headers.Add(new HarHeader | ||
| { | ||
| Name = header.Name, | ||
| Value = headerValue | ||
| }); | ||
|
|
||
| // Check for redirect URL | ||
| if (header.Name.Equals("Location", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| harResponse.RedirectURL = headerValue; | ||
| } | ||
| } | ||
|
|
||
| return harResponse; | ||
| } | ||
|
|
||
| private HarTimings ConvertTimings(FetchTimingInfo timings) | ||
| { | ||
| return new HarTimings | ||
| { | ||
| Blocked = -1, | ||
| Dns = CalculateDuration(timings.DnsStart, timings.DnsEnd), | ||
| Connect = CalculateDuration(timings.ConnectStart, timings.ConnectEnd), | ||
| Ssl = CalculateDuration(timings.TlsStart, timings.ConnectEnd), | ||
| Send = CalculateDuration(timings.RequestStart, timings.RequestStart), | ||
| Wait = CalculateDuration(timings.RequestStart, timings.ResponseStart), | ||
| Receive = CalculateDuration(timings.ResponseStart, timings.ResponseEnd) | ||
| }; | ||
| } | ||
|
|
||
| private double CalculateDuration(double start, double end) | ||
| { | ||
| if (start < 0 || end < 0 || end < start) | ||
| { | ||
| return -1; | ||
| } | ||
| return end - start; | ||
| } | ||
|
|
||
| private double CalculateTotalTime(FetchTimingInfo timings) | ||
| { | ||
| if (timings.FetchStart >= 0 && timings.ResponseEnd >= 0 && timings.ResponseEnd >= timings.FetchStart) | ||
| { | ||
| return timings.ResponseEnd - timings.FetchStart; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the captured HAR file. | ||
| /// </summary> | ||
| /// <returns>The HAR file containing all captured network traffic.</returns> | ||
| public HarFile GetHar() | ||
| { | ||
| return _harFile; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Saves the captured network traffic to a HAR file. | ||
| /// </summary> | ||
| /// <param name="filePath">The path where the HAR file should be saved.</param> | ||
| /// <returns>A task that represents the asynchronous operation.</returns> | ||
| public async Task SaveAsync(string filePath) | ||
| { | ||
| if (string.IsNullOrEmpty(filePath)) | ||
| { | ||
| throw new ArgumentException("File path cannot be null or empty.", nameof(filePath)); | ||
| } | ||
|
|
||
| var options = new JsonSerializerOptions | ||
| { | ||
| WriteIndented = true, | ||
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
| DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(_harFile, options); | ||
| await File.WriteAllTextAsync(filePath, json).ConfigureAwait(false); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Disposes the recorder and unsubscribes from network events. | ||
| /// </summary> | ||
| public async ValueTask DisposeAsync() | ||
| { | ||
| if (_beforeRequestSubscription != null) | ||
| { | ||
| await _beforeRequestSubscription.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
|
|
||
| if (_responseStartedSubscription != null) | ||
| { | ||
| await _responseStartedSubscription.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
|
|
||
| if (_responseCompletedSubscription != null) | ||
| { | ||
| await _responseCompletedSubscription.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
|
|
||
| if (_dataCollector != null) | ||
| { | ||
| await _bidi.Network.RemoveDataCollectorAsync(_dataCollector).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.