|
9 | 9 | using System.IO; |
10 | 10 | using System.Linq; |
11 | 11 | using System.Text; |
12 | | -using System.Threading; |
13 | 12 | using System.Threading.Tasks; |
14 | 13 | using Microsoft.AspNetCore.Http; |
15 | 14 | using Microsoft.Extensions.Logging; |
@@ -185,16 +184,24 @@ public ImageSharpMiddleware( |
185 | 184 | public async Task Invoke(HttpContext context) |
186 | 185 | #pragma warning restore IDE1006 // Naming Styles |
187 | 186 | { |
188 | | - IDictionary<string, string> commands = this.requestParser.ParseRequestCommands(context); |
| 187 | + // We expect to get concrete collection type which removes virtual dispatch concerns and enumerator allocations |
| 188 | + IDictionary<string, string> parsedCommands = this.requestParser.ParseRequestCommands(context); |
| 189 | + Dictionary<string, string> commands = parsedCommands as Dictionary<string, string> ?? new Dictionary<string, string>(parsedCommands, StringComparer.OrdinalIgnoreCase); |
| 190 | + |
189 | 191 | if (commands.Count > 0) |
190 | 192 | { |
191 | | - // Strip out any unknown commands. |
192 | | - foreach (string command in new List<string>(commands.Keys)) |
| 193 | + // Strip out any unknown commands, if needed. |
| 194 | + int index = 0; |
| 195 | + foreach (string command in commands.Keys) |
193 | 196 | { |
194 | 197 | if (!this.knownCommands.Contains(command)) |
195 | 198 | { |
196 | | - commands.Remove(command); |
| 199 | + // Need to actually remove, allocates new list to allow modifications |
| 200 | + this.StripUnknownCommands(commands, startAtIndex: index); |
| 201 | + break; |
197 | 202 | } |
| 203 | + |
| 204 | + ++index; |
198 | 205 | } |
199 | 206 | } |
200 | 207 |
|
@@ -239,6 +246,19 @@ await this.ProcessRequestAsync( |
239 | 246 | commands); |
240 | 247 | } |
241 | 248 |
|
| 249 | + private void StripUnknownCommands(Dictionary<string, string> commands, int startAtIndex) |
| 250 | + { |
| 251 | + var keys = new List<string>(commands.Keys); |
| 252 | + for (var index = startAtIndex; index < keys.Count; index++) |
| 253 | + { |
| 254 | + string command = keys[index]; |
| 255 | + if (!this.knownCommands.Contains(command)) |
| 256 | + { |
| 257 | + commands.Remove(command); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + |
242 | 262 | private async Task ProcessRequestAsync( |
243 | 263 | HttpContext context, |
244 | 264 | IImageResolver sourceImageResolver, |
|
0 commit comments