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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
[Serializable]
class ContextDictionary : IDictionary<string, string>
{
static ContextDictionary empty;
IDictionary<string, string> dictionaryStore;
public ContextDictionary()
{
this.dictionaryStore = new Dictionary<string, string>();
}
public ContextDictionary(IDictionary<string, string> context)
{
this.dictionaryStore = new Dictionary<string, string>();
if (context != null)
{
bool ignoreValidation = context is ContextDictionary;
foreach (KeyValuePair<string, string> pair in context)
{
if (ignoreValidation)
{
this.dictionaryStore.Add(pair);
}
else
{
this.Add(pair);
}
}
}
}
internal static ContextDictionary Empty
{
get
{
if (empty == null)
{
ContextDictionary localEmpty = new ContextDictionary();
localEmpty.dictionaryStore = new ReadOnlyDictionaryInternal<string, string>(new Dictionary<string, string>(0));
empty = localEmpty;
}
return empty;
}
}
public int Count
{
get { return this.dictionaryStore.Count; }
}
public bool IsReadOnly
{
get { return this.dictionaryStore.IsReadOnly; }
}
public ICollection<string> Keys
{
get { return this.dictionaryStore.Keys; }
}
public ICollection<string> Values
{
get { return this.dictionaryStore.Values; }
}
public string this[string key]
{
get
{
ValidateKeyValueSpace(key);
return this.dictionaryStore[key];
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
ValidateKeyValueSpace(key);
this.dictionaryStore[key] = value;
}
}
public void Add(string key, string value)
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
ValidateKeyValueSpace(key);
this.dictionaryStore.Add(key, value);
}
public void Add(KeyValuePair<string, string> item)
{
if (item.Key == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item.Key");
}
if (item.Value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item.Value");
}
ValidateKeyValueSpace(item.Key);
this.dictionaryStore.Add(item);
}
public void Clear()
{
this.dictionaryStore.Clear();
}
public bool Contains(KeyValuePair<string, string> item)
{
if (item.Key == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item.Key");
}
if (item.Value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item.Value");
}
ValidateKeyValueSpace(item.Key);
return this.dictionaryStore.Contains(item);
}
public bool ContainsKey(string key)
{
ValidateKeyValueSpace(key);
return this.dictionaryStore.ContainsKey(key);
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
this.dictionaryStore.CopyTo(array, arrayIndex);
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return this.dictionaryStore.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this.dictionaryStore).GetEnumerator();
}
public bool Remove(string key)
{
ValidateKeyValueSpace(key);
return this.dictionaryStore.Remove(key);
}
public bool Remove(KeyValuePair<string, string> item)
{
if (item.Key == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item.Key");
}
if (item.Value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item.Value");
}
ValidateKeyValueSpace(item.Key);
return this.dictionaryStore.Remove(item);
}
public bool TryGetValue(string key, out string value)
{
ValidateKeyValueSpace(key);
return this.dictionaryStore.TryGetValue(key, out value);
}
internal static bool TryValidateKeyValueSpace(string key)
{
if (string.IsNullOrEmpty(key))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key");
}
for (int counter = 0; counter < key.Length; ++counter)
{
char currentCharacter = key[counter];
if (!IsLetterOrDigit(currentCharacter) && currentCharacter != '-' && currentCharacter != '_' && currentCharacter != '.')
{
return false;
}
}
return true;
}
static bool IsLetterOrDigit(char c)
{
return (('A' <= c) && (c <= 'Z')) ||
(('a' <= c) && (c <= 'z')) ||
(('0' <= c) && (c <= '9'));
}
static void ValidateKeyValueSpace(string key)
{
if (!TryValidateKeyValueSpace(key))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("key", SR.GetString(SR.InvalidCookieContent, key)));
}
}
}
}
|