2004-06-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 using System.Collections;
13 using System.Data;
14 using System.Web;
15 using System.Web.Util;
16 using System.Web.UI;
17 using System.Web.UI.WebControls;
18
19 namespace System.Web.Handlers
20 {
21         public class TraceHandler : IHttpHandler
22         {
23                 void IHttpHandler.ProcessRequest (HttpContext context)
24                 {
25                         TraceManager manager = HttpRuntime.TraceManager;
26
27                         if (manager.LocalOnly && !context.Request.IsLocal) {
28                                 // Need to figure out the error message that goes here
29                                 // but I only have cassini for testing
30                                 return;
31                         }
32                                 
33                         HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
34
35                         if (context.Request.QueryString ["clear"] != null)
36                                 manager.Clear ();
37                         
38                         string id_str = context.Request.QueryString ["id"];
39                         int id = -1;
40                         if (id_str != null)
41                                 id = Int32.Parse (id_str);
42                         
43                         if (id > 0 && id <= manager.ItemCount) {
44                                 RenderItem (manager, output, id);
45                         } else {
46                                 string dir = context.Server.MapPath (UrlUtils.GetDirectory (context.Request.FilePath));
47                                 RenderMenu (manager, output, dir);
48                         }
49                                 
50                 }
51
52                 bool IHttpHandler.IsReusable
53                 {
54                         get {
55                                 return false;
56                         }
57                 }
58
59                 private void RenderMenu (TraceManager manager, HtmlTextWriter output, string dir)
60                 {
61                         
62                         output.RenderBeginTag (HtmlTextWriterTag.Html);
63                         
64                         output.RenderBeginTag (HtmlTextWriterTag.Head);
65                         TraceData.RenderStyleSheet (output);
66                         output.RenderEndTag ();
67
68                         RenderHeader (output, dir);
69                         
70                         output.RenderBeginTag (HtmlTextWriterTag.Body);
71                         output.AddAttribute ("class", "tracecontent");
72                         output.RenderBeginTag (HtmlTextWriterTag.Span);
73
74                         Table table = TraceData.CreateTable ();
75                         
76                         table.Rows.Add (TraceData.AltRow ("Requests to the Application"));
77                         table.Rows.Add (TraceData.SubHeadRow ("No", "Time of Request",
78                                                         "File", "Status Code", "Verb", "&nbsp;"));
79
80                         if (manager.TraceData != null) {
81                                 for (int i=0; i<manager.ItemCount; i++) {
82                                         int item = i + 1;
83                                         TraceData d = manager.TraceData [i];
84                                         TraceData.RenderAltRow (table, i, item.ToString (), d.RequestTime.ToString (),
85                                                         d.RequestPath, d.StatusCode.ToString (), d.RequestType,
86                                                         "<a href=\"Trace.axd?id=" + item + "\" class=\"tinylink\">" +
87                                                         "<b><nobr>View Details</a>");
88                                 }
89                                 table.RenderControl (output);
90                         }
91                         
92                         output.RenderEndTag ();
93                         output.RenderEndTag ();
94                         
95                         output.RenderEndTag ();
96                 }
97
98                 private void RenderHeader (HtmlTextWriter output, string dir)
99                 {
100                         Table table = TraceData.CreateTable ();
101                         TableRow row1 = new TableRow ();
102                         TableRow row2 = new TableRow ();
103                         TableCell cell1 = new TableCell ();
104                         TableCell cell2 = new TableCell ();
105                         TableCell cell3 = new TableCell ();
106                         TableCell cell4 = new TableCell ();
107                         
108                         cell1.Text = "<h1>Application Trace</h1>";
109                         cell2.Text = "[ <a href=\"Trace.axd?clear=1\" class=\"link\">clear current trace</a> ]";
110                         
111                         cell2.HorizontalAlign = HorizontalAlign.Right;
112                         cell2.VerticalAlign = VerticalAlign.Bottom;
113                         
114                         row1.Cells.Add (cell1);
115                         row1.Cells.Add (cell2);
116
117                         cell3.Text = "<h2><h2><p>"; // ummm, WTF?
118                         cell4.Text = "<b>Physical Directory:</b>" + dir;
119
120                         row2.Cells.Add (cell3);
121                         row2.Cells.Add (cell4);
122
123                         table.Rows.Add (row1);
124                         table.Rows.Add (row2);
125
126                         table.RenderControl (output);
127                 }
128                 
129                 private void RenderItem (TraceManager manager, HtmlTextWriter output, int item)
130                 {
131                         manager.TraceData [item - 1].Render (output);
132                 }
133
134                 [MonoTODO ("Appears in class status, but...")]
135                 protected void ShowDetails (DataSet data)
136                 {
137                 }
138
139                 [MonoTODO ("Appears in class status, but...")]
140                 protected void ShowRequests (ArrayList list)
141                 {
142                 }
143         }
144 }
145