TraceHandler.cs 6.0 KB

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