From 0a1ea124ee379bf75785e8294b1d8db365df55cc Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 21 Jul 2016 22:51:53 +0100 Subject: [PATCH] Use HttpVersionType enum rather than string compare --- .../Internal/Http/Frame.cs | 29 +++++++------------ .../Internal/Http/FrameOfT.cs | 2 +- .../Internal/Http/HttpVersion.cs | 12 ++++++++ .../Internal/Http/MessageBody.cs | 5 ++-- .../FrameTests.cs | 2 +- .../MessageBodyTests.cs | 6 ++-- 6 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/HttpVersion.cs diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs index 894b185dd5..e5c41fa0dd 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs @@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http private bool _autoChunk; protected Exception _applicationException; - private HttpVersionType _httpVersion; + protected HttpVersion _httpVersion; private readonly string _pathBase; @@ -89,11 +89,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { get { - if (_httpVersion == HttpVersionType.Http11) + if (_httpVersion == Http.HttpVersion.Http11) { return "HTTP/1.1"; } - if (_httpVersion == HttpVersionType.Http10) + if (_httpVersion == Http.HttpVersion.Http10) { return "HTTP/1.0"; } @@ -104,15 +104,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { if (value == "HTTP/1.1") { - _httpVersion = HttpVersionType.Http11; + _httpVersion = Http.HttpVersion.Http11; } else if (value == "HTTP/1.0") { - _httpVersion = HttpVersionType.Http10; + _httpVersion = Http.HttpVersion.Http10; } else { - _httpVersion = HttpVersionType.Unset; + _httpVersion = Http.HttpVersion.Unset; } } } @@ -290,7 +290,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http PathBase = null; Path = null; QueryString = null; - _httpVersion = HttpVersionType.Unset; + _httpVersion = Http.HttpVersion.Unset; StatusCode = 200; ReasonPhrase = null; @@ -547,7 +547,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http } StringValues expect; - if (_httpVersion == HttpVersionType.Http11 && + if (_httpVersion == Http.HttpVersion.Http11 && RequestHeaders.TryGetValue("Expect", out expect) && (expect.FirstOrDefault() ?? "").Equals("100-continue", StringComparison.OrdinalIgnoreCase)) { @@ -749,7 +749,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http // // A server MUST NOT send a response containing Transfer-Encoding unless the corresponding // request indicates HTTP/1.1 (or later). - if (_httpVersion == HttpVersionType.Http11) + if (_httpVersion == Http.HttpVersion.Http11) { _autoChunk = true; responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked); @@ -761,11 +761,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http } } - if (!_keepAlive && !hasConnection && _httpVersion != HttpVersionType.Http10) + if (!_keepAlive && !hasConnection && _httpVersion != Http.HttpVersion.Http10) { responseHeaders.SetRawConnection("close", _bytesConnectionClose); } - else if (_keepAlive && !hasConnection && _httpVersion == HttpVersionType.Http10) + else if (_keepAlive && !hasConnection && _httpVersion == Http.HttpVersion.Http10) { responseHeaders.SetRawConnection("keep-alive", _bytesConnectionKeepAlive); } @@ -1235,13 +1235,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http Log.ApplicationError(ConnectionId, ex); } - private enum HttpVersionType - { - Unset = -1, - Http10 = 0, - Http11 = 1 - } - protected enum RequestLineStatus { Empty, diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/FrameOfT.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/FrameOfT.cs index ae77ca679b..bc6cb8ad45 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/FrameOfT.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/FrameOfT.cs @@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http if (!_requestProcessingStopping) { - var messageBody = MessageBody.For(HttpVersion, FrameRequestHeaders, this); + var messageBody = MessageBody.For(_httpVersion, FrameRequestHeaders, this); _keepAlive = messageBody.RequestKeepAlive; InitializeStreams(messageBody); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/HttpVersion.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/HttpVersion.cs new file mode 100644 index 0000000000..67712ec54d --- /dev/null +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/HttpVersion.cs @@ -0,0 +1,12 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http +{ + public enum HttpVersion + { + Unset = -1, + Http10 = 0, + Http11 = 1 + } +} diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/MessageBody.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/MessageBody.cs index a96119cb58..d7b8430d28 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/MessageBody.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/MessageBody.cs @@ -97,13 +97,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http public abstract ValueTask ReadAsyncImplementation(ArraySegment buffer, CancellationToken cancellationToken); public static MessageBody For( - string httpVersion, + HttpVersion httpVersion, FrameRequestHeaders headers, Frame context) { // see also http://tools.ietf.org/html/rfc2616#section-4.4 - - var keepAlive = httpVersion != "HTTP/1.0"; + var keepAlive = httpVersion != HttpVersion.Http10; var connection = headers.HeaderConnection.ToString(); if (connection.Length > 0) diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs index c7ada35248..876b0279f5 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs @@ -502,7 +502,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests var frame = new Frame(application: null, context: connectionContext); frame.InitializeHeaders(); - var messageBody = MessageBody.For("HTTP/1.1", (FrameRequestHeaders)frame.RequestHeaders, frame); + var messageBody = MessageBody.For(HttpVersion.Http11, (FrameRequestHeaders)frame.RequestHeaders, frame); frame.InitializeStreams(messageBody); var originalRequestBody = frame.RequestBody; diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs index f8dba124ef..2dcb19ffef 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var input = new TestInput()) { - var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext); + var body = MessageBody.For(HttpVersion.Http10, new FrameRequestHeaders(), input.FrameContext); var stream = new FrameRequestStream(); stream.StartAcceptingReads(body); @@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var input = new TestInput()) { - var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext); + var body = MessageBody.For(HttpVersion.Http10, new FrameRequestHeaders(), input.FrameContext); var stream = new FrameRequestStream(); stream.StartAcceptingReads(body); @@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var input = new TestInput()) { - var body = MessageBody.For("HTTP/1.0", new FrameRequestHeaders(), input.FrameContext); + var body = MessageBody.For(HttpVersion.Http10, new FrameRequestHeaders(), input.FrameContext); var stream = new FrameRequestStream(); stream.StartAcceptingReads(body);