Skip to content

Commit

Permalink
0.13, core and svc
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed May 20, 2023
1 parent 54b0a85 commit e06390c
Show file tree
Hide file tree
Showing 22 changed files with 912 additions and 682 deletions.
23 changes: 11 additions & 12 deletions service/Provider/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ internal static void ParseConfiguration(ServiceProvider prov, bool softReload)
fac.Setup(parameters);
var router = fac.BuildRouter();

ListeningHost host = new ListeningHost(config.ListeningHost.Hostname, config.ListeningHost.Ports);
ListeningHost host = new ListeningHost();
host.Router = router;
host.Label = config.ListeningHost.Label;
host.Ports = config.ListeningHost.Ports.Select(s => new ListeningPort(s)).ToArray();

if (config.ListeningHost.CrossOriginResourceSharingPolicy?.MaxAge != null)
host.CrossOriginResourceSharingPolicy.MaxAge = TimeSpan.FromSeconds((double)config.ListeningHost.CrossOriginResourceSharingPolicy.MaxAge);
Expand All @@ -113,19 +114,20 @@ internal static void ParseConfiguration(ServiceProvider prov, bool softReload)

prov.ServerConfiguration.ListeningHosts.Add(host);

if (prov.__cfg != null)
if (prov.HttpServer == null)
{
prov.__cfg(new ServiceProviderConfigurator(prov.ServerConfiguration, prov));
prov.HttpServer = new HttpServer(prov.ServerConfiguration);
}

if (prov.HttpServer == null)
if (prov.__cfg != null)
{
prov.HttpServer = new HttpServer(prov.ServerConfiguration);
prov.__cfg(new ServiceProviderConfigurator(prov.HttpServer, prov.ServerConfiguration, prov));
}

fac.Bootstrap();

try
{
fac.Bootstrap();
prov.HttpServer.Start();
}
catch (Exception ex)
Expand All @@ -136,11 +138,9 @@ internal static void ParseConfiguration(ServiceProvider prov, bool softReload)

if (prov.Verbose)
{
foreach (ListeningPort p in config.ListeningHost.Ports)
foreach (ListeningPort p in host.Ports)
{
string portStr = "";
if (p.Port != 443 && p.Port != 80) portStr = ":" + p.Port;
Console.WriteLine($"{config.ListeningHost.Label ?? "Sisk"} service is listening on {(p.Secure ? "https" : "http")}://{config.ListeningHost.Hostname}{portStr}/");
Console.WriteLine($"{config.ListeningHost.Label ?? "Sisk"} service is listening on {p}");
}
}
}
Expand Down Expand Up @@ -180,9 +180,8 @@ internal class ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPol
[JsonSerializable(typeof(ConfigStructureFile__ListeningHost))]
internal class ConfigStructureFile__ListeningHost
{
public string Hostname { get; set; } = "";
public string? Label { get; set; }
public ListeningPort[]? Ports { get; set; }
public string[]? Ports { get; set; }

public ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPolicy? CrossOriginResourceSharingPolicy { get; set; }

Expand Down
39 changes: 36 additions & 3 deletions service/Provider/ServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sisk.Core.Http;
using Sisk.Core.Entity;
using Sisk.Core.Http;
using Sisk.Core.Routing;
using System.Globalization;
using System.Reflection.Metadata;
Expand Down Expand Up @@ -344,11 +345,13 @@ internal void Rebuild()
/// </type>
public class ServiceProviderConfigurator
{
private HttpServer _server;
private HttpServerConfiguration _config;
private ServiceProvider _provider;

internal ServiceProviderConfigurator(HttpServerConfiguration config, ServiceProvider provider)
internal ServiceProviderConfigurator(HttpServer server, HttpServerConfiguration config, ServiceProvider provider)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
_config = config ?? throw new ArgumentNullException(nameof(config));
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
}
Expand Down Expand Up @@ -407,9 +410,39 @@ public void UseFlags(HttpServerFlags flags)
/// <type>
/// Method
/// </type>
public void UseOverrides(Action<HttpServerConfiguration> overrideCallback)
public void UseConfiguration(Action<HttpServerConfiguration> overrideCallback)
{
overrideCallback(_config);
}

/// <summary>
/// Calls a callback that has the HTTP server instance as an argument.
/// </summary>
/// <param name="serverCallback">An action where the first argument is the main <see cref="HttpServer"/> object.</param>
/// <definition>
/// public void UseHttpServer(Action serverCallback)
/// </definition>
/// <type>
/// Method
/// </type>
public void UseHttpServer(Action<HttpServer> serverCallback)
{
serverCallback(_server);
}

/// <summary>
/// Calls a callback that has <see cref="CrossOriginResourceSharingHeaders"/> instance from the main listening host as an argument.
/// </summary>
/// <param name="corsCallback">An action where the first argument is the main <see cref="CrossOriginResourceSharingHeaders"/> object.</param>
/// <definition>
/// public void UseCors(Action corsCallback)
/// </definition>
/// <type>
/// Method
/// </type>
public void UseCors(Action<CrossOriginResourceSharingHeaders> corsCallback)
{
corsCallback(_config.ListeningHosts[0].CrossOriginResourceSharingPolicy);
}
}
}
6 changes: 3 additions & 3 deletions service/Sisk.ServiceProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>Sisk.ServiceProvider</Title>
<PackageId>Sisk.ServiceProvider</PackageId>
<Version>0.12</Version>
<Authors>CypherPotato</Authors>
<Company>Project Principium</Company>
<Product>Sisk.ServiceProvider</Product>
Expand All @@ -19,8 +18,9 @@
<RepositoryUrl>https://github.com/sisk-http/core</RepositoryUrl>
<PackageTags>http-server,http,web framework</PackageTags>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>0.12</AssemblyVersion>
<FileVersion>0.12</FileVersion>
<Version>0.13</Version>
<AssemblyVersion>0.13</AssemblyVersion>
<FileVersion>0.13</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<IncludeSymbols>True</IncludeSymbols>
Expand Down
2 changes: 1 addition & 1 deletion src/Http/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class HttpContext
/// <namespace>
/// Sisk.Core.Http
/// </namespace>
public Route? MatchedRoute { get; private set; }
public Route? MatchedRoute { get; internal set; }

internal HttpContext(Dictionary<string, object?> requestBag, HttpServer? httpServer, Route? matchedRoute)
{
Expand Down
28 changes: 27 additions & 1 deletion src/Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public sealed class HttpRequest
internal bool isStreaming;
private HttpRequestEventSource? activeEventSource;
private bool isContentAvailable = false;
private NameValueCollection headers = new NameValueCollection();

internal HttpRequest(
HttpListenerRequest listenerRequest,
Expand Down Expand Up @@ -116,9 +117,34 @@ internal HttpRequest(
}
}

// normalize headers encoding
if (contextServerConfiguration.Flags.NormalizeHeadersEncodings)
{
Encoding entryCodepage = Encoding.GetEncoding("ISO-8859-1");
foreach (string headerName in listenerRequest.Headers)
{
string headerValue = listenerRequest.Headers[headerName]!;
headers.Add(
headerName,
mbConvertCodepage(headerValue, entryCodepage, listenerRequest.ContentEncoding)
);
}
}
else
{
headers = listenerRequest.Headers;
}

this.context = context;
}

internal string mbConvertCodepage(string input, Encoding inEnc, Encoding outEnc)
{
byte[] tempBytes;
tempBytes = inEnc.GetBytes(input);
return outEnc.GetString(tempBytes);
}

#pragma warning disable
~HttpRequest()
{
Expand Down Expand Up @@ -209,7 +235,7 @@ internal void ImportContents(Stream listenerRequest)
/// </namespace>
public NameValueCollection Headers
{
get => listenerRequest.Headers;
get => headers;
}

/// <summary>
Expand Down
20 changes: 2 additions & 18 deletions src/Http/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public class HttpResponse
internal const byte HTTPRESPONSE_CLOSE = 16;
internal int CalculedLength = 0;

#pragma warning disable
~HttpResponse()
{
this.Content = null;
this.Headers = null;
}
#pragma warning restore

/// <summary>
/// Gets or sets an custom HTTP status code and description for this HTTP response. If this property ins't null, it will overwrite
/// the <see cref="Status"/> property in this class.
Expand Down Expand Up @@ -155,7 +147,7 @@ internal HttpResponse(HttpListenerResponse res)
public string GetRawHttpResponse(bool includeBody = true)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"HTTP/1.1 {(int)Status} {Regex.Replace(Status.ToString(), "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled)}");
sb.AppendLine($"HTTP/1.1 {(int)Status}");
foreach (string header in this.Headers)
{
sb.Append(header + ": ");
Expand Down Expand Up @@ -296,7 +288,7 @@ public void SetCookie(string name, string value, DateTime? expires, TimeSpan? ma
}
if (sameSite != null)
{
syntax.Add($"SameSite=${sameSite}");
syntax.Add($"SameSite={sameSite}");
}

Headers.Add("Set-Cookie", String.Join("; ", syntax));
Expand All @@ -313,13 +305,5 @@ public void SetCookie(string name, string value, DateTime? expires, TimeSpan? ma
}
return null;
}

internal long CalcHeadersSize()
{
long l = 0;
// RFC-5987 tells that headers should use UTF-8 characters.
l += Encoding.UTF8.GetByteCount(GetRawHttpResponse(false));
return l;
}
}
}
Loading

0 comments on commit e06390c

Please sign in to comment.