UrlUtils.cs 8.3 KB

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