UrlUtils.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. string str = String.Join ("/", parts, 0, dest);
  137. #if NET_2_0
  138. str = RemoveDoubleSlashes (str);
  139. #endif
  140. return str;
  141. }
  142. internal static string GetDirectory (string url)
  143. {
  144. url = url.Replace('\\','/');
  145. int last = url.LastIndexOf ('/');
  146. if (last > 0) {
  147. if (last < url.Length)
  148. last++;
  149. #if NET_2_0
  150. return RemoveDoubleSlashes (url.Substring (0, last));
  151. #else
  152. return url.Substring (0, last);
  153. #endif
  154. }
  155. return "/";
  156. }
  157. #if NET_2_0
  158. internal static string RemoveDoubleSlashes (string input)
  159. {
  160. // MS VirtualPathUtility removes duplicate '/'
  161. string str = input;
  162. string x;
  163. while ((x = str.Replace ("//", "/")) != str) {
  164. str = x;
  165. }
  166. return str;
  167. }
  168. #endif
  169. internal static string GetFile (string url)
  170. {
  171. url = url.Replace('\\','/');
  172. int last = url.LastIndexOf ('/');
  173. if (last >= 0) {
  174. if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
  175. return "";
  176. return url.Substring (last+1);
  177. }
  178. throw new ArgumentException (String.Format ("GetFile: `{0}' does not contain a /", url));
  179. }
  180. internal static bool IsRooted (string path)
  181. {
  182. if (path == null || path == "")
  183. return true;
  184. char c = path [0];
  185. if (c == '/' || c == '\\')
  186. return true;
  187. return false;
  188. }
  189. internal static bool IsRelativeUrl (string path)
  190. {
  191. return (path [0] != '/' && path.IndexOf (':') == -1);
  192. }
  193. public static string ResolveVirtualPathFromAppAbsolute (string path)
  194. {
  195. if (path [0] != '~') return path;
  196. if (path.Length == 1)
  197. return HttpRuntime.AppDomainAppVirtualPath;
  198. if (path [1] == '/' || path [1] == '\\') {
  199. string appPath = HttpRuntime.AppDomainAppVirtualPath;
  200. if (appPath.Length > 1)
  201. return appPath + "/" + path.Substring (2);
  202. return "/" + path.Substring (2);
  203. }
  204. return path;
  205. }
  206. public static string ResolvePhysicalPathFromAppAbsolute (string path)
  207. {
  208. if (path [0] != '~') return path;
  209. if (path.Length == 1)
  210. return HttpRuntime.AppDomainAppPath;
  211. if (path [1] == '/' || path [1] == '\\') {
  212. string appPath = HttpRuntime.AppDomainAppPath;
  213. if (appPath.Length > 1)
  214. return appPath + "/" + path.Substring (2);
  215. return "/" + path.Substring (2);
  216. }
  217. return path;
  218. }
  219. }
  220. }