2016-11-11 19:55:12 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
namespace SocketHttpListener.Net
|
|
|
|
{
|
|
|
|
public class HttpListenerPrefixCollection : ICollection<string>, IEnumerable<string>, IEnumerable
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
private List<string> _prefixes = new List<string>();
|
|
|
|
private HttpListener _listener;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
private ILogger _logger;
|
|
|
|
|
|
|
|
internal HttpListenerPrefixCollection(ILogger logger, HttpListener listener)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener = listener;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 14:45:36 +00:00
|
|
|
public int Count => _prefixes.Count;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public bool IsReadOnly => false;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public bool IsSynchronized => false;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
public void Add(string uriPrefix)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
2018-09-12 17:26:21 +00:00
|
|
|
//ListenerPrefix.CheckUri(uriPrefix);
|
2019-02-09 14:45:36 +00:00
|
|
|
if (_prefixes.Contains(uriPrefix))
|
|
|
|
{
|
2016-11-11 19:55:12 +00:00
|
|
|
return;
|
2019-02-09 14:45:36 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-02-09 14:45:36 +00:00
|
|
|
_prefixes.Add(uriPrefix);
|
|
|
|
if (_listener.IsListening)
|
|
|
|
{
|
|
|
|
HttpEndPointManager.AddPrefix(_logger, uriPrefix, _listener);
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 14:39:17 +00:00
|
|
|
public void AddRange(IEnumerable<string> uriPrefixes)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
|
|
|
|
2019-02-09 14:39:17 +00:00
|
|
|
foreach (var uriPrefix in uriPrefixes)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
if (_prefixes.Contains(uriPrefix))
|
2019-02-09 14:39:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:45:36 +00:00
|
|
|
_prefixes.Add(uriPrefix);
|
|
|
|
if (_listener.IsListening)
|
2019-02-09 14:39:17 +00:00
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
HttpEndPointManager.AddPrefix(_logger, uriPrefix, _listener);
|
2019-02-09 14:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
public void Clear()
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
|
|
|
_prefixes.Clear();
|
|
|
|
if (_listener.IsListening)
|
|
|
|
{
|
|
|
|
HttpEndPointManager.RemoveListener(_logger, _listener);
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Contains(string uriPrefix)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
|
|
|
return _prefixes.Contains(uriPrefix);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(string[] array, int offset)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
|
|
|
_prefixes.CopyTo(array, offset);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(Array array, int offset)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
|
|
|
((ICollection)_prefixes).CopyTo(array, offset);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator<string> GetEnumerator()
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
return _prefixes.GetEnumerator();
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
return _prefixes.GetEnumerator();
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Remove(string uriPrefix)
|
|
|
|
{
|
2019-02-09 14:45:36 +00:00
|
|
|
_listener.CheckDisposed();
|
2016-11-11 19:55:12 +00:00
|
|
|
if (uriPrefix == null)
|
2019-02-09 14:45:36 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(uriPrefix));
|
2019-02-09 14:45:36 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-02-09 14:45:36 +00:00
|
|
|
bool result = _prefixes.Remove(uriPrefix);
|
|
|
|
if (result && _listener.IsListening)
|
|
|
|
{
|
|
|
|
HttpEndPointManager.RemovePrefix(_logger, uriPrefix, _listener);
|
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|