-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
Showing
10 changed files
with
257 additions
and
15 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
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 | ||
} | ||
} |
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 @@ | ||
<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> |
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,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()); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
<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> |
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
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
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