Skip to content

Commit 1be1089

Browse files
Unify page manipulation APIs
1 parent 967c8d3 commit 1be1089

File tree

4 files changed

+94
-26
lines changed

4 files changed

+94
-26
lines changed

Modules/Authentication.Web/Concern/WebAuthenticationConcern.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ public WebAuthenticationConcern(IHandler parent, Func<IHandler, IHandler> conten
137137
{
138138
var response = await Content.HandleAsync(request);
139139

140-
if ((response != null) && (token != null))
140+
if (response != null)
141141
{
142142
// refresh the token, so the user will not be logged out eventually
143-
SessionHandling.WriteToken(response, token);
143+
SessionHandling.WriteToken(response, token!);
144144
}
145145

146146
return response;

Modules/Pages/Combined/CombinedPageBuilder.cs

+71-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Threading.Tasks;
45

56
using GenHTTP.Api.Content;
67
using GenHTTP.Api.Content.Templating;
7-
using GenHTTP.Api.Content.Websites;
88
using GenHTTP.Api.Protocol;
99

1010
namespace GenHTTP.Modules.Pages.Combined
1111
{
1212

13-
public class CombinedPageBuilder : IHandlerBuilder<CombinedPageBuilder>, IContentInfoBuilder<CombinedPageBuilder>
13+
public class CombinedPageBuilder :
14+
IHandlerBuilder<CombinedPageBuilder>,
15+
IContentInfoBuilder<CombinedPageBuilder>,
16+
IPageAdditionBuilder<CombinedPageBuilder>,
17+
IResponseModification<CombinedPageBuilder>
1418
{
1519
private readonly List<IConcernBuilder> _Concerns = new();
1620

1721
private readonly ContentInfoBuilder _Info = new();
1822

1923
private readonly List<PageFragment> _Fragments = new();
2024

21-
private readonly PageAdditions _Additions = PageAdditions.Create();
25+
private readonly PageAdditionBuilder _Additions = new PageAdditionBuilder();
26+
27+
private readonly ResponseModificationBuilder _Modifications = new ResponseModificationBuilder();
2228

2329
#region Supporting data structures
2430

@@ -66,18 +72,6 @@ public CombinedPageBuilder Title(string title)
6672
return this;
6773
}
6874

69-
public CombinedPageBuilder AddScript(ScriptReference reference)
70-
{
71-
_Additions.Scripts.Add(reference);
72-
return this;
73-
}
74-
75-
public CombinedPageBuilder AddStyle(StyleReference reference)
76-
{
77-
_Additions.Styles.Add(reference);
78-
return this;
79-
}
80-
8175
/// <summary>
8276
/// Adds a dynamically rendered part to the page.
8377
/// </summary>
@@ -124,9 +118,69 @@ public CombinedPageBuilder Add(IConcernBuilder concern)
124118
return this;
125119
}
126120

121+
public CombinedPageBuilder AddScript(string path, bool asynchronous = false)
122+
{
123+
_Additions.AddScript(path, asynchronous);
124+
return this;
125+
}
126+
127+
public CombinedPageBuilder AddStyle(string path)
128+
{
129+
_Additions.AddStyle(path);
130+
return this;
131+
}
132+
133+
public CombinedPageBuilder Status(ResponseStatus status)
134+
{
135+
_Modifications.Status(status);
136+
return this;
137+
}
138+
139+
public CombinedPageBuilder Status(int status, string reason)
140+
{
141+
_Modifications.Status(status, reason);
142+
return this;
143+
}
144+
145+
public CombinedPageBuilder Header(string key, string value)
146+
{
147+
_Modifications.Header(key, value);
148+
return this;
149+
}
150+
151+
public CombinedPageBuilder Expires(DateTime expiryDate)
152+
{
153+
_Modifications.Expires(expiryDate);
154+
return this;
155+
}
156+
157+
public CombinedPageBuilder Modified(DateTime modificationDate)
158+
{
159+
_Modifications.Modified(modificationDate);
160+
return this;
161+
}
162+
163+
public CombinedPageBuilder Cookie(Cookie cookie)
164+
{
165+
_Modifications.Cookie(cookie);
166+
return this;
167+
}
168+
169+
public CombinedPageBuilder Type(FlexibleContentType contentType)
170+
{
171+
_Modifications.Type(contentType);
172+
return this;
173+
}
174+
175+
public CombinedPageBuilder Encoding(string encoding)
176+
{
177+
_Modifications.Encoding(encoding);
178+
return this;
179+
}
180+
127181
public IHandler Build(IHandler parent)
128182
{
129-
return Concerns.Chain(parent, _Concerns, (p) => new CombinedPageProvider(p, _Info.Build(), _Additions, _Fragments));
183+
return Concerns.Chain(parent, _Concerns, (p) => new CombinedPageProvider(p, _Info.Build(), _Additions.Build(), _Modifications.Build(), _Fragments));
130184
}
131185

132186
#endregion

Modules/Pages/Combined/CombinedPageProvider.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ public class CombinedPageProvider : IHandler
2020

2121
private PageAdditions? Additions { get; }
2222

23+
private ResponseModifications? Modifications { get; }
24+
2325
private List<PageFragment> Fragments { get; }
2426

2527
#endregion
2628

2729
#region Initialization
2830

29-
public CombinedPageProvider(IHandler parent, ContentInfo contentInfo, PageAdditions? additions, List<PageFragment> fragments)
31+
public CombinedPageProvider(IHandler parent, ContentInfo contentInfo, PageAdditions? additions, ResponseModifications? modifications, List<PageFragment> fragments)
3032
{
3133
Parent = parent;
3234

3335
ContentInfo = contentInfo;
3436
Additions = additions;
37+
Modifications = modifications;
3538

3639
Fragments = fragments;
3740
}
@@ -61,10 +64,16 @@ public async ValueTask PrepareAsync()
6164

6265
var content = new CombinedPageContent(contentFragments, ContentInfo, Additions, this, request);
6366

64-
return request.Respond()
65-
.Content(content)
66-
.Type(ContentType.TextHtml)
67-
.Build();
67+
var response = request.Respond()
68+
.Content(content)
69+
.Type(ContentType.TextHtml);
70+
71+
if (Modifications != null)
72+
{
73+
Modifications.Apply(response);
74+
}
75+
76+
return response.Build();
6877
}
6978

7079
#endregion

Testing/Acceptance/Modules/PageTests.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using GenHTTP.Modules.IO;
1212
using GenHTTP.Modules.Layouting;
1313
using GenHTTP.Modules.Markdown;
14+
using GenHTTP.Modules.Pages;
1415
using GenHTTP.Modules.Placeholders;
1516
using GenHTTP.Modules.Razor;
1617
using GenHTTP.Modules.Scriban;
@@ -222,7 +223,9 @@ public async Task TestAdditions()
222223
{
223224
ModScriban.Page(Resource.FromString("Scriban")).AddScript("myscript.js").AddStyle("mystyle.css"),
224225
ModRazor.Page(Resource.FromString("Razor")).AddScript("./myscript.js").AddStyle("./mystyle.css"),
225-
Placeholders.Page(Resource.FromString("Markdown")).AddScript("http://myserver/myscript.js").AddStyle("http://myserver/mystyle.css")
226+
Placeholders.Page(Resource.FromString("Placeholders")).AddScript("http://myserver/myscript.js").AddStyle("http://myserver/mystyle.css"),
227+
CombinedPage.Create().AddMarkdown(Resource.FromString("Scriban")).AddScript("myscript.js").AddStyle("mystyle.css"),
228+
ModMarkdown.Page(Resource.FromString("Markdown")).AddScript("myscript.js").AddStyle("mystyle.css")
226229
};
227230

228231
foreach (var provider in providers)
@@ -247,7 +250,9 @@ public async Task TestModifications()
247250
{
248251
ModScriban.Page(Resource.FromString("Scriban")).Status(ResponseStatus.Locked).Header("H", "V").Expires(DateTime.Now).Modified(DateTime.Now).Type(new(ContentType.FontOpenTypeFont)).Encoding("my-encoding").Cookie(cookie),
249252
ModRazor.Page(Resource.FromString("Razor")).Status(ResponseStatus.Locked).Header("H", "V").Expires(DateTime.Now).Modified(DateTime.Now).Type(new(ContentType.FontOpenTypeFont)).Encoding("my-encoding").Cookie(cookie),
250-
Placeholders.Page(Resource.FromString("Markdown")).Status(ResponseStatus.Locked).Header("H", "V").Expires(DateTime.Now).Modified(DateTime.Now).Type(new(ContentType.FontOpenTypeFont)).Encoding("my-encoding").Cookie(cookie)
253+
Placeholders.Page(Resource.FromString("Placeholders")).Status(ResponseStatus.Locked).Header("H", "V").Expires(DateTime.Now).Modified(DateTime.Now).Type(new(ContentType.FontOpenTypeFont)).Encoding("my-encoding").Cookie(cookie),
254+
ModMarkdown.Page(Resource.FromString("Markdown")).Status(ResponseStatus.Locked).Header("H", "V").Expires(DateTime.Now).Modified(DateTime.Now).Type(new(ContentType.FontOpenTypeFont)).Encoding("my-encoding").Cookie(cookie),
255+
CombinedPage.Create().Add("Combined").Status(ResponseStatus.Locked).Header("H", "V").Expires(DateTime.Now).Modified(DateTime.Now).Type(new(ContentType.FontOpenTypeFont)).Encoding("my-encoding").Cookie(cookie)
251256
};
252257

253258
foreach (var provider in providers)

0 commit comments

Comments
 (0)