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

Commit 74fb4ac

Browse files
setting the reason phrase from one place
1 parent 49269f6 commit 74fb4ac

7 files changed

Lines changed: 49 additions & 11 deletions

File tree

src/SqlStreamStore.HAL/AppendStreamMiddleware.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ private static MidFunc AppendStream(StreamResource stream) => next => async env
4040
{
4141
var response = await stream.AppendMessages(options, context.Request.CallCancelled);
4242

43-
if(response.StatusCode == 201)
44-
{
45-
context.Response.ReasonPhrase = "Created";
46-
context.Response.Headers["Location"] = $"streams/{options.StreamId}";
47-
}
48-
4943
await context.WriteHalResponse(response);
5044
}
5145
catch(WrongExpectedVersionException ex)

src/SqlStreamStore.HAL/Constants.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace SqlStreamStore.HAL
22
{
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
36
internal static class Constants
47
{
58
public static class Headers
@@ -12,5 +15,17 @@ public static class ContentTypes
1215
public const string Json = "application/json";
1316
}
1417
}
18+
19+
public static IReadOnlyDictionary<int, string> ReasonPhrases { get; }
20+
= new ReadOnlyDictionary<int, string>(new Dictionary<int, string>
21+
{
22+
[200] = "OK",
23+
[201] = "Created",
24+
[307] = "Moved Temporarily",
25+
[400] = "Bad Request",
26+
[404] = "Not Found",
27+
[405] = "Method Not Allowed",
28+
[409] = "Conflict"
29+
});
1530
}
1631
}

src/SqlStreamStore.HAL/OwinContextExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public static async Task WriteHalResponse(this IOwinContext context, Response re
6868

6969
context.Response.StatusCode = response.StatusCode;
7070

71+
foreach(var header in response.Headers)
72+
{
73+
context.Response.Headers.AppendValues(header.Key, header.Value);
74+
}
75+
7176
using(var stream = s_StreamManager.GetStream())
7277
using(var writer = new StreamWriter(stream))
7378
{
@@ -92,7 +97,6 @@ public static async Task WriteHalResponse(this IOwinContext context, Response re
9297
public static Task WriteProblemDetailsResponse(this IOwinContext context, WrongExpectedVersionException ex)
9398
{
9499
context.Response.StatusCode = 409;
95-
context.Response.ReasonPhrase = "Conflict";
96100
context.Response.ContentType = Constants.Headers.ContentTypes.ProblemDetails;
97101

98102
return context.Response.WriteAsync(ex.ConvertToProblemDetails(), context.Request.CallCancelled);

src/SqlStreamStore.HAL/ReadStreamMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ private static MidFunc GetStreamMessage(StreamResource stream) => next => async
5959
if(options.StreamVersion == StreamVersion.End)
6060
{
6161
context.Response.StatusCode = 307;
62-
context.Response.ReasonPhrase = "Moved Temporarily";
6362
context.Response.Headers["Location"] = $"{((dynamic) response.Hal.Model).StreamVersion}";
6463

6564
return;

src/SqlStreamStore.HAL/Response.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
namespace SqlStreamStore.HAL
22
{
3+
using System.Collections.Generic;
34
using Halcyon.HAL;
45

56
public class Response
67
{
78
public HALResponse Hal { get; }
89
public int StatusCode { get; }
10+
public IDictionary<string, string[]> Headers { get; }
911

1012
public Response(HALResponse hal, int statusCode = 200)
1113
{
1214
Hal = hal;
1315
StatusCode = statusCode;
16+
Headers = new Dictionary<string, string[]>();
1417
}
1518
}
1619
}

src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class SqlStreamStoreHalMiddleware
2121

2222
context.Response.OnSendingHeaders(_ =>
2323
{
24-
context.Response.Headers["Access-Control-Allow-Methods"] = "GET, OPTIONS";
24+
context.Response.Headers["Access-Control-Allow-Methods"] = "GET, HEAD, OPTIONS";
2525
context.Response.Headers["Access-Control-Allow-Headers"]
2626
= "Content-Type, X-Requested-With, Authorization";
2727
context.Response.Headers["Access-Control-Allow-Origin"] = "*";
@@ -31,6 +31,24 @@ public static class SqlStreamStoreHalMiddleware
3131
return next(env);
3232
};
3333

34+
private static MidFunc AddReasonPhrase => next => env =>
35+
{
36+
var context = new OwinContext(env);
37+
38+
context.Response.OnSendingHeaders(_ =>
39+
{
40+
if(!Constants.ReasonPhrases.TryGetValue(context.Response.StatusCode, out var reasonPhrase))
41+
{
42+
return;
43+
}
44+
45+
context.Response.ReasonPhrase = reasonPhrase;
46+
},
47+
null);
48+
49+
return next(env);
50+
};
51+
3452
private static MidFunc Index => next => env =>
3553
{
3654
var context = new OwinContext(env);
@@ -52,6 +70,7 @@ public static MidFunc UseSqlStreamStoreHal(IStreamStore streamStore)
5270

5371
var builder = new AppBuilder()
5472
.Use(AccessControl)
73+
.Use(AddReasonPhrase)
5574
.Use(Index)
5675
.Map("/stream", inner => inner
5776
.Use(ReadAllStreamMiddleware.UseStreamStore(streamStore))
@@ -81,7 +100,6 @@ private static MidFunc MethodsNotAllowed(params string[] methods)
81100
}
82101

83102
context.Response.StatusCode = 405;
84-
context.Response.ReasonPhrase = "Method Not Allowed";
85103

86104
return Task.CompletedTask;
87105
};

src/SqlStreamStore.HAL/StreamResource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ public async Task<Response> AppendMessages(
2727

2828
var result = await operation.Invoke(_streamStore, cancellationToken);
2929

30-
return new Response(
30+
var response = new Response(
3131
new HALResponse(new object()),
3232
options.ExpectedVersion == ExpectedVersion.NoStream
3333
? 201
3434
: 200);
35+
if(options.ExpectedVersion == ExpectedVersion.NoStream)
36+
{
37+
response.Headers["Location"] = new[] { $"streams/{options.StreamId}" };
38+
}
39+
return response;
3540
}
3641

3742
public async Task<Response> GetMessage(

0 commit comments

Comments
 (0)