-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
be19e1c
commit 8a0eadd
Showing
13 changed files
with
276 additions
and
52 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
44 changes: 44 additions & 0 deletions
44
src/libraries/Common/tests/System/Net/Prerequisites/MonoNetTestServer/GenericHandler.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,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
|
||
namespace MonoNetTestServer | ||
{ | ||
public class GenericHandler | ||
{ | ||
RequestDelegate next; | ||
public GenericHandler(RequestDelegate next) | ||
{ | ||
this.next = next; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
PathString path = context.Request.Path; | ||
if (path.Equals(new PathString("/remoteLoop"))) | ||
{ | ||
await RemoteLoopHandler.InvokeAsync(context); | ||
return; | ||
} | ||
|
||
await next(context); | ||
} | ||
} | ||
|
||
public static class GenericHandlerExtensions | ||
{ | ||
public static IApplicationBuilder UseGenericHandler(this IApplicationBuilder builder) | ||
{ | ||
return builder.UseMiddleware<GenericHandler>(); | ||
} | ||
|
||
public static void SetStatusDescription(this HttpResponse response, string description) | ||
{ | ||
response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = description; | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...ies/Common/tests/System/Net/Prerequisites/MonoNetTestServer/Handlers/RemoteLoopHandler.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,93 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MonoNetTestServer | ||
{ | ||
public class RemoteLoopHandler | ||
{ | ||
private const int MaxBufferSize = 128 * 1024; | ||
|
||
public static async Task InvokeAsync(HttpContext context) | ||
{ | ||
try | ||
{ | ||
if (!context.WebSockets.IsWebSocketRequest) | ||
{ | ||
context.Response.StatusCode = 400; | ||
context.Response.ContentType = "text/plain"; | ||
await context.Response.WriteAsync("Not a websocket request"); | ||
|
||
return; | ||
} | ||
|
||
using (WebSocket socket = await context.WebSockets.AcceptWebSocketAsync()) | ||
{ | ||
await ProcessWebSocketRequest(context, socket); | ||
} | ||
|
||
} | ||
catch (Exception) | ||
{ | ||
// We might want to log these exceptions. But for now we ignore them. | ||
} | ||
} | ||
|
||
enum CommandType | ||
{ | ||
Listen, | ||
Open, | ||
Send, | ||
Receive, | ||
Close | ||
} | ||
|
||
class Command | ||
{ | ||
public CommandType Type { get; set; } | ||
public byte[] Data { get; set; } | ||
public int Port { get; set; } | ||
public int ListenBacklog { get; set; } | ||
public IPAddress Address { get; set; } | ||
} | ||
|
||
private static async Task ProcessWebSocketRequest(HttpContext context, WebSocket webSocket) | ||
{ | ||
Socket listenSocket = null; | ||
Socket current = null; | ||
Memory<byte> ms = new Memory<byte>(); | ||
|
||
ValueWebSocketReceiveResult result = await webSocket.ReceiveAsync(ms, CancellationToken.None); | ||
while (result.MessageType != WebSocketMessageType.Close) | ||
{ | ||
Command command = null; // get bytes=> json=>command | ||
switch (command.Type) | ||
{ | ||
case CommandType.Listen: | ||
listenSocket = new Socket(command.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | ||
listenSocket.Bind(new IPEndPoint(command.Address, 0)); | ||
listenSocket.Listen(command.ListenBacklog); | ||
break; | ||
case CommandType.Open: | ||
current = await listenSocket.AcceptAsync().ConfigureAwait(false); | ||
break; | ||
case CommandType.Close: | ||
current.Close(); | ||
break; | ||
} | ||
|
||
result = await webSocket.ReceiveAsync(ms, CancellationToken.None); | ||
} | ||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "closing remoteLoop", CancellationToken.None); | ||
|
||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ibraries/Common/tests/System/Net/Prerequisites/MonoNetTestServer/MonoNetTestServer.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> | ||
<OutputType>Exe</OutputType> | ||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Handlers\RemoteLoopHandler.cs" /> | ||
<Compile Include="GenericHandler.cs" /> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Startup.cs" /> | ||
</ItemGroup> | ||
</Project> |
23 changes: 23 additions & 0 deletions
23
src/libraries/Common/tests/System/Net/Prerequisites/MonoNetTestServer/Program.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,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace MonoNetTestServer | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/libraries/Common/tests/System/Net/Prerequisites/MonoNetTestServer/Startup.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,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace MonoNetTestServer | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
app.UseWebSockets(); | ||
app.UseGenericHandler(); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ries/Common/tests/System/Net/Prerequisites/MonoNetTestServer/appsettings.Development.json
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/libraries/Common/tests/System/Net/Prerequisites/MonoNetTestServer/appsettings.json
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
25 changes: 0 additions & 25 deletions
25
src/libraries/Common/tests/System/Net/Prerequisites/NetCoreServer.sln
This file was deleted.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
src/libraries/Common/tests/System/Net/Prerequisites/NetCoreServer/NetCoreServer.csproj
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
30 changes: 30 additions & 0 deletions
30
src/libraries/Common/tests/System/Net/Prerequisites/NetTestServers.sln
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,30 @@ | ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.31229.75 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoNetTestServer", "MonoNetTestServer\MonoNetTestServer.csproj", "{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreServer", "NetCoreServer\NetCoreServer.csproj", "{2BB687CC-3F0C-43A9-8F38-140E91892EB0}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{86E9A13D-9F4A-45DE-B0BB-CBB6A6533868}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{2BB687CC-3F0C-43A9-8F38-140E91892EB0}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {2F9A0637-452E-4FB9-9403-CB52944982DA} | ||
EndGlobalSection | ||
EndGlobal |
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