Skip to content

Commit d0b2e31

Browse files
committed
make sure to restore body stream in case on exception
reduce log level to debug
1 parent d664660 commit d0b2e31

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ When you install the package, it should be added to your `package.json`. Alterna
2222
```json
2323
{
2424
"dependencies" : {
25-
"ZNetCS.AspNetCore.Compression": "1.0.2"
25+
"ZNetCS.AspNetCore.Compression": "1.0.3"
2626
}
2727
}
2828
```

src/ZNetCS.AspNetCore.Compression/CompressionMiddleware.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ public async Task Invoke(HttpContext context)
9191
var decompressionExecutor = context.RequestServices.GetRequiredService<DecompressionExecutor>();
9292

9393
// first decompress incoming request
94-
this.logger.LogInformation("Checking request for decompression: " + context.Request.Path);
94+
this.logger.LogDebug("Checking request for decompression: " + context.Request.Path);
9595
if (decompressionExecutor.CanDecompress(context, this.options.Decompressors))
9696
{
9797
await decompressionExecutor.ExecuteAsync(context, this.options.Decompressors, cancellationToken);
9898
}
9999

100-
this.logger.LogInformation("Checking response for compression: " + context.Request.Path);
100+
this.logger.LogDebug("Checking response for compression: " + context.Request.Path);
101101
var compressionExecutor = context.RequestServices.GetRequiredService<CompressionExecutor>();
102102

103103
// check we are supporting accepted encodings and request path is not ignored
@@ -108,15 +108,22 @@ public async Task Invoke(HttpContext context)
108108
Stream bodyStream = context.Response.Body;
109109
context.Response.Body = bufferedStream;
110110

111-
await this.next.Invoke(context);
111+
try
112+
{
113+
await this.next.Invoke(context);
114+
}
115+
finally
116+
{
117+
context.Response.Body = bodyStream;
118+
}
112119

113-
context.Response.Body = bodyStream;
114120
bufferedStream.Seek(0, SeekOrigin.Begin);
115121

116122
// skip compression for small requests, and not allowed media types
117123
if ((bufferedStream.Length < this.options.MinimumCompressionThreshold) || !compressionExecutor.CanCompress(context, this.options.AllowedMediaTypes))
118124
{
119125
// simply copy buffed value to output stream
126+
this.logger.LogDebug("Continue response without compression");
120127
await bufferedStream.CopyToAsync(context.Response.Body, Consts.DefaultBufferSize, cancellationToken);
121128
}
122129
else
@@ -128,11 +135,11 @@ public async Task Invoke(HttpContext context)
128135
}
129136
else
130137
{
131-
this.logger.LogInformation("Continue response without compression");
138+
this.logger.LogDebug("Continue response without compression");
132139
await this.next.Invoke(context);
133140
}
134141

135-
this.logger.LogInformation("Finished handling request.");
142+
this.logger.LogDebug("Finished handling request.");
136143
}
137144

138145
#endregion

src/ZNetCS.AspNetCore.Compression/Infrastructure/CompressionExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public async Task ExecuteAsync(HttpContext context, Stream bufferedStream, IColl
144144

145145
if (compressor != null)
146146
{
147-
this.logger.LogInformation($"Compressing response using {compressor.ContentCoding} compressor.");
147+
this.logger.LogDebug($"Compressing response using {compressor.ContentCoding} compressor.");
148148

149149
// we need to wrap to be able to count length after compression, body is unreadable
150150
// also all headers needs to be set before data is starting to be copied to output body
@@ -175,7 +175,7 @@ public async Task ExecuteAsync(HttpContext context, Stream bufferedStream, IColl
175175
await compressionStream.CopyToAsync(context.Response.Body, Consts.DefaultBufferSize, cancellationToken);
176176
}
177177

178-
this.logger.LogInformation("Finished compressing request.");
178+
this.logger.LogDebug("Finished compressing request.");
179179
}
180180
}
181181

src/ZNetCS.AspNetCore.Compression/Infrastructure/DecompressionExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public async Task ExecuteAsync(HttpContext context, IEnumerable<IDecompressor> d
109109

110110
if (decompressor != null)
111111
{
112-
this.logger.LogInformation($"Decompressing request using {decompressor.ContentCoding} decompressor.");
112+
this.logger.LogDebug($"Decompressing request using {decompressor.ContentCoding} decompressor.");
113113

114114
Stream decompressed = new MemoryStream();
115115

@@ -141,7 +141,7 @@ public async Task ExecuteAsync(HttpContext context, IEnumerable<IDecompressor> d
141141
context.Request.Body = decompressed;
142142
}
143143

144-
this.logger.LogInformation("Finished decompressing request.");
144+
this.logger.LogDebug("Finished decompressing request.");
145145
}
146146
}
147147

src/ZNetCS.AspNetCore.Compression/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.2",
2+
"version": "1.0.3",
33
"packOptions": {
44
"owners": [ "Marcin Smółka" ],
55
"licenseUrl": "https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.Compression/master/LICENSE",

0 commit comments

Comments
 (0)