Skip to content

Commit

Permalink
Fixed NullReference on closed stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Anisimova2020 committed Apr 18, 2023
1 parent 029aa1e commit 0486e30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public static async Task RewriteResponse(HttpContext context, Func<Task> next)
await next();

responseBodyStream.Seek(0, SeekOrigin.Begin);
using StreamReader sr = new StreamReader(responseBodyStream);
var responseBody = sr.ReadToEnd();
var responseBody = new StreamReader(responseBodyStream).ReadToEnd();

//Modify the response in some way.
if (context.Response.ContentType != null &&
Expand Down
27 changes: 14 additions & 13 deletions NewPlatform.Flexberry.ORM.ODataService/Handlers/PostPatchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,27 @@ private static async Task RewriteResponse(HttpResponseMessage response)
{
HttpContent content = response.Content;
Stream contentStream = await content.ReadAsStreamAsync();
using StreamReader sr = new StreamReader(contentStream);
string responseStr = sr.ReadToEnd();

responseStr = responseStr
string responseStr = new StreamReader(contentStream).ReadToEnd();
if (!string.IsNullOrEmpty(responseStr) && responseStr.Length > 3)
{
responseStr = responseStr
.Replace("(____.", "(")
.Replace("\"____.", "\"")
.Replace("____.", ".")
.Replace(" Namespace=\"____\"", " Namespace=\"\"");

using (MemoryStream newStream = new MemoryStream())
{
using StreamWriter sw = new StreamWriter(newStream);
sw.Write(responseStr);
sw.Flush();
newStream.Seek(0, SeekOrigin.Begin);
using (MemoryStream newStream = new MemoryStream())
{
using StreamWriter sw = new StreamWriter(newStream);
sw.Write(responseStr);
sw.Flush();
newStream.Seek(0, SeekOrigin.Begin);

contentStream.Position = 0;
contentStream.SetLength(0);
contentStream.Position = 0;
contentStream.SetLength(responseStr.Length);

await newStream.CopyToAsync(contentStream);
await newStream.CopyToAsync(contentStream);
}
}
}
}
Expand Down

0 comments on commit 0486e30

Please sign in to comment.