Skip to content

Commit

Permalink
Add missing samples for chapter 18
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangjinnan committed Mar 27, 2022
1 parent 69d286e commit c29d14e
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 145 deletions.
4 changes: 0 additions & 4 deletions 18/S1802/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HttpMachine" Version="0.9.0" />
</ItemGroup>

</Project>
22 changes: 0 additions & 22 deletions 18/S1802/HttpParserHandler.cs

This file was deleted.

55 changes: 6 additions & 49 deletions 18/S1802/Program.cs
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;
}
}
1 change: 1 addition & 0 deletions 18/S1803/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<ItemGroup>
<PackageReference Include="HttpMachine" Version="0.9.0" />
</ItemGroup>

</Project>
61 changes: 52 additions & 9 deletions 18/S1803/Program.cs
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;
}
}
17 changes: 9 additions & 8 deletions 18/S1804/App.csproj
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.
22 changes: 22 additions & 0 deletions 18/S1804/HttpParserHandler.cs
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.
43 changes: 10 additions & 33 deletions 18/S1804/Program.cs
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.
2 changes: 2 additions & 0 deletions 18/S1805/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>

</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions 18/S1806/App.csproj
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.
25 changes: 25 additions & 0 deletions 18/S1806/Program.cs
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());
}
58 changes: 38 additions & 20 deletions 18/app.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1801\App\App.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "02", "02", "{7A396472-0AD9-45F8-A9FA-8822B388C1B8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1802\App.csproj", "{3DC6FF7F-0947-4FC0-A086-1BC729892C57}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03", "03", "{766E89F3-040A-4F01-8DB0-81B4FFA6DA41}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1803\App.csproj", "{16E2A3BF-02D6-48CD-AD75-C2EFE66A94DF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04", "04", "{F0B75382-6AD2-451E-928E-4A588386B9A3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1804\App.csproj", "{243DF55D-2E11-481F-AA7A-141C2A75792D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1802\App.csproj", "{B301EF61-F0FC-4365-8BB1-795D44DAA596}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1803\App.csproj", "{7FEC7E96-490F-437A-842B-8FD5C8E1ACD0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1804\App.csproj", "{32FCB6EE-F393-4200-AB7E-53AC505E4045}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "05", "05", "{833C70FB-5505-4028-8AA7-746387DF6057}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1805\App.csproj", "{4CEA307B-C6E9-482D-9552-FD887B06A870}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "06", "06", "{D23C6AF0-1B3F-47D2-A4C1-663C69C1B50E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "S1806\App.csproj", "{F6052059-ECC2-4371-88ED-CAFACBF9FDAE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -29,27 +37,37 @@ Global
{D2C80B62-059F-4289-912B-2AC841C2E8C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2C80B62-059F-4289-912B-2AC841C2E8C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2C80B62-059F-4289-912B-2AC841C2E8C4}.Release|Any CPU.Build.0 = Release|Any CPU
{3DC6FF7F-0947-4FC0-A086-1BC729892C57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DC6FF7F-0947-4FC0-A086-1BC729892C57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DC6FF7F-0947-4FC0-A086-1BC729892C57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DC6FF7F-0947-4FC0-A086-1BC729892C57}.Release|Any CPU.Build.0 = Release|Any CPU
{16E2A3BF-02D6-48CD-AD75-C2EFE66A94DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16E2A3BF-02D6-48CD-AD75-C2EFE66A94DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16E2A3BF-02D6-48CD-AD75-C2EFE66A94DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16E2A3BF-02D6-48CD-AD75-C2EFE66A94DF}.Release|Any CPU.Build.0 = Release|Any CPU
{243DF55D-2E11-481F-AA7A-141C2A75792D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{243DF55D-2E11-481F-AA7A-141C2A75792D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{243DF55D-2E11-481F-AA7A-141C2A75792D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{243DF55D-2E11-481F-AA7A-141C2A75792D}.Release|Any CPU.Build.0 = Release|Any CPU
{B301EF61-F0FC-4365-8BB1-795D44DAA596}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B301EF61-F0FC-4365-8BB1-795D44DAA596}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B301EF61-F0FC-4365-8BB1-795D44DAA596}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B301EF61-F0FC-4365-8BB1-795D44DAA596}.Release|Any CPU.Build.0 = Release|Any CPU
{7FEC7E96-490F-437A-842B-8FD5C8E1ACD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FEC7E96-490F-437A-842B-8FD5C8E1ACD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FEC7E96-490F-437A-842B-8FD5C8E1ACD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FEC7E96-490F-437A-842B-8FD5C8E1ACD0}.Release|Any CPU.Build.0 = Release|Any CPU
{32FCB6EE-F393-4200-AB7E-53AC505E4045}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32FCB6EE-F393-4200-AB7E-53AC505E4045}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32FCB6EE-F393-4200-AB7E-53AC505E4045}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32FCB6EE-F393-4200-AB7E-53AC505E4045}.Release|Any CPU.Build.0 = Release|Any CPU
{4CEA307B-C6E9-482D-9552-FD887B06A870}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CEA307B-C6E9-482D-9552-FD887B06A870}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CEA307B-C6E9-482D-9552-FD887B06A870}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CEA307B-C6E9-482D-9552-FD887B06A870}.Release|Any CPU.Build.0 = Release|Any CPU
{F6052059-ECC2-4371-88ED-CAFACBF9FDAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6052059-ECC2-4371-88ED-CAFACBF9FDAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6052059-ECC2-4371-88ED-CAFACBF9FDAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6052059-ECC2-4371-88ED-CAFACBF9FDAE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D2C80B62-059F-4289-912B-2AC841C2E8C4} = {85F54F4E-7E46-4420-85A4-41BAB938AD0A}
{3DC6FF7F-0947-4FC0-A086-1BC729892C57} = {7A396472-0AD9-45F8-A9FA-8822B388C1B8}
{16E2A3BF-02D6-48CD-AD75-C2EFE66A94DF} = {766E89F3-040A-4F01-8DB0-81B4FFA6DA41}
{243DF55D-2E11-481F-AA7A-141C2A75792D} = {F0B75382-6AD2-451E-928E-4A588386B9A3}
{B301EF61-F0FC-4365-8BB1-795D44DAA596} = {7A396472-0AD9-45F8-A9FA-8822B388C1B8}
{7FEC7E96-490F-437A-842B-8FD5C8E1ACD0} = {766E89F3-040A-4F01-8DB0-81B4FFA6DA41}
{32FCB6EE-F393-4200-AB7E-53AC505E4045} = {F0B75382-6AD2-451E-928E-4A588386B9A3}
{4CEA307B-C6E9-482D-9552-FD887B06A870} = {833C70FB-5505-4028-8AA7-746387DF6057}
{F6052059-ECC2-4371-88ED-CAFACBF9FDAE} = {D23C6AF0-1B3F-47D2-A4C1-663C69C1B50E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EC3FCBE3-E76D-4127-B472-026A2C510F05}
Expand Down

0 comments on commit c29d14e

Please sign in to comment.