Skip to content

Commit

Permalink
NuGet v6.1.3, authentication fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristn committed Dec 14, 2023
1 parent 8d129b6 commit 2ce6de2
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 15 deletions.
101 changes: 101 additions & 0 deletions src/Test.Authentication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using RestWrapper;
using WatsonWebserver;
using WatsonWebserver.Core;
using WatsonWebserver.Lite;

namespace Test.Authentication
{
static class Program
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

static bool _UsingLite = false;
static string _Hostname = "localhost";
static int _Port = 8080;
static WebserverSettings _Settings = null;
static WebserverBase _Server = null;
static int _Counter = 0;
static int _Iterations = 10;

static async Task Main()
{
_Settings = new WebserverSettings
{
Hostname = _Hostname,
Port = _Port
};

if (_UsingLite)
{
Console.WriteLine("Initializing webserver lite");
_Server = new WatsonWebserver.Lite.WebserverLite(_Settings, DefaultRoute);
}
else
{
Console.WriteLine("Initializing webserver");
_Server = new WatsonWebserver.Webserver(_Settings, DefaultRoute);
}

_Server.Routes.AuthenticateRequest = AuthenticateRequest;
_Server.Events.ExceptionEncountered += ExceptionEncountered;
_Server.Events.ServerStopped += ServerStopped;
_Server.Events.Logger = Console.WriteLine;

Console.WriteLine("Starting server on: " + _Settings.Prefix);

_Server.Start();

for (int i = 0; i < _Iterations; i++)
{
using (RestRequest req = new RestRequest(_Settings.Prefix))
{
using (RestResponse resp = await req.SendAsync())
{
Console.WriteLine(resp.StatusCode + ": " + resp.DataAsString);
}
}
}
}

private static async Task AuthenticateRequest(HttpContextBase ctx)
{
if (_Counter % 2 == 0)
{
// do nothing, permit
}
else
{
ctx.Response.StatusCode = 401;
await ctx.Response.Send("Denied");
}

_Counter++;
}

static void ExceptionEncountered(object sender, ExceptionEventArgs args)
{
_Server.Events.Logger(args.Exception.ToString());
}

static void ServerStopped(object sender, EventArgs args)
{
_Server.Events.Logger("*** Server stopped");
}

static async Task DefaultRoute(HttpContextBase ctx)
{
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "text/plain";
await ctx.Response.Send("Permitted");
return;
}

#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
}
17 changes: 17 additions & 0 deletions src/Test.Authentication/Test.Authentication.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RestWrapper" Version="3.0.17" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WatsonWebserver.Lite\WatsonWebserver.Lite.csproj" />
<ProjectReference Include="..\WatsonWebserver\WatsonWebserver.csproj" />
</ItemGroup>

</Project>
17 changes: 9 additions & 8 deletions src/Test.Docker/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using WatsonWebserver;
using WatsonWebserver.Core;

namespace Test.Docker
namespace Test.Docker
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using WatsonWebserver;
using WatsonWebserver.Core;
using WatsonWebserver.Lite;

class Program
{
static bool _UsingLite = false;
Expand Down
94 changes: 94 additions & 0 deletions src/Test.HeadResponse/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
namespace Test.HeadResponse
{
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using RestWrapper;
using WatsonWebserver;
using WatsonWebserver.Core;

class Program
{
static bool _UsingLite = false;
static string _Hostname = "localhost";
static int _Port = 8080;
static WebserverSettings _Settings = null;
static WebserverBase _Server = null;
static string _Data = "Hello, world!";
static int _Counter = 0;
static int _Iterations = 10;

static async Task Main(string[] args)
{
if (args != null && args.Length > 0)
{
if (args[0].Equals("lite")) _UsingLite = true;
}

_Settings = new WebserverSettings
{
Hostname = _Hostname,
Port = _Port
};

if (_UsingLite)
{
Console.WriteLine("Initializing webserver lite");
_Server = new WatsonWebserver.Lite.WebserverLite(_Settings, DefaultRoute);
}
else
{
Console.WriteLine("Initializing webserver");
_Server = new Webserver(_Settings, DefaultRoute);
}

Console.WriteLine("Listening on " + _Settings.Prefix);
_Server.Start();

for (_Counter = 0; _Counter < _Iterations; _Counter++)
{
await SendHeadRequest();
}

Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}

static async Task DefaultRoute(HttpContextBase ctx)
{
ctx.Response.StatusCode = 200;

if (_Counter % 2 == 0)
{
Console.WriteLine("Responding using ctx.Response.Send");
await Task.Delay(250);
ctx.Response.ContentLength = _Data.Length;
await ctx.Response.Send();
}
else
{
Console.WriteLine("Responding using ctx.Response.Send(len)");
await Task.Delay(250);
ctx.Response.ContentLength = _Data.Length;
await ctx.Response.Send(_Data.Length);
}

return;
}

static async Task SendHeadRequest()
{
using (RestRequest req = new RestRequest(_Settings.Prefix, System.Net.Http.HttpMethod.Head))
{
Console.WriteLine("Sending REST request");

using (RestResponse resp = await req.SendAsync())
{
Console.WriteLine(resp.ToString());
}
}
}
}
}
17 changes: 17 additions & 0 deletions src/Test.HeadResponse/Test.HeadResponse.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RestWrapper" Version="3.0.17" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WatsonWebserver.Lite\WatsonWebserver.Lite.csproj" />
<ProjectReference Include="..\WatsonWebserver\WatsonWebserver.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/WatsonWebserver.Lite/WatsonWebserver.Lite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net462;net48;net6.0;net7.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>6.1.2</Version>
<Version>6.1.3</Version>
<Description>Simple, fast, async C# web server for handling REST requests with SSL support, targeted to .NET Core, .NET Standard, and .NET Framework. Watson.Lite has no dependency on http.sys.</Description>
<Authors>Joel Christner</Authors>
<Company>Joel Christner</Company>
Expand Down
4 changes: 2 additions & 2 deletions src/WatsonWebserver.Lite/Webserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@ private async void ClientConnected(object sender, ClientConnectedEventArgs args)
{
Events.Logger?.Invoke(_Header + "response sent during authentication for " + ctx.Request.Source.IpAddress + ":" + ctx.Request.Source.Port + " " +
ctx.Request.Method.ToString() + " " + ctx.Request.Url.Full);

return;
}

return;
}
else
{
Expand Down
14 changes: 13 additions & 1 deletion src/WatsonWebserver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WatsonWebserver.Core", "Wat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WatsonWebserver.Lite", "WatsonWebserver.Lite\WatsonWebserver.Lite.csproj", "{24D9EDF0-38CB-4557-B56C-ED4FDDADDCF6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Routing", "Test.Routing\Test.Routing.csproj", "{A7F458E8-D14C-4AA3-889F-7002CD1BC4DB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.Routing", "Test.Routing\Test.Routing.csproj", "{A7F458E8-D14C-4AA3-889F-7002CD1BC4DB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.HeadResponse", "Test.HeadResponse\Test.HeadResponse.csproj", "{804D4BF3-CD72-48C5-B573-FCB95D692BB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Authentication", "Test.Authentication\Test.Authentication.csproj", "{866337D0-67F9-48AE-97F7-9AFBFEFB385F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -87,6 +91,14 @@ Global
{A7F458E8-D14C-4AA3-889F-7002CD1BC4DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7F458E8-D14C-4AA3-889F-7002CD1BC4DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7F458E8-D14C-4AA3-889F-7002CD1BC4DB}.Release|Any CPU.Build.0 = Release|Any CPU
{804D4BF3-CD72-48C5-B573-FCB95D692BB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{804D4BF3-CD72-48C5-B573-FCB95D692BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{804D4BF3-CD72-48C5-B573-FCB95D692BB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{804D4BF3-CD72-48C5-B573-FCB95D692BB2}.Release|Any CPU.Build.0 = Release|Any CPU
{866337D0-67F9-48AE-97F7-9AFBFEFB385F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{866337D0-67F9-48AE-97F7-9AFBFEFB385F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{866337D0-67F9-48AE-97F7-9AFBFEFB385F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{866337D0-67F9-48AE-97F7-9AFBFEFB385F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/WatsonWebserver/WatsonWebserver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net462;net48;net6.0;net7.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>6.1.2</Version>
<Version>6.1.3</Version>
<Description>Simple, fast, async C# web server for handling REST requests with SSL support, targeted to .NET Core, .NET Standard, and .NET Framework.</Description>
<Authors>Joel Christner</Authors>
<Company>Joel Christner</Company>
Expand Down
4 changes: 2 additions & 2 deletions src/WatsonWebserver/Webserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,9 @@ private async Task AcceptConnections(CancellationToken token)
{
Events.Logger?.Invoke(_Header + "response sent during authentication for " + ctx.Request.Source.IpAddress + ":" + ctx.Request.Source.Port + " " +
ctx.Request.Method.ToString() + " " + ctx.Request.Url.Full);

return;
}

return;
}
else
{
Expand Down

0 comments on commit 2ce6de2

Please sign in to comment.