UrlUtils.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // System.Web.UrlUtils.cs
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua ([email protected])
  6. // Jackson Harper ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Web.SessionState;
  31. namespace System.Web.Util {
  32. internal class UrlUtils {
  33. // appRoot + SessionID + vpath
  34. internal static string InsertSessionId (string id, string path)
  35. {
  36. string dir = GetDirectory (path);
  37. if (!dir.EndsWith ("/"))
  38. dir += "/";
  39. string appvpath = HttpRuntime.AppDomainAppVirtualPath;
  40. if (!appvpath.EndsWith ("/"))
  41. appvpath += "/";
  42. if (path.StartsWith (appvpath))
  43. path = path.Substring (appvpath.Length);
  44. if (path [0] == '/')
  45. path = path.Length > 1 ? path.Substring (1) : "";
  46. return Canonic (appvpath + "(" + id + ")/" + path);
  47. }
  48. internal static string GetSessionId (string path)
  49. {
  50. string appvpath = HttpRuntime.AppDomainAppVirtualPath;
  51. if (path.Length <= appvpath.Length)
  52. return null;
  53. path = path.Substring (appvpath.Length);
  54. if (path.Length == 0 || path [0] != '/')
  55. path = '/' + path;
  56. int len = path.Length;
  57. if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
  58. (path [SessionId.IdLength + 2] != ')'))
  59. return null;
  60. return path.Substring (2, SessionId.IdLength);
  61. }
  62. internal static string RemoveSessionId (string base_path, string file_path)
  63. {
  64. // Caller did a GetSessionId first
  65. int idx = base_path.IndexOf ("/(");
  66. string dir = base_path.Substring (0, idx + 1);
  67. if (!dir.EndsWith ("/"))
  68. dir += "/";
  69. idx = base_path.IndexOf (")/");
  70. if (idx != -1 && base_path.Length > idx + 2) {
  71. string dir2 = base_path.Substring (idx + 2);
  72. if (!dir2.EndsWith ("/"))
  73. dir2 += "/";
  74. dir += dir2;
  75. }
  76. return Canonic (dir + GetFile (file_path));
  77. }
  78. public static string Combine (string basePath, string relPath)
  79. {
  80. if (relPath == null)
  81. throw new ArgumentNullException ("relPath");
  82. int rlength = relPath.Length;
  83. if (rlength == 0)
  84. return "";
  85. relPath = relPath.Replace ("\\", "/");
  86. if (IsRooted (relPath))
  87. return Canonic (relPath);
  88. char first = relPath [0];
  89. if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
  90. if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
  91. basePath = String.Empty;
  92. string slash = (first == '/') ? "" : "/";
  93. if (first == '~') {
  94. if (rlength == 1) {
  95. relPath = "";
  96. } else if (rlength > 1 && relPath [1] == '/') {
  97. relPath = relPath.Substring (2);
  98. slash = "/";
  99. }
  100. string appvpath = HttpRuntime.AppDomainAppVirtualPath;
  101. if (appvpath.EndsWith ("/"))
  102. slash = "";
  103. return Canonic (appvpath + slash + relPath);
  104. }
  105. return Canonic (basePath + slash + relPath);
  106. }
  107. if (basePath == null || basePath == "" || basePath [0] == '~')
  108. basePath = HttpRuntime.AppDomainAppVirtualPath;
  109. if (basePath.Length <= 1)
  110. basePath = String.Empty;
  111. return Canonic (basePath + "/" + relPath);
  112. }
  113. static char [] path_sep = {'\\', '/'};
  114. internal static string Canonic (string path)
  115. {
  116. string [] parts = path.Split (path_sep);
  117. int end = parts.Length;
  118. int dest = 0;
  119. for (int i = 0; i < end; i++) {
  120. string current = parts [i];
  121. if (current == "." )
  122. continue;
  123. if (current == "..") {
  124. if (dest == 0) {
  125. if (i == 1) // see bug 52599
  126. continue;
  127. throw new HttpException ("Invalid path.");
  128. }
  129. dest --;
  130. continue;
  131. }
  132. parts [dest++] = current;
  133. }
  134. if (dest == 0)
  135. return "/";
  136. return String.Join ("/", parts, 0, dest);
  137. }
  138. internal static string GetDirectory (string url)
  139. {
  140. url = url.Replace('\\','/');
  141. int last = url.LastIndexOf ('/');
  142. if (last > 0) {
  143. #if NET_2_0
  144. return RemoveDoubleSlashes (url.Substring (0, last));
  145. #else
  146. return url.Substring (0, last);
  147. #endif
  148. }
  149. return "/";
  150. }
  151. #if NET_2_0
  152. internal static string RemoveDoubleSlashes (string input)
  153. {
  154. // MS VirtualPathUtility removes duplicate '/'
  155. string str = input;
  156. string x;
  157. while ((x = str.Replace ("//", "/")) != str) {
  158. str = x;
  159. }
  160. return str;
  161. }
  162. #endif
  163. internal static string GetFile (string url)
  164. {
  165. url = url.Replace('\\','/');
  166. int last = url.LastIndexOf ('/');
  167. if (last >= 0) {
  168. if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
  169. return "";
  170. return url.Substring (last+1);
  171. }
  172. throw new Exception (String.Format ("GetFile: `{0}' does not contain a /", url));
  173. }
  174. internal static bool IsRooted (string path)
  175. {
  176. if (path == null || path == "")
  177. return true;
  178. char c = path [0];
  179. if (c == '/' || c == '\\')
  180. return true;
  181. return false;
  182. }
  183. internal static bool IsRelativeUrl (string path)
  184. {
  185. return (path [0] != '/' && path.IndexOf (':') == -1);
  186. }
  187. public static string ResolveVirtualPathFromAppAbsolute (string path)
  188. {
  189. if (path [0] != '~') return path;
  190. if (path.Length == 1)
  191. return HttpRuntime.AppDomainAppVirtualPath;
  192. if (path [1] == '/' || path [1] == '\\') {
  193. string appPath = HttpRuntime.AppDomainAppVirtualPath;
  194. if (appPath.Length > 1)
  195. return appPath + "/" + path.Substring (2);
  196. return "/" + path.Substring (2);
  197. }
  198. return path;
  199. }
  200. public static string ResolvePhysicalPathFromAppAbsolute (string path)
  201. {
  202. if (path [0] != '~') return path;
  203. if (path.Length == 1)
  204. return HttpRuntime.AppDomainAppPath;
  205. if (path [1] == '/' || path [1] == '\\') {
  206. string appPath = HttpRuntime.AppDomainAppPath;
  207. if (appPath.Length > 1)
  208. return appPath + "/" + path.Substring (2);
  209. return "/" + path.Substring (2);
  210. }
  211. return path;
  212. }
  213. }
  214. }