TraceHandler.cs 6.1 KB

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