Skip to content

Commit 68f6aa1

Browse files
Add web-based authentication (#468)
1 parent 2e8d0b4 commit 68f6aa1

File tree

63 files changed

+2907
-454
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2907
-454
lines changed

API/Content/IContentInfoBuilder.cs

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
namespace GenHTTP.Api.Content
2-
{
3-
4-
/// <summary>
5-
/// Builders implementing this interface allow to
6-
/// customize the meta data of a content element
7-
/// such as a web page.
8-
/// </summary>
9-
/// <typeparam name="T">The type of the builder implementing this interface</typeparam>
10-
public interface IContentInfoBuilder<out T>
11-
{
12-
13-
/// <summary>
14-
/// Sets the title of the content element, e.g.
15-
/// for usage in a site map or as the title
16-
/// of a web page.
17-
/// </summary>
18-
/// <param name="title">The title to be set</param>
19-
T Title(string title);
20-
21-
/// <summary>
22-
/// Sets the description of the content element.
23-
/// </summary>
24-
/// <param name="description">The description to be set</param>
25-
T Description(string description);
26-
27-
}
28-
29-
}
1+
namespace GenHTTP.Api.Content
2+
{
3+
4+
/// <summary>
5+
/// Builders implementing this interface allow to
6+
/// customize the meta data of a content element
7+
/// such as a web page.
8+
/// </summary>
9+
/// <typeparam name="TBuilder">The type of the builder implementing this interface</typeparam>
10+
public interface IContentInfoBuilder<out TBuilder>
11+
{
12+
13+
/// <summary>
14+
/// Sets the title of the content element, e.g.
15+
/// for usage in a site map or as the title
16+
/// of a web page.
17+
/// </summary>
18+
/// <param name="title">The title to be set</param>
19+
TBuilder Title(string title);
20+
21+
/// <summary>
22+
/// Sets the description of the content element.
23+
/// </summary>
24+
/// <param name="description">The description to be set</param>
25+
TBuilder Description(string description);
26+
27+
}
28+
29+
}

API/Content/IPageAdditionBuilder.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace GenHTTP.Api.Content
2+
{
3+
4+
/// <summary>
5+
/// Provides an unified way to add additional stylesheet or
6+
/// script references to a page.
7+
/// </summary>
8+
/// <remarks>
9+
/// When creating a page, you might want to include another
10+
/// script or stylesheet only on this site. This interface
11+
/// should be implemented by all page handler builders
12+
/// to allow this.
13+
/// </remarks>
14+
/// <typeparam name="TBuilder">The type of the builder to enable the builder pattern</typeparam>
15+
public interface IPageAdditionBuilder<out TBuilder>
16+
{
17+
18+
/// <summary>
19+
/// Adds a script reference to be rendered on template level.
20+
/// </summary>
21+
/// <param name="path">The path to the script to be referenced (supports routing)</param>
22+
/// <param name="asynchronous">True, if the script should be loaded by the browser after the page has been rendered</param>
23+
/// <returns>The builder instance</returns>
24+
TBuilder AddScript(string path, bool asynchronous = false);
25+
26+
/// <summary>
27+
/// Adds a style sheet reference to be rendered on template level.
28+
/// </summary>
29+
/// <param name="path">The path to the style sheet to be referenced (supports routing)</param>
30+
/// <returns>The builder instance</returns>
31+
TBuilder AddStyle(string path);
32+
33+
}
34+
35+
}

API/Content/PageAdditionBuilder.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using GenHTTP.Api.Infrastructure;
2+
3+
namespace GenHTTP.Api.Content
4+
{
5+
6+
/// <summary>
7+
/// Can be used by handler builders to create a page addition object
8+
/// so that they can easily offer the page addition feature
9+
/// without the need of implementing everything on their own.
10+
/// </summary>
11+
public class PageAdditionBuilder : IBuilder<PageAdditions?>, IPageAdditionBuilder<PageAdditionBuilder>
12+
{
13+
private PageAdditions? _Additions;
14+
15+
public PageAdditionBuilder AddScript(string path, bool asynchronous = false)
16+
{
17+
EnsureAdditions().Scripts.Add(new(path, asynchronous));
18+
return this;
19+
}
20+
21+
public PageAdditionBuilder AddStyle(string path)
22+
{
23+
EnsureAdditions().Styles.Add(new(path));
24+
return this;
25+
}
26+
27+
private PageAdditions EnsureAdditions() => _Additions ??= PageAdditions.Create();
28+
29+
public PageAdditions? Build() => _Additions;
30+
31+
}
32+
33+
}

API/Content/PageAdditions.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
3+
using GenHTTP.Api.Content.Websites;
4+
5+
namespace GenHTTP.Api.Content
6+
{
7+
8+
/// <summary>
9+
/// Additional information that needs to be applied to
10+
/// a page being rendered within a template.
11+
/// </summary>
12+
/// <param name="Styles">Additional style references to be rendered</param>
13+
/// <param name="Scripts">Additional script references to be rendered</param>
14+
public record PageAdditions
15+
(
16+
17+
List<StyleReference> Styles,
18+
19+
List<ScriptReference> Scripts
20+
21+
)
22+
{
23+
24+
/// <summary>
25+
/// Creates an empty page addition object.
26+
/// </summary>
27+
/// <returns>The newly created object</returns>
28+
public static PageAdditions Create() => new(new(), new());
29+
30+
}
31+
32+
}

API/Content/Templating/TemplateModel.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Threading.Tasks;
2-
32
using GenHTTP.Api.Protocol;
3+
using static System.Net.WebRequestMethods;
44

55
namespace GenHTTP.Api.Content.Templating
66
{
@@ -19,6 +19,12 @@ public sealed class TemplateModel : AbstractModel
1919
/// </summary>
2020
public ContentInfo Meta { get; }
2121

22+
/// <summary>
23+
/// Additional references to styles or scripts to be included
24+
/// when rendering the template.
25+
/// </summary>
26+
public PageAdditions? Additions { get; }
27+
2228
/// <summary>
2329
/// The HTML content to be rendered within the template.
2430
/// </summary>
@@ -34,11 +40,14 @@ public sealed class TemplateModel : AbstractModel
3440
/// <param name="request">The request which caused this call</param>
3541
/// <param name="handler">The handler responsible to render the response</param>
3642
/// <param name="pageInfo">Information about the page to be rendered</param>
43+
/// <param name="additions">Additional references to required scripts or styles</param>
3744
/// <param name="content">The content to be rendered within the template</param>
38-
public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, string content) : base(request, handler)
45+
public TemplateModel(IRequest request, IHandler handler, ContentInfo pageInfo, PageAdditions? additions, string content) : base(request, handler)
3946
{
4047
Content = content;
4148

49+
Additions = additions;
50+
4251
Meta = pageInfo;
4352

4453
if (string.IsNullOrEmpty(Meta.Title))

API/Protocol/IRequestProperties.cs

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public interface IRequestProperties : IDisposable
2828
/// <returns>True if the value could be read, false otherwise</returns>
2929
bool TryGet<T>(string key, [MaybeNullWhen(returnValue: false)] out T entry);
3030

31+
/// <summary>
32+
/// Removes the entry with the given name.
33+
/// </summary>
34+
/// <param name="key">The entry to be removed</param>
35+
void Clear(string key);
36+
3137
}
3238

3339
}

API/Protocol/IResponse.cs

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public interface IResponse : IDisposable
5252
/// </summary>
5353
bool HasCookies { get; }
5454

55+
/// <summary>
56+
/// Adds the given cookie to the cookie collection of this response.
57+
/// </summary>
58+
/// <param name="cookie">The cookie to be added</param>
59+
void SetCookie(Cookie cookie);
60+
5561
#endregion
5662

5763
#region Content

API/Protocol/IResponseModification.cs

+76-68
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,76 @@
1-
using System;
2-
3-
namespace GenHTTP.Api.Protocol
4-
{
5-
6-
/// <summary>
7-
/// The protocol allowing to manipulate the response sent by
8-
/// the server.
9-
/// </summary>
10-
/// <typeparam name="T">The type of builder used as a return value</typeparam>
11-
public interface IResponseModification<out T>
12-
{
13-
14-
/// <summary>
15-
/// Specifies the HTTP status code of the response.
16-
/// </summary>
17-
/// <param name="status">The HTTP status code of the response</param>
18-
T Status(ResponseStatus status);
19-
20-
/// <summary>
21-
/// Specifies the HTTP status code of the response.
22-
/// </summary>
23-
/// <param name="status">The status code of the response</param>
24-
/// <param name="reason">The reason phrase of the response (such as "Not Found" for 404)</param>
25-
T Status(int status, string reason);
26-
27-
/// <summary>
28-
/// Sets the given header field on the response. Changing HTTP
29-
/// protocol headers may cause incorrect behavior.
30-
/// </summary>
31-
/// <param name="key">The name of the header to be set</param>
32-
/// <param name="value">The value of the header field</param>
33-
T Header(string key, string value);
34-
35-
/// <summary>
36-
/// Sets the expiration date of the response.
37-
/// </summary>
38-
/// <param name="expiryDate">The expiration date of the response</param>
39-
T Expires(DateTime expiryDate);
40-
41-
/// <summary>
42-
/// Sets the point in time when the requested resource has been
43-
/// modified last.
44-
/// </summary>
45-
/// <param name="modificationDate">The point in time when the requested resource has been modified last</param>
46-
T Modified(DateTime modificationDate);
47-
48-
/// <summary>
49-
/// Adds the given cookie to the response.
50-
/// </summary>
51-
/// <param name="cookie">The cookie to be added</param>
52-
T Cookie(Cookie cookie);
53-
54-
/// <summary>
55-
/// Specifies the content type of this response.
56-
/// </summary>
57-
/// <param name="contentType">The content type of this response</param>
58-
T Type(FlexibleContentType contentType);
59-
60-
/// <summary>
61-
/// Sets the encoding of the content.
62-
/// </summary>
63-
/// <param name="encoding">The encoding of the content</param>
64-
T Encoding(string encoding);
65-
66-
}
67-
68-
}
1+
using System;
2+
3+
namespace GenHTTP.Api.Protocol
4+
{
5+
6+
/// <summary>
7+
/// Allows the response generated by a builder or handler to be
8+
/// adjusted.
9+
/// </summary>
10+
/// <remarks>
11+
/// This can be useful if you would like to add behavior that the
12+
/// original handler (such as a page renderer) does not provide.
13+
///
14+
/// For example, as the page handlers implement this interface,
15+
/// you can add an additional header to the response being generated
16+
/// for a page.
17+
/// </remarks>
18+
/// <typeparam name="TBuilder">The type of builder used as a return value</typeparam>
19+
public interface IResponseModification<out TBuilder>
20+
{
21+
22+
/// <summary>
23+
/// Specifies the HTTP status code of the response.
24+
/// </summary>
25+
/// <param name="status">The HTTP status code of the response</param>
26+
TBuilder Status(ResponseStatus status);
27+
28+
/// <summary>
29+
/// Specifies the HTTP status code of the response.
30+
/// </summary>
31+
/// <param name="status">The status code of the response</param>
32+
/// <param name="reason">The reason phrase of the response (such as "Not Found" for 404)</param>
33+
TBuilder Status(int status, string reason);
34+
35+
/// <summary>
36+
/// Sets the given header field on the response. Changing HTTP
37+
/// protocol headers may cause incorrect behavior.
38+
/// </summary>
39+
/// <param name="key">The name of the header to be set</param>
40+
/// <param name="value">The value of the header field</param>
41+
TBuilder Header(string key, string value);
42+
43+
/// <summary>
44+
/// Sets the expiration date of the response.
45+
/// </summary>
46+
/// <param name="expiryDate">The expiration date of the response</param>
47+
TBuilder Expires(DateTime expiryDate);
48+
49+
/// <summary>
50+
/// Sets the point in time when the requested resource has been
51+
/// modified last.
52+
/// </summary>
53+
/// <param name="modificationDate">The point in time when the requested resource has been modified last</param>
54+
TBuilder Modified(DateTime modificationDate);
55+
56+
/// <summary>
57+
/// Adds the given cookie to the response.
58+
/// </summary>
59+
/// <param name="cookie">The cookie to be added</param>
60+
TBuilder Cookie(Cookie cookie);
61+
62+
/// <summary>
63+
/// Specifies the content type of this response.
64+
/// </summary>
65+
/// <param name="contentType">The content type of this response</param>
66+
TBuilder Type(FlexibleContentType contentType);
67+
68+
/// <summary>
69+
/// Sets the encoding of the content.
70+
/// </summary>
71+
/// <param name="encoding">The encoding of the content</param>
72+
TBuilder Encoding(string encoding);
73+
74+
}
75+
76+
}

0 commit comments

Comments
 (0)