Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 03f8a7a

Browse files
khellangpakrym
authored andcommitted
Use HeaderUtilities.FormatDate in DateHeaderValueManager (#1132)
1 parent 2c94884 commit 03f8a7a

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/DateHeaderValueManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading;
77
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
8+
using Microsoft.Net.Http.Headers;
89

910
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
1011
{
@@ -170,8 +171,7 @@ private void TimerLoop(object state)
170171
/// <param name="value">A DateTimeOffset value</param>
171172
private void SetDateValues(DateTimeOffset value)
172173
{
173-
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18 for required format of Date header
174-
var dateValue = value.ToString(Constants.RFC1123DateFormat);
174+
var dateValue = HeaderUtilities.FormatDate(value);
175175
var dateBytes = new byte[_datePreambleBytes.Length + dateValue.Length];
176176
Buffer.BlockCopy(_datePreambleBytes, 0, dateBytes, 0, _datePreambleBytes.Length);
177177
Encoding.ASCII.GetBytes(dateValue, 0, dateValue.Length, dateBytes, _datePreambleBytes.Length);

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/Constants.cs

-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ internal class Constants
1717
/// </summary>
1818
public const string UnixPipeHostPrefix = "unix:/";
1919

20-
/// <summary>
21-
/// DateTime format string for RFC1123. See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123
22-
/// for info on the format.
23-
/// </summary>
24-
public const string RFC1123DateFormat = "r";
25-
2620
public const string ServerName = "Kestrel";
2721

2822
private static int? GetEADDRINUSE()

test/Microsoft.AspNetCore.Server.KestrelTests/DateHeaderValueManagerTests.cs

+15-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
using System;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Server.Kestrel.Internal.Http;
7-
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
87
using Microsoft.AspNetCore.Testing;
98
using Xunit;
109

1110
namespace Microsoft.AspNetCore.Server.KestrelTests
1211
{
1312
public class DateHeaderValueManagerTests
1413
{
14+
/// <summary>
15+
/// DateTime format string for RFC1123.
16+
/// </summary>
17+
/// <remarks>
18+
/// See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123 for info on the format.
19+
/// </remarks>
20+
private const string Rfc1123DateFormat = "r";
21+
1522
[Fact]
1623
public void GetDateHeaderValue_ReturnsDateValueInRFC1123Format()
1724
{
@@ -34,7 +41,7 @@ public void GetDateHeaderValue_ReturnsDateValueInRFC1123Format()
3441
dateHeaderValueManager.Dispose();
3542
}
3643

37-
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result);
44+
Assert.Equal(now.ToString(Rfc1123DateFormat), result);
3845
}
3946

4047
[Fact]
@@ -63,8 +70,8 @@ public void GetDateHeaderValue_ReturnsCachedValueBetweenTimerTicks()
6370
dateHeaderValueManager.Dispose();
6471
}
6572

66-
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1);
67-
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result2);
73+
Assert.Equal(now.ToString(Rfc1123DateFormat), result1);
74+
Assert.Equal(now.ToString(Rfc1123DateFormat), result2);
6875
Assert.Equal(1, systemClock.UtcNowCalled);
6976
}
7077

@@ -96,8 +103,8 @@ public async Task GetDateHeaderValue_ReturnsUpdatedValueAfterIdle()
96103
dateHeaderValueManager.Dispose();
97104
}
98105

99-
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1);
100-
Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2);
106+
Assert.Equal(now.ToString(Rfc1123DateFormat), result1);
107+
Assert.Equal(future.ToString(Rfc1123DateFormat), result2);
101108
Assert.True(systemClock.UtcNowCalled >= 2);
102109
}
103110

@@ -119,8 +126,8 @@ public void GetDateHeaderValue_ReturnsDateValueAfterDisposed()
119126
systemClock.UtcNow = future;
120127
var result2 = dateHeaderValueManager.GetDateHeaderValues().String;
121128

122-
Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1);
123-
Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2);
129+
Assert.Equal(now.ToString(Rfc1123DateFormat), result1);
130+
Assert.Equal(future.ToString(Rfc1123DateFormat), result2);
124131
}
125132
}
126133
}

0 commit comments

Comments
 (0)