Skip to content

Commit 7350352

Browse files
committed
Set default Content-Encoding. Resolves #6
1 parent 4015a7a commit 7350352

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Lib.AspNetCore.ServerSentEvents/ServerSentEventsMiddleware.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class ServerSentEventsMiddleware
1414
#region Fields
1515
private readonly RequestDelegate _next;
1616
private readonly ServerSentEventsService _serverSentEventsService;
17+
18+
private static readonly Task _completedTask = Task.FromResult<object>(null);
1719
#endregion
1820

1921
#region Constructor
@@ -41,7 +43,7 @@ public async Task Invoke(HttpContext context)
4143
{
4244
DisableResponseBuffering(context);
4345

44-
context.Response.Headers.Append(Constants.CONTENT_ENCODING_HEADER, Constants.IDENTITY_CONTENT_ENCODING);
46+
HandleContentEncoding(context);
4547

4648
await context.Response.AcceptSse();
4749

@@ -78,6 +80,19 @@ private void DisableResponseBuffering(HttpContext context)
7880
bufferingFeature.DisableResponseBuffering();
7981
}
8082
}
83+
84+
private void HandleContentEncoding(HttpContext context)
85+
{
86+
context.Response.OnStarting(() =>
87+
{
88+
if (!context.Response.Headers.ContainsKey(Constants.CONTENT_ENCODING_HEADER))
89+
{
90+
context.Response.Headers.Append(Constants.CONTENT_ENCODING_HEADER, Constants.IDENTITY_CONTENT_ENCODING);
91+
}
92+
93+
return _completedTask;
94+
});
95+
}
8196
#endregion
8297
}
8398
}

0 commit comments

Comments
 (0)