Skip to content

Commit 2e5ef84

Browse files
authored
Support request streams in classic mode in framework services (#550)
Fixes #549
1 parent dc28598 commit 2e5ef84

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Web;
5+
6+
namespace Microsoft.AspNetCore.SystemWebAdapters;
7+
8+
internal static class RequestExtensions
9+
{
10+
/// <summary>
11+
/// Gets the request stream. If it has already been retrieved, it will be in whatever <see cref="HttpRequest.ReadEntityBodyMode"/>
12+
/// it was already accessed. Otherwise, it will get a bufferless version via <see cref="HttpRequest.GetBufferlessInputStream()"/>.
13+
/// </summary>
14+
/// <param name="request">The request</param>
15+
/// <returns></returns>
16+
public static Stream GetInputStream(this HttpRequestBase request)
17+
{
18+
// If a call has been made to InputStream, we must use that. It is seekable, so we need to rewind it to the beginning
19+
if (request.ReadEntityBodyMode == ReadEntityBodyMode.Classic)
20+
{
21+
var stream = request.InputStream;
22+
stream.Position = 0;
23+
return stream;
24+
}
25+
26+
// If a call has been made to GetBufferedInputStream, we must use that. It is seekable, so we need to rewind it to the beginning
27+
if (request.ReadEntityBodyMode == ReadEntityBodyMode.Buffered)
28+
{
29+
var stream = request.GetBufferedInputStream();
30+
stream.Position = 0;
31+
return stream;
32+
}
33+
34+
// Otherwise, let's attempt to get a bufferless version since we don't need it buffered
35+
return request.GetBufferlessInputStream();
36+
}
37+
}

src/Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices/SessionState/RemoteSession/StoreSessionStateHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task ProcessRequestAsync(HttpContextBase context)
4343
}
4444
else
4545
{
46-
using var content = context.Request.GetBufferlessInputStream();
46+
using var content = context.Request.GetInputStream();
4747
var result = await _cache.SaveAsync(sessionId, content, context.Response.ClientDisconnectedToken);
4848

4949
if (result is SessionSaveResult.Success)

0 commit comments

Comments
 (0)