Skip to content

Commit

Permalink
Merge pull request #6 from Garados007/version-4
Browse files Browse the repository at this point in the history
WIP: Version 4
  • Loading branch information
Garados007 authored Feb 18, 2024
2 parents e1d1c33 + a0d865b commit e12d0e3
Show file tree
Hide file tree
Showing 58 changed files with 453 additions and 919 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Default setting
* auto

# Do nothing
*.sln -text
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
8.0
6.0.102
3.1.101
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ jobs:

steps:
- uses: actions/checkout@v2

- name: Init git submodules
run: git submodule init && git submodule update

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.102
dotnet-version: '8.0'
- name: Install dependencies
run: dotnet restore

- name: Publish MaxLib.WebServer NuGet
uses: brandedoutcast/[email protected]
with:
Expand Down
22 changes: 0 additions & 22 deletions MaxLib.WebServer.Test/MaxLib.WebServer.Test.csproj

This file was deleted.

4 changes: 2 additions & 2 deletions MaxLib.WebServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxLib.WebServer", "MaxLib.WebServer\MaxLib.WebServer.csproj", "{595DB1BF-246A-4A42-99EB-5DAB7709E21E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxLib.WebServer.Test", "MaxLib.WebServer.Test\MaxLib.WebServer.Test.csproj", "{60225D92-5742-4BC0-A3A5-206123F2129D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxLib.WebServer.Test", "tests\MaxLib.WebServer.Test\MaxLib.WebServer.Test.csproj", "{60225D92-5742-4BC0-A3A5-206123F2129D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxLib.WebServer.Example", "example\MaxLib.WebServer.Example\MaxLib.WebServer.Example.csproj", "{6616448A-1AD7-4897-9124-F1560FB12461}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Example", "Example", "{39740782-6F07-470C-92B8-C0A07A5C0DFB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxLib.WebServer.WebSocket.Echo", "MaxLib.WebServer.WebSocket.Echo\MaxLib.WebServer.WebSocket.Echo.csproj", "{01C302FF-8E99-4D54-8234-D4BA59862176}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxLib.WebServer.WebSocket.Echo", "example\MaxLib.WebServer.WebSocket.Echo\MaxLib.WebServer.WebSocket.Echo.csproj", "{01C302FF-8E99-4D54-8234-D4BA59862176}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{29467014-E04B-477A-82B7-16A3BAE7B5A6}"
ProjectSection(SolutionItems) = preProject
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace MaxLib.WebServer.Api
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public abstract class ApiService : WebService
{
public ApiService(params string[] endpoint)
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/ApiGetRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public abstract class ApiGetRule : ApiRule
{
private string? key;
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/ApiLocationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public abstract class ApiLocationRule : ApiRule
{
private int index = 0;
Expand Down
3 changes: 3 additions & 0 deletions MaxLib.WebServer/Api/Rest/ApiRule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#nullable enable

using System;

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public abstract class ApiRule
{
public bool Required { get; set; } = true;
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/ApiRuleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public class ApiRuleFactory
{
public class HostRule : ApiRule
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/RestActionEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public class RestActionEndpoint : RestEndpoint
{
public Func<Dictionary<string, object?>, Task<HttpDataSource?>> HandleRequest { get; set; }
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/RestApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public class RestApiService : ApiService
{
public List<RestEndpoint> RestEndpoints { get; } = new List<RestEndpoint>();
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/RestEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public class RestEndpoint
{
readonly private ApiRuleFactory.GroupRule rules = new ApiRuleFactory.GroupRule();
Expand Down
1 change: 1 addition & 0 deletions MaxLib.WebServer/Api/Rest/RestQueryArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace MaxLib.WebServer.Api.Rest
{
[Obsolete("The ApiService and the RestApiService classes are no longer maintained and will be removed in a future update. Use the Builder system instead.")]
public class RestQueryArgs
{
public string[] Location { get; }
Expand Down
129 changes: 15 additions & 114 deletions MaxLib.WebServer/Chunked/HttpChunkedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,33 @@ public HttpChunkedStream(Stream baseStream, int readBufferLength = 0x8000)

public int ReadBufferLength { get; }

[Obsolete]
public override bool CanAcceptData => BaseStream.CanWrite;

public override bool CanProvideData => BaseStream.CanRead;

public override long? Length() => null;

public override void Dispose()
{
BaseStream.Dispose();
}

protected override async Task<long> WriteStreamInternal(Stream stream, long start, long? stop)
protected override async Task<long> WriteStreamInternal(Stream stream)
{
long total = 0;
int readed;
byte[] buffer = new byte[ReadBufferLength];
do
{
readed = await BaseStream.ReadAsync(
buffer,
0,
(int)Math.Min(buffer.Length, start - total)
).ConfigureAwait(false);
total += readed;
}
while (total < start && readed > 0);
if (readed == 0 && start > 0)
return 0;
int read;
Memory<byte> buffer = new byte[ReadBufferLength];
var ascii = Encoding.ASCII;
var nl = ascii.GetBytes("\r\n");
ReadOnlyMemory<byte> nl = ascii.GetBytes("\r\n");
do
{
var read = stop == null
? buffer.Length
: (int)Math.Min(buffer.Length, stop.Value - total);
readed = await BaseStream.ReadAsync(buffer, 0, read).ConfigureAwait(false);
if (readed <= 0)
return total - start;
var length = ascii.GetBytes(readed.ToString("X"));
read = await BaseStream.ReadAsync(buffer).ConfigureAwait(false);
if (read <= 0)
return total;
ReadOnlyMemory<byte> length = ascii.GetBytes(read.ToString("X"));
try
{
await stream.WriteAsync(length, 0, length.Length).ConfigureAwait(false);
await stream.WriteAsync(nl, 0, nl.Length).ConfigureAwait(false);
await stream.WriteAsync(buffer, 0, readed).ConfigureAwait(false);
await stream.WriteAsync(nl, 0, nl.Length).ConfigureAwait(false);
total += readed;
await stream.WriteAsync(length).ConfigureAwait(false);
await stream.WriteAsync(nl).ConfigureAwait(false);
await stream.WriteAsync(buffer[0..read]).ConfigureAwait(false);
await stream.WriteAsync(nl).ConfigureAwait(false);
total += read;
await stream.FlushAsync().ConfigureAwait(false);
}
catch (IOException)
Expand All @@ -78,87 +58,8 @@ protected override async Task<long> WriteStreamInternal(Stream stream, long star
return total;
}
}
while (readed > 0);
return total - start;
}

[Obsolete]
protected override async Task<long> ReadStreamInternal(Stream stream, long? length)
{
long total = 0;
int readed, numberLength;
var ascii = Encoding.ASCII;
var buffer = new byte[0x10000];
while (true)
{
try { numberLength = await ReadNumber(stream, buffer).ConfigureAwait(false); }
catch (IOException)
{
WebServerLog.Add(ServerLogType.Information, GetType(), "read", "connection closed");
return total;
}
if (numberLength == 0)
return total;
var numberString = ascii.GetString(buffer, 0, numberLength);
if (!long.TryParse(numberString,
NumberStyles.HexNumber,
CultureInfo.InvariantCulture.NumberFormat,
out long number) || number < 0)
{
WebServerLog.Add(ServerLogType.Information, GetType(), "read", "invalid number of bytes indicator");
return total;
}
while (number > 0)
{
try { readed = await stream.ReadAsync(buffer, 0, (int)Math.Min(number, buffer.Length)).ConfigureAwait(false); }
catch (IOException)
{
WebServerLog.Add(ServerLogType.Information, GetType(), "read", "connection closed");
return total;
}
if (readed == 0)
{
WebServerLog.Add(ServerLogType.Information, GetType(), "read", "could not read the block completly");
return total;
}
await BaseStream.WriteAsync(buffer, 0, readed).ConfigureAwait(false);
total += readed;
number -= readed;
}
try
{
readed = await stream.ReadAsync(buffer, 0, 1).ConfigureAwait(false);
if (readed > 0 && buffer[0] == '\r')
readed = await stream.ReadAsync(buffer, 0, 1).ConfigureAwait(false);
if (readed == 0)
return total;
}
catch (IOException)
{
WebServerLog.Add(ServerLogType.Information, GetType(), "read", "connection closed");
return total;
}
}
}

private async Task<int> ReadNumber(Stream stream, byte[] buffer)
{
int offset = 0;
var byteBuffer = new byte[1];
while (true)
{
int readed = await stream.ReadAsync(byteBuffer, 0, 1).ConfigureAwait(false);
if (readed == 0)
return offset;
if (byteBuffer[0] == '\r' || byteBuffer[0] == '\n')
{
if (byteBuffer[0] == '\r')
await stream.ReadAsync(byteBuffer, 0, 1).ConfigureAwait(false);
return offset;
}
buffer[offset] = byteBuffer[0];
offset++;
}
while (read > 0);
return total;
}
}
}
Loading

0 comments on commit e12d0e3

Please sign in to comment.