Faster MessageBody.For

This commit is contained in:
Ben Adams 2015-12-26 07:11:54 +00:00
parent ada61f8181
commit 11b360b667
2 changed files with 10 additions and 36 deletions

View File

@ -73,27 +73,27 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
public static MessageBody For( public static MessageBody For(
string httpVersion, string httpVersion,
IDictionary<string, StringValues> headers, FrameRequestHeaders headers,
FrameContext context) FrameContext context)
{ {
// see also http://tools.ietf.org/html/rfc2616#section-4.4 // see also http://tools.ietf.org/html/rfc2616#section-4.4
var keepAlive = httpVersion != "HTTP/1.0"; var keepAlive = httpVersion != "HTTP/1.0";
string connection; var connection = headers.HeaderConnection.ToString();
if (TryGet(headers, "Connection", out connection)) if (connection.Length > 0)
{ {
keepAlive = connection.Equals("keep-alive", StringComparison.OrdinalIgnoreCase); keepAlive = connection.Equals("keep-alive", StringComparison.OrdinalIgnoreCase);
} }
string transferEncoding; var transferEncoding = headers.HeaderTransferEncoding.ToString();
if (TryGet(headers, "Transfer-Encoding", out transferEncoding)) if (transferEncoding.Length > 0)
{ {
return new ForChunkedEncoding(keepAlive, context); return new ForChunkedEncoding(keepAlive, context);
} }
string contentLength; var contentLength = headers.HeaderContentLength.ToString();
if (TryGet(headers, "Content-Length", out contentLength)) if (contentLength.Length > 0)
{ {
return new ForContentLength(keepAlive, int.Parse(contentLength), context); return new ForContentLength(keepAlive, int.Parse(contentLength), context);
} }
@ -106,30 +106,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
return new ForRemainingData(context); return new ForRemainingData(context);
} }
public static bool TryGet(IDictionary<string, StringValues> 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 class ForRemainingData : MessageBody
{ {
public ForRemainingData(FrameContext context) public ForRemainingData(FrameContext context)

View File

@ -2,11 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Server.Kestrel.Http; using Microsoft.AspNet.Server.Kestrel.Http;
using Microsoft.Extensions.Primitives;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Server.KestrelTests namespace Microsoft.AspNet.Server.KestrelTests
@ -20,7 +18,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
public void Http10ConnectionClose() public void Http10ConnectionClose()
{ {
var input = new TestInput(); var input = new TestInput();
var body = MessageBody.For("HTTP/1.0", new Dictionary<string, StringValues>(), input.FrameContext); var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext);
var stream = new FrameRequestStream().StartAcceptingReads(body); var stream = new FrameRequestStream().StartAcceptingReads(body);
input.Add("Hello", true); input.Add("Hello", true);
@ -38,7 +36,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
public async Task Http10ConnectionCloseAsync() public async Task Http10ConnectionCloseAsync()
{ {
var input = new TestInput(); var input = new TestInput();
var body = MessageBody.For("HTTP/1.0", new Dictionary<string, StringValues>(), input.FrameContext); var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext);
var stream = new FrameRequestStream().StartAcceptingReads(body); var stream = new FrameRequestStream().StartAcceptingReads(body);
input.Add("Hello", true); input.Add("Hello", true);
@ -56,7 +54,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
public async Task CanHandleLargeBlocks() public async Task CanHandleLargeBlocks()
{ {
var input = new TestInput(); var input = new TestInput();
var body = MessageBody.For("HTTP/1.0", new Dictionary<string, StringValues>(), input.FrameContext); var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext);
var stream = new FrameRequestStream().StartAcceptingReads(body); var stream = new FrameRequestStream().StartAcceptingReads(body);
// Input needs to be greater than 4032 bytes to allocate a block not backed by a slab. // Input needs to be greater than 4032 bytes to allocate a block not backed by a slab.