@@ -9,47 +9,49 @@ namespace Aikido.Zen.Core.Patches
99{
1010 internal static class HttpClientPatches
1111 {
12+ /// <summary>
13+ /// Applies patches to HttpClient methods using Harmony and reflection.
14+ /// </summary>
15+ /// <param name="harmony">The Harmony instance used for patching.</param>
1216 public static void ApplyPatches ( Harmony harmony )
1317 {
14- var asyncMethod = AccessTools . Method ( typeof ( HttpClient ) , "SendAsync" , new [ ] {
15- typeof ( HttpRequestMessage ) ,
16- typeof ( HttpCompletionOption ) ,
17- typeof ( CancellationToken )
18- } ) ;
19- var syncMethod = AccessTools . Method ( typeof ( HttpClient ) , "Send" , new [ ] {
20- typeof ( HttpRequestMessage ) ,
21- typeof ( CancellationToken )
22- } ) ;
23- try
24- {
25- if ( asyncMethod != null && ! asyncMethod . IsAbstract )
26- {
27- var patchMethod = new HarmonyMethod ( typeof ( HttpClientPatches ) . GetMethod ( nameof ( CaptureRequest ) , BindingFlags . Static | BindingFlags . NonPublic ) ) ;
28- harmony . Patch ( asyncMethod , patchMethod ) ;
29- }
18+ // Use reflection to get the methods dynamically
19+ PatchMethod ( harmony , "System.Net.Http" , "HttpClient" , "SendAsync" , "System.Net.Http.HttpRequestMessage" , "System.Net.Http.HttpCompletionOption" , "System.Threading.CancellationToken" ) ;
20+ PatchMethod ( harmony , "System.Net.Http" , "HttpClient" , "Send" , "System.Net.Http.HttpRequestMessage" , "System.Threading.CancellationToken" ) ;
21+ }
3022
31- if ( syncMethod != null && ! syncMethod . IsAbstract )
32- {
33- harmony . Patch ( syncMethod , new HarmonyMethod ( typeof ( HttpClientPatches ) . GetMethod ( nameof ( CaptureRequest ) , BindingFlags . Static | BindingFlags . NonPublic ) ) ) ;
34- }
35- }
36- catch ( Exception )
23+ /// <summary>
24+ /// Patches a method using Harmony by dynamically retrieving it via reflection.
25+ /// </summary>
26+ /// <param name="harmony">The Harmony instance used for patching.</param>
27+ /// <param name="assemblyName">The name of the assembly containing the type.</param>
28+ /// <param name="typeName">The name of the type containing the method.</param>
29+ /// <param name="methodName">The name of the method to patch.</param>
30+ /// <param name="parameterTypeNames">The names of the parameter types for the method.</param>
31+ private static void PatchMethod ( Harmony harmony , string assemblyName , string typeName , string methodName , params string [ ] parameterTypeNames )
32+ {
33+ var method = ReflectionHelper . GetMethodFromAssembly ( assemblyName , typeName , methodName , parameterTypeNames ) ;
34+ if ( method != null && ! method . IsAbstract )
3735 {
38- // continue
36+ var patchMethod = new HarmonyMethod ( typeof ( HttpClientPatches ) . GetMethod ( nameof ( CaptureRequest ) , BindingFlags . Static | BindingFlags . NonPublic ) ) ;
37+ harmony . Patch ( method , patchMethod ) ;
3938 }
40-
4139 }
4240
43- internal static bool CaptureRequest (
44- HttpRequestMessage request ,
45- HttpClient __instance )
41+ /// <summary>
42+ /// Callback method executed before the original HttpClient method is executed.
43+ /// </summary>
44+ /// <param name="request">The HttpRequestMessage being sent.</param>
45+ /// <param name="__instance">The instance of HttpClient being used.</param>
46+ /// <returns>True if the original method should continue execution; otherwise, false.</returns>
47+ internal static bool CaptureRequest ( HttpRequestMessage request , HttpClient __instance )
4648 {
4749 var uri = __instance . BaseAddress == null
4850 ? request . RequestUri
4951 : request . RequestUri == null
5052 ? __instance . BaseAddress
5153 : new Uri ( __instance . BaseAddress , request . RequestUri ) ;
52-
54+
5355 var ( hostname , port ) = UriHelper . ExtractHost ( uri ) ;
5456 if ( hostname . EndsWith ( "aikido.dev" ) )
5557 return true ;
0 commit comments