From 11b360b66779416b06b3763b7a68af53cf6662b5 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 26 Dec 2015 07:11:54 +0000 Subject: [PATCH] Faster MessageBody.For --- .../Http/MessageBody.cs | 38 ++++--------------- .../MessageBodyTests.cs | 8 ++-- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/MessageBody.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/MessageBody.cs index 8edd424a10..d1b2643624 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/MessageBody.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/MessageBody.cs @@ -73,27 +73,27 @@ namespace Microsoft.AspNet.Server.Kestrel.Http public static MessageBody For( string httpVersion, - IDictionary headers, + FrameRequestHeaders headers, FrameContext context) { // see also http://tools.ietf.org/html/rfc2616#section-4.4 var keepAlive = httpVersion != "HTTP/1.0"; - string connection; - if (TryGet(headers, "Connection", out connection)) + var connection = headers.HeaderConnection.ToString(); + if (connection.Length > 0) { keepAlive = connection.Equals("keep-alive", StringComparison.OrdinalIgnoreCase); } - string transferEncoding; - if (TryGet(headers, "Transfer-Encoding", out transferEncoding)) + var transferEncoding = headers.HeaderTransferEncoding.ToString(); + if (transferEncoding.Length > 0) { return new ForChunkedEncoding(keepAlive, context); } - string contentLength; - if (TryGet(headers, "Content-Length", out contentLength)) + var contentLength = headers.HeaderContentLength.ToString(); + if (contentLength.Length > 0) { return new ForContentLength(keepAlive, int.Parse(contentLength), context); } @@ -106,30 +106,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http return new ForRemainingData(context); } - public static bool TryGet(IDictionary headers, string name, out string value) - { - StringValues values; - if (!headers.TryGetValue(name, out values) || values.Count == 0) - { - value = null; - return false; - } - var count = values.Count; - if (count == 0) - { - value = null; - return false; - } - if (count == 1) - { - value = values[0]; - return true; - } - value = string.Join(",", values.ToArray()); - return true; - } - - class ForRemainingData : MessageBody { public ForRemainingData(FrameContext context) diff --git a/test/Microsoft.AspNet.Server.KestrelTests/MessageBodyTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/MessageBodyTests.cs index 19e7d2e9b2..f93c144c8f 100644 --- a/test/Microsoft.AspNet.Server.KestrelTests/MessageBodyTests.cs +++ b/test/Microsoft.AspNet.Server.KestrelTests/MessageBodyTests.cs @@ -2,11 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Server.Kestrel.Http; -using Microsoft.Extensions.Primitives; using Xunit; namespace Microsoft.AspNet.Server.KestrelTests @@ -20,7 +18,7 @@ namespace Microsoft.AspNet.Server.KestrelTests public void Http10ConnectionClose() { var input = new TestInput(); - var body = MessageBody.For("HTTP/1.0", new Dictionary(), input.FrameContext); + var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext); var stream = new FrameRequestStream().StartAcceptingReads(body); input.Add("Hello", true); @@ -38,7 +36,7 @@ namespace Microsoft.AspNet.Server.KestrelTests public async Task Http10ConnectionCloseAsync() { var input = new TestInput(); - var body = MessageBody.For("HTTP/1.0", new Dictionary(), input.FrameContext); + var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext); var stream = new FrameRequestStream().StartAcceptingReads(body); input.Add("Hello", true); @@ -56,7 +54,7 @@ namespace Microsoft.AspNet.Server.KestrelTests public async Task CanHandleLargeBlocks() { var input = new TestInput(); - var body = MessageBody.For("HTTP/1.0", new Dictionary(), input.FrameContext); + var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext); var stream = new FrameRequestStream().StartAcceptingReads(body); // Input needs to be greater than 4032 bytes to allocate a block not backed by a slab.