/
HttpCacheFeature.cs
278 lines (232 loc) · 10.2 KB
/
HttpCacheFeature.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using ServiceStack.Web;
namespace ServiceStack
{
public class HttpCacheFeature : IPlugin
{
public TimeSpan DefaultMaxAge { get; set; }
public TimeSpan DefaultExpiresIn { get; set; }
public Func<string, string> CacheControlFilter { get; set; }
public string CacheControlForOptimizedResults { get; set; }
public HttpCacheFeature()
{
DefaultMaxAge = TimeSpan.FromMinutes(10);
DefaultExpiresIn = TimeSpan.FromMinutes(10);
CacheControlForOptimizedResults = "max-age=0";
}
public void Register(IAppHost appHost)
{
appHost.GlobalResponseFilters.Add(HandleCacheResponses);
}
public void HandleCacheResponses(IRequest req, IResponse res, object response)
{
if (req.IsInProcessRequest())
return;
var cacheInfo = req.GetItem(Keywords.CacheInfo) as CacheInfo;
if (cacheInfo != null && cacheInfo.CacheKey != null)
{
if (CacheAndWriteResponse(cacheInfo, req, res, response))
return;
}
var httpResult = response as HttpResult;
if (httpResult == null)
return;
cacheInfo = httpResult.ToCacheInfo();
if ((req.Verb != HttpMethods.Get && req.Verb != HttpMethods.Head) ||
(httpResult.StatusCode != HttpStatusCode.OK && httpResult.StatusCode != HttpStatusCode.NotModified))
return;
if (httpResult.LastModified != null)
httpResult.Headers[HttpHeaders.LastModified] = httpResult.LastModified.Value.ToUniversalTime().ToString("r");
if (httpResult.ETag != null)
httpResult.Headers[HttpHeaders.ETag] = httpResult.ETag.Quoted();
if (httpResult.Expires != null)
httpResult.Headers[HttpHeaders.Expires] = httpResult.Expires.Value.ToUniversalTime().ToString("r");
if (httpResult.Age != null)
httpResult.Headers[HttpHeaders.Age] = httpResult.Age.Value.TotalSeconds.ToString(CultureInfo.InvariantCulture);
var alreadySpecifiedCacheControl = httpResult.Headers.ContainsKey(HttpHeaders.CacheControl);
if (!alreadySpecifiedCacheControl)
{
var cacheControl = BuildCacheControlHeader(cacheInfo);
if (cacheControl != null)
httpResult.Headers[HttpHeaders.CacheControl] = cacheControl;
}
if (req.ETagMatch(httpResult.ETag) || req.NotModifiedSince(httpResult.LastModified))
{
res.EndNotModified();
}
}
private bool CacheAndWriteResponse(CacheInfo cacheInfo, IRequest req, IResponse res, object response)
{
var httpResult = response as IHttpResult;
var dto = httpResult != null ? httpResult.Response : response;
if (dto == null || dto is IPartialWriter || dto is IStreamWriter)
return false;
var expiresIn = cacheInfo.ExpiresIn.GetValueOrDefault(DefaultExpiresIn);
var cache = cacheInfo.LocalCache ? HostContext.LocalCache : HostContext.Cache;
var responseBytes = dto as byte[];
if (responseBytes == null)
{
var rawStr = dto as string;
if (rawStr != null)
responseBytes = rawStr.ToUtf8Bytes();
else
{
var stream = dto as Stream;
if (stream != null)
responseBytes = stream.ReadFully();
}
}
var encoding = !cacheInfo.NoCompression
? req.GetCompressionType()
: null;
var cacheKeyEncoded = encoding != null ? cacheInfo.CacheKey + "." + encoding : null;
if (responseBytes != null || req.ResponseContentType.IsBinary())
{
if (responseBytes == null)
responseBytes = HostContext.ContentTypes.SerializeToBytes(req, dto);
cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);
if (encoding != null)
{
res.AddHeader(HttpHeaders.ContentEncoding, encoding);
responseBytes = responseBytes.CompressBytes(encoding);
cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
}
}
else
{
var serializedDto = req.SerializeToString(dto);
if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
{
var jsonp = req.GetJsonpCallback();
if (jsonp != null)
serializedDto = jsonp + "(" + serializedDto + ")";
}
responseBytes = serializedDto.ToUtf8Bytes();
cache.Set(cacheInfo.CacheKey, responseBytes, expiresIn);
if (encoding != null)
{
responseBytes = responseBytes.CompressBytes(encoding);
cache.Set(cacheKeyEncoded, responseBytes, expiresIn);
res.AddHeader(HttpHeaders.ContentEncoding, encoding);
}
}
var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None;
if (doHttpCaching)
{
var cacheControl = BuildCacheControlHeader(cacheInfo);
if (cacheControl != null)
{
var lastModified = cacheInfo.LastModified.GetValueOrDefault(DateTime.UtcNow);
cache.Set("date:" + cacheInfo.CacheKey, lastModified, expiresIn);
res.AddHeaderLastModified(lastModified);
res.AddHeader(HttpHeaders.CacheControl, cacheControl);
if (encoding != null)
res.AddHeader(HttpHeaders.Vary, "Accept-Encoding");
if (cacheInfo.VaryByUser)
res.AddHeader(HttpHeaders.Vary, "Cookie");
}
}
if (httpResult != null)
{
foreach (var header in httpResult.Headers)
{
res.AddHeader(header.Key, header.Value);
}
}
res.WriteBytesToResponse(responseBytes, req.ResponseContentType);
return true;
}
public string BuildCacheControlHeader(CacheInfo cacheInfo)
{
var maxAge = cacheInfo.MaxAge;
if (maxAge == null && (cacheInfo.LastModified != null || cacheInfo.ETag != null))
maxAge = DefaultMaxAge;
var cacheHeader = new List<string>();
if (maxAge != null)
cacheHeader.Add("max-age=" + maxAge.Value.TotalSeconds);
if (cacheInfo.CacheControl != CacheControl.None)
{
var cache = cacheInfo.CacheControl;
if (cache.Has(CacheControl.Public))
cacheHeader.Add("public");
else if (cache.Has(CacheControl.Private))
cacheHeader.Add("private");
if (cache.Has(CacheControl.MustRevalidate))
cacheHeader.Add("must-revalidate");
if (cache.Has(CacheControl.NoCache))
cacheHeader.Add("no-cache");
if (cache.Has(CacheControl.NoStore))
cacheHeader.Add("no-store");
if (cache.Has(CacheControl.NoTransform))
cacheHeader.Add("no-transform");
if (cache.Has(CacheControl.ProxyRevalidate))
cacheHeader.Add("proxy-revalidate");
}
if (cacheHeader.Count <= 0)
return null;
var cacheControl = cacheHeader.ToArray().Join(", ");
return CacheControlFilter != null
? CacheControlFilter(cacheControl)
: cacheControl;
}
}
public static class HttpCacheExtensions
{
public static bool Has(this CacheControl cache, CacheControl flag)
{
return (flag & cache) != 0;
}
public static void EndNotModified(this IResponse res, string description=null)
{
res.StatusCode = 304;
res.StatusDescription = description ?? HostContext.ResolveLocalizedString(LocalizedStrings.NotModified);
res.EndRequest();
}
public static bool ETagMatch(this IRequest req, string eTag)
{
if (string.IsNullOrEmpty(eTag))
return false;
return eTag.StripWeakRef().Quoted() == req.Headers[HttpHeaders.IfNoneMatch].StripWeakRef().Quoted();
}
public static bool NotModifiedSince(this IRequest req, DateTime? lastModified)
{
if (lastModified != null)
{
var ifModifiedSince = req.Headers[HttpHeaders.IfModifiedSince];
if (ifModifiedSince != null)
{
DateTime modifiedSinceDate;
if (DateTime.TryParse(ifModifiedSince, out modifiedSinceDate))
return modifiedSinceDate <= lastModified.Value;
}
}
return false;
}
public static bool HasValidCache(this IRequest req, string eTag)
{
return req.ETagMatch(eTag);
}
public static bool HasValidCache(this IRequest req, DateTime? lastModified)
{
return req.NotModifiedSince(lastModified);
}
public static bool HasValidCache(this IRequest req, string eTag, DateTime? lastModified)
{
return req.ETagMatch(eTag) || req.NotModifiedSince(lastModified);
}
public static bool ShouldAddLastModifiedToOptimizedResults(this HttpCacheFeature feature)
{
return feature != null && feature.CacheControlForOptimizedResults != null;
}
internal static string StripWeakRef(this string eTag)
{
return eTag != null && eTag.StartsWith("W/")
? eTag.Substring(2)
: eTag;
}
}
}