-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceHandler.cs
More file actions
139 lines (120 loc) · 3.17 KB
/
ResourceHandler.cs
File metadata and controls
139 lines (120 loc) · 3.17 KB
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
using System;
using System.Globalization;
using System.Text;
using System.Web;
using System.IO;
using System.IO.Compression;
namespace netzlib
{
public class ResourceHandler : IHttpHandler
{
readonly Settings settings = Settings.Default;
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext ctx)
{
uint hash;
if (!uint.TryParse(ctx.Request["v"],
NumberStyles.HexNumber, CultureInfo.CurrentCulture, out hash))
{
Throw404(ctx);
}
var repository = ctx.Application[NetzlibModule.RESOURCE_REPOSITORY] as IResourceRepository;
if (repository == null)
{
throw new InvalidOperationException("no repository registered");
}
var script = repository.GetContent(hash);
if (script == null)
{
Throw404(ctx);
}
if (ctx.IsDebuggingEnabled)
{
ResponseNoCache(ctx.Response);
}
else
{
ResponseCache(ctx.Response);
}
ctx.Response.ContentType = "text/javascript";
ctx.Response.ContentEncoding = Encoding.UTF8;
if (UseCompression(ctx))
{
WriteCompressedResponse(ctx, script);
}
else
{
ctx.Response.Write(script);
}
}
private static bool UseCompression(HttpContext ctx)
{
if (ctx.IsDebuggingEnabled)
{
return false;
}
string acceptEncoding = ctx.Request.Headers["Accept-Encoding"];
bool result = (!string.IsNullOrEmpty(acceptEncoding)) &&
(acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate"));
if (ctx.Request.Browser.IsBrowser("IE"))
{
return result && ctx.Request.Browser.MajorVersion > 6;
}
return result;
}
private static void WriteCompressedResponse(HttpContext ctx, string content)
{
string acceptEncoding = ctx.Request.Headers["Accept-Encoding"];
using (var ms = new MemoryStream())
{
if (acceptEncoding.Contains("gzip"))
{
using (var gzip = new GZipStream(ms, CompressionMode.Compress))
{
using (var writer = new StreamWriter(gzip, Encoding.UTF8))
{
writer.Write(content);
}
}
}
else if (acceptEncoding.Contains("deflate"))
{
using (var deflate = new DeflateStream(ms, CompressionMode.Compress))
{
using (var writer = new StreamWriter(deflate, Encoding.UTF8))
{
writer.Write(content);
}
}
}
byte[] buffer = ms.ToArray();
ctx.Response.AddHeader("Content-encoding", "gzip");
ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
}
private static void ResponseNoCache(HttpResponse response)
{
var cache = response.Cache;
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoServerCaching();
}
private void ResponseCache(HttpResponse response)
{
var cache = response.Cache;
var now = DateTime.Now;
cache.SetCacheability(HttpCacheability.Public);
cache.SetMaxAge(TimeSpan.FromSeconds(settings.CombinedScriptCacheDuration));
cache.VaryByParams["v"] = true;
cache.SetExpires(now.AddSeconds(settings.CombinedScriptCacheDuration));
cache.SetValidUntilExpires(true);
cache.SetLastModified(now);
}
private static void Throw404(HttpContext ctx)
{
throw new HttpException(404, "could not find " + ctx.Request.Url);
}
}
}