forked from jiangjinnan/InsideAspNet6
-
Notifications
You must be signed in to change notification settings - Fork 0
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
69d286e
commit c29d14e
Showing
17 changed files
with
175 additions
and
145 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,10 @@ | ||
using App; | ||
using HttpMachine; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using System.Buffers; | ||
using System.IO.Pipelines; | ||
using System.Net; | ||
using System.Text; | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.WebHost | ||
.UseKestrel(kestrel => kestrel.ListenLocalhost(8000)) | ||
.UseUrls("http://localhost:9000"); | ||
var app = builder.Build(); | ||
app.Run(); | ||
|
||
var factory = WebApplication.Create().Services.GetRequiredService<IConnectionListenerFactory>(); | ||
var listener = await factory.BindAsync(new IPEndPoint(IPAddress.Any, 5000)); | ||
while (true) | ||
{ | ||
var context = await listener.AcceptAsync(); | ||
_ = HandleAsync(context!); | ||
|
||
static async Task HandleAsync(ConnectionContext connection) | ||
{ | ||
var reader = connection!.Transport.Input; | ||
while (true) | ||
{ | ||
var result = await reader.ReadAsync(); | ||
var request = ParseRequest(result); | ||
reader.AdvanceTo(result.Buffer.End); | ||
Console.WriteLine("[{0}]Receive request: {1} {2} Connection:{3}", connection.ConnectionId, request.Method, request.Path, request.Headers?["Connection"] ?? "N/A"); | ||
|
||
var response = @"HTTP/1.1 200 OK | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Length: 12 | ||
|
||
Hello World!"; | ||
await connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes(response)); | ||
if (request.Headers!.TryGetValue("Connection", out var value) && string.Compare(value, "close", true) == 0) | ||
{ | ||
await connection.DisposeAsync(); | ||
return; | ||
} | ||
if (result.IsCompleted) | ||
{ | ||
break; | ||
} | ||
} | ||
await reader.CompleteAsync(); | ||
} | ||
|
||
static HttpRequestFeature ParseRequest(ReadResult result) | ||
{ | ||
var handler = new HttpParserHandler(); | ||
var parserHandler = new HttpParser(handler); | ||
parserHandler.Execute(new ArraySegment<byte>(result.Buffer.ToArray())); | ||
return handler.Request; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,4 +10,5 @@ | |
<ItemGroup> | ||
<PackageReference Include="HttpMachine" Version="0.9.0" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,53 @@ | ||
using App; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
var builder = WebApplication.CreateBuilder(); | ||
builder.WebHost.UseKestrel(kestrel => kestrel.ListenLocalhost(5000)); | ||
builder.Services.Replace(ServiceDescriptor.Singleton<IServer, MiniKestrelServer>()); | ||
var app = builder.Build(); | ||
app.Run(context => context.Response.WriteAsync("Hello World!")); | ||
app.Run(); | ||
using HttpMachine; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using System.Buffers; | ||
using System.IO.Pipelines; | ||
using System.Net; | ||
using System.Text; | ||
|
||
var factory = WebApplication.Create().Services.GetRequiredService<IConnectionListenerFactory>(); | ||
var listener = await factory.BindAsync(new IPEndPoint(IPAddress.Any, 5000)); | ||
while (true) | ||
{ | ||
var context = await listener.AcceptAsync(); | ||
_ = HandleAsync(context!); | ||
|
||
static async Task HandleAsync(ConnectionContext connection) | ||
{ | ||
var reader = connection!.Transport.Input; | ||
while (true) | ||
{ | ||
var result = await reader.ReadAsync(); | ||
var request = ParseRequest(result); | ||
reader.AdvanceTo(result.Buffer.End); | ||
Console.WriteLine("[{0}]Receive request: {1} {2} Connection:{3}", connection.ConnectionId, request.Method, request.Path, request.Headers?["Connection"] ?? "N/A"); | ||
|
||
var response = @"HTTP/1.1 200 OK | ||
Content-Type: text/plain; charset=utf-8 | ||
Content-Length: 12 | ||
Hello World!"; | ||
await connection.Transport.Output.WriteAsync(Encoding.UTF8.GetBytes(response)); | ||
if (request.Headers!.TryGetValue("Connection", out var value) && string.Compare(value, "close", true) == 0) | ||
{ | ||
await connection.DisposeAsync(); | ||
return; | ||
} | ||
if (result.IsCompleted) | ||
{ | ||
break; | ||
} | ||
} | ||
await reader.CompleteAsync(); | ||
} | ||
|
||
static HttpRequestFeature ParseRequest(ReadResult result) | ||
{ | ||
var handler = new HttpParserHandler(); | ||
var parserHandler = new HttpParser(handler); | ||
parserHandler.Execute(new ArraySegment<byte>(result.Buffer.ToArray())); | ||
return handler.Request; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> | ||
|
||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="HttpMachine" Version="0.9.0" /> | ||
</ItemGroup> | ||
</Project> |
File renamed without changes.
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,22 @@ | ||
using HttpMachine; | ||
using Microsoft.AspNetCore.Http.Features; | ||
|
||
namespace App | ||
{ | ||
public class HttpParserHandler : IHttpParserHandler | ||
{ | ||
public HttpRequestFeature Request { get; } = new HttpRequestFeature(); | ||
|
||
private string? headerName = null; | ||
public void OnBody(HttpParser parser, ArraySegment<byte> data) => Request.Body = new MemoryStream(data.Array!, data.Offset, data.Count); | ||
public void OnFragment(HttpParser parser, string fragment) { } | ||
public void OnHeaderName(HttpParser parser, string name) => headerName = name; | ||
public void OnHeadersEnd(HttpParser parser) { } | ||
public void OnHeaderValue(HttpParser parser, string value) => Request.Headers[headerName!] = value; | ||
public void OnMessageBegin(HttpParser parser) { } | ||
public void OnMessageEnd(HttpParser parser) { } | ||
public void OnMethod(HttpParser parser, string method) => Request.Method = method; | ||
public void OnQueryString(HttpParser parser, string queryString) => Request.QueryString = queryString; | ||
public void OnRequestUri(HttpParser parser, string requestUri) => Request.Path = requestUri; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,33 +1,10 @@ | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
var app = WebApplication.Create(args); | ||
app.Run(HandleAsync); | ||
app.Run(); | ||
|
||
static Task HandleAsync(HttpContext httpContext) | ||
{ | ||
var request = httpContext.Request; | ||
var configuration = httpContext.RequestServices.GetRequiredService<IConfiguration>(); | ||
var builder = new StringBuilder(); | ||
builder.AppendLine($"Process: {Process.GetCurrentProcess().ProcessName}"); | ||
builder.AppendLine($"MS-ASPNETCORE-TOKEN: {request.Headers["MS-ASPNETCORE-TOKEN"]}"); | ||
builder.AppendLine($"PathBase: {request.PathBase}"); | ||
builder.AppendLine("Environment Variables"); | ||
foreach (string key in Environment.GetEnvironmentVariables().Keys) | ||
{ | ||
if (key.StartsWith("ASPNETCORE_")) | ||
{ | ||
builder.AppendLine($"\t{key}={Environment.GetEnvironmentVariable(key)}"); | ||
} | ||
} | ||
return httpContext.Response.WriteAsync(builder.ToString()); | ||
} | ||
|
||
|
||
|
||
|
||
// | ||
// Summary: | ||
// The minimum data rate for incoming connections. | ||
using App; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
var builder = WebApplication.CreateBuilder(); | ||
builder.WebHost.UseKestrel(kestrel => kestrel.ListenLocalhost(5000)); | ||
builder.Services.Replace(ServiceDescriptor.Singleton<IServer, MiniKestrelServer>()); | ||
var app = builder.Build(); | ||
app.Run(context => context.Response.WriteAsync("Hello World!")); | ||
app.Run(); |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
</PropertyGroup> | ||
|
||
</Project> |
File renamed without changes.
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,25 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
var app = WebApplication.Create(args); | ||
app.Run(HandleAsync); | ||
app.Run(); | ||
|
||
static Task HandleAsync(HttpContext httpContext) | ||
{ | ||
var request = httpContext.Request; | ||
var configuration = httpContext.RequestServices.GetRequiredService<IConfiguration>(); | ||
var builder = new StringBuilder(); | ||
builder.AppendLine($"Process: {Process.GetCurrentProcess().ProcessName}"); | ||
builder.AppendLine($"MS-ASPNETCORE-TOKEN: {request.Headers["MS-ASPNETCORE-TOKEN"]}"); | ||
builder.AppendLine($"PathBase: {request.PathBase}"); | ||
builder.AppendLine("Environment Variables"); | ||
foreach (string key in Environment.GetEnvironmentVariables().Keys) | ||
{ | ||
if (key.StartsWith("ASPNETCORE_")) | ||
{ | ||
builder.AppendLine($"\t{key}={Environment.GetEnvironmentVariable(key)}"); | ||
} | ||
} | ||
return httpContext.Response.WriteAsync(builder.ToString()); | ||
} |
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