diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/DateHeaderValueManager.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/DateHeaderValueManager.cs index 04dc75f464..c471067eca 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/DateHeaderValueManager.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/DateHeaderValueManager.cs @@ -59,7 +59,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http // string used here. // The null-coalesce here is to protect against returning null after Dispose() is called, at which // point _dateValue will be null forever after. - return _dateValue ?? _systemClock.UtcNow.ToString("r"); + return _dateValue ?? _systemClock.UtcNow.ToString(Constants.RFC1123DateFormat); } /// @@ -69,11 +69,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http { lock (_timerLocker) { - if (_dateValueTimer != null) - { - DisposeTimer(); - } - + DisposeTimer(); + _isDisposed = true; } } @@ -94,7 +91,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http // Immediately assign the date value and start the timer again. We assign the value immediately // here as the timer won't fire until the timer interval has passed and we want a value assigned // inline now to serve requests that occur in the meantime. - _dateValue = _systemClock.UtcNow.ToString("r"); + _dateValue = _systemClock.UtcNow.ToString(Constants.RFC1123DateFormat); _dateValueTimer = new Timer(UpdateDateValue, state: null, dueTime: _timerInterval, period: _timerInterval); } } @@ -107,7 +104,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http var now = _systemClock.UtcNow; // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18 for required format of Date header - _dateValue = now.ToString("r"); + _dateValue = now.ToString(Constants.RFC1123DateFormat); if (_hadRequestsSinceLastTimerTick) { @@ -137,9 +134,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Http private void DisposeTimer() { - _dateValueTimer.Dispose(); - _dateValueTimer = null; - _dateValue = null; + if (_dateValueTimer != null) + { + _dateValueTimer.Dispose(); + _dateValueTimer = null; + _dateValue = null; + } } } } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/Constants.cs b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/Constants.cs index 0f4b72424e..9b72df3361 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/Constants.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/Constants.cs @@ -14,5 +14,11 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure /// Prefix of host name used to specify Unix sockets in the configuration. /// public const string UnixPipeHostPrefix = "unix:/"; + + /// + /// DateTime format string for RFC1123. See https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#RFC1123 + /// for info on the format. + /// + public const string RFC1123DateFormat = "r"; } } diff --git a/test/Microsoft.AspNet.Server.KestrelTests/DateHeaderValueManagerTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/DateHeaderValueManagerTests.cs index 71c294caae..ca4fd8ff01 100644 --- a/test/Microsoft.AspNet.Server.KestrelTests/DateHeaderValueManagerTests.cs +++ b/test/Microsoft.AspNet.Server.KestrelTests/DateHeaderValueManagerTests.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Server.Kestrel.Http; +using Microsoft.AspNet.Server.Kestrel.Infrastructure; using Microsoft.AspNet.Server.KestrelTests.TestHelpers; using Xunit; @@ -33,7 +34,7 @@ namespace Microsoft.AspNet.Server.KestrelTests dateHeaderValueManager.Dispose(); } - Assert.Equal(now.ToString("r"), result); + Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result); } [Fact] @@ -62,8 +63,8 @@ namespace Microsoft.AspNet.Server.KestrelTests dateHeaderValueManager.Dispose(); } - Assert.Equal(now.ToString("r"), result1); - Assert.Equal(now.ToString("r"), result2); + Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); + Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result2); Assert.Equal(1, systemClock.UtcNowCalled); } @@ -95,8 +96,8 @@ namespace Microsoft.AspNet.Server.KestrelTests dateHeaderValueManager.Dispose(); } - Assert.Equal(now.ToString("r"), result1); - Assert.Equal(future.ToString("r"), result2); + Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); + Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2); Assert.True(systemClock.UtcNowCalled >= 2); } @@ -118,8 +119,8 @@ namespace Microsoft.AspNet.Server.KestrelTests systemClock.UtcNow = future; var result2 = dateHeaderValueManager.GetDateHeaderValue(); - Assert.Equal(now.ToString("r"), result1); - Assert.Equal(future.ToString("r"), result2); + Assert.Equal(now.ToString(Constants.RFC1123DateFormat), result1); + Assert.Equal(future.ToString(Constants.RFC1123DateFormat), result2); } } }