Skip to content
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

Support for hosting on linux #14

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net48'">
<PackageReference Include="Microsoft.Net.Http" Version="2.2.29" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Net.Http.WinHttpHandler">
<Version>7.0.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Net.Http.WinHttpHandler">
<Version>7.0.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="System.Net.Http.WinHttpHandler">
<Version>7.0.0</Version>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
using EasyMicroservices.Laboratory.Constants;
using EasyMicroservices.Laboratory.Engine;
using EasyMicroservices.Laboratory.Engine.Net;
using EasyMicroservices.Laboratory.Engine.Net.Http;
using EasyMicroservices.Laboratory.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace EasyMicroservice.Laboratory.Tests.Engine.Net
{
public abstract class BaseHandlerTest
{
public HttpClient GetHttpClient()
{
HttpClient httpClient = default;
// if (System.Environment.OSVersion.Platform != PlatformID.Unix)
// {
//#if (NET452)
// httpClient = new HttpClient();
//#else
// var handler = new WinHttpHandler();
// httpClient = new HttpClient(handler);
//#endif
// }
// else
httpClient = new HttpClient();

#if (!NET452 && !NET48)
httpClient.DefaultRequestVersion = HttpVersion.Version20;
#endif
return httpClient;
}
protected abstract BaseHandler GetHandler(ResourceManager resourceManager);
public string GetHttpResponseHeaders(string response)
{
Expand Down Expand Up @@ -49,7 +67,7 @@ public async Task CheckSimpleRequestAndResponse(string request, string response)
resourceManager.Append(request, GetHttpResponseHeaders(response));
var port = await GetHandler(resourceManager).Start();

HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();
var data = new StringContent(request);
var httpResponse = await httpClient.PostAsync($"http://localhost:{port}", data);
Assert.Equal(await httpResponse.Content.ReadAsStringAsync(), response);
Expand All @@ -72,7 +90,7 @@ public async Task ConcurrentCheckSimpleRequestAndResponse(string request, string
{
all.Add(Task.Run(async () =>
{
HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();
var data = new StringContent(request);
var httpResponse = await httpClient.PostAsync($"http://localhost:{port}", data);
Assert.Equal(await httpResponse.Content.ReadAsStringAsync(), response);
Expand All @@ -94,10 +112,10 @@ public async Task ConcurrentSingleHttpClientCheckSimpleRequestAndResponse(string
ResourceManager resourceManager = new ResourceManager();
resourceManager.Append(request, GetHttpResponseHeaders(response));
var port = await GetHandler(resourceManager).Start();
HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();

List<Task<bool>> all = new List<Task<bool>>();
for (int i = 0; i < 100; i++)
for (int i = 0; i < 20; i++)
{
all.Add(Task.Run(async () =>
{
Expand All @@ -120,7 +138,7 @@ public async Task CheckSimpleRequestToGiveMeFullRequestHeaderValue(string reques
ResourceManager resourceManager = new ResourceManager();
var port = await GetHandler(resourceManager).Start();
response = response.Replace("*MyPort*", port.ToString());
HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();
httpClient.DefaultRequestHeaders.ExpectContinue = false;
httpClient.DefaultRequestHeaders.Add(RequestTypeHeaderConstants.RequestTypeHeader, RequestTypeHeaderConstants.GiveMeFullRequestHeaderValue);
var data = new StringContent(request);
Expand All @@ -140,14 +158,14 @@ public async Task CheckSimpleRequestToGiveMeLastFullRequestHeaderValue(string re
ResourceManager resourceManager = new ResourceManager();
var port = await GetHandler(resourceManager).Start();
response = response.Replace("*MyPort*", port.ToString());
HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();
httpClient.DefaultRequestHeaders.ExpectContinue = false;
httpClient.DefaultRequestHeaders.Add(RequestTypeHeaderConstants.RequestTypeHeader, RequestTypeHeaderConstants.GiveMeFullRequestHeaderValue);
var data = new StringContent(request);
var httpResponse = await httpClient.PostAsync($"http://localhost:{port}", data);
var textResponse = await httpResponse.Content.ReadAsStringAsync();

httpClient = new HttpClient();
httpClient = GetHttpClient();
httpClient.DefaultRequestHeaders.Add(RequestTypeHeaderConstants.RequestTypeHeader, RequestTypeHeaderConstants.GiveMeLastFullRequestHeaderValue);
httpResponse = await httpClient.GetAsync($"http://localhost:{port}");
textResponse = await httpResponse.Content.ReadAsStringAsync();
Expand Down Expand Up @@ -185,7 +203,7 @@ public async Task CheckComplex(string request, string response, string simpleRes
ResourceManager resourceManager = new ResourceManager();
resourceManager.Append(request, response);
var port = await GetHandler(resourceManager).Start();
HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();
httpClient.DefaultRequestHeaders.Add("x-amz-meta-title", "someTitle");
httpClient.DefaultRequestHeaders.Add("User-Agent", "aws-sdk-dotnet-coreclr/3.7.101.44 aws-sdk-dotnet-core/3.7.103.6 .NET_Core/6.0.11 OS/Microsoft_Windows_10.0.22000 ClientAsync");
httpClient.DefaultRequestHeaders.Add("amz-sdk-invocation-id", "guid");
Expand Down Expand Up @@ -259,19 +277,19 @@ public async Task CheckScope()
client.DefaultRequestHeaders.Add("Authorization", "empty");
client.DefaultRequestHeaders.Add("Host", "s3.eu-west-1.amazonaws.com");
};
HttpClient httpClient = new HttpClient();
HttpClient httpClient = GetHttpClient();
addHeaders(httpClient);
var httpResponse = await httpClient.PutAsync($"http://localhost:{port}", null);
var textResponse = await httpResponse.Content.ReadAsStringAsync();
Assert.Equal("Ali", textResponse);

httpClient = new HttpClient();
httpClient = GetHttpClient();
addHeaders(httpClient);
httpResponse = await httpClient.PutAsync($"http://localhost:{port}", null);
textResponse = await httpResponse.Content.ReadAsStringAsync();
Assert.Equal("Reza", textResponse);

httpClient = new HttpClient();
httpClient = GetHttpClient();
addHeaders(httpClient);
httpResponse = await httpClient.PutAsync($"http://localhost:{port}", null);
textResponse = await httpResponse.Content.ReadAsStringAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if(NET6_0_OR_GREATER)
using EasyMicroservice.Laboratory.Tests.Engine.Net;
using EasyMicroservices.Laboratory.Engine;
using EasyMicroservices.Laboratory.Engine.Net;
using EasyMicroservices.Laboratory.Engine.Net.Http;

namespace EasyMicroservices.Laboratory.Tests.Engine.Net
{
public class HostHttpHandlerTest : BaseHandlerTest
{
protected override BaseHandler GetHandler(ResourceManager resourceManager)
{
return new HostHttpHandler(resourceManager);
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.14</Version>
<Version>0.0.0.15</Version>
<Description>Laboratory of http client.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>test,tests,http,https,httpclient,laboratory</PackageTags>
Expand All @@ -19,4 +19,20 @@
<ItemGroup>
<PackageReference Include="EasyMicroservices.Utilities" Version="0.0.0.11" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>7.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>7.0.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>7.0.1</Version>
</PackageReference>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
25 changes: 17 additions & 8 deletions src/CSharp/EasyMicroservices.Laboratory/Engine/Net/BaseHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using EasyMicroservices.Laboratory.Constants;
using EasyMicroservices.Laboratory.Engine.Net.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace EasyMicroservices.Laboratory.Engine.Net
Expand Down Expand Up @@ -55,5 +48,21 @@ public int GetRandomPort()
{
return _random.Next(1111, 9999);
}
/// <summary>
///
/// </summary>
/// <param name="resourceManager"></param>
/// <returns></returns>
public static BaseHandler CreateOSHandler(ResourceManager resourceManager)
{
#if (NET6_0_OR_GREATER)
if (System.Environment.OSVersion.Platform == PlatformID.Unix)
return new HostHttpHandler(resourceManager);
else
return new HttpHandler(resourceManager);
#else
throw new NotSupportedException("Only support on net6.0 or grater!");
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#if(NET6_0_OR_GREATER)
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace EasyMicroservices.Laboratory.Engine.Net.Http
{
/// <summary>
///
/// </summary>
public class HostHttpHandler : HostHttpHandlerBase
{
/// <summary>
///
/// </summary>
/// <param name="resourceManager"></param>
public HostHttpHandler(ResourceManager resourceManager) : base(resourceManager)
{

}

/// <summary>
///
/// </summary>
/// <param name="httpClient"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
protected override async Task HandleHttpClient(HttpContext httpClient)
{
var reader = new StreamReader(httpClient.Request.Body);
var requestBody = await reader.ReadToEndAsync();
var firstLine = $"{httpClient.Request.Method} {httpClient.Request.Path} {httpClient.Request.Protocol}";
var headers = httpClient.Request.Headers.ToDictionary((x) => x.Key, (v) => v.Value.FirstOrDefault());
StringBuilder fullBody = new StringBuilder();
fullBody.AppendLine(firstLine);
foreach (var item in headers.OrderBy(x => x.Key))
{
fullBody.AppendLine($"{item.Key}: {item.Value}");
}
fullBody.Append(requestBody);
var responseBody = await WriteResponseAsync(firstLine, headers, requestBody, fullBody);
using (var responseReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(responseBody))))
{
await responseReader.ReadLineAsync();
do
{
var line = await responseReader.ReadLineAsync();
if (string.IsNullOrEmpty(line))
break;
var header = line.Split(':');
if (header[0].Equals("content-length", StringComparison.OrdinalIgnoreCase))
continue;
httpClient.Response.Headers.Add(header[0], header[1]);
}
while (true);
var body = await responseReader.ReadToEndAsync();
var bytes = Encoding.UTF8.GetBytes(body);
httpClient.Response.ContentLength = bytes.Length;

await httpClient.Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
}
}
}
#endif
Loading
Loading