Skip to content

Commit

Permalink
upstream fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Sep 23, 2024
1 parent 5d610d2 commit 307ab06
Show file tree
Hide file tree
Showing 34 changed files with 255 additions and 242 deletions.
4 changes: 2 additions & 2 deletions src/Entity/HttpHeaderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ public HttpHeaderCollection() : base(StringComparer.InvariantCultureIgnoreCase)
/// Gets or sets the value of the HTTP X-Forwarded-For header.
/// </summary>
/// <remarks>
/// Tip: enable the <see cref="HttpServerConfiguration.ResolveForwardedOriginAddress"/> property to obtain the user client proxied IP throught <see cref="HttpRequest.RemoteAddress"/>.
/// Tip: use the <see cref="HttpServerConfiguration.ForwardingResolver"/> property to obtain the user client proxied IP throught <see cref="HttpRequest.RemoteAddress"/>.
/// </remarks>
public string? XForwardedFor { get => this[Header.XForwardedFor]; set => this[Header.XForwardedFor] = value; }

/// <summary>
/// Gets or sets the value of the HTTP X-Forwarded-Host header.
/// </summary>
/// <remarks>
/// Tip: enable the <see cref="HttpServerConfiguration.ResolveForwardedOriginHost"/> property to obtain the client requested host throught <see cref="HttpRequest.Host"/>.
/// Tip: use the <see cref="HttpServerConfiguration.ForwardingResolver"/> property to obtain the client requested host throught <see cref="HttpRequest.Host"/>.
/// </remarks>
public string? XForwardedHost { get => this[Header.XForwardedHost]; set => this[Header.XForwardedHost] = value; }

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/MultipartFormReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ NameValueCollection ReadHeaders()
string? line;
while (!string.IsNullOrEmpty(line = this.ReadLine()))
{
int sepIndex = line.IndexOf(':');
int sepIndex = line.IndexOf(':', StringComparison.Ordinal);
if (sepIndex == -1)
break;

Expand Down
25 changes: 22 additions & 3 deletions src/Entity/MultipartObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Sisk.Core.Entity
/// <summary>
/// Represents an multipart/form-data object.
/// </summary>
public sealed class MultipartObject
public sealed class MultipartObject : IEquatable<MultipartObject>
{
private readonly Encoding _baseEncoding;

Expand Down Expand Up @@ -44,7 +44,7 @@ public sealed class MultipartObject
/// <summary>
/// Gets this <see cref="MultipartObject"/> form data content length in byte count.
/// </summary>
public int ContentLength { get; private set; }
public int ContentLength { get => this.ContentBytes.Length; }

/// <summary>
/// Gets an booolean indicating if this <see cref="MultipartObject"/> has contents or not.
Expand Down Expand Up @@ -137,7 +137,6 @@ internal MultipartObject(NameValueCollection headers, string? filename, string n
this.Filename = filename;
this.Name = name;
this.ContentBytes = body ?? Array.Empty<byte>();
this.ContentLength = body?.Length ?? 0;
this._baseEncoding = encoding;
}

Expand Down Expand Up @@ -316,5 +315,25 @@ bool Equals(byte[] source, byte[] separator, int index)

return new MultipartFormCollection(outputObjects);
}

/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(this.Name.GetHashCode(), this.ContentLength.GetHashCode(), this.Filename?.GetHashCode() ?? 0);
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
if (obj is MultipartObject mo)
return this.Equals(mo);
return false;
}

/// <inheritdoc/>
public bool Equals(MultipartObject? other)
{
return this.GetHashCode() == other?.GetHashCode();
}
}
}
18 changes: 9 additions & 9 deletions src/Entity/MultipartObjectCommonFormatByteMark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

namespace Sisk.Core.Entity;

internal static class MultipartObjectCommonFormatByteMark
static class MultipartObjectCommonFormatByteMark
{
public static readonly byte[] PNG = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
public static readonly byte[] PNG = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];

public static readonly byte[] PDF = new byte[] { 0x25, 0x50, 0x44, 0x46 };
public static readonly byte[] WEBP = new byte[] { 0x52, 0x49, 0x46, 0x46 };
public static readonly byte[] TIFF = new byte[] { 0x4D, 0x4D, 0x00, 0x2A };
public static readonly byte[] WEBM = new byte[] { 0x1A, 0x45, 0xDF, 0xA3 };
public static readonly byte[] PDF = [0x25, 0x50, 0x44, 0x46];
public static readonly byte[] WEBP = [0x52, 0x49, 0x46, 0x46];
public static readonly byte[] TIFF = [0x4D, 0x4D, 0x00, 0x2A];
public static readonly byte[] WEBM = [0x1A, 0x45, 0xDF, 0xA3];

public static readonly byte[] JPEG = new byte[] { 0xFF, 0xD8, 0xFF };
public static readonly byte[] GIF = new byte[] { 0x47, 0x46, 0x49 };
public static readonly byte[] JPEG = [0xFF, 0xD8, 0xFF];
public static readonly byte[] GIF = [0x47, 0x46, 0x49];

public static readonly byte[] BMP = new byte[] { 0x42, 0x4D };
public static readonly byte[] BMP = [0x42, 0x4D];
}
2 changes: 1 addition & 1 deletion src/Entity/StringKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void ParseQueryString(string queryString, char pairSeparator, char valueSeparato
{
string part = kvPairs[i];

int eqPos = part.IndexOf(valueSeparator);
int eqPos = part.IndexOf(valueSeparator, StringComparison.Ordinal);
if (eqPos < 0)
{
this.AddInternal(part, new string[1] { string.Empty });
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/StringValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ public override bool Equals(object? obj)
}
else if (obj is StringValue sv)
{
return this._ref?.Equals(sv._ref) == true;
return this._ref?.Equals(sv._ref, StringComparison.Ordinal) == true;
}
else if (obj is string ss)
{
return this._ref?.Equals(ss) == true;
return this._ref?.Equals(ss, StringComparison.Ordinal) == true;
}
else
{
Expand All @@ -382,7 +382,7 @@ public override bool Equals(object? obj)
/// <exclude/>
public override int GetHashCode()
{
return this._ref?.GetHashCode() ?? 0;
return this._ref?.GetHashCode(StringComparison.Ordinal) ?? 0;
}

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

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Scope = "member", Target = "~M:Sisk.Core.Http.HttpStatusInformation.op_Implicit(System.Net.HttpStatusCode)~Sisk.Core.Http.HttpStatusInformation")]
[assembly: SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Scope = "member", Target = "~M:Sisk.Core.Http.HttpStatusInformation.op_Implicit(System.Int32)~Sisk.Core.Http.HttpStatusInformation")]
80 changes: 66 additions & 14 deletions src/Http/DefaultMessagePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// File name: DefaultMessagePage.cs
// Repository: https://github.com/sisk-http/core

using System.Text;
using System.Web;

namespace Sisk.Core.Http;
Expand All @@ -24,18 +23,71 @@ public static class DefaultMessagePage
/// <param name="description">The static page description text.</param>
public static string CreateDefaultPageHtml(string firstHeader, string description)
{
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.Append("<!DOCTYPE html>");
htmlBuilder.Append("<html><head><title>");
htmlBuilder.Append(HttpUtility.HtmlEncode(firstHeader));
htmlBuilder.Append("</title></head><body><h1>");
htmlBuilder.Append(HttpUtility.HtmlEncode(firstHeader));
htmlBuilder.Append("</h1><p>");
htmlBuilder.Append(HttpUtility.HtmlEncode(description));
htmlBuilder.Append("</p><hr><i>Sisk v.");
htmlBuilder.Append(HttpServer.SiskVersion);
htmlBuilder.Append("</i></body></html>");

return htmlBuilder.ToString();
firstHeader = HttpUtility.HtmlEncode(firstHeader);
description = HttpUtility.HtmlEncode(description);

return $$"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{firstHeader}</title>
</head>

<body>
<style>
body {
background-color: #eeeeee;
font-family: sans-serif;
}

main {
background-color: white;
padding: 25px;
border-radius: 10px;
border: 1px solid #dddddd;
display: block;
margin: 30px auto 0 auto;
max-width: 600px;
}

h1 {
margin: 0;
}

hr {
border-top: 1px solid #bbbbbb;
border-bottom: none;
}

small {
color: #777777;
}
</style>
<main>
<h1>{{firstHeader}}</h1>
<p>{{description}}</p>
<hr>
<small>Sisk/{{HttpServer.SiskVersion.ToString(3)}}</small>
</main>
</body>
</html>
""";
}

/// <summary>
/// Creates an static default page with given status code and description.
/// </summary>
/// <param name="status">The static page status code.</param>
/// <param name="longDescription">The static page description text.</param>
public static HttpResponse CreateDefaultResponse(in HttpStatusInformation status, string longDescription)
{
string html = CreateDefaultPageHtml(status.Description, longDescription);
return new HttpResponse()
{
Status = status,
Content = new HtmlContent(html)
};
}
}
2 changes: 1 addition & 1 deletion src/Http/ForwardingResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Sisk.Core.Http;
/// Provides HTTP forwarding resolving methods that can be used to resolving the client remote
/// address, host and protocol of a proxy, load balancer or CDN, through the HTTP request.
/// </summary>
public class ForwardingResolver
public abstract class ForwardingResolver
{
/// <summary>
/// Method that is called when resolving the IP address of the client in the request.
Expand Down
9 changes: 2 additions & 7 deletions src/Http/HtmlContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ namespace Sisk.Core.Http;
/// </summary>
public class HtmlContent : StringContent
{
/// <summary>
/// Gets or sets the default encoding which will be used on constructors.
/// </summary>
public static Encoding DefaultEncoding { get; set; } = Encoding.UTF8;

/// <summary>
/// Creates an new <see cref="HtmlContent"/> class with given HTML content and encoding.
/// </summary>
Expand All @@ -31,8 +26,8 @@ public HtmlContent(string content, Encoding encoding) : base(content, encoding,
}

/// <summary>
/// Creates an new <see cref="HtmlContent"/> class with given HTML content, using the <see cref="DefaultEncoding"/> encoding.
/// Creates an new <see cref="HtmlContent"/> class with given HTML content, using the environment default encoding.
/// </summary>
/// <param name="content">The HTML content string.</param>
public HtmlContent(string content) : this(content, DefaultEncoding) { }
public HtmlContent(string content) : this(content, Encoding.Default) { }
}
16 changes: 9 additions & 7 deletions src/Http/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,39 @@ public sealed class HttpContext
public TypedValueDictionary RequestBag { get; set; } = new TypedValueDictionary();

/// <summary>
/// Gets the context HTTP Server instance.
/// Gets the context <see cref="Http.HttpServer"/> instance.
/// </summary>
public HttpServer HttpServer { get; private set; }

/// <summary>
/// Gets or sets the HTTP response for this context. This property is only not null when a post-executing <see cref="IRequestHandler"/> was executed for this router context.
/// Gets the <see cref="Http.HttpResponse"/> for this context. This property acessible when a post-executing
/// <see cref="IRequestHandler"/> was executed for this router context.
/// </summary>
public HttpResponse? RouterResponse { get; internal set; } = null!;
public HttpResponse? RouterResponse { get; internal set; }

/// <summary>
/// Gets the HTTP request which is contained in this HTTP context.
/// Gets the <see cref="Http.HttpRequest"/> which is contained in this HTTP context.
/// </summary>
public HttpRequest Request { get; private set; }

/// <summary>
/// Gets the matched HTTP Route object from the Router.
/// Gets the matched <see cref="Routing.Route"/> for this context.
/// </summary>
public Route? MatchedRoute { get; internal set; }

/// <summary>
/// Gets the <see cref="Sisk.Core.Routing.Router"/> where this context was
/// Gets the <see cref="Routing.Router"/> where this context was
/// created.
/// </summary>
public Router? Router { get; internal set; }
public Router Router { get; internal set; }

internal HttpContext(HttpServer httpServer, HttpRequest request, Route? matchedRoute, ListeningHost host)
{
this.Request = request;
this.HttpServer = httpServer;
this.MatchedRoute = matchedRoute;
this.ListeningHost = host;
this.Router = null!; // will be associated later in the router executor
}
}
}
Loading

0 comments on commit 307ab06

Please sign in to comment.