This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
/
DateHeaderValueManager.cs
194 lines (170 loc) · 6.48 KB
/
DateHeaderValueManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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.
using System;
using System.Text;
using System.Threading;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking;
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
{
/// <summary>
/// Manages the generation of the date header value.
/// </summary>
public class DateHeaderValueManager : IDisposable
{
private static readonly byte[] _datePreambleBytes = Encoding.ASCII.GetBytes("\r\nDate: ");
private readonly ISystemClock _systemClock;
private readonly TimeSpan _timeWithoutRequestsUntilIdle;
private readonly TimeSpan _timerInterval;
private readonly object _timerLocker = new object();
private DateHeaderValues _dateValues;
private volatile bool _isDisposed = false;
private volatile bool _hadRequestsSinceLastTimerTick = false;
private Timer _dateValueTimer;
private long _lastRequestSeenTicks;
private volatile bool _timerIsRunning;
/// <summary>
/// Initializes a new instance of the <see cref="DateHeaderValueManager"/> class.
/// </summary>
public DateHeaderValueManager()
: this(systemClock: new SystemClock())
{
}
// Internal for testing
internal DateHeaderValueManager(ISystemClock systemClock)
: this(
systemClock: systemClock,
timeWithoutRequestsUntilIdle: TimeSpan.FromSeconds(10),
timerInterval: TimeSpan.FromSeconds(1))
{
}
// Internal for testing
internal DateHeaderValueManager(
ISystemClock systemClock,
TimeSpan timeWithoutRequestsUntilIdle,
TimeSpan timerInterval)
{
if (systemClock == null)
{
throw new ArgumentNullException(nameof(systemClock));
}
_systemClock = systemClock;
_timeWithoutRequestsUntilIdle = timeWithoutRequestsUntilIdle;
_timerInterval = timerInterval;
_dateValueTimer = new Timer(TimerLoop, state: null, dueTime: Timeout.Infinite, period: Timeout.Infinite);
}
/// <summary>
/// Returns a value representing the current server date/time for use in the HTTP "Date" response header
/// in accordance with http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18
/// </summary>
/// <returns>The value in string and byte[] format.</returns>
public DateHeaderValues GetDateHeaderValues()
{
_hadRequestsSinceLastTimerTick = !_isDisposed;
if (!_timerIsRunning)
{
StartTimer();
}
return _dateValues;
}
/// <summary>
/// Releases all resources used by the current instance of <see cref="DateHeaderValueManager"/>.
/// </summary>
public void Dispose()
{
if (!_isDisposed)
{
_isDisposed = true;
_hadRequestsSinceLastTimerTick = false;
lock (_timerLocker)
{
if (_dateValueTimer != null)
{
_timerIsRunning = false;
_dateValueTimer.Dispose();
_dateValueTimer = null;
}
}
}
}
/// <summary>
/// Starts the timer
/// </summary>
private void StartTimer()
{
var now = _systemClock.UtcNow;
SetDateValues(now);
if (!_isDisposed)
{
lock (_timerLocker)
{
if (!_timerIsRunning && _dateValueTimer != null)
{
_timerIsRunning = true;
_dateValueTimer.Change(_timerInterval, _timerInterval);
}
}
}
}
/// <summary>
/// Stops the timer
/// </summary>
private void StopTimer()
{
if (!_isDisposed)
{
lock (_timerLocker)
{
if (_dateValueTimer != null)
{
_timerIsRunning = false;
_dateValueTimer.Change(Timeout.Infinite, Timeout.Infinite);
_hadRequestsSinceLastTimerTick = false;
}
}
}
}
// Called by the Timer (background) thread
private void TimerLoop(object state)
{
var now = _systemClock.UtcNow;
SetDateValues(now);
if (_hadRequestsSinceLastTimerTick)
{
// We served requests since the last tick, reset the flag and return as we're still active
_hadRequestsSinceLastTimerTick = false;
Interlocked.Exchange(ref _lastRequestSeenTicks, now.Ticks);
return;
}
// No requests since the last timer tick, we need to check if we're beyond the idle threshold
if ((now.Ticks - PlatformApis.VolatileRead(ref _lastRequestSeenTicks)) >= _timeWithoutRequestsUntilIdle.Ticks)
{
// No requests since idle threshold so stop the timer if it's still running
StopTimer();
}
}
/// <summary>
/// Sets date values from a provided ticks value
/// </summary>
/// <param name="value">A DateTimeOffset value</param>
private void SetDateValues(DateTimeOffset value)
{
var dateValue = HeaderUtilities.FormatDate(value);
var dateBytes = new byte[_datePreambleBytes.Length + dateValue.Length];
Buffer.BlockCopy(_datePreambleBytes, 0, dateBytes, 0, _datePreambleBytes.Length);
Encoding.ASCII.GetBytes(dateValue, 0, dateValue.Length, dateBytes, _datePreambleBytes.Length);
var dateValues = new DateHeaderValues()
{
Bytes = dateBytes,
String = dateValue
};
Volatile.Write(ref _dateValues, dateValues);
}
public class DateHeaderValues
{
public byte[] Bytes;
public string String;
}
}
}