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
|
|
|
|
{
|
|
|
|
List<string> prefixes = new List<string>();
|
|
|
|
HttpListener listener;
|
|
|
|
|
|
|
|
private ILogger _logger;
|
|
|
|
|
|
|
|
internal HttpListenerPrefixCollection(ILogger logger, HttpListener listener)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +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)
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
2018-09-12 17:26:21 +00:00
|
|
|
//ListenerPrefix.CheckUri(uriPrefix);
|
2016-11-11 19:55:12 +00:00
|
|
|
if (prefixes.Contains(uriPrefix))
|
|
|
|
return;
|
|
|
|
|
|
|
|
prefixes.Add(uriPrefix);
|
|
|
|
if (listener.IsListening)
|
2018-09-12 17:26:21 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
|
|
|
//ListenerPrefix.CheckUri(uriPrefix);
|
|
|
|
foreach (var uriPrefix in uriPrefixes)
|
|
|
|
{
|
|
|
|
if (prefixes.Contains(uriPrefix))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixes.Add(uriPrefix);
|
|
|
|
if (listener.IsListening)
|
|
|
|
{
|
|
|
|
HttpEndPointManager.AddPrefix(_logger, uriPrefix, listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
|
|
|
prefixes.Clear();
|
|
|
|
if (listener.IsListening)
|
2018-09-12 17:26:21 +00:00
|
|
|
HttpEndPointManager.RemoveListener(_logger, listener);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Contains(string uriPrefix)
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
|
|
|
return prefixes.Contains(uriPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(string[] array, int offset)
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
|
|
|
prefixes.CopyTo(array, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(Array array, int offset)
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
|
|
|
((ICollection)prefixes).CopyTo(array, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator<string> GetEnumerator()
|
|
|
|
{
|
|
|
|
return prefixes.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
return prefixes.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Remove(string uriPrefix)
|
|
|
|
{
|
|
|
|
listener.CheckDisposed();
|
|
|
|
if (uriPrefix == null)
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(uriPrefix));
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
bool result = prefixes.Remove(uriPrefix);
|
|
|
|
if (result && listener.IsListening)
|
2018-09-12 17:26:21 +00:00
|
|
|
HttpEndPointManager.RemovePrefix(_logger, uriPrefix, listener);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|