Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit a4605d3

Browse files
Merge pull request #20 from thefringeninja/optional-redirects
Make 308 Redirects Optional
2 parents 98d7022 + b3c654d commit a4605d3

12 files changed

Lines changed: 53 additions & 33 deletions

src/SqlStreamStore.HAL.DevServer/DevServerStartup.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
internal class DevServerStartup : IStartup
1717
{
1818
private readonly IStreamStore _streamStore;
19+
private readonly SqlStreamStoreMiddlewareOptions _options;
1920

20-
public DevServerStartup(IStreamStore streamStore)
21+
public DevServerStartup(
22+
IStreamStore streamStore,
23+
SqlStreamStoreMiddlewareOptions options)
2124
{
2225
_streamStore = streamStore;
26+
_options = options;
2327
}
2428

2529
public IServiceProvider ConfigureServices(IServiceCollection services) => services
@@ -31,7 +35,7 @@ public void Configure(IApplicationBuilder app) => app
3135
.Use(VaryAccept)
3236
.Use(CatchAndDisplayErrors)
3337
.UseSqlStreamStoreBrowser()
34-
.UseSqlStreamStoreHal(_streamStore);
38+
.UseSqlStreamStoreHal(_streamStore, _options);
3539

3640
private static MidFunc CatchAndDisplayErrors => async (context, next) =>
3741
{

src/SqlStreamStore.HAL.DevServer/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal class Program : IDisposable
1616
private readonly IConfigurationRoot _configuration;
1717

1818
private bool Interactive => _configuration.GetValue<bool>("interactive");
19+
private bool UseCanonicalUrls => _configuration.GetValue<bool>("canonical");
1920

2021
public static async Task<int> Main(string[] args)
2122
{
@@ -47,7 +48,10 @@ private async Task<int> Run()
4748
using(var streamStore = await SqlStreamStoreFactory.Create())
4849
using(var host = new WebHostBuilder()
4950
.UseKestrel()
50-
.UseStartup(new DevServerStartup(streamStore))
51+
.UseStartup(new DevServerStartup(streamStore, new SqlStreamStoreMiddlewareOptions
52+
{
53+
UseCanonicalUrls = UseCanonicalUrls
54+
}))
5155
.UseSerilog()
5256
.Build())
5357
{

src/SqlStreamStore.HAL/AllStreamOptionsMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static class AllStreamOptionsMiddleware
1515
{
1616
public static IApplicationBuilder UseAllStreamOptions(this IApplicationBuilder builder, IStreamStore streamStore)
1717
{
18-
var allStream = new AllStreamResource(streamStore);
18+
var allStream = new AllStreamResource(streamStore, false);
1919
var allStreamMessages = new AllStreamMessageResource(streamStore);
2020

2121
return builder

src/SqlStreamStore.HAL/AppendStreamMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class AppendStreamMiddleware
1313
{
1414
public static IApplicationBuilder UseAppendStream(this IApplicationBuilder builder, IStreamStore streamStore)
1515
{
16-
var stream = new StreamResource(streamStore);
16+
var stream = new StreamResource(streamStore, false);
1717

1818
return builder.MapWhen(IsStream, inner => inner.Use(AppendStream(stream)));
1919
}

src/SqlStreamStore.HAL/DeleteStreamMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class DeleteStreamMiddleware
1313
{
1414
public static IApplicationBuilder UseDeleteStream(this IApplicationBuilder builder, IStreamStore streamStore)
1515
{
16-
var streams = new StreamResource(streamStore);
16+
var streams = new StreamResource(streamStore, false);
1717
var streamMessages = new StreamMessageResource(streamStore);
1818

1919
return builder

src/SqlStreamStore.HAL/ReadAllStreamMiddleware.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ namespace SqlStreamStore.HAL
1111

1212
internal static class ReadAllStreamMiddleware
1313
{
14-
public static IApplicationBuilder UseReadAllStream(this IApplicationBuilder builder, IStreamStore streamStore)
14+
public static IApplicationBuilder UseReadAllStream(
15+
this IApplicationBuilder builder,
16+
IStreamStore streamStore,
17+
SqlStreamStoreMiddlewareOptions options)
1518
{
16-
var allStream = new AllStreamResource(streamStore);
19+
var allStream = new AllStreamResource(streamStore, options.UseCanonicalUrls);
1720
var allStreamMessages = new AllStreamMessageResource(streamStore);
1821

1922
return builder

src/SqlStreamStore.HAL/ReadStreamMiddleware.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ namespace SqlStreamStore.HAL
1111

1212
internal static class ReadStreamMiddleware
1313
{
14-
public static IApplicationBuilder UseReadStream(this IApplicationBuilder builder, IStreamStore streamStore)
14+
public static IApplicationBuilder UseReadStream(
15+
this IApplicationBuilder builder,
16+
IStreamStore streamStore,
17+
SqlStreamStoreMiddlewareOptions options)
1518
{
16-
var streams = new StreamResource(streamStore);
19+
var streams = new StreamResource(streamStore, options.UseCanonicalUrls);
1720
var streamMessages = new StreamMessageResource(streamStore);
1821

1922
return builder

src/SqlStreamStore.HAL/Resources/AllStreamResource.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
internal class AllStreamResource : IResource
1313
{
1414
private readonly IStreamStore _streamStore;
15+
private readonly bool _useCanonicalUrls;
1516

1617
public HttpMethod[] Allowed { get; } =
1718
{
@@ -20,18 +21,19 @@ internal class AllStreamResource : IResource
2021
HttpMethod.Options
2122
};
2223

23-
public AllStreamResource(IStreamStore streamStore)
24+
public AllStreamResource(IStreamStore streamStore, bool useCanonicalUrls)
2425
{
2526
if(streamStore == null)
2627
throw new ArgumentNullException(nameof(streamStore));
2728
_streamStore = streamStore;
29+
_useCanonicalUrls = useCanonicalUrls;
2830
}
2931

3032
public async Task<Response> Get(
3133
ReadAllStreamOperation operation,
3234
CancellationToken cancellationToken)
3335
{
34-
if(!operation.IsUriCanonical)
36+
if(_useCanonicalUrls && !operation.IsUriCanonical)
3537
{
3638
return new Response(new HALResponse(null), 308)
3739
{

src/SqlStreamStore.HAL/Resources/StreamResource.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace SqlStreamStore.HAL.Resources
1212
internal class StreamResource : IResource
1313
{
1414
private readonly IStreamStore _streamStore;
15+
private readonly bool _useCanonicalUris;
1516

1617
public HttpMethod[] Allowed { get; } =
1718
{
@@ -22,11 +23,12 @@ internal class StreamResource : IResource
2223
HttpMethod.Delete
2324
};
2425

25-
public StreamResource(IStreamStore streamStore)
26+
public StreamResource(IStreamStore streamStore, bool useCanonicalUris)
2627
{
2728
if(streamStore == null)
2829
throw new ArgumentNullException(nameof(streamStore));
2930
_streamStore = streamStore;
31+
_useCanonicalUris = useCanonicalUris;
3032
}
3133

3234
public async Task<Response> Post(

src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@
1616

1717
public static class SqlStreamStoreHalMiddleware
1818
{
19-
private static readonly string[] s_NoCache =
20-
{
21-
"max-age=0",
22-
"no-cache",
23-
"must-revalidate"
24-
};
25-
26-
private static readonly string[] s_CacheForOneYear =
27-
{
28-
"max-age=31536000"
29-
};
30-
3119
private static MidFunc CaseSensitiveQueryStrings => (context, next) =>
3220
{
3321
if(context.Request.QueryString != QueryString.Empty)
@@ -81,36 +69,43 @@ private static MidFunc MethodsNotAllowed(params string[] methods) => (context, n
8169

8270
public static IApplicationBuilder UseSqlStreamStoreHal(
8371
this IApplicationBuilder builder,
84-
IStreamStore streamStore)
72+
IStreamStore streamStore,
73+
SqlStreamStoreMiddlewareOptions options = default)
8574
{
8675
if(builder == null)
8776
throw new ArgumentNullException(nameof(builder));
8877
if(streamStore == null)
8978
throw new ArgumentNullException(nameof(streamStore));
9079

80+
options = options ?? new SqlStreamStoreMiddlewareOptions();
81+
9182
return builder
9283
.UseExceptionHandling()
9384
.Use(CaseSensitiveQueryStrings)
9485
.Use(AcceptHalJson)
9586
.Use(HeadRequests)
9687
.UseIndex()
97-
.Map("/stream", UseAllStream(streamStore))
98-
.Map("/streams", UseStream(streamStore));
88+
.Map("/stream", UseAllStream(streamStore, options))
89+
.Map("/streams", UseStream(streamStore, options));
9990
}
10091

101-
private static Action<IApplicationBuilder> UseStream(IStreamStore streamStore)
92+
private static Action<IApplicationBuilder> UseStream(
93+
IStreamStore streamStore,
94+
SqlStreamStoreMiddlewareOptions options)
10295
=> builder => builder
10396
.MapWhen(IsOptions, inner => inner.UseStreamOptions(streamStore))
10497
.UseStreamMetadata(streamStore)
105-
.UseReadStream(streamStore)
98+
.UseReadStream(streamStore, options)
10699
.UseAppendStream(streamStore)
107100
.UseDeleteStream(streamStore)
108101
.Use(MethodsNotAllowed("TRACE", "PATCH"));
109102

110-
private static Action<IApplicationBuilder> UseAllStream(IStreamStore streamStore)
103+
private static Action<IApplicationBuilder> UseAllStream(
104+
IStreamStore streamStore,
105+
SqlStreamStoreMiddlewareOptions options)
111106
=> builder => builder
112107
.MapWhen(IsOptions, inner => inner.UseAllStreamOptions(streamStore))
113-
.UseReadAllStream(streamStore)
108+
.UseReadAllStream(streamStore, options)
114109
.Use(MethodsNotAllowed("POST", "PUT", "DELETE", "TRACE", "PATCH"));
115110

116111
private static bool IsOptions(HttpContext context) => context.IsOptions();

0 commit comments

Comments
 (0)