-
Notifications
You must be signed in to change notification settings - Fork 77
Adds the HarGeneratorPlugin. Closes #1412 #1421
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using DevProxy.Abstractions.Plugins; | ||
| using DevProxy.Abstractions.Proxy; | ||
| using DevProxy.Abstractions.Utils; | ||
| using DevProxy.Plugins.Models; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Diagnostics; | ||
| using System.Text.Json; | ||
| using System.Web; | ||
|
|
||
| namespace DevProxy.Plugins.Generation; | ||
|
|
||
| public sealed class HarGeneratorPluginConfiguration | ||
| { | ||
| public bool IncludeSensitiveInformation { get; set; } | ||
| public bool IncludeResponse { get; set; } | ||
| } | ||
|
|
||
| public sealed class HarGeneratorPlugin( | ||
| HttpClient httpClient, | ||
| ILogger<HarGeneratorPlugin> logger, | ||
| ISet<UrlToWatch> urlsToWatch, | ||
| IProxyConfiguration proxyConfiguration, | ||
| IConfigurationSection pluginConfigurationSection) : | ||
| BaseReportingPlugin<HarGeneratorPluginConfiguration>( | ||
| httpClient, | ||
| logger, | ||
| urlsToWatch, | ||
| proxyConfiguration, | ||
| pluginConfigurationSection) | ||
| { | ||
| public override string Name => nameof(HarGeneratorPlugin); | ||
|
|
||
| public override async Task AfterRecordingStopAsync(RecordingArgs e, CancellationToken cancellationToken) | ||
| { | ||
| Logger.LogTrace("{Method} called", nameof(AfterRecordingStopAsync)); | ||
|
|
||
| ArgumentNullException.ThrowIfNull(e); | ||
|
|
||
| if (!e.RequestLogs.Any()) | ||
| { | ||
| Logger.LogDebug("No requests to process"); | ||
| return; | ||
| } | ||
|
|
||
| Logger.LogInformation("Creating HAR file from recorded requests..."); | ||
|
|
||
| var harFile = new HarFile | ||
| { | ||
| Log = new HarLog | ||
| { | ||
| Creator = new HarCreator | ||
| { | ||
| Name = "DevProxy", | ||
| Version = ProxyUtils.ProductVersion | ||
| }, | ||
| Entries = [.. e.RequestLogs.Where(r => | ||
| r.MessageType == MessageType.InterceptedResponse && | ||
| r is not null && | ||
| r.Context is not null && | ||
| r.Context.Session is not null && | ||
| ProxyUtils.MatchesUrlToWatch(UrlsToWatch, r.Context.Session.HttpClient.Request.RequestUri.AbsoluteUri)).Select(CreateHarEntry)] | ||
| } | ||
| }; | ||
|
|
||
| Logger.LogDebug("Serializing HAR file..."); | ||
| var harFileJson = JsonSerializer.Serialize(harFile, ProxyUtils.JsonSerializerOptions); | ||
| var fileName = $"devproxy-{DateTime.Now:yyyyMMddHHmmss}.har"; | ||
|
|
||
| Logger.LogDebug("Writing HAR file to {FileName}...", fileName); | ||
| await File.WriteAllTextAsync(fileName, harFileJson, cancellationToken); | ||
|
|
||
| Logger.LogInformation("Created HAR file {FileName}", fileName); | ||
|
|
||
| StoreReport(fileName, e); | ||
|
|
||
| Logger.LogTrace("Left {Name}", nameof(AfterRecordingStopAsync)); | ||
| } | ||
|
|
||
| private string GetHeaderValue(string headerName, string originalValue) | ||
| { | ||
| if (!Configuration.IncludeSensitiveInformation && | ||
| Http.SensitiveHeaders.Contains(headerName, StringComparer.OrdinalIgnoreCase)) | ||
| { | ||
| return "REDACTED"; | ||
| } | ||
| return originalValue; | ||
| } | ||
|
|
||
| private HarEntry CreateHarEntry(RequestLog log) | ||
| { | ||
| Debug.Assert(log is not null); | ||
| Debug.Assert(log.Context is not null); | ||
|
|
||
| var request = log.Context.Session.HttpClient.Request; | ||
| var response = log.Context.Session.HttpClient.Response; | ||
| var currentTime = DateTime.UtcNow; | ||
|
|
||
| var entry = new HarEntry | ||
| { | ||
| StartedDateTime = currentTime.ToString("o"), | ||
| Time = 0, // We don't have actual timing data in RequestLog | ||
| Request = new HarRequest | ||
| { | ||
| Method = request.Method, | ||
| Url = request.RequestUri?.ToString(), | ||
| HttpVersion = $"HTTP/{request.HttpVersion}", | ||
| Headers = [.. request.Headers.Select(h => new HarHeader { Name = h.Name, Value = GetHeaderValue(h.Name, string.Join(", ", h.Value)) })], | ||
| QueryString = [.. HttpUtility.ParseQueryString(request.RequestUri?.Query ?? "") | ||
| .AllKeys | ||
| .Where(key => key is not null) | ||
| .Select(key => new HarQueryParam { Name = key!, Value = HttpUtility.ParseQueryString(request.RequestUri?.Query ?? "")[key] ?? "" })], | ||
| Cookies = [.. request.Headers | ||
| .Where(h => string.Equals(h.Name, "Cookie", StringComparison.OrdinalIgnoreCase)) | ||
| .Select(h => h.Value) | ||
| .SelectMany(v => v.Split(';')) | ||
| .Select(c => | ||
| { | ||
| var parts = c.Split('=', 2); | ||
| return new HarCookie { Name = parts[0].Trim(), Value = parts.Length > 1 ? parts[1].Trim() : "" }; | ||
| })], | ||
| HeadersSize = request.Headers?.ToString()?.Length ?? 0, | ||
| BodySize = request.HasBody ? (request.BodyString?.Length ?? 0) : 0, | ||
| PostData = request.HasBody ? new HarPostData | ||
| { | ||
| MimeType = request.ContentType, | ||
| Text = request.BodyString ?? "" | ||
| } | ||
| : null | ||
| }, | ||
| Response = response is not null ? new HarResponse | ||
| { | ||
| Status = response.StatusCode, | ||
| StatusText = response.StatusDescription, | ||
| HttpVersion = $"HTTP/{response.HttpVersion}", | ||
| Headers = [.. response.Headers.Select(h => new HarHeader { Name = h.Name, Value = GetHeaderValue(h.Name, string.Join(", ", h.Value)) })], | ||
waldekmastykarz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Cookies = [.. response.Headers | ||
| .Where(h => string.Equals(h.Name, "Set-Cookie", StringComparison.OrdinalIgnoreCase)) | ||
| .Select(h => h.Value) | ||
| .Select(sc => | ||
| { | ||
| var parts = sc.Split(';')[0].Split('=', 2); | ||
| return new HarCookie { Name = parts[0].Trim(), Value = parts.Length > 1 ? parts[1].Trim() : "" }; | ||
| })], | ||
| Content = new HarContent | ||
| { | ||
| Size = response.HasBody ? (response.BodyString?.Length ?? 0) : 0, | ||
| MimeType = response.ContentType ?? "", | ||
| Text = Configuration.IncludeResponse && response.HasBody ? response.BodyString : null | ||
| }, | ||
| HeadersSize = response.Headers?.ToString()?.Length ?? 0, | ||
| BodySize = response.HasBody ? (response.BodyString?.Length ?? 0) : 0 | ||
| } : null | ||
| }; | ||
|
|
||
| return entry; | ||
| } | ||
| } | ||
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
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
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,117 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace DevProxy.Plugins.Models; | ||
|
|
||
| internal sealed class HarFile | ||
| { | ||
| public HarLog? Log { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarLog | ||
| { | ||
| public string Version { get; set; } = "1.2"; | ||
| public HarCreator Creator { get; set; } = new(); | ||
| public List<HarEntry> Entries { get; set; } = []; | ||
| } | ||
|
|
||
| internal sealed class HarCreator | ||
| { | ||
| public string? Name { get; set; } | ||
| public string? Version { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarEntry | ||
| { | ||
| public string? StartedDateTime { get; set; } | ||
| public double Time { get; set; } | ||
| public HarRequest? Request { get; set; } | ||
| public HarResponse? Response { get; set; } | ||
| public HarCache Cache { get; set; } = new(); | ||
| public HarTimings Timings { get; set; } = new(); | ||
| } | ||
|
|
||
| internal sealed class HarRequest | ||
| { | ||
| public string? Method { get; set; } | ||
| public string? Url { get; set; } | ||
| public string? HttpVersion { get; set; } | ||
| public List<HarHeader> Headers { get; set; } = []; | ||
| public List<HarQueryParam> QueryString { get; set; } = []; | ||
| public List<HarCookie> Cookies { get; set; } = []; | ||
| public long HeadersSize { get; set; } | ||
| public long BodySize { get; set; } | ||
| public HarPostData? PostData { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarResponse | ||
| { | ||
| public int Status { get; set; } | ||
| public string? StatusText { get; set; } | ||
| public string? HttpVersion { get; set; } | ||
| public List<HarHeader> Headers { get; set; } = []; | ||
| public List<HarCookie> Cookies { get; set; } = []; | ||
| public HarContent Content { get; set; } = new(); | ||
| public string RedirectURL { get; set; } = ""; | ||
| public long HeadersSize { get; set; } | ||
| public long BodySize { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarHeader | ||
| { | ||
| public string? Name { get; set; } | ||
| public string? Value { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarQueryParam | ||
| { | ||
| public string? Name { get; set; } | ||
| public string? Value { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarCookie | ||
| { | ||
| public string? Name { get; set; } | ||
| public string? Value { get; set; } | ||
| public string? Path { get; set; } | ||
| public string? Domain { get; set; } | ||
| public string? Expires { get; set; } | ||
| public bool? HttpOnly { get; set; } | ||
| public bool? Secure { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarPostData | ||
| { | ||
| public string? MimeType { get; set; } | ||
| public string? Text { get; set; } | ||
| public List<HarParam>? Params { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarParam | ||
| { | ||
| public string? Name { get; set; } | ||
| public string? Value { get; set; } | ||
| public string? FileName { get; set; } | ||
| public string? ContentType { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarContent | ||
| { | ||
| public long Size { get; set; } | ||
| public string MimeType { get; set; } = ""; | ||
| public string? Text { get; set; } | ||
| public string? Encoding { get; set; } | ||
| } | ||
|
|
||
| internal sealed class HarCache | ||
| { | ||
| // Minimal - can be expanded if needed | ||
| } | ||
|
|
||
| internal sealed class HarTimings | ||
| { | ||
| public double Send { get; set; } | ||
| public double Wait { get; set; } | ||
| public double Receive { get; set; } | ||
| } |
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
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.