-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dda79b7
commit c0998ee
Showing
9 changed files
with
380 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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
17 changes: 17 additions & 0 deletions
17
src/CSharp/EasyMicroservices.Laboratory.Tests/Engine/Net/HostHttpHandlerTest.cs
This file contains 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,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 |
This file contains 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 |
---|---|---|
|
@@ -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> | ||
|
@@ -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> |
This file contains 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
69 changes: 69 additions & 0 deletions
69
src/CSharp/EasyMicroservices.Laboratory/Engine/Net/Http/HostHttpHandler.cs
This file contains 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,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 |
Oops, something went wrong.