2004-11-18 Lluis Sanchez Gual <lluis@novell.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;
32 using System.Web.UI;
33 using System.Reflection;
34 using System.IO;
35
36 namespace System.Web.Handlers {
37         [MonoTODO ("Should we cache stuff?")]
38         #if NET_2_0
39         public
40         #else
41         internal // since this is in the .config file, we need to support it, since we dont have versoned support.
42         #endif
43                 class AssemblyResourceLoader : IHttpHandler {
44                 
45                 internal static string GetResourceUrl (Type type, string resourceName)
46                 {
47                         string aname = type.Assembly == typeof(AssemblyResourceLoader).Assembly ? "s" : HttpUtility.UrlEncode (type.Assembly.GetName ().FullName);
48                         return "WebResource.axd?a=" 
49                                 + aname 
50                                 + "&r=" 
51                                 + HttpUtility.UrlEncode (resourceName);
52                 }
53
54         
55                 [MonoTODO ("Substitution not implemented")]
56                 void System.Web.IHttpHandler.ProcessRequest (HttpContext context)
57                 {
58 #if NET_2_0
59                         string resourceName = context.Request.QueryString ["r"];
60                         string asmName = context.Request.QueryString ["a"];
61                         Assembly assembly;
62                         
63                         if (asmName == null || asmName == "s") assembly = GetType().Assembly;
64                         else assembly = Assembly.Load (asmName);
65                         
66                         bool found = false;
67                         foreach (WebResourceAttribute wra in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) {
68                                 if (wra.WebResource == resourceName) {
69                                         context.Response.ContentType = wra.ContentType;
70                                         
71                                         if (wra.PerformSubstitution)
72                                                 throw new NotImplementedException ("Substitution not implemented");
73                                         
74                                         found = true;
75                                         break;
76                                 }
77                         }
78                         if (!found)
79                                 return;
80                         
81                         Stream s = assembly.GetManifestResourceStream (resourceName);
82                         
83                         byte [] buf = new byte [1024];
84                         Stream output = context.Response.OutputStream;
85                         int c;
86                         do {
87                                 c = s.Read (buf, 0, 1024);
88                                 output.Write (buf, 0, c);
89                         } while (c > 0);
90 #endif
91                 }
92                 
93                 bool System.Web.IHttpHandler.IsReusable { get { return true; } }
94         }
95 }
96