-
Notifications
You must be signed in to change notification settings - Fork 162
/
ResponseWrapper.cs
157 lines (139 loc) · 5.29 KB
/
ResponseWrapper.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
using Roadkill.Core.Extensions;
using System;
using System.IO;
using System.Web;
namespace Roadkill.Core.Attachments
{
/// <summary>
/// A wrapper around HttpResponse, including caching capabilities.
/// </summary>
public class ResponseWrapper : IResponseWrapper
{
private HttpResponseBase _context;
public int StatusCode { get; set; }
public string ContentType { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ResponseWrapper"/> class.
/// </summary>
public ResponseWrapper()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ResponseWrapper"/> class.
/// </summary>
/// <param name="context">The <see cref="HttpResponseBase"/> context.</param>
public ResponseWrapper(HttpResponseBase context)
{
_context = context;
}
/// <summary>
/// Writes the specified text to the HttpResponse, using the current content type.
/// </summary>
/// <param name="text">The text.</param>
public void Write(string text)
{
if (_context != null)
{
_context.ContentType = ContentType;
_context.Write(text);
}
}
/// <summary>
/// Writes binary output to the HttpResponse.
/// </summary>
/// <param name="buffer">The buffer.</param>
public void BinaryWrite(byte[] buffer)
{
if (_context != null)
{
_context.ContentType = ContentType;
_context.BinaryWrite(buffer);
}
}
public void End()
{
if (_context != null)
_context.End();
}
/// <summary>
/// Adds the HTTP headers for cache expiry, and status code to the current response.
/// </summary>
/// <param name="fullPath">The full virtual path of the file to add cache settings for.</param>
/// <param name="modifiedSinceHeader">The incoming modified since header sent by the browser.</param>
public void AddStatusCodeForCache(string fullPath, string modifiedSinceHeader)
{
if (_context != null)
{
// https://developers.google.com/speed/docs/best-practices/caching
_context.AddFileDependency(fullPath);
FileInfo info = new FileInfo(fullPath);
_context.Cache.SetCacheability(HttpCacheability.Public);
_context.Headers.Add("Expires", "-1"); // always followed by the browser
_context.Cache.SetLastModifiedFromFileDependencies(); // sometimes followed by the browser
int statusCode = GetStatusCodeForCache(info.LastWriteTimeUtc, modifiedSinceHeader);
_context.StatusCode = statusCode;
StatusCode = statusCode;
}
}
/// <summary>
/// Adds the HTTP headers for cache expiry, and status code to the current response.
/// </summary>
/// <param name="fullPath">The full virtual path of the file to add cache settings for.</param>
/// <param name="modifiedSinceHeader">The incoming modified since header sent by the browser.</param>
public void AddStatusCodeForCache(string fileName, string modifiedSinceHeader, DateTime lastWriteTimeUtc)
{
if (_context != null)
{
// https://developers.google.com/speed/docs/best-practices/caching
_context.AddFileDependency(fileName);
_context.Cache.SetCacheability(HttpCacheability.Public);
_context.Headers.Add("Expires", "-1"); // always followed by the browser
_context.Cache.SetLastModifiedFromFileDependencies(); // sometimes followed by the browser
int statusCode = GetStatusCodeForCache(lastWriteTimeUtc, modifiedSinceHeader);
_context.StatusCode = statusCode;
StatusCode = statusCode;
}
}
/// <summary>
/// Gets a 304 HTTP response if there is a "If-Modified-Since" header and it matches
/// the fileDate. Otherwise a 200 OK is given.
/// </summary>
/// <param name="fileDate">The date the item was last modified.</param>
/// <param name="modifiedSinceHeader">The modified since header (an ISO date). If this doesn't
/// exist then 200 is returned.</param>
/// <returns>The status code for the cache - 200 or 304.</returns>
public static int GetStatusCodeForCache(DateTime fileDate, string modifiedSinceHeader)
{
int status = 200;
// When If-modified is sent (never when it's incognito mode), it matches the
// the write time you send back for the file. So 1st Jan 2001, it will send back
// 1st Jan 2001 for If-Modified.
DateTime modifiedSinceDate = GetLastModifiedDate(modifiedSinceHeader);
if (modifiedSinceDate != DateTime.MinValue)
{
status = 304;
DateTime lastWriteTime = new DateTime(fileDate.Year, fileDate.Month, fileDate.Day, fileDate.Hour, fileDate.Minute, fileDate.Second, 0, DateTimeKind.Utc);
if (lastWriteTime != modifiedSinceDate)
status = 200;
}
return status;
}
/// <summary>
/// Parses the modified string given, turning the date into a UTC date and removing any milliseconds
/// from the DateTime returned. If the string isn't a valid date, DateTime.Min is returned.
/// </summary>
public static DateTime GetLastModifiedDate(string modifiedSince)
{
DateTime modifiedSinceDate = DateTime.MinValue;
if (!string.IsNullOrWhiteSpace(modifiedSince))
{
if (DateTime.TryParse(modifiedSince, out modifiedSinceDate))
{
modifiedSinceDate = modifiedSinceDate.ToUniversalTime();
modifiedSinceDate = modifiedSinceDate.ClearMilliseconds();
}
}
return modifiedSinceDate;
}
}
}