Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HeaderUtilities.cs
Go to the documentation of this file.
4using System.Text;
5
7
8internal static class HeaderUtilities
9{
11
13
15
16 internal static void SetQuality(ObjectCollection<NameValueHeaderValue> parameters, double? value)
17 {
19 if (value.HasValue)
20 {
22 {
23 throw new ArgumentOutOfRangeException("value");
24 }
25 string value2 = value.Value.ToString("0.0##", NumberFormatInfo.InvariantInfo);
26 if (nameValueHeaderValue != null)
27 {
28 nameValueHeaderValue.Value = value2;
29 }
30 else
31 {
32 parameters.Add(new NameValueHeaderValue("q", value2));
33 }
34 }
35 else if (nameValueHeaderValue != null)
36 {
37 parameters.Remove(nameValueHeaderValue);
38 }
39 }
40
41 internal static bool ContainsNonAscii(string input)
42 {
43 foreach (char c in input)
44 {
45 if (c > '\u007f')
46 {
47 return true;
48 }
49 }
50 return false;
51 }
52
53 internal static string Encode5987(string input)
54 {
56 byte[] array = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetMaxByteCount(input.Length));
57 int bytes = Encoding.UTF8.GetBytes(input, 0, input.Length, array, 0);
58 stringBuilder.Append("utf-8''");
59 for (int i = 0; i < bytes; i++)
60 {
61 byte b = array[i];
62 if (b > 127)
63 {
65 }
66 else if (!HttpRuleParser.IsTokenChar((char)b) || b == 42 || b == 39 || b == 37)
67 {
69 }
70 else
71 {
72 stringBuilder.Append((char)b);
73 }
74 }
76 ArrayPool<byte>.Shared.Return(array);
78 }
79
80 private static void AddHexEscaped(byte c, StringBuilder destination)
81 {
82 destination.Append('%');
85 }
86
87 internal static double? GetQuality(ObjectCollection<NameValueHeaderValue> parameters)
88 {
90 if (nameValueHeaderValue != null)
91 {
92 double result = 0.0;
93 if (double.TryParse(nameValueHeaderValue.Value, NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out result))
94 {
95 return result;
96 }
97 if (System.Net.NetEventSource.Log.IsEnabled())
98 {
100 }
101 }
102 return null;
103 }
104
105 internal static void CheckValidToken(string value, string parameterName)
106 {
107 if (string.IsNullOrEmpty(value))
108 {
110 }
111 if (HttpRuleParser.GetTokenLength(value, 0) != value.Length)
112 {
114 }
115 }
116
117 internal static void CheckValidComment(string value, string parameterName)
118 {
119 if (string.IsNullOrEmpty(value))
120 {
122 }
123 int length = 0;
124 if (HttpRuleParser.GetCommentLength(value, 0, out length) != 0 || length != value.Length)
125 {
127 }
128 }
129
130 internal static void CheckValidQuotedString(string value, string parameterName)
131 {
132 if (string.IsNullOrEmpty(value))
133 {
135 }
136 int length = 0;
138 {
140 }
141 }
142
144 {
145 return AreEqualCollections(x, y, null);
146 }
147
149 {
150 if (x == null)
151 {
152 if (y != null)
153 {
154 return y.Count == 0;
155 }
156 return true;
157 }
158 if (y == null)
159 {
160 return x.Count == 0;
161 }
162 if (x.Count != y.Count)
163 {
164 return false;
165 }
166 if (x.Count == 0)
167 {
168 return true;
169 }
170 bool[] array = new bool[x.Count];
171 int num = 0;
172 foreach (T item in x)
173 {
174 num = 0;
175 bool flag = false;
176 foreach (T item2 in y)
177 {
178 if (!array[num] && ((comparer == null && item.Equals(item2)) || (comparer != null && comparer.Equals(item, item2))))
179 {
180 array[num] = true;
181 flag = true;
182 break;
183 }
184 num++;
185 }
186 if (!flag)
187 {
188 return false;
189 }
190 }
191 return true;
192 }
193
195 {
196 separatorFound = false;
198 if (num == input.Length || input[num] != ',')
199 {
200 return num;
201 }
202 separatorFound = true;
203 num++;
205 if (skipEmptyValues)
206 {
207 while (num < input.Length && input[num] == ',')
208 {
209 num++;
211 }
212 }
213 return num;
214 }
215
217 {
218 object parsedValues = store.GetParsedValues(descriptor);
219 if (parsedValues != null)
220 {
222 }
223 if (defaultValue.HasValue && store.Contains(descriptor))
224 {
225 return defaultValue;
226 }
227 return null;
228 }
229
231 {
232 object parsedValues = store.GetParsedValues(descriptor);
233 if (parsedValues != null)
234 {
235 return (TimeSpan)parsedValues;
236 }
237 return null;
238 }
239
240 internal static bool TryParseInt32(string value, out int result)
241 {
242 return int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out result);
243 }
244
245 internal static bool TryParseInt32(string value, int offset, int length, out int result)
246 {
247 if (offset < 0 || length < 0 || offset > value.Length - length)
248 {
249 result = 0;
250 return false;
251 }
252 return int.TryParse(value.AsSpan(offset, length), NumberStyles.None, CultureInfo.InvariantCulture, out result);
253 }
254
255 internal static bool TryParseInt64(string value, int offset, int length, out long result)
256 {
257 if (offset < 0 || length < 0 || offset > value.Length - length)
258 {
259 result = 0L;
260 return false;
261 }
262 return long.TryParse(value.AsSpan(offset, length), NumberStyles.None, CultureInfo.InvariantCulture, out result);
263 }
264
266 {
267 sb.AppendLine("{");
268 foreach (HttpHeaders httpHeaders in headers)
269 {
270 if (httpHeaders == null)
271 {
272 continue;
273 }
275 {
276 foreach (string item2 in item.Value)
277 {
278 sb.Append(" ");
279 sb.Append(item.Key);
280 sb.Append(": ");
281 sb.AppendLine(item2);
282 }
283 }
284 }
285 sb.Append('}');
286 }
287
289 {
290 CheckValidToken(value, "item");
291 }
292
306}
static unsafe void Clear(Array array)
Definition Array.cs:755
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
static CultureInfo InvariantCulture
static char ToCharUpper(int value)
static ? TimeSpan GetTimeSpanValue(HeaderDescriptor descriptor, HttpHeaders store)
static void AddHexEscaped(byte c, StringBuilder destination)
static void SetQuality(ObjectCollection< NameValueHeaderValue > parameters, double? value)
static bool AreEqualCollections< T >(ObjectCollection< T > x, ObjectCollection< T > y)
static readonly NameValueWithParametersHeaderValue ExpectContinue
static void ValidateToken(HttpHeaderValueCollection< string > collection, string value)
static void CheckValidComment(string value, string parameterName)
static bool TryParseInt32(string value, out int result)
static readonly Action< HttpHeaderValueCollection< string >, string > TokenValidator
static ? DateTimeOffset GetDateTimeOffsetValue(HeaderDescriptor descriptor, HttpHeaders store, DateTimeOffset? defaultValue=null)
static readonly TransferCodingHeaderValue TransferEncodingChunked
static void CheckValidQuotedString(string value, string parameterName)
static ObjectCollection< NameValueHeaderValue > Clone(this ObjectCollection< NameValueHeaderValue > source)
static bool TryParseInt32(string value, int offset, int length, out int result)
static bool ContainsNonAscii(string input)
static ? double GetQuality(ObjectCollection< NameValueHeaderValue > parameters)
static string Encode5987(string input)
static void DumpHeaders(StringBuilder sb, params HttpHeaders[] headers)
static bool TryParseInt64(string value, int offset, int length, out long result)
static void CheckValidToken(string value, string parameterName)
static int GetNextNonEmptyOrWhitespaceIndex(string input, int startIndex, bool skipEmptyValues, out bool separatorFound)
static NameValueHeaderValue Find(ObjectCollection< NameValueHeaderValue > values, string name)
static HttpParseResult GetCommentLength(string input, int startIndex, out int length)
static HttpParseResult GetQuotedStringLength(string input, int startIndex, out int length)
static int GetTokenLength(string input, int startIndex)
static int GetWhitespaceLength(string input, int startIndex)
static bool IsTokenChar(char character)
static readonly System.Net.NetEventSource Log
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_http_headers_invalid_value
Definition SR.cs:26
static string net_http_log_headers_invalid_quality
Definition SR.cs:78
static string net_http_argument_empty_string
Definition SR.cs:52
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
static string GetStringAndRelease(StringBuilder sb)
static StringBuilder Acquire(int capacity=16)
StringBuilder Append(char value, int repeatCount)