2 // ScriptResourceHandler.cs
\r
5 // Igor Zelmanovich <igorz@mainsoft.com>
\r
6 // Marek Habersack <grendel@twistedcode.net>
\r
8 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
\r
9 // (C) 2011 Novell, Inc. http://novell.com
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
\r
13 // a copy of this software and associated documentation files (the
\r
14 // "Software"), to deal in the Software without restriction, including
\r
15 // without limitation the rights to use, copy, modify, merge, publish,
\r
16 // distribute, sublicense, and/or sell copies of the Software, and to
\r
17 // permit persons to whom the Software is furnished to do so, subject to
\r
18 // the following conditions:
\r
20 // The above copyright notice and this permission notice shall be
\r
21 // included in all copies or substantial portions of the Software.
\r
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
33 using System.Collections;
\r
34 using System.Collections.Generic;
\r
36 using System.Security.Cryptography;
\r
37 using System.Reflection;
\r
38 using System.Resources;
\r
40 using System.Threading;
\r
41 using System.Web.Configuration;
\r
42 using System.Web.Hosting;
\r
43 using System.Web.UI;
\r
44 using System.Web.Util;
\r
46 namespace System.Web.Handlers
\r
48 public partial class ScriptResourceHandler : IHttpHandler
\r
50 protected virtual bool IsReusable {
\r
51 get { return true; }
\r
54 #region IHttpHandler Members
\r
56 bool IHttpHandler.IsReusable {
\r
57 get { return IsReusable; }
\r
60 void IHttpHandler.ProcessRequest (HttpContext context) {
\r
61 ProcessRequest (context);
\r
65 void AppendResourceScriptContents (StringWriter sw, CompositeEntry entry)
\r
67 if (entry.Assembly == null || entry.Attribute == null || String.IsNullOrEmpty (entry.NameOrPath))
\r
70 using (Stream s = entry.Assembly.GetManifestResourceStream (entry.NameOrPath)) {
\r
72 throw new HttpException (404, "Resource '" + entry.NameOrPath + "' not found");
\r
74 if (entry.Attribute.PerformSubstitution) {
\r
75 using (var r = new StreamReader (s)) {
\r
76 new PerformSubstitutionHelper (entry.Assembly).PerformSubstitution (r, sw);
\r
79 using (var r = new StreamReader (s)) {
\r
80 string line = r.ReadLine ();
\r
81 while (line != null) {
\r
82 sw.WriteLine (line);
\r
83 line = r.ReadLine ();
\r
90 void AppendFileScriptContents (StringWriter sw, CompositeEntry entry)
\r
92 // FIXME: should we limit the script size in any way?
\r
93 if (String.IsNullOrEmpty (entry.NameOrPath))
\r
97 if (!HostingEnvironment.HaveCustomVPP) {
\r
98 // We'll take a shortcut here by bypassing the default VPP layers
\r
99 mappedPath = HostingEnvironment.MapPath (entry.NameOrPath);
\r
100 if (!File.Exists (mappedPath))
\r
102 sw.Write (File.ReadAllText (mappedPath));
\r
106 VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
\r
107 if (!vpp.FileExists (entry.NameOrPath))
\r
109 VirtualFile file = vpp.GetFile (entry.NameOrPath);
\r
112 using (Stream s = file.Open ()) {
\r
113 using (var r = new StreamReader (s)) {
\r
114 string line = r.ReadLine ();
\r
115 while (line != null) {
\r
116 sw.WriteLine (line);
\r
117 line = r.ReadLine ();
\r
123 void AppendScriptContents (StringWriter sw, CompositeEntry entry)
\r
125 if (entry.Assembly != null)
\r
126 AppendResourceScriptContents (sw, entry);
\r
128 AppendFileScriptContents (sw, entry);
\r
131 void SendCompositeScript (HttpContext context, HttpRequest request, bool notifyScriptLoaded, List <CompositeEntry> entries)
\r
133 if (entries.Count == 0)
\r
134 throw new HttpException (404, "Resource not found");
\r
136 DateTime modifiedSince;
\r
137 bool hasIfModifiedSince = HasIfModifiedSince (context.Request, out modifiedSince);
\r
139 if (hasIfModifiedSince) {
\r
140 bool notModified = true;
\r
142 foreach (CompositeEntry entry in entries) {
\r
146 if (hasIfModifiedSince && entry.IsModifiedSince (modifiedSince))
\r
147 notModified = false;
\r
152 RespondWithNotModified (context);
\r
157 StringBuilder contents = new StringBuilder ();
\r
158 using (var sw = new StringWriter (contents)) {
\r
159 foreach (CompositeEntry entry in entries) {
\r
162 AppendScriptContents (sw, entry);
\r
165 if (contents.Length == 0)
\r
166 throw new HttpException (404, "Resource not found");
\r
168 HttpResponse response = context.Response;
\r
169 DateTime utcnow = DateTime.UtcNow;
\r
171 response.ContentType = "text/javascript";
\r
172 response.Headers.Add ("Last-Modified", utcnow.ToString ("r"));
\r
173 response.ExpiresAbsolute = utcnow.AddYears (1);
\r
174 response.CacheControl = "public";
\r
176 response.Output.Write (contents.ToString ());
\r
177 if (notifyScriptLoaded)
\r
178 OutputScriptLoadedNotification (response.Output);
\r
180 void OutputScriptLoadedNotification (TextWriter writer)
\r
182 writer.WriteLine ();
\r
183 writer.WriteLine ("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();");
\r
186 protected virtual void ProcessRequest (HttpContext context)
\r
188 HttpRequest request = context.Request;
\r
189 bool notifyScriptLoaded = request.QueryString ["n"] == "t";
\r
190 List <CompositeEntry> compositeEntries = CompositeScriptReference.GetCompositeScriptEntries (request.RawUrl);
\r
191 if (compositeEntries != null) {
\r
192 SendCompositeScript (context, request, notifyScriptLoaded, compositeEntries);
\r
195 EmbeddedResource res;
\r
196 Assembly assembly;
\r
197 SendEmbeddedResource (context, out res, out assembly);
\r
199 HttpResponse response = context.Response;
\r
200 TextWriter writer = response.Output;
\r
201 foreach (ScriptResourceAttribute sra in assembly.GetCustomAttributes (typeof (ScriptResourceAttribute), false)) {
\r
202 if (String.Compare (sra.ScriptName, res.Name, StringComparison.Ordinal) == 0) {
\r
203 string scriptResourceName = sra.ScriptResourceName;
\r
204 ResourceSet rset = null;
\r
206 rset = new ResourceManager (scriptResourceName, assembly).GetResourceSet (Threading.Thread.CurrentThread.CurrentUICulture, true, true);
\r
208 catch (MissingManifestResourceException) {
\r
209 if (scriptResourceName.EndsWith (".resources", RuntimeHelpers.StringComparison)) {
\r
210 scriptResourceName = scriptResourceName.Substring (0, scriptResourceName.Length - 10);
\r
211 rset = new ResourceManager (scriptResourceName, assembly).GetResourceSet (Threading.Thread.CurrentThread.CurrentUICulture, true, true);
\r
218 writer.WriteLine ();
\r
219 string ns = sra.TypeName;
\r
220 int indx = ns.LastIndexOf ('.');
\r
222 writer.WriteLine ("Type.registerNamespace('" + ns.Substring (0, indx) + "')");
\r
223 writer.Write ("{0}={{", sra.TypeName);
\r
225 foreach (DictionaryEntry de in rset) {
\r
226 string value = de.Value as string;
\r
227 if (value != null) {
\r
231 writer.Write (',');
\r
232 writer.WriteLine ();
\r
233 writer.Write ("{0}:{1}", GetScriptStringLiteral ((string) de.Key), GetScriptStringLiteral (value));
\r
236 writer.WriteLine ();
\r
237 writer.WriteLine ("};");
\r
242 if (notifyScriptLoaded)
\r
243 OutputScriptLoadedNotification (writer);
\r
245 static void CheckIfResourceIsCompositeScript (string resourceName, ref bool includeTimeStamp)
\r
247 bool isCompositeScript = resourceName.StartsWith (CompositeScriptReference.COMPOSITE_SCRIPT_REFERENCE_PREFIX, StringComparison.Ordinal);
\r
248 if (!isCompositeScript)
\r
251 includeTimeStamp = false;
\r
254 bool HandleCompositeScriptRequest (HttpContext context, HttpRequest request, string d)
\r
258 // TODO: add value cache?
\r
259 static string GetScriptStringLiteral (string value)
\r
261 if (String.IsNullOrEmpty (value))
\r
262 return "\"" + value + "\"";
\r
264 var sb = new StringBuilder ("\"");
\r
265 for (int i = 0; i < value.Length; i++) {
\r
266 char ch = value [i];
\r
269 sb.Append ("\\u0027");
\r
273 sb.Append ("\\\"");
\r
277 sb.Append ("\\\\");
\r
295 return sb.ToString ();
\r