Skip to content

Commit

Permalink
small fixes before final release
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Jan 3, 2024
1 parent e11a748 commit 46068a1
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/Http/Hosting/HttpServerHostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ public void Start(bool verbose = true, bool preventHault = true)
if (preventHault)
Thread.Sleep(-1);
}

public async Task StartAsync(bool verbose = true, bool preventHault = true)
{
await Task.Run(() => Start(verbose, preventHault));
}
}
6 changes: 3 additions & 3 deletions src/Http/HttpServer__Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ private async Task ProcessRequest(HttpListenerContext context)
baseResponse.AddHeader(incameHeader, value);
}

if (response.Content != null && responseContentLength > 0)
if (response.Content != null)
{
// determines the content type
baseResponse.ContentType = resHeaders["Content-Type"] ?? response.Content.Headers.ContentType?.ToString();

// determines if the response should be sent as chunked or normal
if (response.SendChunked)
if (response.SendChunked || responseContentLength == null)
{
baseResponse.SendChunked = true;
}
Expand All @@ -325,7 +325,7 @@ private async Task ProcessRequest(HttpListenerContext context)
// write the output buffer
if (context.Request.HttpMethod != "HEAD")
{
outcomingSize += responseContentLength.Value;
outcomingSize += responseContentLength ?? -1;

bool isPayloadStreamable =
response.Content is StreamContent ||
Expand Down
77 changes: 77 additions & 0 deletions src/Http/LogStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class LogStream : IDisposable
private ManualResetEvent terminate = new ManualResetEvent(false);
private Thread loggingThread;
private bool isBlocking = false;
internal RotatingLogPolicy? rotatingLogPolicy = null;

/// <summary>
/// Represents a LogStream that writes its output to the <see cref="Console.Out"/> stream.
Expand All @@ -41,6 +42,30 @@ public class LogStream : IDisposable
/// </type>
public static LogStream ConsoleOutput = new LogStream(Console.Out);

/// <summary>
/// Gets the defined <see cref="RotatingLogPolicy"/> for this <see cref="LogStream"/>.
/// </summary>
/// <remarks>
/// Internally, this property creates a new <see cref="RotatingLogPolicy"/> for this log stream if it is not defined before.
/// </remarks>
/// <definition>
/// public RotatingLogPolicy RotatingPolicy
/// </definition>
/// <type>
/// Property
/// </type>
public RotatingLogPolicy RotatingPolicy
{
get
{
if (rotatingLogPolicy == null)
{
rotatingLogPolicy = new RotatingLogPolicy(this);
}
return rotatingLogPolicy;
}
}

/// <summary>
/// Gets the absolute path to the file where the log is being written to.
/// </summary>
Expand All @@ -63,6 +88,17 @@ public class LogStream : IDisposable
/// </type>
public TextWriter? TextWriter { get; private set; } = null;

/// <summary>
/// Gets or sets the function that formats input when used with <see cref="WriteFormat(object?)"/>.
/// </summary>
/// <definition>
/// public Func{{object?, string}}? Format { get; set; }
/// </definition>
/// <type>
/// Property
/// </type>
public Func<object?, string>? Format { get; set; }

/// <summary>
/// Gets or sets the encoding used for writting data to the output file. This property is only appliable if
/// this instance is using an file-based output.
Expand Down Expand Up @@ -265,6 +301,26 @@ public void WriteLine()
}
}

/// <summary>
/// Writes the input, formatting with <see cref="Format"/> handler, at the end of the output.
/// </summary>
/// <param name="input">The input object which will be formatted and written to the output.</param>
/// <definition>
/// public void WriteFormat(object? input)
/// </definition>
/// <type>
/// Method
/// </type>
public void WriteFormat(object? input)
{
if (Format == null)
{
throw new InvalidOperationException(SR.LogStream_NoFormat);
}
string format = Format(input);
Write(format);
}

/// <summary>
/// Writes the text and concats an line-break at the end into the output.
/// </summary>
Expand Down Expand Up @@ -360,6 +416,27 @@ public void Dispose()
TextWriter?.Close();
}

/// <summary>
/// Defines the time interval and size threshold for starting the task, and then starts the task. This method is an
/// shortcut for calling <see cref="RotatingLogPolicy.Configure(long, TimeSpan)"/> of this defined <see cref="RotatingPolicy"/> method.
/// </summary>
/// <remarks>
/// The first run is performed immediately after calling this method.
/// </remarks>
/// <param name="maximumSize">The non-negative size threshold of the log file size in byte count.</param>
/// <param name="dueTime">The time interval between checks.</param>
/// <definition>
/// public void Configure(long maximumSize, TimeSpan due)
/// </definition>
/// <type>
/// Method
/// </type>
public void ConfigureRotatingPolicy(long maximumSize, TimeSpan dueTime)
{
var policy = RotatingPolicy;
policy.Configure(maximumSize, dueTime);
}

void WriteExceptionInternal(StringBuilder exceptionStr, Exception exp, int depth = 0)
{
if (depth == 0)
Expand Down
15 changes: 12 additions & 3 deletions src/Http/RotatingLogPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ public class RotatingLogPolicy : IDisposable
/// Creates an new <see cref="RotatingLogPolicy"/> instance with the given <see cref="LogStream"/> object to watch.
/// </summary>
/// <definition>
/// public RotatingLogPolicy(LogStream? ls)
/// public RotatingLogPolicy(LogStream ls)
/// </definition>
/// <type>
/// Constructor
/// </type>
public RotatingLogPolicy(LogStream? ls)
public RotatingLogPolicy(LogStream ls)
{
if (ls.rotatingLogPolicy != null)
{
throw new InvalidOperationException(SR.LogStream_RotatingLogPolicy_AlreadyBind);
}
this._logStream = ls;
this._logStream.rotatingLogPolicy = this;
checkThread = new Thread(new ThreadStart(Check));
checkThread.IsBackground = true;
}
Expand All @@ -78,13 +83,17 @@ public RotatingLogPolicy(LogStream? ls)
/// </definition>
/// <type>
/// Method
/// </type>
/// </type>
public void Configure(long maximumSize, TimeSpan due)
{
if (string.IsNullOrEmpty(_logStream?.FilePath))
{
throw new NotSupportedException(SR.LogStream_RotatingLogPolicy_NotLocalFile);
}
if (checkThread.IsAlive)
{
throw new NotSupportedException(SR.LogStream_RotatingLogPolicy_AlreadyRunning);
}
MaximumSize = maximumSize;
Due = due;
checkThread.Start();
Expand Down
3 changes: 3 additions & 0 deletions src/Internal/SR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ static class SR
public const string LogStream_Peek_NotFilePath = "This method only works when the LogStream is appending contents to an file.";
public const string LogStream_ExceptionDump_Header = "Exception thrown at {0}";
public const string LogStream_ExceptionDump_TrimmedFooter = " + ... other trimmed inner exceptions";
public const string LogStream_NoFormat = "No format is defined in this LogStream.";
public const string LogStream_RotatingLogPolicy_NotLocalFile = "Cannot link an rotaging log policy to an log stream which ins't pointing to an local file.";
public const string LogStream_RotatingLogPolicy_AlreadyRunning = "This RotatingLogPolicy has already been configured and it is running.";
public const string LogStream_RotatingLogPolicy_AlreadyBind = "The specified LogStream is already binded to another RotatingLogPolicy.";

public const string HttpRequestEventSource_KeepAliveDisposed = "Cannot keep alive an instance that has it's connection disposed.";

Expand Down
53 changes: 53 additions & 0 deletions src/Routing/RegexRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// The Sisk Framework source code
// Copyright (c) 2023 PROJECT PRINCIPIUM
//
// The code below is licensed under the MIT license as
// of the date of its publication, available at
//
// File name: RegexRoute.cs
// Repository: https://github.com/sisk-http/core

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sisk.Core.Routing;

/// <summary>
/// Represents an <see cref="Route"/> which it's implementation already enables
/// <see cref="Route.UseRegex"/>.
/// </summary>
/// <definition>
/// public class RegexRoute
/// </definition>
/// <type>
/// Class
/// </type>
public class RegexRoute : Route
{
/// <inheritdoc/>
/// <nodoc/>
#if NET6_0
public RegexRoute(RouteMethod method, string path, RouteAction action) : base(method, path, action)
#elif NET7_0_OR_GREATER
public RegexRoute(RouteMethod method, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern, RouteAction action) : base(method, pattern, action)
#endif

{
UseRegex = true;
}

/// <inheritdoc/>
/// <nodoc/>
#if NET6_0
public RegexRoute(RouteMethod method, string path, string? name, RouteAction action, IRequestHandler[]? beforeCallback) : base(method, path, name, action, beforeCallback)
#elif NET7_0_OR_GREATER
public RegexRoute(RouteMethod method, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern, string? name, RouteAction action, IRequestHandler[]? beforeCallback) : base(method, pattern, name, action, beforeCallback)
#endif
{
UseRegex = true;
}
}
2 changes: 1 addition & 1 deletion src/Routing/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,4 @@ public enum LogOutput
/// </summary>
None = 0
}
}
}
2 changes: 1 addition & 1 deletion src/Sisk.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<AssemblyVersion>0.16.0.0</AssemblyVersion>
<FileVersion>0.16.0.0</FileVersion>
<Version>0.16-rc-3</Version>
<Version>0.16-rc-4</Version>

<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<SignAssembly>False</SignAssembly>
Expand Down

0 comments on commit 46068a1

Please sign in to comment.