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

Commit 451322a

Browse files
make 308 redirects on non canonical urls optional
1 parent 98d7022 commit 451322a

10 files changed

Lines changed: 42 additions & 30 deletions

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();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SqlStreamStore.HAL
2+
{
3+
public class SqlStreamStoreMiddlewareOptions
4+
{
5+
public bool UseCanonicalUrls { get; set; } = true;
6+
}
7+
}

src/SqlStreamStore.HAL/StreamOptionsMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static class StreamOptionsMiddleware
1515
{
1616
public static IApplicationBuilder UseStreamOptions(this IApplicationBuilder builder, IStreamStore streamStore)
1717
{
18-
var streams = new StreamResource(streamStore);
18+
var streams = new StreamResource(streamStore, false);
1919
var streamMessages = new StreamMessageResource(streamStore);
2020
var streamsMetadata = new StreamMetadataResource(streamStore);
2121

0 commit comments

Comments
 (0)