RegionURI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * - Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * - Neither the name of the openmetaverse.org nor the names
  11. * of its contributors may be used to endorse or promote products derived from
  12. * this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.Net;
  28. using OpenMetaverse;
  29. namespace OpenSim.Framework
  30. {
  31. public class RegionURI
  32. {
  33. private static byte[] schemaSep = osUTF8.GetASCIIBytes("://");
  34. private static byte[] altschemaSep = osUTF8.GetASCIIBytes("|!!");
  35. private static byte[] nameSep = osUTF8.GetASCIIBytes(":/ ");
  36. private static byte[] altnameSep = osUTF8.GetASCIIBytes(":/ +|");
  37. private static byte[] escapePref = osUTF8.GetASCIIBytes("+%");
  38. private static byte[] altPortSepPref = osUTF8.GetASCIIBytes(":|");
  39. public enum URIFlags : int
  40. {
  41. None = 0,
  42. Valid = 1 << 0,
  43. HasHost = 1 << 1,
  44. HasResolvedHost = 1 << 2,
  45. HasUserName = 1 << 3,
  46. HasUserPass = 1 << 4,
  47. HasRegionName = 1 << 5,
  48. HasCoords = 1 << 6,
  49. IsLocalGrid = 1 << 7 // this must be set externally
  50. }
  51. public URIFlags Flags;
  52. public IPAddress IP;
  53. public string originalURI = string.Empty;
  54. public string Schema = "http://";
  55. public string Host = string.Empty;
  56. public int Port = 80;
  57. public string RegionName = string.Empty;
  58. public string Username = string.Empty;
  59. public string UserPass = string.Empty;
  60. public int X = 127;
  61. public int Y = 127;
  62. public int Z = 2;
  63. public RegionURI(string _originalURI)
  64. {
  65. originalURI = _originalURI;
  66. Parse(_originalURI);
  67. if (!HasHost)
  68. Flags |= URIFlags.IsLocalGrid;
  69. }
  70. public RegionURI(string _originalURI, GridInfo gi)
  71. {
  72. originalURI = _originalURI;
  73. Parse(_originalURI);
  74. if(!HasHost)
  75. {
  76. Flags |= URIFlags.IsLocalGrid;
  77. return;
  78. }
  79. if(gi == null)
  80. return;
  81. if (gi.IsLocalGrid(HostUrl) == 1)
  82. {
  83. Host = string.Empty;
  84. Flags &= ~URIFlags.HasHost;
  85. Flags |= URIFlags.IsLocalGrid;
  86. return;
  87. }
  88. if (!ResolveDNS())
  89. {
  90. Flags = URIFlags.None;
  91. }
  92. }
  93. public void Parse(string inputURI)
  94. {
  95. Flags = URIFlags.None;
  96. if (string.IsNullOrWhiteSpace(inputURI))
  97. return;
  98. osUTF8Slice input = new osUTF8Slice(inputURI);
  99. input.SelfTrimStart((byte)' ');
  100. input.SelfTrimStart((byte)'+');
  101. int firstDot = input.IndexOf((byte)'.');
  102. if (firstDot == 0)
  103. return;
  104. osUTF8Slice tmpSlice;
  105. int indx = input.IndexOf(schemaSep);
  106. if (indx == 0)
  107. return;
  108. if (indx < 0)
  109. indx = input.IndexOf(altschemaSep);
  110. if (indx == 0)
  111. return;
  112. if (indx > 0)
  113. {
  114. if (indx < 2 || input.Length < indx + 4 || (firstDot > 0 && indx > firstDot))
  115. return;
  116. bool issecure = false;
  117. tmpSlice = input.SubUTF8(0, indx).Clone();
  118. tmpSlice.ToASCIILowerSelf();
  119. if (tmpSlice.EndsWith((byte)'s'))
  120. {
  121. issecure = true;
  122. tmpSlice.SelfTrimEnd((byte)'s');
  123. }
  124. switch (tmpSlice.ToString())
  125. {
  126. case "hg":
  127. case "hop":
  128. case "http":
  129. case "surl":
  130. if (issecure)
  131. {
  132. Schema = "https://";
  133. Port = 443;
  134. }
  135. break;
  136. default:
  137. return;
  138. }
  139. indx += 3;
  140. input.SubUTF8Self(indx);
  141. firstDot -= indx;
  142. }
  143. int namestart = 0;
  144. if (firstDot > 0)
  145. {
  146. int hostend = -1;
  147. osUTF8Slice hosttmp = input.Clone();
  148. indx = input.IndexOfAny(altPortSepPref);
  149. if (indx > 0)
  150. {
  151. if (indx < firstDot)
  152. return;
  153. hostend = indx;
  154. ++indx;
  155. int tmpport = 0;
  156. byte c;
  157. while (indx < input.Length)
  158. {
  159. c = input[indx];
  160. if (c < (byte)'0' || c > (byte)'9')
  161. break;
  162. tmpport *= 10;
  163. tmpport += c - (byte)'0';
  164. ++indx;
  165. }
  166. if (indx > hostend + 1)
  167. {
  168. if (tmpport > 64 * 1024)
  169. return;
  170. Port = tmpport;
  171. }
  172. input.SubUTF8Self(indx);
  173. input.SelfTrimStart(altnameSep);
  174. }
  175. else
  176. {
  177. indx = input.IndexOfAny(altnameSep);
  178. if (indx < 0)
  179. {
  180. hostend = input.Length;
  181. namestart = -1;
  182. }
  183. else
  184. {
  185. hostend = indx;
  186. namestart = indx + 1;
  187. }
  188. }
  189. if (hostend <= 0)
  190. return;
  191. hosttmp.SubUTF8Self(0, hostend);
  192. indx = hosttmp.IndexOf((byte)'@');
  193. if (indx >= 0)
  194. {
  195. if (indx > 0)
  196. {
  197. tmpSlice = hosttmp.SubUTF8(0, indx);
  198. int indx2 = tmpSlice.IndexOfAny(escapePref);
  199. if (indx2 >= 0)
  200. {
  201. Username = Uri.UnescapeDataString(tmpSlice.ToString());
  202. Username = Username.Replace('+', ' ');
  203. }
  204. else
  205. Username = tmpSlice.ToString();
  206. if (Username.Length > 0)
  207. Flags |= URIFlags.HasUserName;
  208. }
  209. ++indx;
  210. hosttmp.SubUTF8Self(indx);
  211. }
  212. if (hosttmp.Length == 0)
  213. {
  214. Flags = URIFlags.None;
  215. return;
  216. }
  217. indx = hosttmp.IndexOfAny(escapePref);
  218. if (indx >= 0)
  219. {
  220. string blabla = Uri.UnescapeDataString(hosttmp.ToString());
  221. blabla = blabla.Replace('+', ' ');
  222. hosttmp = new osUTF8Slice(blabla);
  223. }
  224. hosttmp.ToASCIILowerSelf();
  225. Host = hosttmp.ToString();
  226. UriHostNameType type = Uri.CheckHostName(Host);
  227. if (type == UriHostNameType.Unknown || type == UriHostNameType.Basic)
  228. {
  229. Flags = URIFlags.None;
  230. return;
  231. }
  232. Flags |= URIFlags.HasHost;
  233. }
  234. if (namestart < 0 || input.Length == 0)
  235. return;
  236. input.SubUTF8Self(namestart);
  237. input.SelfTrimStart((byte)' ');
  238. input.SelfTrimStart((byte)'+');
  239. int firstCoord = input.IndexOf((byte)'/');
  240. if (firstCoord == 0)
  241. {
  242. Flags = URIFlags.None;
  243. return;
  244. }
  245. if (firstCoord < 0)
  246. firstCoord = input.IndexOf((byte)'(');
  247. if (firstCoord > 0)
  248. tmpSlice = input.SubUTF8(0, firstCoord);
  249. else
  250. tmpSlice = input;
  251. indx = tmpSlice.IndexOfAny(escapePref);
  252. if (indx >= 0)
  253. {
  254. string blabla = Uri.UnescapeDataString(tmpSlice.ToString());
  255. blabla = blabla.Replace('+', ' ');
  256. tmpSlice = new osUTF8Slice(blabla);
  257. }
  258. tmpSlice.SelfTrimEnd((byte)' ');
  259. if (tmpSlice.Length <= 0)
  260. return;
  261. RegionName = tmpSlice.ToString();
  262. Flags |= URIFlags.HasRegionName;
  263. if (firstCoord > 0)
  264. {
  265. if (input[firstCoord] == (byte)'/')
  266. {
  267. ++firstCoord;
  268. int tmp = 0;
  269. tmpSlice = input.SubUTF8(firstCoord);
  270. int indx2 = 0;
  271. while (indx2 < tmpSlice.Length)
  272. {
  273. byte c = tmpSlice[indx2];
  274. if (c < (byte)'0' || c > (byte)'9')
  275. break;
  276. tmp *= 10;
  277. tmp += c - (byte)'0';
  278. ++indx2;
  279. }
  280. if (indx2 == 0)
  281. {
  282. Flags = URIFlags.None;
  283. return;
  284. }
  285. X = tmp;
  286. tmpSlice.SubUTF8Self(indx2);
  287. indx = tmpSlice.IndexOf((byte)'/');
  288. if (indx >= 0)
  289. {
  290. ++indx;
  291. tmpSlice.SubUTF8Self(indx);
  292. indx2 = 0;
  293. tmp = 0;
  294. while (indx2 < tmpSlice.Length)
  295. {
  296. byte c = tmpSlice[indx2];
  297. if (c < (byte)'0' || c > (byte)'9')
  298. break;
  299. tmp *= 10;
  300. tmp += c - (byte)'0';
  301. ++indx2;
  302. }
  303. if (indx2 == 0)
  304. {
  305. Flags = URIFlags.None;
  306. return;
  307. }
  308. Y = tmp;
  309. tmpSlice.SubUTF8Self(indx2);
  310. indx = tmpSlice.IndexOf((byte)'/');
  311. if (indx >= 0)
  312. {
  313. ++indx;
  314. tmpSlice.SubUTF8Self(indx);
  315. indx2 = 0;
  316. tmp = 0;
  317. int sig = 1;
  318. if (tmpSlice[indx2] == (byte)'-')
  319. {
  320. sig = -1;
  321. indx2++;
  322. }
  323. while (indx2 < tmpSlice.Length)
  324. {
  325. byte c = tmpSlice[indx2];
  326. if (c < (byte)'0' || c > (byte)'9')
  327. break;
  328. tmp *= 10;
  329. tmp += c - (byte)'0';
  330. ++indx2;
  331. }
  332. if (indx2 == 0)
  333. {
  334. Flags = URIFlags.None;
  335. return;
  336. }
  337. Z = sig * tmp;
  338. }
  339. }
  340. }
  341. else // (,,) case
  342. {
  343. ++firstCoord;
  344. int tmp = 0;
  345. tmpSlice = input.SubUTF8(firstCoord);
  346. int indx2 = tmpSlice.IndexOf((byte)')');
  347. if (indx2 == 0)
  348. return;
  349. if (indx2 > 0)
  350. tmpSlice.SubUTF8Self(0, indx2);
  351. indx2 = 0;
  352. tmpSlice.SelfTrimStart((byte)' ');
  353. tmpSlice.SelfTrimStart((byte)'+');
  354. while (indx2 < tmpSlice.Length)
  355. {
  356. byte c = tmpSlice[indx2];
  357. if (c < (byte)'0' || c > (byte)'9')
  358. break;
  359. tmp *= 10;
  360. tmp += c - (byte)'0';
  361. ++indx2;
  362. }
  363. if (indx2 == 0)
  364. {
  365. Flags = URIFlags.None;
  366. return;
  367. }
  368. X = tmp;
  369. tmpSlice.SubUTF8Self(indx2);
  370. indx = tmpSlice.IndexOf((byte)',');
  371. if (indx >= 0)
  372. {
  373. ++indx;
  374. tmpSlice.SubUTF8Self(indx);
  375. tmpSlice.SelfTrimStart((byte)' ');
  376. tmpSlice.SelfTrimStart((byte)'+');
  377. indx2 = 0;
  378. tmp = 0;
  379. while (indx2 < tmpSlice.Length)
  380. {
  381. byte c = tmpSlice[indx2];
  382. if (c < (byte)'0' || c > (byte)'9')
  383. break;
  384. tmp *= 10;
  385. tmp += c - (byte)'0';
  386. ++indx2;
  387. }
  388. if (indx2 == 0)
  389. {
  390. Flags = URIFlags.None;
  391. return;
  392. }
  393. Y = tmp;
  394. tmpSlice.SubUTF8Self(indx2);
  395. indx = tmpSlice.IndexOf((byte)',');
  396. if (indx >= 0)
  397. {
  398. ++indx;
  399. tmpSlice.SubUTF8Self(indx);
  400. tmpSlice.SelfTrimStart((byte)' ');
  401. tmpSlice.SelfTrimStart((byte)'+');
  402. indx2 = 0;
  403. tmp = 0;
  404. int sig = 1;
  405. if (tmpSlice[indx2] == (byte)'-')
  406. {
  407. sig = -1;
  408. indx2++;
  409. }
  410. while (indx2 < tmpSlice.Length)
  411. {
  412. byte c = tmpSlice[indx2];
  413. if (c < (byte)'0' || c > (byte)'9')
  414. break;
  415. tmp *= 10;
  416. tmp += c - (byte)'0';
  417. ++indx2;
  418. }
  419. if (indx2 == 0)
  420. {
  421. Flags = URIFlags.None;
  422. return;
  423. }
  424. Z = sig * tmp;
  425. }
  426. }
  427. }
  428. }
  429. return;
  430. }
  431. public bool ResolveDNS()
  432. {
  433. if ((Flags & URIFlags.HasHost) != 0)
  434. {
  435. IPAddress ip = Util.GetHostFromDNS(Host);
  436. if (ip != null)
  437. {
  438. IP = ip;
  439. Flags |= URIFlags.HasResolvedHost;
  440. return true;
  441. }
  442. }
  443. return false;
  444. }
  445. public bool IsValid
  446. {
  447. get { return (Flags & (URIFlags.HasHost | URIFlags.HasRegionName)) != 0; }
  448. }
  449. public bool HasHost
  450. {
  451. get { return (Flags & URIFlags.HasHost) != 0; }
  452. }
  453. public bool HasRegionName
  454. {
  455. get { return (Flags & URIFlags.HasRegionName) != 0; }
  456. }
  457. public string HostUrl
  458. {
  459. get { return (Flags & URIFlags.HasHost) != 0 ? (Schema + Host + ":" + Port) : ""; }
  460. }
  461. public string HostUrlEndSlash
  462. {
  463. get { return (Flags & URIFlags.HasHost) != 0 ? (Schema + Host + ":" + Port + "/") : ""; }
  464. }
  465. public string RegionUrlAndName
  466. {
  467. get
  468. {
  469. string ret = (Flags & URIFlags.HasHost) != 0 ? (Schema + Host + ":" + Port + "/") : "";
  470. if ((Flags & URIFlags.HasRegionName) != 0)
  471. ret += RegionName;
  472. return ret;
  473. }
  474. }
  475. public string RegionHostPortSpaceName
  476. {
  477. get
  478. {
  479. string ret = (Flags & URIFlags.HasHost) != 0 ? (Host + ":" + Port + "/ ") : ""; // space needed for compatibility
  480. if ((Flags & URIFlags.HasRegionName) != 0)
  481. ret += RegionName;
  482. return ret;
  483. }
  484. }
  485. // this needs to be set before get
  486. public bool IsLocalGrid
  487. {
  488. get { return (Flags & URIFlags.IsLocalGrid) != 0; }
  489. set
  490. {
  491. if(value)
  492. {
  493. Host = string.Empty;
  494. Flags &= ~URIFlags.HasHost;
  495. Flags |= URIFlags.IsLocalGrid;
  496. }
  497. }
  498. }
  499. }
  500. }