Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Http2Client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,44 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{4A
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F245E407-5E9D-427E-98C8-D79116882421}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2Client.Console", "examples\Http2Client.Console\Http2Client.Console.csproj", "{16B97360-39EA-4A84-B3A0-D028DB8C8635}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2Client.Console", "examples\Http2Client.Console.csproj", "{16B97360-39EA-4A84-B3A0-D028DB8C8635}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2Client", "src\Http2Client\Http2Client.csproj", "{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2Client", "src\Http2Client.csproj", "{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2Client.Test", "tests\Http2Client.Test\Http2Client.Test.csproj", "{00D83329-045D-4431-819E-C3A45034A4EF}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2Client.Test", "tests\Http2Client.Test.csproj", "{00D83329-045D-4431-819E-C3A45034A4EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Debug|x64.ActiveCfg = Debug|Any CPU
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Debug|x64.Build.0 = Debug|Any CPU
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Release|Any CPU.Build.0 = Release|Any CPU
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Release|x64.ActiveCfg = Release|x64
{16B97360-39EA-4A84-B3A0-D028DB8C8635}.Release|x64.Build.0 = Release|x64
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Debug|x64.ActiveCfg = Debug|x64
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Debug|x64.Build.0 = Debug|x64
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Release|Any CPU.Build.0 = Release|Any CPU
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Release|x64.ActiveCfg = Release|x64
{5D33478C-EF54-4CFE-BC78-7CB6D40D0DCB}.Release|x64.Build.0 = Release|x64
{00D83329-045D-4431-819E-C3A45034A4EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Debug|x64.ActiveCfg = Debug|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Debug|x64.Build.0 = Debug|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Release|Any CPU.Build.0 = Release|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Release|x64.ActiveCfg = Release|Any CPU
{00D83329-045D-4431-819E-C3A45034A4EF}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
21 changes: 21 additions & 0 deletions examples/Http2Client.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>Latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Http2Client.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Native\tls-client-windows-64-1.11.0.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
15 changes: 0 additions & 15 deletions examples/Http2Client.Console/Http2Client.Console.csproj

This file was deleted.

2 changes: 0 additions & 2 deletions examples/Http2Client.Console/Program.cs

This file was deleted.

Binary file added examples/Native/tls-client-windows-64-1.11.0.dll
Binary file not shown.
39 changes: 39 additions & 0 deletions examples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Http2Client.Builders;
using Http2Client.Core.Enums;
using Http2Client.Core.Request;

using System;

internal class Program
{
private const string PATH_LIB = "Native\\tls-client-windows-64-1.11.0.dll";

private static void Main(string[] args)
{
using var client = CreateClientBuilder().Build();

var request = new HttpRequest()
{
RequestUrl = "https://tls.peet.ws/api/all",
RequestMethod = "GET",
BrowserType = BrowserType.Chrome133,
};

var response = client.Send(request);

Console.WriteLine(response.Status);
Console.WriteLine(response.Body);
}

private static HttpClientBuilder CreateClientBuilder()
{
//const string Proxy = "https://localhost:1777";
const string UserAgent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36";

return new HttpClientBuilder()
.WithUserAgent(UserAgent)
.WithCookies()
.WithLibraryPath(PATH_LIB)
.WithRandomTlsExtensions();
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>netstandard2.0;net5.0;net6.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
Expand Down
103 changes: 0 additions & 103 deletions src/Http2Client/Native/NativeLoader.cs

This file was deleted.

File renamed without changes.
128 changes: 128 additions & 0 deletions src/Native/NativeLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using Http2Client.Utilities;

using System;
using System.Runtime.InteropServices;

namespace Http2Client.Native;

/// <summary>
/// Cross-platform native library loader. Handles OS-specific loading of .dll/.so/.dylib files.
/// </summary>
internal static class NativeLoader
{
/// <summary>
/// Loads native library and returns handle for function calls.
/// </summary>
public static IntPtr LoadLibrary(string path)
{
if (PlatformSupport.IsLinux) return Linux.LoadLibrary(path);
if (PlatformSupport.IsMacOS) return MacOS.LoadLibrary(path);
if (PlatformSupport.IsWindows) return Windows.LoadLibrary(path);

throw new PlatformNotSupportedException("Don't know how to load libraries on this platform");
}

/// <summary>
/// Finds function in loaded library by name.
/// </summary>
/// <param name="handle">Library handle</param>
/// <param name="name">Function name</param>
public static IntPtr GetProcAddress(nint handle, string name)
{
if (PlatformSupport.IsLinux) return Linux.GetProcAddress(handle, name);
if (PlatformSupport.IsMacOS) return MacOS.GetProcAddress(handle, name);
if (PlatformSupport.IsWindows) return Windows.GetProcAddress(handle, name);

throw new PlatformNotSupportedException("Don't know how to find functions on this platform");
}

/// <summary>
/// Unloads library and frees memory.
/// </summary>
/// <param name="handle">Library handle</param>
public static bool FreeLibrary(nint handle)
{
// Linux and macOS return 0 on success, non-zero on failure
if (PlatformSupport.IsLinux) return Linux.FreeLibrary(handle) == 0;
if (PlatformSupport.IsMacOS) return MacOS.FreeLibrary(handle) == 0;
if (PlatformSupport.IsWindows) return Windows.FreeLibrary(handle);

throw new PlatformNotSupportedException("Don't know how to unload libraries on this platform");
}

/// <summary>
/// Windows native library functions.
/// </summary>
private static class Windows
{
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, EntryPoint = "LoadLibraryW")]
public static extern IntPtr LoadLibrary([In][MarshalAs(UnmanagedType.LPWStr)] string path);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetProcAddress([In] IntPtr hModule, [In] string procName);

[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "FreeLibrary")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeLibrary([In] IntPtr hLibrary);
}

/// <summary>
/// Linux dynamic loading functions.
/// </summary>
private static class Linux
{
[Flags]
public enum LoadLibraryFlags
{
None = 0,
Lazy = 0x0001,
Now = 0x0002,
BindingMask = 0x0003,
NoLoad = 0x0004,
DeepBind = 0x0008,
Local = None,
Global = 0x0100,
NoDelete = 0x1000
}

[DllImport("libdl.so.2", EntryPoint = "dlopen")]
public static extern IntPtr LoadLibrary(
[In][MarshalAs(UnmanagedType.LPStr)] string path,
[In]LoadLibraryFlags flags = LoadLibraryFlags.Now | LoadLibraryFlags.Global);

[DllImport("libdl.so.2", EntryPoint = "dlclose")]
public static extern int FreeLibrary([In] IntPtr hLibrary);

[DllImport("libdl.so.2", EntryPoint = "dlsym")]
public static extern IntPtr GetProcAddress([In] IntPtr handle, [In] string symbol);
}

/// <summary>
/// macOS dynamic loading functions.
/// </summary>
private static class MacOS
{
[Flags]
public enum LoadLibraryFlags
{
None = 0,
Lazy = 0x01,
Now = 0x02,
Local = 0x04,
Global = 0x08,
NoLoad = 0x10,
NoDelete = 0x80
}

[DllImport("libdl.dylib", EntryPoint = "dlopen")]
public static extern IntPtr LoadLibrary(
[In][MarshalAs(UnmanagedType.LPStr)] string path,
[In]LoadLibraryFlags flags = LoadLibraryFlags.Now | LoadLibraryFlags.Global);

[DllImport("libdl.dylib", EntryPoint = "dlclose")]
public static extern int FreeLibrary([In] IntPtr hLibrary);

[DllImport("libdl.dylib", EntryPoint = "dlsym")]
public static extern IntPtr GetProcAddress([In] IntPtr handle, [In] string symbol);
}
}
Loading