Skip to content

Commit

Permalink
Adapt major version, fix checksum handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Jun 14, 2021
1 parent 9638817 commit 65df25c
Show file tree
Hide file tree
Showing 69 changed files with 537 additions and 305 deletions.
28 changes: 28 additions & 0 deletions API/Content/ContentElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ public ContentElement(string path, ContentInfo details, FlexibleContentType cont

#endregion

#region Functionality

public ulong CalculateChecksum() => CalculateChecksum(this);

private static ulong CalculateChecksum(ContentElement element)
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)element.Path.GetHashCode();
hash = hash * 23 + (uint)element.Details.GetHashCode();
hash = hash * 23 + (uint)element.ContentType.GetHashCode();

if (element.Children != null)
{
foreach (var child in element.Children)
{
hash = hash * 23 + CalculateChecksum(child);
}
}

return hash;
}
}

#endregion

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -7,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// Model for pages which can be served by an application
/// to the clients.
/// </summary>
public class PageModel : IBaseModel
public abstract class AbstractModel : IModel
{

#region Get-/Setters
Expand All @@ -24,21 +26,27 @@ public class PageModel : IBaseModel

#endregion

#region Functionality
#region Initialization

/// <summary>
/// Creates a new page model for the given request.
/// </summary>
/// <param name="request">The request which caused this rendering call</param>
/// <param name="handler">The handler responsible to render the response</param>
public PageModel(IRequest request, IHandler handler)
public AbstractModel(IRequest request, IHandler handler)
{
Request = request;
Handler = handler;
}

#endregion

#region Functionality

public abstract ValueTask<ulong> CalculateChecksumAsync();

#endregion

}

}
32 changes: 32 additions & 0 deletions API/Content/Templating/BasicModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{

/// <summary>
/// Simple page model with no further information than the mandatory request
/// and handler values.
/// </summary>
public class BasicModel : AbstractModel
{

#region Initialization

public BasicModel(IRequest request, IHandler handler) : base(request, handler)
{

}

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync() => new(17);

#endregion

}

}
24 changes: 23 additions & 1 deletion API/Content/Templating/ErrorModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

Expand All @@ -8,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// <summary>
/// Model for errors that occur when handling requests.
/// </summary>
public sealed class ErrorModel : PageModel
public sealed class ErrorModel : AbstractModel
{

#region Get-/Setters
Expand Down Expand Up @@ -48,6 +49,27 @@ public ErrorModel(IRequest request, IHandler handler, ResponseStatus status, str

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync()
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)Status;

hash = hash * 23 + (uint)Message.GetHashCode();

hash = hash * 23 + (uint)(DevelopmentMode ? 1 : 0);
hash = hash * 23 + (uint)(Cause?.GetHashCode() ?? 0);

return new(hash);
}
}

#endregion

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -7,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// Interface which needs to be implemented by all models
/// used to render pages or templates.
/// </summary>
public interface IBaseModel
public interface IModel
{

/// <summary>
Expand All @@ -20,6 +22,16 @@ public interface IBaseModel
/// </summary>
IHandler Handler { get; }

/// <summary>
/// Calculates the checksum of this model.
/// </summary>
/// <returns>The checksum of this model</returns>
/// <remarks>
/// Required to determine, whether an already cached page
/// needs to be refreshed or not.
/// </remarks>
ValueTask<ulong> CalculateChecksumAsync();

}

}
2 changes: 1 addition & 1 deletion API/Content/Templating/IRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GenHTTP.Api.Content.Templating
/// Allows to render models of the given type.
/// </summary>
/// <typeparam name="T">The type of the model to be rendered</typeparam>
public interface IRenderer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] in T> where T : class, IBaseModel
public interface IRenderer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] in T> where T : class, IModel
{

/// <summary>
Expand Down
36 changes: 21 additions & 15 deletions API/Content/Templating/TemplateModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -7,7 +9,7 @@ namespace GenHTTP.Api.Content.Templating
/// Model used by templates to render the actual HTML returned
/// to the client.
/// </summary>
public sealed class TemplateModel : IBaseModel
public sealed class TemplateModel : AbstractModel
{

#region Get-/Setters
Expand All @@ -22,16 +24,6 @@ public sealed class TemplateModel : IBaseModel
/// </summary>
public string Content { get; }

/// <summary>
/// The request which caused this call.
/// </summary>
public IRequest Request { get; }

/// <summary>
/// The handler responsible to render the response.
/// </summary>
public IHandler Handler { get; }

#endregion

#region Initialization
Expand All @@ -43,7 +35,7 @@ public sealed class TemplateModel : IBaseModel
/// <param name="handler">The handler responsible to render the response</param>
/// <param name="pageInfo">Information about the page to be rendered</param>
/// <param name="content">The content to be rendered within the template</param>
public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, string content)
public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, string content) : base(request, handler)
{
Content = content;

Expand All @@ -53,9 +45,23 @@ public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, s
{
Meta = Meta with { Title = "Untitled Page" };
}
}

Request = request;
Handler = handler;
#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync()
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)Content.GetHashCode();
hash = hash * 23 + (uint)Meta.GetHashCode();

return new(hash);
}
}

#endregion
Expand Down
9 changes: 8 additions & 1 deletion API/Content/Templating/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GenHTTP.Api.Protocol;
using System.Threading.Tasks;

namespace GenHTTP.Api.Content.Templating
{
Expand All @@ -22,7 +23,7 @@ public ViewModel(IRequest request, IHandler handler) : base(request, handler, nu

}

public class ViewModel<T> : PageModel where T : class
public class ViewModel<T> : AbstractModel where T : class
{

#region Get-/Setters
Expand All @@ -40,6 +41,12 @@ public ViewModel(IRequest request, IHandler handler, T? data) : base(request, ha

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync() => new((ulong)(Data?.GetHashCode() ?? 17));

#endregion

}

}
52 changes: 37 additions & 15 deletions API/Content/Websites/WebsiteModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;

using GenHTTP.Api.Content.Templating;
using GenHTTP.Api.Protocol;
Expand All @@ -10,7 +11,7 @@ namespace GenHTTP.Api.Content.Websites
/// All information that is needed by a theme to render
/// a web page for a website.
/// </summary>
public sealed class WebsiteModel : IBaseModel
public sealed class WebsiteModel : AbstractModel
{

#region Get-/Setters
Expand All @@ -33,16 +34,6 @@ public sealed class WebsiteModel : IBaseModel
/// </summary>
public List<StyleReference> Styles { get; }

/// <summary>
/// The currently handled request.
/// </summary>
public IRequest Request { get; }

/// <summary>
/// The currently executed handler.
/// </summary>
public IHandler Handler { get; }

/// <summary>
/// The theme that is responsible to generate
/// the page.
Expand Down Expand Up @@ -70,11 +61,8 @@ public sealed class WebsiteModel : IBaseModel
object? themeModel,
List<ContentElement> menu,
List<ScriptReference> scripts,
List<StyleReference> styles)
List<StyleReference> styles) : base(request, handler)
{
Request = request;
Handler = handler;

Content = content;
Scripts = scripts;
Styles = styles;
Expand All @@ -87,6 +75,40 @@ public sealed class WebsiteModel : IBaseModel

#endregion

#region Functionality

public override ValueTask<ulong> CalculateChecksumAsync()
{
unchecked
{
ulong hash = 17;

hash = hash * 23 + (uint)Content.Content.GetHashCode();
hash = hash * 23 + (uint)Content.Meta.GetHashCode();

foreach (var script in Scripts)
{
hash = hash * 23 + (uint)script.GetHashCode();
}

foreach (var style in Styles)
{
hash = hash * 23 + (uint)style.GetHashCode();
}

foreach (var item in Menu)
{
hash = hash * 23 + item.CalculateChecksum();
}

hash = hash * 23 + (uint)(Model?.GetHashCode() ?? 0);

return new(hash);
}
}

#endregion

}

}
6 changes: 3 additions & 3 deletions API/GenHTTP.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<AssemblyVersion>5.3.0.0</AssemblyVersion>
<FileVersion>5.3.0.0</FileVersion>
<Version>5.3.0</Version>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<FileVersion>6.0.0.0</FileVersion>
<Version>6.0.0</Version>

<Authors>Andreas Nägeli</Authors>
<Company />
Expand Down
Loading

0 comments on commit 65df25c

Please sign in to comment.