[corlib] Remove unused files
[mono.git] / mcs / class / System.Web / System.Web.Handlers / TraceHandler.cs
1 //
2 // System.Web.Handlers.TraceHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Jackson Harper (jackson@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections;
34 using System.Data;
35 using System.Security.Permissions;
36 using System.Web.Util;
37 using System.Web.UI;
38 using System.Web.UI.WebControls;
39
40 namespace System.Web.Handlers
41 {
42         [Serializable]
43         class TraceNotAvailableException : HttpException
44         {
45                 bool notLocal;
46
47                 public TraceNotAvailableException (bool notLocal) :
48                         base (notLocal ? 403 : 500, "Trace Error")
49                 {
50                         this.notLocal = notLocal;
51                 }
52
53                 internal override string Description {
54                         get {
55                                 if (notLocal)
56                                         return "Trace is not enabled for remote clients.";
57
58                                 return "Trace.axd is not enabled in the configuration file for this application.";
59                         }
60                 }
61         }
62
63         // CAS
64         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
65         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
66         public class TraceHandler : IHttpHandler
67         {
68                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
69                 public TraceHandler ()
70                 {
71                         // LAMESPEC: the ctor is documented to have a Demand for a SecurityPermission
72                         // but doesn't specify which one it is (tests shows it's UnmanagedCode)
73                 }
74
75                 void IHttpHandler.ProcessRequest (HttpContext context)
76                 {
77                         ProcessRequest (context);
78                 }
79
80                 protected void ProcessRequest (HttpContext context)
81                 {
82                         TraceManager manager = HttpRuntime.TraceManager;
83
84                         if (!manager.Enabled || manager.LocalOnly && !context.Request.IsLocal)
85                                 throw new TraceNotAvailableException (manager.Enabled);
86                                 
87                         HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
88
89                         if (context.Request.QueryString ["clear"] != null) {
90                                 manager.Clear ();
91                                 context.Response.Redirect (context.Request.FilePath);
92                         }
93                         
94                         string id_str = context.Request.QueryString ["id"];
95                         int id = -1;
96                         if (id_str != null)
97                                 id = Int32.Parse (id_str);
98                         
99                         if (id > 0 && id <= manager.ItemCount) {
100                                 RenderItem (manager, output, id);
101                         } else {
102                                 string dir = context.Server.MapPath (UrlUtils.GetDirectory (context.Request.FilePath));
103                                 RenderMenu (manager, output, dir);
104                         }
105                                 
106                 }
107
108                 bool IHttpHandler.IsReusable {
109                         get { return IsReusable; }
110                 }
111
112                 protected bool IsReusable {
113                         get {
114                                 return false;
115                         }
116                 }
117
118                 void RenderMenu (TraceManager manager, HtmlTextWriter output, string dir)
119                 {
120                         
121                         output.RenderBeginTag (HtmlTextWriterTag.Html);
122                         
123                         output.RenderBeginTag (HtmlTextWriterTag.Head);
124                         TraceData.RenderStyleSheet (output);
125                         output.RenderEndTag ();
126
127                         RenderHeader (output, dir);
128                         
129                         output.RenderBeginTag (HtmlTextWriterTag.Body);
130                         output.AddAttribute ("class", "tracecontent");
131                         output.RenderBeginTag (HtmlTextWriterTag.Span);
132
133                         Table table = TraceData.CreateTable ();
134                         
135                         table.Rows.Add (TraceData.AltRow ("Requests to the Application"));
136                         table.Rows.Add (TraceData.SubHeadRow ("No", "Time of Request",
137                                                         "File", "Status Code", "Verb", "&nbsp;"));
138
139                         if (manager.TraceData != null) {
140                                 for (int i=0; i<manager.ItemCount; i++) {
141                                         int item = i + 1;
142                                         TraceData d = manager.TraceData [i];
143                                         TraceData.RenderAltRow (table, i, item.ToString (), d.RequestTime.ToString (),
144                                                         d.RequestPath, d.StatusCode.ToString (), d.RequestType,
145                                                         "<a href=\"Trace.axd?id=" + item + "\" class=\"tinylink\">" +
146                                                         "<b><nobr>View Details</a>");
147                                 }
148                                 table.RenderControl (output);
149                         }
150                         
151                         output.RenderEndTag ();
152                         output.RenderEndTag ();
153                         
154                         output.RenderEndTag ();
155                 }
156
157                 void RenderHeader (HtmlTextWriter output, string dir)
158                 {
159                         Table table = TraceData.CreateTable ();
160                         TableRow row1 = new TableRow ();
161                         TableRow row2 = new TableRow ();
162                         TableCell cell1 = new TableCell ();
163                         TableCell cell2 = new TableCell ();
164                         TableCell cell3 = new TableCell ();
165                         TableCell cell4 = new TableCell ();
166                         
167                         cell1.Text = "<h1>Application Trace</h1>";
168                         cell2.Text = "[ <a href=\"Trace.axd?clear=1\" class=\"link\">clear current trace</a> ]";
169                         
170                         cell2.HorizontalAlign = HorizontalAlign.Right;
171                         cell2.VerticalAlign = VerticalAlign.Bottom;
172                         
173                         row1.Cells.Add (cell1);
174                         row1.Cells.Add (cell2);
175
176                         cell3.Text = "<h2><h2><p>"; // ummm, WTF?
177                         cell4.Text = "<b>Physical Directory:</b>" + dir;
178
179                         row2.Cells.Add (cell3);
180                         row2.Cells.Add (cell4);
181
182                         table.Rows.Add (row1);
183                         table.Rows.Add (row2);
184
185                         table.RenderControl (output);
186                 }
187                 
188                 void RenderItem (TraceManager manager, HtmlTextWriter output, int item)
189                 {
190                         manager.TraceData [item - 1].Render (output);
191                 }
192
193                 [MonoLimitation ("Not implemented, does nothing")]
194                 protected void ShowDetails (DataSet data)
195                 {
196                 }
197                 [MonoLimitation ("Not implemented, does nothing")]
198                 protected void ShowRequests (IList data)
199                 {
200                 }
201
202                 [MonoLimitation ("Not implemented, does nothing")]
203                 protected void ShowVersionDetails ()
204                 {
205                 }
206         }
207 }