Skip to content

Commit

Permalink
Fix issues reported by Rider (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat authored Jan 21, 2025
1 parent a741cbf commit df6d6f9
Show file tree
Hide file tree
Showing 44 changed files with 134 additions and 212 deletions.
5 changes: 3 additions & 2 deletions API/Content/IErrorMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IErrorMapper<in T> where T : Exception
/// <param name="handler">The handler which catched the exception</param>
/// <param name="error">The actual exception to be mapped</param>
/// <returns>
/// A HTTP response to be sent or null, if the error should be handled as not found by the next error handler in
/// An HTTP response to be sent or null, if the error should be handled as not found by the next error handler in
/// the chain
/// </returns>
ValueTask<IResponse?> Map(IRequest request, IHandler handler, T error);
Expand All @@ -28,8 +28,9 @@ public interface IErrorMapper<in T> where T : Exception
/// <param name="request">The currently handled request</param>
/// <param name="handler">The inner handler of the error handling concern</param>
/// <returns>
/// A HTTP response to be sent or null, if the error should be handled as not found by the next error handler in
/// An HTTP response to be sent or null, if the error should be handled as not found by the next error handler in
/// the chain
/// </returns>
ValueTask<IResponse?> GetNotFound(IRequest request, IHandler handler);

}
1 change: 1 addition & 0 deletions API/Protocol/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ public static FlexibleContentType Parse(string header)
{
return Get(contentType, span[(charsetIndex + 1)..].Trim().ToString());
}

return Get(contentType);
}
return Get(header);
Expand Down
2 changes: 1 addition & 1 deletion API/Protocol/IRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public interface IRequest : IDisposable
string? UserAgent { get; }

/// <summary>
/// The referrer which caused the invociation of this request, if any.
/// The referrer which caused the invocation of this request, if any.
/// </summary>
string? Referer { get; }

Expand Down
8 changes: 4 additions & 4 deletions API/Protocol/ResponseStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public readonly struct FlexibleResponseStatus

#region Mapping

private static readonly Dictionary<ResponseStatus, string> MAPPING = new()
private static readonly Dictionary<ResponseStatus, string> Mapping = new()
{
{
ResponseStatus.Accepted, "Accepted"
Expand Down Expand Up @@ -295,7 +295,7 @@ public readonly struct FlexibleResponseStatus
}
};

private static readonly Dictionary<int, ResponseStatus> CODE_MAPPING = MAPPING.Keys.ToDictionary(k => (int)k, k => k);
private static readonly Dictionary<int, ResponseStatus> CodeMapping = Mapping.Keys.ToDictionary(k => (int)k, k => k);

#endregion

Expand All @@ -306,7 +306,7 @@ public FlexibleResponseStatus(int status, string phrase)
RawStatus = status;
Phrase = phrase;

if (CODE_MAPPING.TryGetValue(status, out var known))
if (CodeMapping.TryGetValue(status, out var known))
{
KnownStatus = known;
}
Expand All @@ -321,7 +321,7 @@ public FlexibleResponseStatus(ResponseStatus status)
KnownStatus = status;

RawStatus = (int)status;
Phrase = MAPPING[status];
Phrase = Mapping[status];
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions API/Routing/RoutingTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// </remarks>
public sealed class RoutingTarget
{
private static readonly List<WebPathPart> EMPTY_LIST = [];
private static readonly List<WebPathPart> EmptyList = [];

private int _Index;

Expand Down Expand Up @@ -74,7 +74,7 @@ public WebPath GetRemaining()
{
var remaining = Path.Parts.Count - _Index;

var resultList = remaining > 0 ? new List<WebPathPart>(remaining) : EMPTY_LIST;
var resultList = remaining > 0 ? new List<WebPathPart>(remaining) : EmptyList;

for (var i = _Index; i < Path.Parts.Count; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions Adapters/AspNetCore/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void Map(this WebApplication app, string path, IHandlerBuilder han
/// <param name="handler">The handler to be registered</param>
/// <param name="companion">An object that will be informed about handled requests and any error</param>
public static void Map(this WebApplication app, string path, IHandler handler, IServerCompanion? companion = null)
=> app.Map(path + "/{*any}", async (context) => await Bridge.MapAsync(context, handler, companion: companion, registeredPath: path));
=> app.Map(path + "/{*any}", async context => await Bridge.MapAsync(context, handler, companion: companion, registeredPath: path));

/// <summary>
/// Registers the given handler to respond to any request.
Expand All @@ -42,7 +42,7 @@ public static void Map(this WebApplication app, string path, IHandler handler, I
/// <param name="handler">The handler to be registered</param>
/// <param name="server">The server instance that would like to execute requests</param>
public static void Run(this IApplicationBuilder app, IHandler handler, IServer server)
=> app.Run(async (context) => await Bridge.MapAsync(context, handler, server));
=> app.Run(async context => await Bridge.MapAsync(context, handler, server));

/// <summary>
/// Enables default features on the given handler. This should be used on the
Expand Down
5 changes: 1 addition & 4 deletions Adapters/AspNetCore/Server/EmptyEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

namespace GenHTTP.Adapters.AspNetCore.Server;

public class EmptyEndpoints : List<IEndPoint>, IEndPointCollection
{

}
public class EmptyEndpoints : List<IEndPoint>, IEndPointCollection;
2 changes: 1 addition & 1 deletion Adapters/AspNetCore/Types/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public IHeaderCollection Headers
get { return _Headers ??= new Headers(Context); }
}

public Stream? Content => Context.BodyReader.AsStream(true);
public Stream Content => Context.BodyReader.AsStream(true);

public FlexibleContentType? ContentType => (Context.ContentType != null) ? new(Context.ContentType) : null;

Expand Down
5 changes: 1 addition & 4 deletions Engine/Internal/Infrastructure/Endpoints/SecureEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ internal SecureEndPoint(IServer server, IPAddress? address, ushort port, Securit
{
EnabledSslProtocols = Options.Protocols,
AllowRenegotiation = true,
ApplicationProtocols = new List<SslApplicationProtocol>
{
SslApplicationProtocol.Http11
},
ApplicationProtocols = [ SslApplicationProtocol.Http11 ],
EncryptionPolicy = EncryptionPolicy.RequireEncryption,
ServerCertificateSelectionCallback = SelectCertificate,
ClientCertificateRequired = Options.CertificateValidator?.RequireCertificate ?? false,
Expand Down
4 changes: 2 additions & 2 deletions Engine/Internal/Protocol/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ private async ValueTask<ConnectionStatus> HandlePipe(PipeReader reader)

var parser = new RequestParser(Configuration);

RequestBuilder? request;

try
{
RequestBuilder? request;

while (Server.Running && (request = await parser.TryParseAsync(buffer)) is not null)
{
var status = await HandleRequest(request, !buffer.ReadRequired);
Expand Down
5 changes: 1 addition & 4 deletions Engine/Kestrel/Hosting/KestrelEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

namespace GenHTTP.Engine.Kestrel.Hosting;

public sealed class KestrelEndpoints : List<IEndPoint>, IEndPointCollection
{

}
public sealed class KestrelEndpoints : List<IEndPoint>, IEndPointCollection;
2 changes: 1 addition & 1 deletion Engine/Shared/Security/SimpleCertificateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public SimpleCertificateProvider(X509Certificate2 certificate)

#region Get-/Setters

internal X509Certificate2 Certificate { get; }
public X509Certificate2 Certificate { get; }

#endregion

Expand Down
13 changes: 1 addition & 12 deletions Engine/Shared/Types/PooledDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,7 @@ public class PooledDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOn

#region Get-/Setters

private KeyValuePair<TKey, TValue>[] Entries
{
get
{
if (_Entries is null)
{
_Entries = Pool.Rent(Capacity);
}

return _Entries!;
}
}
private KeyValuePair<TKey, TValue>[] Entries => _Entries ??= Pool.Rent(Capacity);

private bool HasEntries => _Entries is not null;

Expand Down
2 changes: 1 addition & 1 deletion Engine/Shared/Types/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void SetCookie(Cookie cookie)

public ulong? ContentLength { get; set; }

public bool Upgraded { get; set; }
public bool Upgraded { get; init; }

public IResponseContent? Content { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Modules/ApiBrowsing/Common/BrowserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public BrowserHandler(string resourceRoot, BrowserMetaData metaData)
{
if (!request.HasType(RequestMethod.Get, RequestMethod.Head))
{
throw new ProviderException(ResponseStatus.MethodNotAllowed, "Only GET requests are allowed by this handler", (b) => b.Header("Allow", "GET"));
throw new ProviderException(ResponseStatus.MethodNotAllowed, "Only GET requests are allowed by this handler", b => b.Header("Allow", "GET"));
}

if (request.Target.Ended)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class MultiAuthenticationConcernBuilder : IConcernBuilder
{
#region Fields

private readonly List<IConcernBuilder> _delegatingConcernsBuilders = [];
private readonly List<IConcernBuilder> _DelegatingConcernsBuilders = [];

#endregion

Expand All @@ -24,7 +24,7 @@ private MultiAuthenticationConcernBuilder AddIfNotNull(IConcernBuilder concernBu
{
if (concernBuilder == null) return this;

_delegatingConcernsBuilders.Add(concernBuilder);
_DelegatingConcernsBuilders.Add(concernBuilder);
return this;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public MultiAuthenticationConcernBuilder Add(ClientCertificateAuthenticationBuil
/// <returns></returns>
public IConcern Build(IHandler content)
{
var delegatingConcerns = _delegatingConcernsBuilders.Select(x => x.Build(content)).ToArray();
var delegatingConcerns = _DelegatingConcernsBuilders.Select(x => x.Build(content)).ToArray();
if (delegatingConcerns.Length == 0) throw new BuilderMissingPropertyException("Concerns");

return new MultiAuthenticationConcern(
Expand Down
2 changes: 1 addition & 1 deletion Modules/Authentication/Roles/RoleInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Configure(object attribute)

if (missing.Count > 0)
{
throw new ProviderException(ResponseStatus.Forbidden, $"User is not authorized to access this endpoint.");
throw new ProviderException(ResponseStatus.Forbidden, "User is not authorized to access this endpoint.");
}
}

Expand Down
7 changes: 1 addition & 6 deletions Modules/Caching/Memory/StreamMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ public async ValueTask<Stream[]> GetEntriesAsync(string key)
{
var entry = await _Cache.GetEntryAsync(key, variation);

if (entry != null)
{
return new MemoryStream(entry);
}

return null;
return (entry != null) ? new MemoryStream(entry) : null;
}

public async ValueTask StoreAsync(string key, string variation, Stream? entry)
Expand Down
2 changes: 1 addition & 1 deletion Modules/ClientCaching/Validation/CacheValidationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public CacheValidationHandler(IHandler content)

if (request.HasType(SupportedMethods))
{
if (response is not null && response.Content is not null)
if (response?.Content != null)
{
var eTag = await CalculateETag(response);

Expand Down
53 changes: 12 additions & 41 deletions Modules/Functional/Provider/InlineBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GenHTTP.Api.Content;
using GenHTTP.Api.Infrastructure;
using GenHTTP.Api.Protocol;

using GenHTTP.Modules.Conversion;
using GenHTTP.Modules.Conversion.Formatters;
using GenHTTP.Modules.Conversion.Serializers;
Expand All @@ -11,7 +12,7 @@ namespace GenHTTP.Modules.Functional.Provider;

public class InlineBuilder : IHandlerBuilder<InlineBuilder>
{
private static readonly HashSet<FlexibleRequestMethod> AllMethods = [..Enum.GetValues<RequestMethod>().Select(m => FlexibleRequestMethod.Get(m))];
private static readonly HashSet<FlexibleRequestMethod> AllMethods = [..Enum.GetValues<RequestMethod>().Select(FlexibleRequestMethod.Get)];

private readonly List<IConcernBuilder> _Concerns = [];

Expand Down Expand Up @@ -73,92 +74,62 @@ public InlineBuilder Formatters(IBuilder<FormatterRegistry> registry)
/// Adds a route for a GET request to the root of the handler.
/// </summary>
/// <param name="function">The logic to be executed</param>
public InlineBuilder Get(Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Get)
});
public InlineBuilder Get(Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Get)]);

/// <summary>
/// Adds a route for a GET request to the specified path.
/// </summary>
/// <param name="path">The path of the request to handle (e.g. "/my-method")</param>
/// <param name="function">The logic to be executed</param>
public InlineBuilder Get(string path, Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Get)
}, path);
public InlineBuilder Get(string path, Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Get)], path);

/// <summary>
/// Adds a route for a HEAD request to the root of the handler.
/// </summary>
/// <param name="function">The logic to be executed</param>
public InlineBuilder Head(Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Head)
});
public InlineBuilder Head(Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Head)]);

/// <summary>
/// Adds a route for a HEAD request to the specified path.
/// </summary>
/// <param name="path">The path of the request to handle (e.g. "/my-method")</param>
public InlineBuilder Head(string path, Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Head)
}, path);
public InlineBuilder Head(string path, Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Head)], path);

/// <summary>
/// Adds a route for a POST request to the root of the handler.
/// </summary>
/// <param name="function">The logic to be executed</param>
public InlineBuilder Post(Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Post)
});
public InlineBuilder Post(Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Post)]);

/// <summary>
/// Adds a route for a POST request to the specified path.
/// </summary>
/// <param name="path">The path of the request to handle (e.g. "/my-method")</param>
public InlineBuilder Post(string path, Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Post)
}, path);
public InlineBuilder Post(string path, Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Post)], path);

/// <summary>
/// Adds a route for a PUT request to the root of the handler.
/// </summary>
/// <param name="function">The logic to be executed</param>
public InlineBuilder Put(Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Put)
});
public InlineBuilder Put(Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Put)]);

/// <summary>
/// Adds a route for a PUT request to the specified path.
/// </summary>
/// <param name="path">The path of the request to handle (e.g. "/my-method")</param>
public InlineBuilder Put(string path, Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Put)
}, path);
public InlineBuilder Put(string path, Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Put)], path);

/// <summary>
/// Adds a route for a DELETE request to the root of the handler.
/// </summary>
/// <param name="function">The logic to be executed</param>
public InlineBuilder Delete(Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Delete)
});
public InlineBuilder Delete(Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Delete)]);

/// <summary>
/// Adds a route for a DELETE request to the specified path.
/// </summary>
/// <param name="path">The path of the request to handle (e.g. "/my-method")</param>
public InlineBuilder Delete(string path, Delegate function) => On(function, new HashSet<FlexibleRequestMethod>
{
FlexibleRequestMethod.Get(RequestMethod.Delete)
}, path);
public InlineBuilder Delete(string path, Delegate function) => On(function, [FlexibleRequestMethod.Get(RequestMethod.Delete)], path);

/// <summary>
/// Executes the given function for the specified path and method.
Expand Down
Loading

0 comments on commit df6d6f9

Please sign in to comment.