-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathProxyEvents.cs
More file actions
113 lines (96 loc) · 4.06 KB
/
Copy pathProxyEvents.cs
File metadata and controls
113 lines (96 loc) · 4.06 KB
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
// 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.Utils;
using System.CommandLine;
using System.Text.Json.Serialization;
using Titanium.Web.Proxy.EventArguments;
namespace DevProxy.Abstractions.Proxy;
public class ProxyEventArgsBase
{
public Dictionary<string, object> SessionData { get; init; } = [];
public Dictionary<string, object> GlobalData { get; init; } = [];
}
public class ProxyHttpEventArgsBase(SessionEventArgs session) : ProxyEventArgsBase
{
public SessionEventArgs Session { get; } = session ??
throw new ArgumentNullException(nameof(session));
public bool HasRequestUrlMatch(ISet<UrlToWatch> watchedUrls) =>
ProxyUtils.MatchesUrlToWatch(watchedUrls, Session.HttpClient.Request.RequestUri.AbsoluteUri);
}
public class ProxyRequestArgs(SessionEventArgs session, ResponseState responseState) :
ProxyHttpEventArgsBase(session)
{
public ResponseState ResponseState { get; } = responseState ??
throw new ArgumentNullException(nameof(responseState));
public bool ShouldExecute(ISet<UrlToWatch> watchedUrls) =>
!ResponseState.HasBeenSet
&& HasRequestUrlMatch(watchedUrls);
}
public class ProxyResponseArgs(SessionEventArgs session, ResponseState responseState) :
ProxyHttpEventArgsBase(session)
{
public ResponseState ResponseState { get; } = responseState ??
throw new ArgumentNullException(nameof(responseState));
}
public class InitArgs
{
public required IServiceProvider ServiceProvider { get; init; }
/// <summary>
/// Indicates whether the proxy command (root command) is being invoked.
/// When false, a CLI subcommand is being executed and plugins should
/// skip initialization that is only relevant for proxy operation
/// (e.g., opening browsers, starting listeners).
/// </summary>
public bool IsProxyCommand { get; init; } = true;
}
public class OptionsLoadedArgs(ParseResult parseResult)
{
public ParseResult ParseResult { get; set; } = parseResult ??
throw new ArgumentNullException(nameof(parseResult));
}
public class RequestLog
{
[JsonIgnore]
public LoggingContext? Context { get; set; }
public string Message { get; set; }
public MessageType MessageType { get; set; }
public string? Method { get; init; }
public string? PluginName { get; set; }
public DateTimeOffset Timestamp { get; init; } = DateTimeOffset.UtcNow;
public string? Url { get; init; }
public RequestLog(string message, MessageType messageType, LoggingContext? context) :
this(message, messageType, context?.Session.HttpClient.Request.Method, context?.Session.HttpClient.Request.Url, context)
{
}
public RequestLog(string message, MessageType messageType, string method, string url) :
this(message, messageType, method, url, context: null)
{
}
private RequestLog(string message, MessageType messageType, string? method, string? url, LoggingContext? context)
{
Message = message ?? throw new ArgumentNullException(nameof(message));
MessageType = messageType;
Context = context;
Method = method;
Url = url;
}
public void Deconstruct(out string message, out MessageType messageType, out LoggingContext? context, out string? method, out string? url)
{
message = Message;
messageType = MessageType;
context = Context;
method = Method;
url = Url;
}
}
public class RecordingArgs(IEnumerable<RequestLog> requestLogs) : ProxyEventArgsBase
{
public IEnumerable<RequestLog> RequestLogs { get; set; } = requestLogs ??
throw new ArgumentNullException(nameof(requestLogs));
}
public class RequestLogArgs(RequestLog requestLog)
{
public RequestLog RequestLog { get; set; } = requestLog ??
throw new ArgumentNullException(nameof(requestLog));
}