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