This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
UrlPrefixCollection.cs
162 lines (145 loc) · 4.17 KB
/
UrlPrefixCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Server.HttpSys
{
/// <summary>
/// A collection or URL prefixes
/// </summary>
public class UrlPrefixCollection : ICollection<UrlPrefix>
{
private readonly IDictionary<int, UrlPrefix> _prefixes = new Dictionary<int, UrlPrefix>(1);
private UrlGroup _urlGroup;
private int _nextId = 1;
internal UrlPrefixCollection()
{
}
public int Count
{
get
{
lock (_prefixes)
{
return _prefixes.Count;
}
}
}
public bool IsReadOnly
{
get { return false; }
}
public void Add(string prefix)
{
Add(UrlPrefix.Create(prefix));
}
public void Add(UrlPrefix item)
{
lock (_prefixes)
{
var id = _nextId++;
if (_urlGroup != null)
{
_urlGroup.RegisterPrefix(item.FullPrefix, id);
}
_prefixes.Add(id, item);
}
}
internal UrlPrefix GetPrefix(int id)
{
lock (_prefixes)
{
return _prefixes[id];
}
}
public void Clear()
{
lock (_prefixes)
{
if (_urlGroup != null)
{
UnregisterAllPrefixes();
}
_prefixes.Clear();
}
}
public bool Contains(UrlPrefix item)
{
lock (_prefixes)
{
return _prefixes.Values.Contains(item);
}
}
public void CopyTo(UrlPrefix[] array, int arrayIndex)
{
lock (_prefixes)
{
_prefixes.Values.CopyTo(array, arrayIndex);
}
}
public bool Remove(string prefix)
{
return Remove(UrlPrefix.Create(prefix));
}
public bool Remove(UrlPrefix item)
{
lock (_prefixes)
{
int? id = null;
foreach (var pair in _prefixes)
{
if (pair.Value.Equals(item))
{
id = pair.Key;
if (_urlGroup != null)
{
_urlGroup.UnregisterPrefix(pair.Value.FullPrefix);
}
}
}
if (id.HasValue)
{
_prefixes.Remove(id.Value);
return true;
}
return false;
}
}
public IEnumerator<UrlPrefix> GetEnumerator()
{
lock (_prefixes)
{
return _prefixes.Values.GetEnumerator();
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
internal void RegisterAllPrefixes(UrlGroup urlGroup)
{
lock (_prefixes)
{
_urlGroup = urlGroup;
// go through the uri list and register for each one of them
foreach (var pair in _prefixes)
{
// We'll get this index back on each request and use it to look up the prefix to calculate PathBase.
_urlGroup.RegisterPrefix(pair.Value.FullPrefix, pair.Key);
}
}
}
internal void UnregisterAllPrefixes()
{
lock (_prefixes)
{
// go through the uri list and unregister for each one of them
foreach (var prefix in _prefixes.Values)
{
// ignore possible failures
_urlGroup.UnregisterPrefix(prefix.FullPrefix);
}
}
}
}
}