AssemblyResourceLoader.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // System.Web.Handlers.AssemblyResourceLoader
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Web.UI;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Resources;
  33. using System.Collections;
  34. using System.Text.RegularExpressions;
  35. namespace System.Web.Handlers {
  36. #if SYSTEM_WEB_EXTENSIONS
  37. partial class ScriptResourceHandler
  38. {
  39. const string HandlerFileName = "ScriptResource.axd";
  40. #else
  41. #if NET_2_0
  42. public sealed
  43. #else
  44. internal // since this is in the .config file, we need to support it, since we dont have versoned support.
  45. #endif
  46. class AssemblyResourceLoader : IHttpHandler {
  47. const string HandlerFileName = "WebResource.axd";
  48. #endif
  49. #if NET_2_0
  50. const char QueryParamSeparator = ';';
  51. #else
  52. const char QueryParamSeparator = '&';
  53. #endif
  54. internal static string GetResourceUrl (Type type, string resourceName)
  55. {
  56. return GetResourceUrl (type.Assembly, resourceName, false);
  57. }
  58. internal static string GetResourceUrl (Assembly assembly, string resourceName, bool notifyScriptLoaded)
  59. {
  60. string aname = assembly == typeof (AssemblyResourceLoader).Assembly ? "s" : HttpUtility.UrlEncode (assembly.GetName ().FullName);
  61. string apath = assembly.Location;
  62. string atime = String.Empty;
  63. string extra = String.Empty;
  64. #if SYSTEM_WEB_EXTENSIONS
  65. extra = String.Format ("{0}n={1}", QueryParamSeparator, notifyScriptLoaded ? "t" : "f");
  66. #endif
  67. #if TARGET_JVM
  68. atime = String.Format ("{0}t={1}", QueryParamSeparator, assembly.GetHashCode ());
  69. #else
  70. if (apath != String.Empty)
  71. atime = String.Format ("{0}t={1}", QueryParamSeparator, File.GetLastWriteTimeUtc (apath).Ticks);
  72. #endif
  73. string href = String.Format ("{0}?a={2}{1}r={3}{4}{5}", HandlerFileName,
  74. QueryParamSeparator, aname,
  75. HttpUtility.UrlEncode (resourceName), atime, extra);
  76. HttpContext ctx = HttpContext.Current;
  77. if (ctx != null && ctx.Request != null) {
  78. string appPath = VirtualPathUtility.AppendTrailingSlash (ctx.Request.ApplicationPath);
  79. href = appPath + href;
  80. }
  81. return href;
  82. }
  83. #if SYSTEM_WEB_EXTENSIONS
  84. protected virtual void ProcessRequest (HttpContext context)
  85. #else
  86. [MonoTODO ("Substitution not implemented")]
  87. void System.Web.IHttpHandler.ProcessRequest (HttpContext context)
  88. #endif
  89. {
  90. string resourceName = context.Request.QueryString ["r"];
  91. string asmName = context.Request.QueryString ["a"];
  92. Assembly assembly;
  93. if (asmName == null || asmName == "s")
  94. assembly = typeof (AssemblyResourceLoader).Assembly;
  95. else
  96. assembly = Assembly.Load (asmName);
  97. WebResourceAttribute wra = null;
  98. WebResourceAttribute [] attrs = (WebResourceAttribute []) assembly.GetCustomAttributes (typeof (WebResourceAttribute), false);
  99. for (int i = 0; i < attrs.Length; i++) {
  100. if (attrs [i].WebResource == resourceName) {
  101. wra = attrs [i];
  102. break;
  103. }
  104. }
  105. #if SYSTEM_WEB_EXTENSIONS
  106. if (wra == null && resourceName.Length > 9 && resourceName.EndsWith (".debug.js", StringComparison.OrdinalIgnoreCase)) {
  107. resourceName = String.Concat (resourceName.Substring (0, resourceName.Length - 9), ".js");
  108. for (int i = 0; i < attrs.Length; i++) {
  109. if (attrs [i].WebResource == resourceName) {
  110. wra = attrs [i];
  111. break;
  112. }
  113. }
  114. }
  115. #endif
  116. if (wra == null)
  117. return;
  118. context.Response.ContentType = wra.ContentType;
  119. /* tell the client they can cache resources for 1 year */
  120. context.Response.ExpiresAbsolute = DateTime.Now.AddYears (1);
  121. context.Response.CacheControl = "public";
  122. context.Response.Cache.VaryByParams ["r"] = true;
  123. context.Response.Cache.VaryByParams ["t"] = true;
  124. Stream s = assembly.GetManifestResourceStream (resourceName);
  125. if (s == null)
  126. return;
  127. if (wra.PerformSubstitution) {
  128. StreamReader r = new StreamReader (s);
  129. TextWriter w = context.Response.Output;
  130. new PerformSubstitutionHelper (assembly).PerformSubstitution (r, w);
  131. }
  132. else {
  133. byte [] buf = new byte [1024];
  134. Stream output = context.Response.OutputStream;
  135. int c;
  136. do {
  137. c = s.Read (buf, 0, 1024);
  138. output.Write (buf, 0, c);
  139. } while (c > 0);
  140. }
  141. #if SYSTEM_WEB_EXTENSIONS
  142. TextWriter writer = context.Response.Output;
  143. foreach (ScriptResourceAttribute sra in assembly.GetCustomAttributes (typeof (ScriptResourceAttribute), false)) {
  144. if (sra.ScriptName == resourceName) {
  145. string scriptResourceName = sra.ScriptResourceName;
  146. ResourceSet rset = null;
  147. try {
  148. rset = new ResourceManager (scriptResourceName, assembly).GetResourceSet (Threading.Thread.CurrentThread.CurrentUICulture, true, true);
  149. }
  150. catch (MissingManifestResourceException) {
  151. #if TARGET_JVM // GetResourceSet does not throw MissingManifestResourceException if ressource is not exists
  152. }
  153. if (rset == null) {
  154. #endif
  155. if (scriptResourceName.EndsWith (".resources")) {
  156. scriptResourceName = scriptResourceName.Substring (0, scriptResourceName.Length - 10);
  157. rset = new ResourceManager (scriptResourceName, assembly).GetResourceSet (Threading.Thread.CurrentThread.CurrentUICulture, true, true);
  158. }
  159. #if !TARGET_JVM
  160. else
  161. throw;
  162. #endif
  163. }
  164. if (rset == null)
  165. break;
  166. writer.WriteLine ();
  167. writer.WriteLine ("{0}={{", sra.TypeName);
  168. foreach (DictionaryEntry entry in rset) {
  169. string value = entry.Value as string;
  170. if (value != null)
  171. writer.WriteLine ("{0}:{1},", GetScriptStringLiteral ((string) entry.Key), GetScriptStringLiteral (value));
  172. }
  173. writer.WriteLine ("};");
  174. break;
  175. }
  176. }
  177. bool notifyScriptLoaded = context.Request.QueryString ["n"] == "t";
  178. if (notifyScriptLoaded) {
  179. writer.WriteLine ();
  180. writer.WriteLine ("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();");
  181. }
  182. #endif
  183. }
  184. sealed class PerformSubstitutionHelper
  185. {
  186. readonly Assembly _assembly;
  187. static readonly Regex _regex = new Regex (@"\<%=[ ]*WebResource[ ]*\([ ]*""([^""]+)""[ ]*\)[ ]*%\>");
  188. public PerformSubstitutionHelper (Assembly assembly) {
  189. _assembly = assembly;
  190. }
  191. public void PerformSubstitution (TextReader reader, TextWriter writer) {
  192. string line = reader.ReadLine ();
  193. while (line != null) {
  194. if (line.Length > 0 && _regex.IsMatch (line))
  195. line = _regex.Replace (line, new MatchEvaluator (PerformSubstitutionReplace));
  196. writer.WriteLine (line);
  197. line = reader.ReadLine ();
  198. }
  199. }
  200. string PerformSubstitutionReplace (Match m) {
  201. string resourceName = m.Groups [1].Value;
  202. #if SYSTEM_WEB_EXTENSIONS
  203. return ScriptResourceHandler.GetResourceUrl (_assembly, resourceName, false);
  204. #else
  205. return AssemblyResourceLoader.GetResourceUrl (_assembly, resourceName, false);
  206. #endif
  207. }
  208. }
  209. #if !SYSTEM_WEB_EXTENSIONS
  210. bool System.Web.IHttpHandler.IsReusable { get { return true; } }
  211. #endif
  212. }
  213. }