Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

BUG: Restore Request.InputStream position after body has been read #215

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/app/SharpRaven/Data/DefaultHttpRequestBodyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ public bool TryConvert(dynamic httpContext, out object converted)
{
using (var stream = new MemoryStream())
{
httpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
httpContext.Request.InputStream.CopyTo(stream);
converted = Encoding.UTF8.GetString(stream.ToArray());
var position = httpContext.Request.InputStream.Position;
try
{
httpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
httpContext.Request.InputStream.CopyTo(stream);
converted = Encoding.UTF8.GetString(stream.ToArray());

return true;
return true;
}
finally
{
httpContext.Request.InputStream.Position = position;
}
}
}
catch (Exception exception)
Expand Down
14 changes: 11 additions & 3 deletions src/app/SharpRaven/Data/JsonHttpRequestBodyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,17 @@ public bool TryConvert(dynamic httpContext, out object converted)

using (var stream = new MemoryStream())
{
httpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
httpContext.Request.InputStream.CopyTo(stream);
body = Encoding.UTF8.GetString(stream.ToArray());
var position = httpContext.Request.InputStream.Position;
try
{
httpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
httpContext.Request.InputStream.CopyTo(stream);
body = Encoding.UTF8.GetString(stream.ToArray());
}
finally
{
httpContext.Request.InputStream.Position = position;
}
}

converted = JsonConvert.DeserializeObject<Dictionary<string, object>>(body);
Expand Down