UrlUtils.cs 7.8 KB

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