Skip to content

Commit 945a019

Browse files
committed
In WebMarkupMin.AspNetCore3 added support of ASP.NET Core 3.0 Preview 8
1 parent 8293349 commit 945a019

File tree

13 files changed

+524
-233
lines changed

13 files changed

+524
-233
lines changed

samples/WebMarkupMin.Sample.AspNetCore3.Mvc3/Startup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public void ConfigureServices(IServiceCollection services)
7878
services.AddSingleton<IWmmLogger, WmmThrowExceptionLogger>();
7979

8080
services.AddControllersWithViews();
81-
services.AddRazorPages();
8281
}
8382

8483
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -111,7 +110,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
111110
endpoints.MapControllerRoute(
112111
name: "default",
113112
pattern: "{controller=Home}/{action=Index}/{id?}");
114-
endpoints.MapRazorPages();
115113
});
116114
}
117115
}

src/WebMarkupMin.AspNetCore1/BodyWrapperStream.cs renamed to src/WebMarkupMin.AspNetCore1/BodyWrapperStreamBase.cs

Lines changed: 82 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading.Tasks;
88

99
using Microsoft.AspNetCore.Http;
10-
using Microsoft.AspNetCore.Http.Features;
1110
using Microsoft.Extensions.Primitives;
1211
using Microsoft.Net.Http.Headers;
1312

@@ -28,14 +27,14 @@ namespace WebMarkupMin.AspNetCore3
2827
#endif
2928
{
3029
/// <summary>
31-
/// Stream wrapper that apply a markup minification and compression only if necessary
30+
/// Base class of stream wrapper that apply a markup minification and compression only if necessary
3231
/// </summary>
33-
internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
32+
internal abstract class BodyWrapperStreamBase : Stream
3433
{
3534
/// <summary>
3635
/// HTTP context
3736
/// </summary>
38-
private readonly HttpContext _context;
37+
protected readonly HttpContext _context;
3938

4039
/// <summary>
4140
/// Original stream
@@ -55,7 +54,7 @@ internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
5554
/// <summary>
5655
/// Flag for whether to do automatically flush the compression stream
5756
/// </summary>
58-
private bool _autoFlushCompressionStream = false;
57+
protected bool _autoFlushCompressionStream = false;
5958

6059
/// <summary>
6160
/// WebMarkupMin configuration
@@ -72,11 +71,6 @@ internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
7271
/// </summary>
7372
private readonly IHttpCompressionManager _compressionManager;
7473

75-
/// <summary>
76-
/// HTTP buffering feature
77-
/// </summary>
78-
private readonly IHttpBufferingFeature _bufferingFeature;
79-
8074
/// <summary>
8175
/// Flag indicating whether the stream wrapper is initialized
8276
/// </summary>
@@ -85,12 +79,12 @@ internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
8579
/// <summary>
8680
/// Flag indicating whether a markup minification is enabled
8781
/// </summary>
88-
private bool _minificationEnabled = false;
82+
protected bool _minificationEnabled = false;
8983

9084
/// <summary>
9185
/// Flag indicating whether a HTTP compression is enabled
9286
/// </summary>
93-
private bool _compressionEnabled = false;
87+
protected bool _compressionEnabled = false;
9488

9589
/// <summary>
9690
/// Current URL
@@ -110,7 +104,7 @@ internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
110104
/// <summary>
111105
/// Current HTTP compressor
112106
/// </summary>
113-
private ICompressor _currentCompressor;
107+
protected ICompressor _currentCompressor;
114108

115109
/// <summary>
116110
/// Flag indicating whether the current HTTP compressor is initialized
@@ -122,6 +116,11 @@ internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
122116
/// </summary>
123117
private InterlockedStatedFlag _httpHeadersModifiedForCompressionFlag = new InterlockedStatedFlag();
124118

119+
/// <summary>
120+
/// Flag that the stream wrapper is destroyed
121+
/// </summary>
122+
private InterlockedStatedFlag _disposedFlag = new InterlockedStatedFlag();
123+
125124

126125
/// <summary>
127126
/// Constructs an instance of the stream wrapper
@@ -131,21 +130,19 @@ internal sealed class BodyWrapperStream : Stream, IHttpBufferingFeature
131130
/// <param name="options">WebMarkupMin configuration</param>
132131
/// <param name="minificationManagers">List of markup minification managers</param>
133132
/// <param name="compressionManager">HTTP compression manager</param>
134-
/// <param name="bufferingFeature">HTTP buffering feature</param>
135-
internal BodyWrapperStream(HttpContext context, Stream originalStream,
133+
protected BodyWrapperStreamBase(HttpContext context, Stream originalStream,
136134
WebMarkupMinOptions options, IList<IMarkupMinificationManager> minificationManagers,
137-
IHttpCompressionManager compressionManager, IHttpBufferingFeature bufferingFeature)
135+
IHttpCompressionManager compressionManager)
138136
{
139137
_context = context;
140138
_originalStream = originalStream;
141139
_options = options;
142140
_minificationManagers = minificationManagers;
143141
_compressionManager = compressionManager;
144-
_bufferingFeature = bufferingFeature;
145142
}
146143

147144

148-
private void Initialize()
145+
protected void Initialize()
149146
{
150147
if (_wrapperInitializedFlag.Set())
151148
{
@@ -235,7 +232,7 @@ private void Initialize()
235232
}
236233
}
237234

238-
private ICompressor InitializeCurrentCompressor(string acceptEncoding)
235+
protected ICompressor InitializeCurrentCompressor(string acceptEncoding)
239236
{
240237
if (_currentCompressorInitializedFlag.Set())
241238
{
@@ -258,8 +255,40 @@ private void ModifyHttpHeadersForCompressionOnce()
258255
responseHeaders.Remove(HeaderNames.ContentLength);
259256
}
260257
}
258+
#if NET451 || NETSTANDARD2_0 || NETCOREAPP3_0
259+
260+
private async void InternalWriteAsync(byte[] buffer, int offset, int count, AsyncCallback callback,
261+
TaskCompletionSource<object> tcs)
262+
{
263+
try
264+
{
265+
await WriteAsync(buffer, offset, count);
266+
tcs.TrySetResult(null);
267+
}
268+
catch (Exception ex)
269+
{
270+
tcs.TrySetException(ex);
271+
}
272+
273+
if (callback != null)
274+
{
275+
// Offload callbacks to avoid stack dives on sync completions
276+
var ignored = Task.Run(() =>
277+
{
278+
try
279+
{
280+
callback(tcs.Task);
281+
}
282+
catch (Exception)
283+
{
284+
// Suppress exceptions on background threads
285+
}
286+
});
287+
}
288+
}
289+
#endif
261290

262-
public async Task Finish()
291+
protected async Task InternalFinishAsync()
263292
{
264293
if (_minificationEnabled)
265294
{
@@ -345,38 +374,11 @@ public async Task Finish()
345374
_cachedStream.Clear();
346375
}
347376
}
348-
#if NET451 || NETSTANDARD2_0 || NETCOREAPP3_0
349377

350-
private async void InternalWriteAsync(byte[] buffer, int offset, int count, AsyncCallback callback,
351-
TaskCompletionSource<object> tcs)
378+
public virtual async Task FinishAsync()
352379
{
353-
try
354-
{
355-
await WriteAsync(buffer, offset, count);
356-
tcs.TrySetResult(null);
357-
}
358-
catch (Exception ex)
359-
{
360-
tcs.TrySetException(ex);
361-
}
362-
363-
if (callback != null)
364-
{
365-
// Offload callbacks to avoid stack dives on sync completions
366-
var ignored = Task.Run(() =>
367-
{
368-
try
369-
{
370-
callback(tcs.Task);
371-
}
372-
catch (Exception)
373-
{
374-
// Suppress exceptions on background threads
375-
}
376-
});
377-
}
380+
await Task.Run(() => throw new NotImplementedException());
378381
}
379-
#endif
380382

381383
#region Stream overrides
382384

@@ -527,79 +529,56 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count,
527529

528530
protected override void Dispose(bool disposing)
529531
{
530-
if (disposing)
532+
if (_disposedFlag.Set())
531533
{
532-
if (_compressionStream != null)
534+
if (disposing)
533535
{
534-
_compressionStream.Dispose();
535-
_compressionStream = null;
536-
}
536+
if (_compressionStream != null)
537+
{
538+
_compressionStream.Dispose();
539+
_compressionStream = null;
540+
}
537541

538-
_currentCompressor = null;
542+
_currentCompressor = null;
539543

540-
if (_cachedStream != null)
541-
{
542-
_cachedStream.Dispose();
543-
_cachedStream = null;
544+
if (_cachedStream != null)
545+
{
546+
_cachedStream.Dispose();
547+
_cachedStream = null;
548+
}
549+
550+
_currentMinificationManager = null;
544551
}
545552

546-
_currentMinificationManager = null;
553+
base.Dispose(disposing);
547554
}
548-
549-
base.Dispose(disposing);
550555
}
551556
#if NETCOREAPP3_0
552557

553558
public override async ValueTask DisposeAsync()
554559
{
555-
if (_compressionStream != null)
556-
{
557-
await _compressionStream.DisposeAsync();
558-
_compressionStream = null;
559-
}
560-
561-
_currentCompressor = null;
562-
563-
if (_cachedStream != null)
560+
if (_disposedFlag.Set())
564561
{
565-
await _cachedStream.DisposeAsync();
566-
_cachedStream = null;
567-
}
568-
569-
_currentMinificationManager = null;
570-
571-
await base.DisposeAsync();
572-
}
573-
#endif
574-
575-
#endregion
562+
if (_compressionStream != null)
563+
{
564+
await _compressionStream.DisposeAsync();
565+
_compressionStream = null;
566+
}
576567

577-
#region IHttpBufferingFeature implementation
568+
_currentCompressor = null;
578569

579-
public void DisableRequestBuffering()
580-
{
581-
_bufferingFeature?.DisableRequestBuffering();
582-
}
570+
if (_cachedStream != null)
571+
{
572+
await _cachedStream.DisposeAsync();
573+
_cachedStream = null;
574+
}
583575

584-
public void DisableResponseBuffering()
585-
{
586-
string acceptEncoding = _context.Request.Headers[HeaderNames.AcceptEncoding];
587-
ICompressor compressor = InitializeCurrentCompressor(acceptEncoding);
576+
_currentMinificationManager = null;
588577

589-
if (compressor?.SupportsFlush == false)
590-
{
591-
// Some of the compressors don't support flushing which would block real-time
592-
// responses like SignalR.
593-
_compressionEnabled = false;
594-
_currentCompressor = null;
578+
await base.DisposeAsync();
595579
}
596-
else
597-
{
598-
_autoFlushCompressionStream = true;
599-
}
600-
601-
_bufferingFeature?.DisableResponseBuffering();
602580
}
581+
#endif
603582

604583
#endregion
605584
}

0 commit comments

Comments
 (0)