New test.
[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 #if NET_2_0
43         [Serializable]
44 #endif
45         class TraceNotAvailableException : HttpException
46         {
47                 bool notLocal;
48
49                 public TraceNotAvailableException (bool notLocal) :
50                         base (notLocal ? 403 : 500, "Trace Error")
51                 {
52                         this.notLocal = notLocal;
53                 }
54
55                 internal override string Description {
56                         get {
57                                 if (notLocal)
58                                         return "Trace is not enabled for remote clients.";
59
60                                 return "Trace.axd is not enabled in the configuration file for this application.";
61                         }
62                 }
63         }
64
65         // CAS
66         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
67         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
68         public class TraceHandler : IHttpHandler
69         {
70                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
71                 public TraceHandler ()
72                 {
73                         // LAMESPEC: the ctor is documented to have a Demand for a SecurityPermission
74                         // but doesn't specify which one it is (tests shows it's UnmanagedCode)
75                 }
76
77 #if NET_2_0
78                 void IHttpHandler.ProcessRequest (HttpContext context)
79                 {
80                         ProcessRequest (context);
81                 }
82
83                 protected void ProcessRequest (HttpContext context)
84 #else
85                 void IHttpHandler.ProcessRequest (HttpContext context)
86 #endif
87                 {
88                         TraceManager manager = HttpRuntime.TraceManager;
89
90                         if (!manager.Enabled || manager.LocalOnly && !context.Request.IsLocal)
91                                 throw new TraceNotAvailableException (manager.Enabled);
92                                 
93                         HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
94
95                         if (context.Request.QueryString ["clear"] != null) {
96                                 manager.Clear ();
97                                 context.Response.Redirect (context.Request.FilePath);
98                         }
99                         
100                         string id_str = context.Request.QueryString ["id"];
101                         int id = -1;
102                         if (id_str != null)
103                                 id = Int32.Parse (id_str);
104                         
105                         if (id > 0 && id <= manager.ItemCount) {
106                                 RenderItem (manager, output, id);
107                         } else {
108                                 string dir = context.Server.MapPath (UrlUtils.GetDirectory (context.Request.FilePath));
109                                 RenderMenu (manager, output, dir);
110                         }
111                                 
112                 }
113
114 #if NET_2_0
115                 bool IHttpHandler.IsReusable {
116                         get { return IsReusable; }
117                 }
118
119                 protected bool IsReusable {
120 #else
121                 bool IHttpHandler.IsReusable {
122 #endif
123                         get {
124                                 return false;
125                         }
126                 }
127
128                 private void RenderMenu (TraceManager manager, HtmlTextWriter output, string dir)
129                 {
130                         
131                         output.RenderBeginTag (HtmlTextWriterTag.Html);
132                         
133                         output.RenderBeginTag (HtmlTextWriterTag.Head);
134                         TraceData.RenderStyleSheet (output);
135                         output.RenderEndTag ();
136
137                         RenderHeader (output, dir);
138                         
139                         output.RenderBeginTag (HtmlTextWriterTag.Body);
140                         output.AddAttribute ("class", "tracecontent");
141                         output.RenderBeginTag (HtmlTextWriterTag.Span);
142
143                         Table table = TraceData.CreateTable ();
144                         
145                         table.Rows.Add (TraceData.AltRow ("Requests to the Application"));
146                         table.Rows.Add (TraceData.SubHeadRow ("No", "Time of Request",
147                                                         "File", "Status Code", "Verb", "&nbsp;"));
148
149                         if (manager.TraceData != null) {
150                                 for (int i=0; i<manager.ItemCount; i++) {
151                                         int item = i + 1;
152                                         TraceData d = manager.TraceData [i];
153                                         TraceData.RenderAltRow (table, i, item.ToString (), d.RequestTime.ToString (),
154                                                         d.RequestPath, d.StatusCode.ToString (), d.RequestType,
155                                                         "<a href=\"Trace.axd?id=" + item + "\" class=\"tinylink\">" +
156                                                         "<b><nobr>View Details</a>");
157                                 }
158                                 table.RenderControl (output);
159                         }
160                         
161                         output.RenderEndTag ();
162                         output.RenderEndTag ();
163                         
164                         output.RenderEndTag ();
165                 }
166
167                 private void RenderHeader (HtmlTextWriter output, string dir)
168                 {
169                         Table table = TraceData.CreateTable ();
170                         TableRow row1 = new TableRow ();
171                         TableRow row2 = new TableRow ();
172                         TableCell cell1 = new TableCell ();
173                         TableCell cell2 = new TableCell ();
174                         TableCell cell3 = new TableCell ();
175                         TableCell cell4 = new TableCell ();
176                         
177                         cell1.Text = "<h1>Application Trace</h1>";
178                         cell2.Text = "[ <a href=\"Trace.axd?clear=1\" class=\"link\">clear current trace</a> ]";
179                         
180                         cell2.HorizontalAlign = HorizontalAlign.Right;
181                         cell2.VerticalAlign = VerticalAlign.Bottom;
182                         
183                         row1.Cells.Add (cell1);
184                         row1.Cells.Add (cell2);
185
186                         cell3.Text = "<h2><h2><p>"; // ummm, WTF?
187                         cell4.Text = "<b>Physical Directory:</b>" + dir;
188
189                         row2.Cells.Add (cell3);
190                         row2.Cells.Add (cell4);
191
192                         table.Rows.Add (row1);
193                         table.Rows.Add (row2);
194
195                         table.RenderControl (output);
196                 }
197                 
198                 private void RenderItem (TraceManager manager, HtmlTextWriter output, int item)
199                 {
200                         manager.TraceData [item - 1].Render (output);
201                 }
202
203                 [MonoTODO ("Appears in class status, but...")]
204                 protected void ShowDetails (DataSet data)
205                 {
206                 }
207 #if NET_2_0
208                 [MonoTODO ("Appears in class status, but...")]
209                 protected void ShowRequests (IList data)
210                 {
211                 }
212
213                 [MonoTODO]
214                 protected void ShowVersionDetails ()
215                 {
216                 }
217 #else
218                 [MonoTODO ("Appears in class status, but...")]
219                 protected void ShowRequests (ArrayList list)
220                 {
221                 }
222 #endif
223         }
224 }