Skip to content

Commit

Permalink
0.16.2, optimizations, fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Mar 18, 2024
1 parent 43cda03 commit 69866e6
Show file tree
Hide file tree
Showing 39 changed files with 754 additions and 381 deletions.
5 changes: 0 additions & 5 deletions src/Entity/MultipartFormCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sisk.Core.Entity;

Expand Down
1 change: 0 additions & 1 deletion src/Entity/MultipartObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Http;
using Sisk.Core.Internal;
using System.Collections.Specialized;
using System.Text;

Expand Down
24 changes: 16 additions & 8 deletions src/Entity/StringValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
// The code below is licensed under the MIT license as
// of the date of its publication, available at
//
// File name: ValueItem.cs
// File name: StringValue.cs
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sisk.Core.Entity;

/// <summary>
Expand Down Expand Up @@ -272,9 +265,24 @@ void ThrowIfNull()
}
}

/// <inheritdoc/>
/// <nodocs/>
public static implicit operator string?(StringValue i)
{
return i.Value;
}

/// <inheritdoc/>
/// <nodocs/>
public static bool operator ==(StringValue i, string? other)
{
return i.Value == other;
}

/// <inheritdoc/>
/// <nodocs/>
public static bool operator !=(StringValue i, string? other)
{
return i.Value != other;
}
}
8 changes: 1 addition & 7 deletions src/Entity/StringValueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@
// The code below is licensed under the MIT license as
// of the date of its publication, available at
//
// File name: KeyValueCollection.cs
// File name: StringValueCollection.cs
// Repository: https://github.com/sisk-http/core

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

namespace Sisk.Core.Entity;

Expand Down
3 changes: 2 additions & 1 deletion src/Http/CookieHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// File name: CookieHelpers.cs
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Internal;
using System.Web;

namespace Sisk.Core.Http;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void SetCookie(string name, string value, DateTime? expires, TimeSpan? ma
syntax.Add($"SameSite={sameSite}");
}

SetCookieHeader("Set-Cookie", String.Join("; ", syntax));
SetCookieHeader(HttpKnownHeaderNames.SetCookie, String.Join("; ", syntax));
}

}
5 changes: 0 additions & 5 deletions src/Http/Handlers/DefaultHttpServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sisk.Core.Http.Handlers;

Expand Down
9 changes: 5 additions & 4 deletions src/Http/Handlers/HttpServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
namespace Sisk.Core.Http.Handlers;

/// <summary>
/// Represents a handler for the <see cref="HttpServer"/>, router, and related modules.
/// Represents an event handler for the <see cref="HttpServer"/>, router, and related events.
/// </summary>
/// <definition>
/// public class HttpServerHandler
/// public abstract class HttpServerHandler
/// </definition>
/// <type>
/// Class
/// </type>
public class HttpServerHandler
public abstract class HttpServerHandler
{
/// <summary>
/// Method that is called immediately before starting the <see cref="HttpServer"/>.
Expand Down Expand Up @@ -99,7 +99,8 @@ public virtual void OnHttpRequestOpen(HttpRequest request) { }
public virtual void OnHttpRequestClose(HttpServerExecutionResult result) { }

/// <summary>
/// Method that is called when an exception is caught in the Http server.
/// Method that is called when an exception is caught in the Http server. This method is called
/// regardless of whether <see cref="HttpServerConfiguration.ThrowExceptions"/> is enabled or not.
/// </summary>
/// <param name="exception">The exception object.</param>
/// <definition>
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Handlers/HttpServerHandlerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal class HttpServerHandlerRepository

public void RegisterHandler(HttpServerHandler handler)
{
this.handlers.Add(handler);
handlers.Add(handler);
}

private void CallEvery(Action<HttpServerHandler> action)
{
foreach (HttpServerHandler handler in this.handlers)
foreach (HttpServerHandler handler in handlers)
action(handler);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/Hosting/HttpServerHostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Entity;
using Sisk.Core.Internal;
using Sisk.Core.Routing;

namespace Sisk.Core.Http.Hosting;
Expand Down Expand Up @@ -66,8 +65,9 @@ public class HttpServerHostContext
/// <type>
/// Property
/// </type>
public CrossOriginResourceSharingHeaders CrossOriginResourceSharingPolicy {
get => HttpServer.ServerConfiguration.ListeningHosts[0].CrossOriginResourceSharingPolicy;
public CrossOriginResourceSharingHeaders CrossOriginResourceSharingPolicy
{
get => HttpServer.ServerConfiguration.ListeningHosts[0].CrossOriginResourceSharingPolicy;
set => HttpServer.ServerConfiguration.ListeningHosts[0].CrossOriginResourceSharingPolicy = value;
}

Expand Down Expand Up @@ -133,7 +133,7 @@ public void Start(bool verbose = true, bool preventHault = true)
Console.WriteLine(SR.Httpserver_StartMessage);
foreach (string prefix in HttpServer.ListeningPrefixes)
Console.WriteLine("- {0}", prefix);
}
}

if (preventHault)
Thread.Sleep(-1);
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Hosting/HttpServerHostContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using Sisk.Core.Entity;
using Sisk.Core.Http.Handlers;
using Sisk.Core.Internal;
using Sisk.Core.Routing;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -140,12 +139,12 @@ public void UsePortableConfiguration(Action<PortableConfigurationBuilder> portab
/// </summary>
/// <param name="port">The port the server will listen on.</param>
/// <definition>
/// public void UseListeningPort(int port)
/// public void UseListeningPort(ushort port)
/// </definition>
/// <type>
/// Method
/// </type>
public void UseListeningPort(int port)
public void UseListeningPort(ushort port)
{
_context.ServerConfiguration.ListeningHosts[0].Ports[0] = new ListeningPort(port);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Hosting/InitializationParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public class InitializationParameterCollection : IDictionary<string, string?>
mappingValue = Parseable.ParseInternal(value, propType)!;
}
#endif
} else
}
else
{
throw new InvalidCastException(string.Format(SR.InitializationParameterCollection_MapCastException, value, propType.FullName));
}
Expand Down
6 changes: 0 additions & 6 deletions src/Http/Hosting/PortableConfigurationRequireSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
// File name: PortableConfigurationRequireSection.cs
// Repository: https://github.com/sisk-http/core

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

namespace Sisk.Core.Http.Hosting;

/// <summary>
Expand Down
23 changes: 21 additions & 2 deletions src/Http/HttpContextBagRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// File name: HttpContextBagRepository.cs
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Internal;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

Expand All @@ -27,7 +26,7 @@ public class HttpContextBagRepository : IDictionary<string, object?>
private readonly Dictionary<string, object?> _values = new();

private static string GetTypeKeyName(Type t) =>
t.Name + "+" + t.GetHashCode();
t.FullName + "+" + t.GetHashCode();

/// <summary>
/// Creates an new instance of the <see cref="HttpContextBagRepository"/> class.
Expand Down Expand Up @@ -254,6 +253,26 @@ public Boolean TryGetValue(String key, [MaybeNullWhen(false)] out Object? value)
return b;
}

/// <summary>
/// Gets the value associated with the specified key and casts it into <typeparamref name="TResult"/>.
/// </summary>
/// <typeparam name="TResult">The type which will be casted into.</typeparam>
/// <param name="key">The key whose to get.</param>
/// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
/// <returns>true if the object is find with the specified key; otherwise, false.</returns>
/// <definition>
/// public Boolean TryGetValue{{TResult}}(String key, [MaybeNullWhen(false)] out TResult? value)
/// </definition>
/// <type>
/// Method
/// </type>
public Boolean TryGetValue<TResult>(String key, [MaybeNullWhen(false)] out TResult? value)
{
bool b = _values.TryGetValue(key, out Object? v);
value = (TResult?)v;
return b;
}

/// <inheritdoc />
/// <nodoc />
IEnumerator IEnumerable.GetEnumerator()
Expand Down
16 changes: 3 additions & 13 deletions src/Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public NameValueCollection Cookies
if (cookies == null)
{
cookies = new NameValueCollection();
string? cookieHeader = listenerRequest.Headers["cookie"];
string? cookieHeader = listenerRequest.Headers[HttpKnownHeaderNames.Cookie];
if (cookieHeader != null)
{
string[] cookieParts = cookieHeader.Split(';');
Expand Down Expand Up @@ -429,15 +429,15 @@ public StringValueCollection Query
/// public IPAddress Origin { get; }
/// </definition>
/// <type>
/// Property
/// Property
/// </type>
public IPAddress RemoteAddress
{
get
{
if (contextServerConfiguration.ResolveForwardedOriginAddress)
{
string? forwardedIp = listenerRequest.Headers["X-Forwarded-For"];
string? forwardedIp = listenerRequest.Headers[HttpKnownHeaderNames.XForwardedFor];
if (forwardedIp != null)
{
string forwardedIpLiteralStr = forwardedIp.Contains(',') ? forwardedIp.Substring(forwardedIp.IndexOf(',') + 1) : forwardedIp;
Expand Down Expand Up @@ -702,16 +702,6 @@ public HttpResponse Close()
return new HttpResponse(HttpResponse.HTTPRESPONSE_SERVER_REFUSE);
}

/// <summary>
///
/// </summary>
/// <param name="statusCode"></param>
public void Send(HttpStatusCode statusCode)
{
byte[] data = Encoding.UTF8.GetBytes($"HTTP/1.1 {(int)statusCode} {statusCode}\n\n");
listenerResponse.OutputStream.Write(data);
}

/// <summary>
/// Gets the HTTP request content stream. This property is only available while the
/// content has not been imported by the HTTP server and will invalidate the body content
Expand Down
19 changes: 18 additions & 1 deletion src/Http/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// File name: HttpResponse.cs
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Internal;
using Sisk.Core.Routing;
using System.Collections.Specialized;
using System.Net;
Expand Down Expand Up @@ -271,6 +270,24 @@ public HttpResponse WithStatus(int status)
return this;
}

/// <summary>
/// Sets the HTTP response status information from an <see cref="HttpStatusInformation"/> object.
/// </summary>
/// <param name="statusInformation">The status information.</param>
/// <returns>The self <see cref="HttpResponse"/> object.</returns>
/// <definition>
/// public HttpResponse WithStatus(int status)
/// </definition>
/// <type>
/// Method
/// </type>
public HttpResponse WithStatus(HttpStatusInformation statusInformation)
{
this.Status = default;
this.CustomStatus = statusInformation;
return this;
}

/// <summary>
/// Sets the HTTP response status code.
/// </summary>
Expand Down
Loading

0 comments on commit 69866e6

Please sign in to comment.