-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(exception-handling): Check if response has started before setting…
… header and status code
- Loading branch information
Showing
4 changed files
with
35 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 20 additions & 7 deletions
27
...NGYUN.Abp.AspNetCore.Wrapper/LINGYUN/Abp/AspNetCore/Wrapper/DefaultHttpResponseWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,48 @@ | ||
using LINGYUN.Abp.Wrapper; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace LINGYUN.Abp.AspNetCore.Wrapper; | ||
|
||
public class DefaultHttpResponseWrapper : IHttpResponseWrapper, ITransientDependency | ||
{ | ||
public ILogger<DefaultHttpResponseWrapper> Logger { protected get; set; } | ||
|
||
protected AbpWrapperOptions Options { get; } | ||
|
||
public DefaultHttpResponseWrapper(IOptions<AbpWrapperOptions> options) | ||
{ | ||
Options = options.Value; | ||
|
||
Logger = NullLogger<DefaultHttpResponseWrapper>.Instance; | ||
} | ||
|
||
public virtual void Wrap(HttpResponseWrapperContext context) | ||
{ | ||
context.HttpContext.Response.StatusCode = context.HttpStatusCode; | ||
if (context.HttpHeaders != null) | ||
if (!context.HttpContext.Response.HasStarted) | ||
{ | ||
foreach (var header in context.HttpHeaders) | ||
context.HttpContext.Response.StatusCode = context.HttpStatusCode; | ||
if (context.HttpHeaders != null) | ||
{ | ||
if (!context.HttpContext.Response.Headers.ContainsKey(header.Key)) | ||
foreach (var header in context.HttpHeaders) | ||
{ | ||
context.HttpContext.Response.Headers.Append(header.Key, header.Value); | ||
if (!context.HttpContext.Response.Headers.ContainsKey(header.Key)) | ||
{ | ||
context.HttpContext.Response.Headers.Append(header.Key, header.Value); | ||
} | ||
} | ||
} | ||
if (!context.HttpContext.Response.Headers.ContainsKey(AbpHttpWrapConsts.AbpWrapResult)) | ||
{ | ||
context.HttpContext.Response.Headers.Append(AbpHttpWrapConsts.AbpWrapResult, "true"); | ||
} | ||
} | ||
if (!context.HttpContext.Response.Headers.ContainsKey(AbpHttpWrapConsts.AbpWrapResult)) | ||
else | ||
{ | ||
context.HttpContext.Response.Headers.Append(AbpHttpWrapConsts.AbpWrapResult, "true"); | ||
Logger.LogWarning("HTTP response has already started, cannot set headers and status code!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters