Use HeaderUtilities.FormatDate in DateHeaderValueManager (#1132)

This commit is contained in:
Kristian Hellang 2016-10-06 20:56:49 +02:00 committed by Pavel Krymets
parent 2c94884da9
commit 03f8a7a217
3 changed files with 17 additions and 16 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
{ {
@ -170,8 +171,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
/// <param name="value">A DateTimeOffset value</param> /// <param name="value">A DateTimeOffset value</param>
private void SetDateValues(DateTimeOffset value) private void SetDateValues(DateTimeOffset value)
{ {
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18 for required format of Date header var dateValue = HeaderUtilities.FormatDate(value);
var dateValue = value.ToString(Constants.RFC1123DateFormat);
var dateBytes = new byte[_datePreambleBytes.Length + dateValue.Length]; var dateBytes = new byte[_datePreambleBytes.Length + dateValue.Length];
Buffer.BlockCopy(_datePreambleBytes, 0, dateBytes, 0, _datePreambleBytes.Length); Buffer.BlockCopy(_datePreambleBytes, 0, dateBytes, 0, _datePreambleBytes.Length);
Encoding.ASCII.GetBytes(dateValue, 0, dateValue.Length, dateBytes, _datePreambleBytes.Length); Encoding.ASCII.GetBytes(dateValue, 0, dateValue.Length, dateBytes, _datePreambleBytes.Length);

View File

@ -17,12 +17,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
/// </summary> /// </summary>
public const string UnixPipeHostPrefix = "unix:/"; public const string UnixPipeHostPrefix = "unix:/";
/// <summary>
/// DateTime format string for RFC1123. See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123
/// for info on the format.
/// </summary>
public const string RFC1123DateFormat = "r";
public const string ServerName = "Kestrel"; public const string ServerName = "Kestrel";
private static int? GetEADDRINUSE() private static int? GetEADDRINUSE()

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; using Microsoft.AspNetCore.Server.Kestrel.Internal.Http;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Xunit; using Xunit;
@ -12,6 +11,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
public class DateHeaderValueManagerTests public class DateHeaderValueManagerTests
{ {
/// <summary>
/// DateTime format string for RFC1123.
/// </summary>
/// <remarks>
/// See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123 for info on the format.
/// </remarks>
private const string Rfc1123DateFormat = "r";
[Fact] [Fact]
public void GetDateHeaderValue_ReturnsDateValueInRFC1123Format() public void GetDateHeaderValue_ReturnsDateValueInRFC1123Format()
{ {
@ -34,7 +41,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
dateHeaderValueManager.Dispose(); dateHeaderValueManager.Dispose();
} }
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result); Assert.Equal(now.ToString(Rfc1123DateFormat), result);
} }
[Fact] [Fact]
@ -63,8 +70,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
dateHeaderValueManager.Dispose(); dateHeaderValueManager.Dispose();
} }
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); Assert.Equal(now.ToString(Rfc1123DateFormat), result1);
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result2); Assert.Equal(now.ToString(Rfc1123DateFormat), result2);
Assert.Equal(1, systemClock.UtcNowCalled); Assert.Equal(1, systemClock.UtcNowCalled);
} }
@ -96,8 +103,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
dateHeaderValueManager.Dispose(); dateHeaderValueManager.Dispose();
} }
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); Assert.Equal(now.ToString(Rfc1123DateFormat), result1);
Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2); Assert.Equal(future.ToString(Rfc1123DateFormat), result2);
Assert.True(systemClock.UtcNowCalled >= 2); Assert.True(systemClock.UtcNowCalled >= 2);
} }
@ -119,8 +126,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
systemClock.UtcNow = future; systemClock.UtcNow = future;
var result2 = dateHeaderValueManager.GetDateHeaderValues().String; var result2 = dateHeaderValueManager.GetDateHeaderValues().String;
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); Assert.Equal(now.ToString(Rfc1123DateFormat), result1);
Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2); Assert.Equal(future.ToString(Rfc1123DateFormat), result2);
} }
} }
} }