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

Commit 33c0387

Browse files
tentative support for HEAD requests on streams
1 parent 00ed2f6 commit 33c0387

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

src/KestrelPureOwin/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following

src/SqlStreamStore.HAL.DevServer/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using KestrelPureOwin;
8-
using Microsoft.AspNetCore.Server.Kestrel;
98
using Microsoft.AspNetCore.Server.Kestrel.Core;
109
using SqlStreamStore.Streams;
1110
using BuildFunc = System.Action<

src/SqlStreamStore.HAL/OwinContextExtensions.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
11
namespace SqlStreamStore.HAL
22
{
3+
using System;
34
using System.IO;
5+
using System.Threading;
46
using System.Threading.Tasks;
57
using Microsoft.IO;
68
using Microsoft.Owin;
79
using Newtonsoft.Json;
810
using Newtonsoft.Json.Serialization;
911
using SqlStreamStore.Streams;
12+
13+
internal class OptionalHeadRequestWrapper : IDisposable
14+
{
15+
private readonly IOwinContext _context;
16+
private readonly Stream _originalBody;
17+
18+
public OptionalHeadRequestWrapper(IOwinContext context)
19+
{
20+
_context = context;
21+
_originalBody = _context.Response.Body;
22+
if(context.Request.Method == "HEAD")
23+
{
24+
context.Response.Body = new HeadRequestStream();
25+
}
26+
}
27+
28+
public void Dispose()
29+
{
30+
_context.Response.Body = _originalBody;
31+
}
32+
33+
class HeadRequestStream : Stream
34+
{
35+
private long _length;
36+
37+
public override void Flush() => FlushAsync(CancellationToken.None).Wait();
38+
public override int Read(byte[] buffer, int offset, int count) => throw new NotImplementedException();
39+
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();
40+
public override void SetLength(long value) => throw new NotImplementedException();
41+
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
42+
43+
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
44+
{
45+
_length += count;
46+
Position += count;
47+
return Task.CompletedTask;
48+
}
49+
50+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
51+
52+
public override bool CanRead { get; } = false;
53+
public override bool CanSeek { get; } = false;
54+
public override bool CanWrite { get; } = true;
55+
public override long Length => _length;
56+
public override long Position { get; set; }
57+
}
58+
}
1059

1160
internal static class OwinContextExtensions
1261
{

src/SqlStreamStore.HAL/ReadAllStreamMiddleware.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,24 @@ private static MidFunc GetStream(AllStreamResource allStream) => next => async e
4141

4242
var response = await allStream.GetPage(options, context.Request.CallCancelled);
4343

44-
await context.WriteHalResponse(response);
44+
using(new OptionalHeadRequestWrapper(context))
45+
{
46+
await context.WriteHalResponse(response);
47+
}
4548
};
4649

4750
private static MidFunc GetStreamMessage(AllStreamResource allStream) => next => async env =>
4851
{
4952
var context = new OwinContext(env);
5053

51-
var resource = await allStream.GetMessage(
54+
var response = await allStream.GetMessage(
5255
new ReadAllStreamMessageOptions(context.Request),
5356
context.Request.CallCancelled);
5457

55-
await context.WriteHalResponse(resource);
58+
using(new OptionalHeadRequestWrapper(context))
59+
{
60+
await context.WriteHalResponse(response);
61+
}
5662
};
5763
}
5864
}

src/SqlStreamStore.HAL/ReadStreamMiddleware.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ private static MidFunc GetStream(StreamResource stream) => next => async env =>
4242

4343
var response = await stream.GetPage(options, context.Request.CallCancelled);
4444

45-
await context.WriteHalResponse(response);
45+
using(new OptionalHeadRequestWrapper(context))
46+
{
47+
await context.WriteHalResponse(response);
48+
}
4649
};
4750

4851
private static MidFunc GetStreamMessage(StreamResource stream) => next => async env =>
@@ -62,7 +65,10 @@ private static MidFunc GetStreamMessage(StreamResource stream) => next => async
6265
return;
6366
}
6467

65-
await context.WriteHalResponse(response);
68+
using(new OptionalHeadRequestWrapper(context))
69+
{
70+
await context.WriteHalResponse(response);
71+
}
6672
};
6773
}
6874
}

0 commit comments

Comments
 (0)