UrlUtils.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. using System.Text;
  32. namespace System.Web.Util {
  33. internal class UrlUtils {
  34. // appRoot + SessionID + vpath
  35. internal static string InsertSessionId (string id, string path)
  36. {
  37. string dir = GetDirectory (path);
  38. if (!dir.EndsWith ("/"))
  39. dir += "/";
  40. string appvpath = HttpRuntime.AppDomainAppVirtualPath;
  41. if (!appvpath.EndsWith ("/"))
  42. appvpath += "/";
  43. if (path.StartsWith (appvpath))
  44. path = path.Substring (appvpath.Length);
  45. if (path [0] == '/')
  46. path = path.Length > 1 ? path.Substring (1) : "";
  47. return Canonic (appvpath + "(" + id + ")/" + path);
  48. }
  49. internal static string GetSessionId (string path)
  50. {
  51. if (path == null)
  52. return null;
  53. string appvpath = HttpRuntime.AppDomainAppVirtualPath;
  54. int appvpathlen = appvpath.Length;
  55. if (path.Length <= appvpathlen)
  56. return null;
  57. path = path.Substring (appvpathlen);
  58. int len = path.Length;
  59. if (len == 0 || path [0] != '/') {
  60. path = '/' + path;
  61. len++;
  62. }
  63. if ((len < SessionId.IdLength + 3) || (path [1] != '(') ||
  64. (path [SessionId.IdLength + 2] != ')'))
  65. return null;
  66. return path.Substring (2, SessionId.IdLength);
  67. }
  68. internal static bool HasSessionId (string path)
  69. {
  70. if (path == null || path.Length < 5)
  71. return false;
  72. return (StrUtils.StartsWith (path, "/(") && path.IndexOf (")/") > 2);
  73. }
  74. internal static string RemoveSessionId (string base_path, string file_path)
  75. {
  76. // Caller did a GetSessionId first
  77. int idx = base_path.IndexOf ("/(");
  78. string dir = base_path.Substring (0, idx + 1);
  79. if (!dir.EndsWith ("/"))
  80. dir += "/";
  81. idx = base_path.IndexOf (")/");
  82. if (idx != -1 && base_path.Length > idx + 2) {
  83. string dir2 = base_path.Substring (idx + 2);
  84. if (!dir2.EndsWith ("/"))
  85. dir2 += "/";
  86. dir += dir2;
  87. }
  88. return Canonic (dir + GetFile (file_path));
  89. }
  90. public static string Combine (string basePath, string relPath)
  91. {
  92. if (relPath == null)
  93. throw new ArgumentNullException ("relPath");
  94. int rlength = relPath.Length;
  95. if (rlength == 0)
  96. return "";
  97. relPath = relPath.Replace ("\\", "/");
  98. if (IsRooted (relPath))
  99. return Canonic (relPath);
  100. char first = relPath [0];
  101. if (rlength < 3 || first == '~' || first == '/' || first == '\\') {
  102. if (basePath == null || (basePath.Length == 1 && basePath [0] == '/'))
  103. basePath = String.Empty;
  104. string slash = (first == '/') ? "" : "/";
  105. if (first == '~') {
  106. if (rlength == 1) {
  107. relPath = "";
  108. } else if (rlength > 1 && relPath [1] == '/') {
  109. relPath = relPath.Substring (2);
  110. slash = "/";
  111. }
  112. string appvpath = HttpRuntime.AppDomainAppVirtualPath;
  113. if (appvpath.EndsWith ("/"))
  114. slash = "";
  115. return Canonic (appvpath + slash + relPath);
  116. }
  117. return Canonic (basePath + slash + relPath);
  118. }
  119. if (basePath == null || basePath.Length == 0 || basePath [0] == '~')
  120. basePath = HttpRuntime.AppDomainAppVirtualPath;
  121. if (basePath.Length <= 1)
  122. basePath = String.Empty;
  123. return Canonic (basePath + "/" + relPath);
  124. }
  125. static char [] path_sep = {'\\', '/'};
  126. internal static string Canonic (string path)
  127. {
  128. bool isRooted = IsRooted(path);
  129. bool endsWithSlash = path.EndsWith("/");
  130. string [] parts = path.Split (path_sep);
  131. int end = parts.Length;
  132. int dest = 0;
  133. for (int i = 0; i < end; i++) {
  134. string current = parts [i];
  135. if (current.Length == 0)
  136. continue;
  137. if (current == "." )
  138. continue;
  139. if (current == "..") {
  140. dest --;
  141. continue;
  142. }
  143. if (dest < 0)
  144. if (!isRooted)
  145. throw new HttpException ("Invalid path.");
  146. else
  147. dest = 0;
  148. parts [dest++] = current;
  149. }
  150. if (dest < 0)
  151. throw new HttpException ("Invalid path.");
  152. if (dest == 0)
  153. return "/";
  154. string str = String.Join ("/", parts, 0, dest);
  155. #if NET_2_0
  156. str = RemoveDoubleSlashes (str);
  157. #endif
  158. if (isRooted)
  159. str = "/" + str;
  160. if (endsWithSlash)
  161. str = str + "/";
  162. return str;
  163. }
  164. internal static string GetDirectory (string url)
  165. {
  166. url = url.Replace('\\','/');
  167. int last = url.LastIndexOf ('/');
  168. if (last > 0) {
  169. if (last < url.Length)
  170. last++;
  171. #if NET_2_0
  172. return RemoveDoubleSlashes (url.Substring (0, last));
  173. #else
  174. return url.Substring (0, last);
  175. #endif
  176. }
  177. return "/";
  178. }
  179. #if NET_2_0
  180. internal static string RemoveDoubleSlashes (string input)
  181. {
  182. // MS VirtualPathUtility removes duplicate '/'
  183. int index = -1;
  184. for (int i = 1; i < input.Length; i++)
  185. if (input [i] == '/' && input [i - 1] == '/') {
  186. index = i - 1;
  187. break;
  188. }
  189. if (index == -1) // common case optimization
  190. return input;
  191. StringBuilder sb = new StringBuilder (input.Length);
  192. sb.Append (input, 0, index);
  193. for (int i = index; i < input.Length; i++) {
  194. if (input [i] == '/') {
  195. int next = i + 1;
  196. if (next < input.Length && input [next] == '/')
  197. continue;
  198. sb.Append ('/');
  199. }
  200. else {
  201. sb.Append (input [i]);
  202. }
  203. }
  204. return sb.ToString ();
  205. }
  206. #endif
  207. internal static string GetFile (string url)
  208. {
  209. url = url.Replace('\\','/');
  210. int last = url.LastIndexOf ('/');
  211. if (last >= 0) {
  212. if (url.Length == 1) // Empty file name instead of ArgumentOutOfRange
  213. return "";
  214. return url.Substring (last+1);
  215. }
  216. throw new ArgumentException (String.Format ("GetFile: `{0}' does not contain a /", url));
  217. }
  218. internal static bool IsRooted (string path)
  219. {
  220. if (path == null || path.Length == 0)
  221. return true;
  222. char c = path [0];
  223. if (c == '/' || c == '\\')
  224. return true;
  225. return false;
  226. }
  227. internal static bool IsRelativeUrl (string path)
  228. {
  229. return (path [0] != '/' && path.IndexOf (':') == -1);
  230. }
  231. public static string ResolveVirtualPathFromAppAbsolute (string path)
  232. {
  233. if (path [0] != '~') return path;
  234. if (path.Length == 1)
  235. return HttpRuntime.AppDomainAppVirtualPath;
  236. if (path [1] == '/' || path [1] == '\\') {
  237. string appPath = HttpRuntime.AppDomainAppVirtualPath;
  238. if (appPath.Length > 1)
  239. return appPath + "/" + path.Substring (2);
  240. return "/" + path.Substring (2);
  241. }
  242. return path;
  243. }
  244. public static string ResolvePhysicalPathFromAppAbsolute (string path)
  245. {
  246. if (path [0] != '~') return path;
  247. if (path.Length == 1)
  248. return HttpRuntime.AppDomainAppPath;
  249. if (path [1] == '/' || path [1] == '\\') {
  250. string appPath = HttpRuntime.AppDomainAppPath;
  251. if (appPath.Length > 1)
  252. return appPath + "/" + path.Substring (2);
  253. return "/" + path.Substring (2);
  254. }
  255. return path;
  256. }
  257. }
  258. }