Messages.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // **********************************************************************************
  2. // CassiniDev - http://cassinidev.codeplex.com
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. // This source code is subject to terms and conditions of the Microsoft Public
  7. // License (Ms-PL). A copy of the license can be found in the license.txt file
  8. // included in this distribution.
  9. //
  10. // You must not remove this notice, or any other, from this software.
  11. //
  12. // **********************************************************************************
  13. #region
  14. using System.IO;
  15. using System.Text;
  16. using System.Web;
  17. #endregion
  18. namespace CassiniDev
  19. {
  20. /// <summary>
  21. /// TODO: get this into resources
  22. /// </summary>
  23. internal static class Messages
  24. {
  25. private const string _dirListingDirFormat =
  26. @"{0,38:dddd, MMMM dd, yyyy hh:mm tt} &lt;dir&gt; <A href=""{1}/"">{2}</A>
  27. ";
  28. private const string _dirListingFileFormat =
  29. @"{0,38:dddd, MMMM dd, yyyy hh:mm tt} {1,12:n0} <A href=""{2}"">{3}</A>
  30. ";
  31. private const string _dirListingFormat1 =
  32. @"<html>
  33. <head>
  34. <title>Directory Listing -- {0}</title>
  35. ";
  36. private const string _dirListingFormat2 =
  37. @" </head>
  38. <body bgcolor=""white"">
  39. <h2> <i>Directory Listing -- {0}</i> </h2></span>
  40. <hr width=100% size=1 color=silver>
  41. <PRE>
  42. ";
  43. private const string _dirListingParentFormat =
  44. @"<A href=""{0}"">[To Parent Directory]</A>
  45. ";
  46. private const string _httpErrorFormat1 =
  47. @"<html>
  48. <head>
  49. <title>{0}</title>
  50. ";
  51. private const string _httpStyle =
  52. @" <style>
  53. body {font-family:""Verdana"";font-weight:normal;font-size: 8pt;color:black;}
  54. p {font-family:""Verdana"";font-weight:normal;color:black;margin-top: -5px}
  55. b {font-family:""Verdana"";font-weight:bold;color:black;margin-top: -5px}
  56. h1 { font-family:""Verdana"";font-weight:normal;font-size:18pt;color:red }
  57. h2 { font-family:""Verdana"";font-weight:normal;font-size:14pt;color:maroon }
  58. pre {font-family:""Lucida Console"";font-size: 8pt}
  59. .marker {font-weight: bold; color: black;text-decoration: none;}
  60. .version {color: gray;}
  61. .error {margin-bottom: 10px;}
  62. .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
  63. </style>
  64. ";
  65. private static readonly string _dirListingTail =
  66. @"</PRE>
  67. <hr width=100% size=1 color=silver>
  68. <b>Version Information:</b>&nbsp;CassiniDev Web Server " +
  69. VersionString + @"
  70. </font>
  71. </body>
  72. </html>
  73. ";
  74. private static readonly string _httpErrorFormat2 =
  75. @" </head>
  76. <body bgcolor=""white"">
  77. <span><h1>Server Error in '{0}' Application.<hr width=100% size=1 color=silver></h1>
  78. <h2> <i>HTTP Error {1} - {2}.</i> </h2></span>
  79. <hr width=100% size=1 color=silver>
  80. <b>Version Information:</b>&nbsp;CassiniDev Web Server " +
  81. VersionString + @"
  82. </font>
  83. </body>
  84. </html>
  85. ";
  86. public static string VersionString = typeof (Server).Assembly.GetName().Version.ToString();
  87. public static string FormatDirectoryListing(string dirPath, string parentPath, FileSystemInfo[] elements)
  88. {
  89. StringBuilder sb = new StringBuilder();
  90. sb.Append(string.Format(_dirListingFormat1, dirPath));
  91. sb.Append(_httpStyle);
  92. sb.Append(string.Format(_dirListingFormat2, dirPath));
  93. if (parentPath != null)
  94. {
  95. if (!parentPath.EndsWith("/"))
  96. {
  97. parentPath += "/";
  98. }
  99. sb.Append(string.Format(_dirListingParentFormat, parentPath));
  100. }
  101. if (elements != null)
  102. {
  103. for (int i = 0; i < elements.Length; i++)
  104. {
  105. if (elements[i] is FileInfo)
  106. {
  107. FileInfo fi = (FileInfo) elements[i];
  108. sb.Append(string.Format(_dirListingFileFormat,
  109. fi.LastWriteTime, fi.Length, fi.Name, fi.Name));
  110. }
  111. else if (elements[i] is DirectoryInfo)
  112. {
  113. DirectoryInfo di = (DirectoryInfo) elements[i];
  114. sb.Append(string.Format(_dirListingDirFormat,
  115. di.LastWriteTime, di.Name, di.Name));
  116. }
  117. }
  118. }
  119. sb.Append(_dirListingTail);
  120. return sb.ToString();
  121. }
  122. public static string FormatErrorMessageBody(int statusCode, string appName)
  123. {
  124. string desc = HttpWorkerRequest.GetStatusDescription(statusCode);
  125. return string.Format(_httpErrorFormat1, desc)
  126. + _httpStyle
  127. + string.Format(_httpErrorFormat2, appName, statusCode, desc);
  128. }
  129. }
  130. }