AssemblyResourceLoader.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. namespace System.Web.Handlers {
  35. #if SYSTEM_WEB_EXTENSIONS
  36. partial class ScriptResourceHandler
  37. {
  38. const string HandlerFileName = "ScriptResource.axd";
  39. #else
  40. #if NET_2_0
  41. public sealed
  42. #else
  43. internal // since this is in the .config file, we need to support it, since we dont have versoned support.
  44. #endif
  45. class AssemblyResourceLoader : IHttpHandler {
  46. const string HandlerFileName = "WebResource.axd";
  47. #endif
  48. #if NET_2_0
  49. const char QueryParamSeparator = ';';
  50. #else
  51. const char QueryParamSeparator = '&';
  52. #endif
  53. internal static string GetResourceUrl (Type type, string resourceName)
  54. {
  55. return GetResourceUrl (type.Assembly, resourceName, false);
  56. }
  57. internal static string GetResourceUrl (Assembly assembly, string resourceName, bool notifyScriptLoaded)
  58. {
  59. string aname = assembly == typeof (AssemblyResourceLoader).Assembly ? "s" : HttpUtility.UrlEncode (assembly.GetName ().FullName);
  60. string apath = assembly.Location;
  61. string atime = String.Empty;
  62. string extra = String.Empty;
  63. #if SYSTEM_WEB_EXTENSIONS
  64. extra = String.Format ("{0}n={1}", QueryParamSeparator, notifyScriptLoaded ? "t" : "f");
  65. #endif
  66. #if TARGET_JVM
  67. atime = String.Format ("{0}t={1}", QueryParamSeparator, assembly.GetHashCode ());
  68. #else
  69. if (apath != String.Empty)
  70. atime = String.Format ("{0}t={1}", QueryParamSeparator, File.GetLastWriteTimeUtc (apath).Ticks);
  71. #endif
  72. string href = String.Format ("{0}?a={2}{1}r={3}{4}{5}", HandlerFileName,
  73. QueryParamSeparator, aname,
  74. HttpUtility.UrlEncode (resourceName), atime, extra);
  75. HttpContext ctx = HttpContext.Current;
  76. if (ctx != null && ctx.Request != null) {
  77. string appPath = VirtualPathUtility.AppendTrailingSlash (ctx.Request.ApplicationPath);
  78. href = appPath + href;
  79. }
  80. return href;
  81. }
  82. #if SYSTEM_WEB_EXTENSIONS
  83. protected virtual void ProcessRequest (HttpContext context)
  84. #else
  85. [MonoTODO ("Substitution not implemented")]
  86. void System.Web.IHttpHandler.ProcessRequest (HttpContext context)
  87. #endif
  88. {
  89. string resourceName = context.Request.QueryString ["r"];
  90. string asmName = context.Request.QueryString ["a"];
  91. Assembly assembly;
  92. if (asmName == null || asmName == "s")
  93. assembly = typeof (AssemblyResourceLoader).Assembly;
  94. else
  95. assembly = Assembly.Load (asmName);
  96. bool found = false;
  97. foreach (WebResourceAttribute wra in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) {
  98. if (wra.WebResource == resourceName) {
  99. context.Response.ContentType = wra.ContentType;
  100. /* tell the client they can cache resources for 1 year */
  101. context.Response.ExpiresAbsolute = DateTime.Now.AddYears(1);
  102. context.Response.CacheControl = "public";
  103. context.Response.Cache.VaryByParams ["r"] = true;
  104. context.Response.Cache.VaryByParams ["t"] = true;
  105. if (wra.PerformSubstitution)
  106. throw new NotImplementedException ("Substitution not implemented");
  107. found = true;
  108. break;
  109. }
  110. }
  111. if (!found)
  112. return;
  113. Stream s = assembly.GetManifestResourceStream (resourceName);
  114. if (s == null)
  115. return;
  116. byte [] buf = new byte [1024];
  117. Stream output = context.Response.OutputStream;
  118. int c;
  119. do {
  120. c = s.Read (buf, 0, 1024);
  121. output.Write (buf, 0, c);
  122. } while (c > 0);
  123. #if SYSTEM_WEB_EXTENSIONS
  124. TextWriter writer = context.Response.Output;
  125. foreach (ScriptResourceAttribute sra in assembly.GetCustomAttributes (typeof (ScriptResourceAttribute), false)) {
  126. if (sra.ScriptName == resourceName) {
  127. writer.WriteLine ();
  128. writer.WriteLine ("{0}={{", sra.TypeName);
  129. ResourceManager res=new ResourceManager(sra.ScriptResourceName, assembly);
  130. foreach (DictionaryEntry entry in res.GetResourceSet (Threading.Thread.CurrentThread.CurrentUICulture, true, true)) {
  131. string value = entry.Value as string;
  132. if (value != null)
  133. writer.WriteLine ("{0}:{1},", GetScriptStringLiteral ((string) entry.Key), GetScriptStringLiteral (value));
  134. }
  135. writer.WriteLine ("};");
  136. break;
  137. }
  138. }
  139. bool notifyScriptLoaded = context.Request.QueryString ["n"] == "t";
  140. if (notifyScriptLoaded) {
  141. writer.WriteLine ();
  142. writer.WriteLine ("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();");
  143. }
  144. #endif
  145. }
  146. #if !SYSTEM_WEB_EXTENSIONS
  147. bool System.Web.IHttpHandler.IsReusable { get { return true; } }
  148. #endif
  149. }
  150. }