diff options
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r-- | OpenSim/Framework/Util.cs | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index a6fd99f..b622523 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -414,6 +414,140 @@ namespace OpenSim.Framework | |||
414 | return regionCoord << 8; | 414 | return regionCoord << 8; |
415 | } | 415 | } |
416 | 416 | ||
417 | public static bool checkServiceURI(string uristr, out string serviceURI) | ||
418 | { | ||
419 | serviceURI = string.Empty; | ||
420 | try | ||
421 | { | ||
422 | Uri uri = new Uri(uristr); | ||
423 | serviceURI = uri.AbsoluteUri; | ||
424 | if(uri.Port == 80) | ||
425 | serviceURI = serviceURI.Trim(new char[] { '/', ' ' }) +":80/"; | ||
426 | else if(uri.Port == 443) | ||
427 | serviceURI = serviceURI.Trim(new char[] { '/', ' ' }) +":443/"; | ||
428 | return true; | ||
429 | } | ||
430 | catch | ||
431 | { | ||
432 | serviceURI = string.Empty; | ||
433 | } | ||
434 | return false; | ||
435 | } | ||
436 | |||
437 | public static bool buildHGRegionURI(string inputName, out string serverURI, out string regionName) | ||
438 | { | ||
439 | serverURI = string.Empty; | ||
440 | regionName = string.Empty; | ||
441 | |||
442 | inputName = inputName.Trim(); | ||
443 | |||
444 | if (!inputName.StartsWith("http") && !inputName.StartsWith("https")) | ||
445 | { | ||
446 | // Formats: grid.example.com:8002:region name | ||
447 | // grid.example.com:region name | ||
448 | // grid.example.com:8002 | ||
449 | // grid.example.com | ||
450 | |||
451 | string host; | ||
452 | uint port = 80; | ||
453 | |||
454 | string[] parts = inputName.Split(new char[] { ':' }); | ||
455 | int indx; | ||
456 | if(parts.Length == 0) | ||
457 | return false; | ||
458 | if (parts.Length == 1) | ||
459 | { | ||
460 | indx = inputName.IndexOf('/'); | ||
461 | if (indx < 0) | ||
462 | serverURI = "http://"+ inputName + "/"; | ||
463 | else | ||
464 | { | ||
465 | serverURI = "http://"+ inputName.Substring(0,indx + 1); | ||
466 | if(indx + 2 < inputName.Length) | ||
467 | regionName = inputName.Substring(indx + 1); | ||
468 | } | ||
469 | } | ||
470 | else | ||
471 | { | ||
472 | host = parts[0]; | ||
473 | |||
474 | if (parts.Length >= 2) | ||
475 | { | ||
476 | indx = parts[1].IndexOf('/'); | ||
477 | if(indx < 0) | ||
478 | { | ||
479 | // If it's a number then assume it's a port. Otherwise, it's a region name. | ||
480 | if (!UInt32.TryParse(parts[1], out port)) | ||
481 | { | ||
482 | port = 80; | ||
483 | regionName = parts[1]; | ||
484 | } | ||
485 | } | ||
486 | else | ||
487 | { | ||
488 | string portstr = parts[1].Substring(0, indx); | ||
489 | if(indx + 2 < parts[1].Length) | ||
490 | regionName = parts[1].Substring(indx + 1); | ||
491 | if (!UInt32.TryParse(portstr, out port)) | ||
492 | port = 80; | ||
493 | } | ||
494 | } | ||
495 | // always take the last one | ||
496 | if (parts.Length >= 3) | ||
497 | { | ||
498 | regionName = parts[2]; | ||
499 | } | ||
500 | |||
501 | serverURI = "http://"+ host +":"+ port.ToString() + "/"; | ||
502 | } | ||
503 | } | ||
504 | else | ||
505 | { | ||
506 | // Formats: http://grid.example.com region name | ||
507 | // http://grid.example.com "region name" | ||
508 | // http://grid.example.com | ||
509 | |||
510 | string[] parts = inputName.Split(new char[] { ' ' }); | ||
511 | |||
512 | if (parts.Length == 0) | ||
513 | return false; | ||
514 | |||
515 | serverURI = parts[0]; | ||
516 | |||
517 | int indx = serverURI.LastIndexOf('/'); | ||
518 | if(indx > 10) | ||
519 | { | ||
520 | if(indx + 2 < inputName.Length) | ||
521 | regionName = inputName.Substring(indx + 1); | ||
522 | serverURI = inputName.Substring(0, indx + 1); | ||
523 | } | ||
524 | else if (parts.Length >= 2) | ||
525 | { | ||
526 | regionName = inputName.Substring(serverURI.Length); | ||
527 | } | ||
528 | } | ||
529 | |||
530 | // use better code for sanity check | ||
531 | Uri uri; | ||
532 | try | ||
533 | { | ||
534 | uri = new Uri(serverURI); | ||
535 | } | ||
536 | catch | ||
537 | { | ||
538 | return false; | ||
539 | } | ||
540 | |||
541 | if(!string.IsNullOrEmpty(regionName)) | ||
542 | regionName = regionName.Trim(new char[] { '"', ' ' }); | ||
543 | serverURI = uri.AbsoluteUri; | ||
544 | if(uri.Port == 80) | ||
545 | serverURI = serverURI.Trim(new char[] { '/', ' ' }) +":80/"; | ||
546 | else if(uri.Port == 443) | ||
547 | serverURI = serverURI.Trim(new char[] { '/', ' ' }) +":443/"; | ||
548 | return true; | ||
549 | } | ||
550 | |||
417 | public static T Clamp<T>(T x, T min, T max) | 551 | public static T Clamp<T>(T x, T min, T max) |
418 | where T : IComparable<T> | 552 | where T : IComparable<T> |
419 | { | 553 | { |