Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WarningHeaderValue.cs
Go to the documentation of this file.
3using System.Text;
4
6
8{
9 private readonly int _code;
10
11 private readonly string _agent;
12
13 private readonly string _text;
14
15 private readonly DateTimeOffset? _date;
16
17 public int Code => _code;
18
19 public string Agent => _agent;
20
21 public string Text => _text;
22
24
25 public WarningHeaderValue(int code, string agent, string text)
26 {
27 CheckCode(code);
28 CheckAgent(agent);
30 _code = code;
31 _agent = agent;
32 _text = text;
33 }
34
35 public WarningHeaderValue(int code, string agent, string text, DateTimeOffset date)
36 {
37 CheckCode(code);
38 CheckAgent(agent);
40 _code = code;
41 _agent = agent;
42 _text = text;
43 _date = date;
44 }
45
47 {
48 _code = source._code;
49 _agent = source._agent;
50 _text = source._text;
51 _date = source._date;
52 }
53
54 public override string ToString()
55 {
57 StringBuilder stringBuilder2 = stringBuilder;
60 handler.AppendFormatted(_code, "000");
61 stringBuilder2.Append(invariantInfo, ref handler);
62 stringBuilder.Append(' ');
63 stringBuilder.Append(_agent);
64 stringBuilder.Append(' ');
65 stringBuilder.Append(_text);
66 if (_date.HasValue)
67 {
68 stringBuilder.Append(" \"");
69 stringBuilder.Append(HttpDateParser.DateToString(_date.Value));
70 stringBuilder.Append('"');
71 }
73 }
74
75 public override bool Equals([NotNullWhen(true)] object? obj)
76 {
77 if (!(obj is WarningHeaderValue warningHeaderValue))
78 {
79 return false;
80 }
81 if (_code != warningHeaderValue._code || !string.Equals(_agent, warningHeaderValue._agent, StringComparison.OrdinalIgnoreCase) || !string.Equals(_text, warningHeaderValue._text, StringComparison.Ordinal))
82 {
83 return false;
84 }
85 if (_date.HasValue)
86 {
87 if (warningHeaderValue._date.HasValue)
88 {
89 return _date.Value == warningHeaderValue._date.Value;
90 }
91 return false;
92 }
93 return !warningHeaderValue._date.HasValue;
94 }
95
96 public override int GetHashCode()
97 {
98 int num = _code.GetHashCode() ^ StringComparer.OrdinalIgnoreCase.GetHashCode(_agent) ^ _text.GetHashCode();
99 if (_date.HasValue)
100 {
101 num ^= _date.Value.GetHashCode();
102 }
103 return num;
104 }
105
106 public static WarningHeaderValue Parse(string? input)
107 {
108 int index = 0;
110 }
111
112 public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out WarningHeaderValue? parsedValue)
113 {
114 int index = 0;
115 parsedValue = null;
116 if (GenericHeaderParser.SingleValueWarningParser.TryParseValue(input, null, ref index, out var parsedValue2))
117 {
118 parsedValue = (WarningHeaderValue)parsedValue2;
119 return true;
120 }
121 return false;
122 }
123
124 internal static int GetWarningLength(string input, int startIndex, out object parsedValue)
125 {
126 parsedValue = null;
127 if (string.IsNullOrEmpty(input) || startIndex >= input.Length)
128 {
129 return 0;
130 }
131 int current = startIndex;
132 if (!TryReadCode(input, ref current, out var code))
133 {
134 return 0;
135 }
136 if (!TryReadAgent(input, current, ref current, out var agent))
137 {
138 return 0;
139 }
140 int length = 0;
141 int startIndex2 = current;
142 if (HttpRuleParser.GetQuotedStringLength(input, current, out length) != 0)
143 {
144 return 0;
145 }
146 string text = input.Substring(startIndex2, length);
147 current += length;
148 DateTimeOffset? date = null;
149 if (!TryReadDate(input, ref current, out date))
150 {
151 return 0;
152 }
153 parsedValue = ((!date.HasValue) ? new WarningHeaderValue(code, agent, text) : new WarningHeaderValue(code, agent, text, date.Value));
154 return current - startIndex;
155 }
156
157 private static bool TryReadAgent(string input, int startIndex, ref int current, [NotNullWhen(true)] out string agent)
158 {
159 int hostLength = HttpRuleParser.GetHostLength(input, startIndex, allowToken: true, out agent);
160 if (hostLength == 0)
161 {
162 return false;
163 }
164 current += hostLength;
165 int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);
166 current += whitespaceLength;
167 if (whitespaceLength == 0 || current == input.Length)
168 {
169 return false;
170 }
171 return true;
172 }
173
174 private static bool TryReadCode(string input, ref int current, out int code)
175 {
176 code = 0;
177 int numberLength = HttpRuleParser.GetNumberLength(input, current, allowDecimal: false);
178 if (numberLength == 0 || numberLength > 3)
179 {
180 return false;
181 }
182 if (!HeaderUtilities.TryParseInt32(input, current, numberLength, out code))
183 {
184 return false;
185 }
186 current += numberLength;
187 int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);
188 current += whitespaceLength;
189 if (whitespaceLength == 0 || current == input.Length)
190 {
191 return false;
192 }
193 return true;
194 }
195
196 private static bool TryReadDate(string input, ref int current, out DateTimeOffset? date)
197 {
198 date = null;
199 int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);
200 current += whitespaceLength;
201 if (current < input.Length && input[current] == '"')
202 {
203 if (whitespaceLength == 0)
204 {
205 return false;
206 }
207 current++;
208 int num = current;
209 while (current < input.Length && input[current] != '"')
210 {
211 current++;
212 }
213 if (current == input.Length || current == num)
214 {
215 return false;
216 }
217 if (!HttpDateParser.TryParse(input.AsSpan(num, current - num), out var result))
218 {
219 return false;
220 }
221 date = result;
222 current++;
223 current += HttpRuleParser.GetWhitespaceLength(input, current);
224 }
225 return true;
226 }
227
229 {
230 return new WarningHeaderValue(this);
231 }
232
233 private static void CheckCode(int code)
234 {
235 if (code < 0 || code > 999)
236 {
237 throw new ArgumentOutOfRangeException("code");
238 }
239 }
240
241 private static void CheckAgent(string agent)
242 {
243 if (string.IsNullOrEmpty(agent))
244 {
246 }
247 if (HttpRuleParser.GetHostLength(agent, 0, allowToken: true, out var _) != agent.Length)
248 {
250 }
251 }
252}
static CultureInfo InvariantCulture
static string DateToString(DateTimeOffset dateTime)
static bool TryParse(ReadOnlySpan< char > input, out DateTimeOffset result)
static readonly GenericHeaderParser SingleValueWarningParser
static bool TryParseInt32(string value, out int result)
static void CheckValidQuotedString(string value, string parameterName)
static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out WarningHeaderValue? parsedValue)
static bool TryReadCode(string input, ref int current, out int code)
static int GetWarningLength(string input, int startIndex, out object parsedValue)
static bool TryReadDate(string input, ref int current, out DateTimeOffset? date)
WarningHeaderValue(int code, string agent, string text, DateTimeOffset date)
static WarningHeaderValue Parse(string? input)
WarningHeaderValue(int code, string agent, string text)
static bool TryReadAgent(string input, int startIndex, ref int current, [NotNullWhen(true)] out string agent)
override bool Equals([NotNullWhen(true)] object? obj)
static int GetHostLength(string input, int startIndex, bool allowToken, out string host)
static HttpParseResult GetQuotedStringLength(string input, int startIndex, out int length)
static int GetWhitespaceLength(string input, int startIndex)
static int GetNumberLength(string input, int startIndex, bool allowDecimal)
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_argument_empty_string
Definition SR.cs:52
Definition SR.cs:7
static StringComparer OrdinalIgnoreCase
static string GetStringAndRelease(StringBuilder sb)
static StringBuilder Acquire(int capacity=16)
StringBuilder Append(char value, int repeatCount)
override int GetHashCode()