2005-12-06 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 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Web.UI;
32 using System.Reflection;
33 using System.IO;
34
35 namespace System.Web.Handlers {
36         [MonoTODO ("Should we cache stuff?")]
37         #if NET_2_0
38         public sealed
39         #else
40         internal // since this is in the .config file, we need to support it, since we dont have versoned support.
41         #endif
42                 class AssemblyResourceLoader : IHttpHandler {
43                 
44                 internal static string GetResourceUrl (Type type, string resourceName)
45                 {
46                         string aname = type.Assembly == typeof(AssemblyResourceLoader).Assembly ? "s" : HttpUtility.UrlEncode (type.Assembly.GetName ().FullName);
47                         return "WebResource.axd?a=" 
48                                 + aname 
49                                 + "&r=" 
50                                 + HttpUtility.UrlEncode (resourceName);
51                 }
52
53         
54                 [MonoTODO ("Substitution not implemented")]
55                 void System.Web.IHttpHandler.ProcessRequest (HttpContext context)
56                 {
57                         string resourceName = context.Request.QueryString ["r"];
58                         string asmName = context.Request.QueryString ["a"];
59                         Assembly assembly;
60                         
61                         if (asmName == null || asmName == "s") assembly = GetType().Assembly;
62                         else assembly = Assembly.Load (asmName);
63                         
64                         bool found = false;
65                         foreach (WebResourceAttribute wra in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) {
66                                 if (wra.WebResource == resourceName) {
67                                         context.Response.ContentType = wra.ContentType;
68                                         
69                                         if (wra.PerformSubstitution)
70                                                 throw new NotImplementedException ("Substitution not implemented");
71                                         
72                                         found = true;
73                                         break;
74                                 }
75                         }
76                         if (!found)
77                                 return;
78                         
79                         Stream s = assembly.GetManifestResourceStream (resourceName);
80                         
81                         byte [] buf = new byte [1024];
82                         Stream output = context.Response.OutputStream;
83                         int c;
84                         do {
85                                 c = s.Read (buf, 0, 1024);
86                                 output.Write (buf, 0, c);
87                         } while (c > 0);
88                 }
89                 
90                 bool System.Web.IHttpHandler.IsReusable { get { return true; } }
91         }
92 }
93