From e943d2d5a3b31747b01053e2004e87ea0c715905 Mon Sep 17 00:00:00 2001 From: Bruno Date: Tue, 6 Feb 2018 09:32:12 +0100 Subject: [PATCH] BUG: Restore Request.InputStream position after body has been read --- .../Data/DefaultHttpRequestBodyConverter.cs | 16 ++++++++++++---- .../Data/JsonHttpRequestBodyConverter.cs | 14 +++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/app/SharpRaven/Data/DefaultHttpRequestBodyConverter.cs b/src/app/SharpRaven/Data/DefaultHttpRequestBodyConverter.cs index f11ca92e..03f756d8 100644 --- a/src/app/SharpRaven/Data/DefaultHttpRequestBodyConverter.cs +++ b/src/app/SharpRaven/Data/DefaultHttpRequestBodyConverter.cs @@ -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) diff --git a/src/app/SharpRaven/Data/JsonHttpRequestBodyConverter.cs b/src/app/SharpRaven/Data/JsonHttpRequestBodyConverter.cs index ad3ef593..ac3af56b 100644 --- a/src/app/SharpRaven/Data/JsonHttpRequestBodyConverter.cs +++ b/src/app/SharpRaven/Data/JsonHttpRequestBodyConverter.cs @@ -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>(body);