RegionURI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 "http":
  127. case "hg":
  128. case "hop":
  129. case "surl":
  130. case "x-grid-info":
  131. // only https has this defined
  132. if (issecure)
  133. {
  134. Schema = "https://";
  135. Port = 443;
  136. }
  137. break;
  138. default:
  139. return;
  140. }
  141. indx += 3;
  142. input.SubUTF8Self(indx);
  143. firstDot -= indx;
  144. }
  145. int namestart = 0;
  146. if (firstDot > 0)
  147. {
  148. int hostend = -1;
  149. osUTF8Slice hosttmp = input.Clone();
  150. indx = input.IndexOfAny(altPortSepPref);
  151. if (indx > 0)
  152. {
  153. if (indx < firstDot)
  154. return;
  155. hostend = indx;
  156. ++indx;
  157. int tmpport = 0;
  158. byte c;
  159. while (indx < input.Length)
  160. {
  161. c = input[indx];
  162. if (c < (byte)'0' || c > (byte)'9')
  163. break;
  164. tmpport *= 10;
  165. tmpport += c - (byte)'0';
  166. ++indx;
  167. }
  168. if (indx > hostend + 1)
  169. {
  170. if (tmpport > 64 * 1024)
  171. return;
  172. Port = tmpport;
  173. }
  174. input.SubUTF8Self(indx);
  175. input.SelfTrimStart(altnameSep);
  176. }
  177. else
  178. {
  179. indx = input.IndexOfAny(altnameSep);
  180. if (indx < 0)
  181. {
  182. hostend = input.Length;
  183. namestart = -1;
  184. }
  185. else
  186. {
  187. hostend = indx;
  188. namestart = indx + 1;
  189. }
  190. }
  191. if (hostend <= 0)
  192. return;
  193. hosttmp.SubUTF8Self(0, hostend);
  194. indx = hosttmp.IndexOf((byte)'@');
  195. if (indx >= 0)
  196. {
  197. if (indx > 0)
  198. {
  199. tmpSlice = hosttmp.SubUTF8(0, indx);
  200. int indx2 = tmpSlice.IndexOfAny(escapePref);
  201. if (indx2 >= 0)
  202. {
  203. Username = Uri.UnescapeDataString(tmpSlice.ToString());
  204. Username = Username.Replace('+', ' ');
  205. }
  206. else
  207. Username = tmpSlice.ToString();
  208. if (Username.Length > 0)
  209. Flags |= URIFlags.HasUserName;
  210. }
  211. ++indx;
  212. hosttmp.SubUTF8Self(indx);
  213. }
  214. if (hosttmp.Length == 0)
  215. {
  216. Flags = URIFlags.None;
  217. return;
  218. }
  219. indx = hosttmp.IndexOfAny(escapePref);
  220. if (indx >= 0)
  221. {
  222. string blabla = Uri.UnescapeDataString(hosttmp.ToString());
  223. blabla = blabla.Replace('+', ' ');
  224. hosttmp = new osUTF8Slice(blabla);
  225. }
  226. hosttmp.ToASCIILowerSelf();
  227. Host = hosttmp.ToString();
  228. UriHostNameType type = Uri.CheckHostName(Host);
  229. if (type == UriHostNameType.Unknown || type == UriHostNameType.Basic)
  230. {
  231. Flags = URIFlags.None;
  232. return;
  233. }
  234. Flags |= URIFlags.HasHost;
  235. }
  236. if (namestart < 0 || input.Length == 0)
  237. return;
  238. input.SubUTF8Self(namestart);
  239. input.SelfTrimStart((byte)' ');
  240. input.SelfTrimStart((byte)'+');
  241. int firstCoord = input.IndexOf((byte)'/');
  242. if (firstCoord == 0)
  243. {
  244. Flags = URIFlags.None;
  245. return;
  246. }
  247. if (firstCoord < 0)
  248. firstCoord = input.IndexOf((byte)'(');
  249. if (firstCoord > 0)
  250. tmpSlice = input.SubUTF8(0, firstCoord);
  251. else
  252. tmpSlice = input;
  253. indx = tmpSlice.IndexOfAny(escapePref);
  254. if (indx >= 0)
  255. {
  256. string blabla = Uri.UnescapeDataString(tmpSlice.ToString());
  257. blabla = blabla.Replace('+', ' ');
  258. tmpSlice = new osUTF8Slice(blabla);
  259. }
  260. tmpSlice.SelfTrimEnd((byte)' ');
  261. if (tmpSlice.Length <= 0)
  262. return;
  263. RegionName = tmpSlice.ToString();
  264. Flags |= URIFlags.HasRegionName;
  265. if (firstCoord > 0)
  266. {
  267. if (input[firstCoord] == (byte)'/')
  268. {
  269. ++firstCoord;
  270. int tmp = 0;
  271. tmpSlice = input.SubUTF8(firstCoord);
  272. int indx2 = 0;
  273. while (indx2 < tmpSlice.Length)
  274. {
  275. byte c = tmpSlice[indx2];
  276. if (c < (byte)'0' || c > (byte)'9')
  277. break;
  278. tmp *= 10;
  279. tmp += c - (byte)'0';
  280. ++indx2;
  281. }
  282. if (indx2 == 0)
  283. {
  284. Flags = URIFlags.None;
  285. return;
  286. }
  287. X = tmp;
  288. tmpSlice.SubUTF8Self(indx2);
  289. indx = tmpSlice.IndexOf((byte)'/');
  290. if (indx >= 0)
  291. {
  292. ++indx;
  293. tmpSlice.SubUTF8Self(indx);
  294. indx2 = 0;
  295. tmp = 0;
  296. while (indx2 < tmpSlice.Length)
  297. {
  298. byte c = tmpSlice[indx2];
  299. if (c < (byte)'0' || c > (byte)'9')
  300. break;
  301. tmp *= 10;
  302. tmp += c - (byte)'0';
  303. ++indx2;
  304. }
  305. if (indx2 == 0)
  306. {
  307. Flags = URIFlags.None;
  308. return;
  309. }
  310. Y = tmp;
  311. tmpSlice.SubUTF8Self(indx2);
  312. indx = tmpSlice.IndexOf((byte)'/');
  313. if (indx >= 0)
  314. {
  315. ++indx;
  316. tmpSlice.SubUTF8Self(indx);
  317. indx2 = 0;
  318. tmp = 0;
  319. int sig = 1;
  320. if (tmpSlice[indx2] == (byte)'-')
  321. {
  322. sig = -1;
  323. indx2++;
  324. }
  325. while (indx2 < tmpSlice.Length)
  326. {
  327. byte c = tmpSlice[indx2];
  328. if (c < (byte)'0' || c > (byte)'9')
  329. break;
  330. tmp *= 10;
  331. tmp += c - (byte)'0';
  332. ++indx2;
  333. }
  334. if (indx2 == 0)
  335. {
  336. Flags = URIFlags.None;
  337. return;
  338. }
  339. Z = sig * tmp;
  340. }
  341. }
  342. }
  343. else // (,,) case
  344. {
  345. ++firstCoord;
  346. int tmp = 0;
  347. tmpSlice = input.SubUTF8(firstCoord);
  348. int indx2 = tmpSlice.IndexOf((byte)')');
  349. if (indx2 == 0)
  350. return;
  351. if (indx2 > 0)
  352. tmpSlice.SubUTF8Self(0, indx2);
  353. indx2 = 0;
  354. tmpSlice.SelfTrimStart((byte)' ');
  355. tmpSlice.SelfTrimStart((byte)'+');
  356. while (indx2 < tmpSlice.Length)
  357. {
  358. byte c = tmpSlice[indx2];
  359. if (c < (byte)'0' || c > (byte)'9')
  360. break;
  361. tmp *= 10;
  362. tmp += c - (byte)'0';
  363. ++indx2;
  364. }
  365. if (indx2 == 0)
  366. {
  367. Flags = URIFlags.None;
  368. return;
  369. }
  370. X = tmp;
  371. tmpSlice.SubUTF8Self(indx2);
  372. indx = tmpSlice.IndexOf((byte)',');
  373. if (indx >= 0)
  374. {
  375. ++indx;
  376. tmpSlice.SubUTF8Self(indx);
  377. tmpSlice.SelfTrimStart((byte)' ');
  378. tmpSlice.SelfTrimStart((byte)'+');
  379. indx2 = 0;
  380. tmp = 0;
  381. while (indx2 < tmpSlice.Length)
  382. {
  383. byte c = tmpSlice[indx2];
  384. if (c < (byte)'0' || c > (byte)'9')
  385. break;
  386. tmp *= 10;
  387. tmp += c - (byte)'0';
  388. ++indx2;
  389. }
  390. if (indx2 == 0)
  391. {
  392. Flags = URIFlags.None;
  393. return;
  394. }
  395. Y = tmp;
  396. tmpSlice.SubUTF8Self(indx2);
  397. indx = tmpSlice.IndexOf((byte)',');
  398. if (indx >= 0)
  399. {
  400. ++indx;
  401. tmpSlice.SubUTF8Self(indx);
  402. tmpSlice.SelfTrimStart((byte)' ');
  403. tmpSlice.SelfTrimStart((byte)'+');
  404. indx2 = 0;
  405. tmp = 0;
  406. int sig = 1;
  407. if (tmpSlice[indx2] == (byte)'-')
  408. {
  409. sig = -1;
  410. indx2++;
  411. }
  412. while (indx2 < tmpSlice.Length)
  413. {
  414. byte c = tmpSlice[indx2];
  415. if (c < (byte)'0' || c > (byte)'9')
  416. break;
  417. tmp *= 10;
  418. tmp += c - (byte)'0';
  419. ++indx2;
  420. }
  421. if (indx2 == 0)
  422. {
  423. Flags = URIFlags.None;
  424. return;
  425. }
  426. Z = sig * tmp;
  427. }
  428. }
  429. }
  430. }
  431. return;
  432. }
  433. public bool ResolveDNS()
  434. {
  435. if ((Flags & URIFlags.HasHost) != 0)
  436. {
  437. IPAddress ip = Util.GetHostFromDNS(Host);
  438. if (ip != null)
  439. {
  440. IP = ip;
  441. Flags |= URIFlags.HasResolvedHost;
  442. return true;
  443. }
  444. }
  445. return false;
  446. }
  447. public bool IsValid
  448. {
  449. get { return (Flags & (URIFlags.HasHost | URIFlags.HasRegionName)) != 0; }
  450. }
  451. public bool HasHost
  452. {
  453. get { return (Flags & URIFlags.HasHost) != 0; }
  454. }
  455. public bool HasRegionName
  456. {
  457. get { return (Flags & URIFlags.HasRegionName) != 0; }
  458. }
  459. public string HostUrl
  460. {
  461. get { return (Flags & URIFlags.HasHost) != 0 ? (Schema + Host + ":" + Port) : ""; }
  462. }
  463. public string HostUrlEndSlash
  464. {
  465. get { return (Flags & URIFlags.HasHost) != 0 ? (Schema + Host + ":" + Port + "/") : ""; }
  466. }
  467. public string RegionUrlAndName
  468. {
  469. get
  470. {
  471. string ret = (Flags & URIFlags.HasHost) != 0 ? (Schema + Host + ":" + Port + "/") : "";
  472. if ((Flags & URIFlags.HasRegionName) != 0)
  473. ret += RegionName;
  474. return ret;
  475. }
  476. }
  477. public string RegionHostPortSpaceName
  478. {
  479. get
  480. {
  481. string ret = (Flags & URIFlags.HasHost) != 0 ? (Host + ":" + Port + "/ ") : ""; // space needed for compatibility
  482. if ((Flags & URIFlags.HasRegionName) != 0)
  483. ret += RegionName;
  484. return ret;
  485. }
  486. }
  487. // this needs to be set before get
  488. public bool IsLocalGrid
  489. {
  490. get { return (Flags & URIFlags.IsLocalGrid) != 0; }
  491. set
  492. {
  493. if(value)
  494. {
  495. Host = string.Empty;
  496. Flags &= ~URIFlags.HasHost;
  497. Flags |= URIFlags.IsLocalGrid;
  498. }
  499. }
  500. }
  501. }
  502. }