ListenerPrefix.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Net;
  3. using MediaBrowser.Model.Net;
  4. namespace SocketHttpListener.Net
  5. {
  6. internal sealed class ListenerPrefix
  7. {
  8. private string _original;
  9. private string _host;
  10. private ushort _port;
  11. private string _path;
  12. private bool _secure;
  13. private IPAddress[] _addresses;
  14. internal HttpListener _listener;
  15. public ListenerPrefix(string prefix)
  16. {
  17. _original = prefix;
  18. Parse(prefix);
  19. }
  20. public override string ToString()
  21. {
  22. return _original;
  23. }
  24. public IPAddress[] Addresses
  25. {
  26. get { return _addresses; }
  27. set { _addresses = value; }
  28. }
  29. public bool Secure
  30. {
  31. get { return _secure; }
  32. }
  33. public string Host
  34. {
  35. get { return _host; }
  36. }
  37. public int Port
  38. {
  39. get { return _port; }
  40. }
  41. public string Path
  42. {
  43. get { return _path; }
  44. }
  45. // Equals and GetHashCode are required to detect duplicates in HttpListenerPrefixCollection.
  46. public override bool Equals(object o)
  47. {
  48. ListenerPrefix other = o as ListenerPrefix;
  49. if (other == null)
  50. return false;
  51. return (_original == other._original);
  52. }
  53. public override int GetHashCode()
  54. {
  55. return _original.GetHashCode();
  56. }
  57. private void Parse(string uri)
  58. {
  59. ushort default_port = 80;
  60. if (uri.StartsWith("https://"))
  61. {
  62. default_port = 443;
  63. _secure = true;
  64. }
  65. int length = uri.Length;
  66. int start_host = uri.IndexOf(':') + 3;
  67. if (start_host >= length)
  68. throw new ArgumentException("net_listener_host");
  69. int colon = uri.IndexOf(':', start_host, length - start_host);
  70. int root;
  71. if (colon > 0)
  72. {
  73. _host = uri.Substring(start_host, colon - start_host);
  74. root = uri.IndexOf('/', colon, length - colon);
  75. _port = (ushort)int.Parse(uri.Substring(colon + 1, root - colon - 1));
  76. _path = uri.Substring(root);
  77. }
  78. else
  79. {
  80. root = uri.IndexOf('/', start_host, length - start_host);
  81. _host = uri.Substring(start_host, root - start_host);
  82. _port = default_port;
  83. _path = uri.Substring(root);
  84. }
  85. if (_path.Length != 1)
  86. _path = _path.Substring(0, _path.Length - 1);
  87. }
  88. }
  89. }