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(
string httpVersion,
IDictionary<string, StringValues> 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<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
{
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.
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<string, StringValues>(), 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<string, StringValues>(), 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<string, StringValues>(), 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.