AssemblyResourceLoader.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // System.Web.Handlers.AssemblyResourceLoader
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. using System.Web;
  10. using System.Web.UI;
  11. using System.Reflection;
  12. using System.IO;
  13. namespace System.Web.Handlers {
  14. [MonoTODO ("Should we cache stuff?")]
  15. #if NET_1_2
  16. public
  17. #else
  18. internal // since this is in the .config file, we need to support it, since we dont have versoned support.
  19. #endif
  20. class AssemblyResourceLoader : IHttpHandler {
  21. internal static string GetResourceUrl (Type type, string resourceName)
  22. {
  23. return "WebResource.axd?assembly="
  24. + HttpUtility.UrlEncode (type.Assembly.GetName ().FullName)
  25. + "&resource="
  26. + HttpUtility.UrlEncode (resourceName);
  27. }
  28. [MonoTODO ("Substitution not implemented")]
  29. void System.Web.IHttpHandler.ProcessRequest (HttpContext context)
  30. {
  31. #if NET_1_2
  32. string resourceName = context.Request.QueryString ["resource"];
  33. Assembly assembly = Assembly.Load (context.Request.QueryString ["assembly"]);
  34. bool found = false;
  35. foreach (WebResourceAttribute wra in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) {
  36. if (wra.WebResource == resourceName) {
  37. context.Response.ContentType = wra.ContentType;
  38. if (wra.PerformSubstitution)
  39. throw new NotImplementedException ("Substitution not implemented");
  40. found = true;
  41. break;
  42. }
  43. }
  44. if (!found)
  45. return;
  46. Stream s = assembly.GetManifestResourceStream (resourceName);
  47. byte [] buf = new byte [1024];
  48. Stream output = context.Response.OutputStream;
  49. int c;
  50. do {
  51. c = s.Read (buf, 0, 1024);
  52. output.Write (buf, 0, c);
  53. } while (c > 0);
  54. #endif
  55. }
  56. bool System.Web.IHttpHandler.IsReusable { get { return true; } }
  57. }
  58. }