@@ -25,9 +25,6 @@ public class AppStaticFileMiddleware
25
25
26
26
private static readonly ConcurrentDictionary < string , bool > _pathInParsePattern = new ( ) ;
27
27
28
- const long StreamingThreshold = 1024 * 1024 * 10 ; // 10MB
29
- const int MaxBufferSize = 8192 ; // 8KB
30
-
31
28
public static void ConfigureStaticFileMiddleware (
32
29
bool parse ,
33
30
string [ ] ? parsePatterns ,
@@ -143,69 +140,38 @@ public async Task InvokeAsync(HttpContext context)
143
140
customClaims : _customClaimTags ,
144
141
customParameters : null ) ;
145
142
146
-
147
- if ( fileInfo . Length < StreamingThreshold )
148
- {
149
- byte [ ] buffer = new byte [ ( int ) fileInfo . Length ] ;
150
- await fileStream . ReadExactlyAsync ( buffer , context . RequestAborted ) ;
151
- int charCount = Encoding . UTF8 . GetCharCount ( buffer ) ;
152
- char [ ] chars = ArrayPool < char > . Shared . Rent ( charCount ) ;
153
- try
154
- {
155
- Encoding . UTF8 . GetChars ( buffer , 0 , buffer . Length , chars , 0 ) ;
156
- ReadOnlySpan < char > result = parser . Parse ( new ReadOnlySpan < char > ( chars , 0 , charCount ) , context ) ;
157
- var writer = PipeWriter . Create ( context . Response . Body ) ;
158
- try
159
- {
160
- int maxBytesNeeded = Encoding . UTF8 . GetMaxByteCount ( result . Length ) ;
161
- Memory < byte > memory = writer . GetMemory ( maxBytesNeeded ) ;
162
- int actualBytesWritten = Encoding . UTF8 . GetBytes ( result , memory . Span ) ;
163
- writer . Advance ( actualBytesWritten ) ;
164
- context . Response . ContentLength = actualBytesWritten ;
165
- await writer . FlushAsync ( context . RequestAborted ) ;
166
- }
167
- finally
168
- {
169
- await writer . CompleteAsync ( ) ;
170
- }
171
- }
172
- finally
173
- {
174
- ArrayPool < char > . Shared . Return ( chars ) ;
175
- }
176
- }
177
- else
143
+ byte [ ] buffer = new byte [ ( int ) fileInfo . Length ] ;
144
+ await fileStream . ReadExactlyAsync ( buffer , context . RequestAborted ) ;
145
+ int charCount = Encoding . UTF8 . GetCharCount ( buffer ) ;
146
+ char [ ] chars = ArrayPool < char > . Shared . Rent ( charCount ) ;
147
+ try
178
148
{
149
+ Encoding . UTF8 . GetChars ( buffer , 0 , buffer . Length , chars , 0 ) ;
150
+ ReadOnlySpan < char > result = parser . Parse ( new ReadOnlySpan < char > ( chars , 0 , charCount ) , context ) ;
179
151
var writer = PipeWriter . Create ( context . Response . Body ) ;
180
152
try
181
153
{
182
- using var reader = new StreamReader ( fileStream , Encoding . UTF8 , leaveOpen : true ) ;
183
- char [ ] chars = ArrayPool < char > . Shared . Rent ( MaxBufferSize ) ;
184
- try
185
- {
186
- int charsRead ;
187
- while ( ( charsRead = await reader . ReadAsync ( chars , context . RequestAborted ) ) > 0 )
188
- {
189
- var result = parser . Parse ( chars . AsSpan ( 0 , charsRead ) , context ) ;
190
- int bytesWritten = Encoding . UTF8 . GetBytes ( result , writer . GetSpan ( result . Length ) ) ;
191
- writer . Advance ( bytesWritten ) ;
192
- await writer . FlushAsync ( context . RequestAborted ) ;
193
- }
194
- }
195
- finally
196
- {
197
- ArrayPool < char > . Shared . Return ( chars ) ;
198
- }
154
+ int maxBytesNeeded = Encoding . UTF8 . GetMaxByteCount ( result . Length ) ;
155
+ Memory < byte > memory = writer . GetMemory ( maxBytesNeeded ) ;
156
+ int actualBytesWritten = Encoding . UTF8 . GetBytes ( result , memory . Span ) ;
157
+ writer . Advance ( actualBytesWritten ) ;
158
+ context . Response . ContentLength = actualBytesWritten ;
159
+ await writer . FlushAsync ( context . RequestAborted ) ;
199
160
}
200
161
finally
201
162
{
202
163
await writer . CompleteAsync ( ) ;
203
164
}
204
165
}
166
+ finally
167
+ {
168
+ ArrayPool < char > . Shared . Return ( chars ) ;
169
+ }
205
170
}
206
171
catch ( IOException ex )
207
172
{
208
173
_logger ? . Error ( ex , "Failed to serve static file {Path}" , path . ToString ( ) ) ;
174
+
209
175
context . Response . Clear ( ) ;
210
176
await _next ( context ) ;
211
177
return ;
0 commit comments