Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ViaHeaderValue.cs
Go to the documentation of this file.
3using System.Text;
4
6
8{
9 private readonly string _protocolName;
10
11 private readonly string _protocolVersion;
12
13 private readonly string _receivedBy;
14
15 private readonly string _comment;
16
17 public string? ProtocolName => _protocolName;
18
20
21 public string ReceivedBy => _receivedBy;
22
23 public string? Comment => _comment;
24
25 public ViaHeaderValue(string protocolVersion, string receivedBy)
26 : this(protocolVersion, receivedBy, null, null)
27 {
28 }
29
30 public ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName)
31 : this(protocolVersion, receivedBy, protocolName, null)
32 {
33 }
34
35 public ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName, string? comment)
36 {
37 HeaderUtilities.CheckValidToken(protocolVersion, "protocolVersion");
38 CheckReceivedBy(receivedBy);
39 if (!string.IsNullOrEmpty(protocolName))
40 {
41 HeaderUtilities.CheckValidToken(protocolName, "protocolName");
42 _protocolName = protocolName;
43 }
44 if (!string.IsNullOrEmpty(comment))
45 {
46 HeaderUtilities.CheckValidComment(comment, "comment");
47 _comment = comment;
48 }
49 _protocolVersion = protocolVersion;
50 _receivedBy = receivedBy;
51 }
52
54 {
55 _protocolName = source._protocolName;
56 _protocolVersion = source._protocolVersion;
57 _receivedBy = source._receivedBy;
58 _comment = source._comment;
59 }
60
61 public override string ToString()
62 {
64 if (!string.IsNullOrEmpty(_protocolName))
65 {
66 stringBuilder.Append(_protocolName);
67 stringBuilder.Append('/');
68 }
69 stringBuilder.Append(_protocolVersion);
70 stringBuilder.Append(' ');
71 stringBuilder.Append(_receivedBy);
72 if (!string.IsNullOrEmpty(_comment))
73 {
74 stringBuilder.Append(' ');
75 stringBuilder.Append(_comment);
76 }
78 }
79
80 public override bool Equals([NotNullWhen(true)] object? obj)
81 {
82 if (!(obj is ViaHeaderValue viaHeaderValue))
83 {
84 return false;
85 }
86 if (string.Equals(_protocolVersion, viaHeaderValue._protocolVersion, StringComparison.OrdinalIgnoreCase) && string.Equals(_receivedBy, viaHeaderValue._receivedBy, StringComparison.OrdinalIgnoreCase) && string.Equals(_protocolName, viaHeaderValue._protocolName, StringComparison.OrdinalIgnoreCase))
87 {
88 return string.Equals(_comment, viaHeaderValue._comment, StringComparison.Ordinal);
89 }
90 return false;
91 }
92
93 public override int GetHashCode()
94 {
96 if (!string.IsNullOrEmpty(_protocolName))
97 {
99 }
100 if (!string.IsNullOrEmpty(_comment))
101 {
102 num ^= _comment.GetHashCode();
103 }
104 return num;
105 }
106
107 public static ViaHeaderValue Parse(string? input)
108 {
109 int index = 0;
111 }
112
113 public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out ViaHeaderValue? parsedValue)
114 {
115 int index = 0;
116 parsedValue = null;
117 if (GenericHeaderParser.SingleValueViaParser.TryParseValue(input, null, ref index, out var parsedValue2))
118 {
119 parsedValue = (ViaHeaderValue)parsedValue2;
120 return true;
121 }
122 return false;
123 }
124
125 internal static int GetViaLength(string input, int startIndex, out object parsedValue)
126 {
127 parsedValue = null;
128 if (string.IsNullOrEmpty(input) || startIndex >= input.Length)
129 {
130 return 0;
131 }
132 int protocolEndIndex = GetProtocolEndIndex(input, startIndex, out var protocolName, out var protocolVersion);
133 if (protocolEndIndex == startIndex || protocolEndIndex == input.Length)
134 {
135 return 0;
136 }
137 string host;
138 int hostLength = HttpRuleParser.GetHostLength(input, protocolEndIndex, allowToken: true, out host);
139 if (hostLength == 0)
140 {
141 return 0;
142 }
143 protocolEndIndex += hostLength;
144 protocolEndIndex += HttpRuleParser.GetWhitespaceLength(input, protocolEndIndex);
145 string comment = null;
146 if (protocolEndIndex < input.Length && input[protocolEndIndex] == '(')
147 {
148 int length = 0;
149 if (HttpRuleParser.GetCommentLength(input, protocolEndIndex, out length) != 0)
150 {
151 return 0;
152 }
153 comment = input.Substring(protocolEndIndex, length);
154 protocolEndIndex += length;
155 protocolEndIndex += HttpRuleParser.GetWhitespaceLength(input, protocolEndIndex);
156 }
157 parsedValue = new ViaHeaderValue(protocolVersion, host, protocolName, comment);
158 return protocolEndIndex - startIndex;
159 }
160
161 private static int GetProtocolEndIndex(string input, int startIndex, out string protocolName, out string protocolVersion)
162 {
163 protocolName = null;
164 protocolVersion = null;
165 int startIndex2 = startIndex;
166 int tokenLength = HttpRuleParser.GetTokenLength(input, startIndex2);
167 if (tokenLength == 0)
168 {
169 return 0;
170 }
171 startIndex2 = startIndex + tokenLength;
172 int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, startIndex2);
173 startIndex2 += whitespaceLength;
174 if (startIndex2 == input.Length)
175 {
176 return 0;
177 }
178 if (input[startIndex2] == '/')
179 {
180 protocolName = input.Substring(startIndex, tokenLength);
181 startIndex2++;
182 startIndex2 += HttpRuleParser.GetWhitespaceLength(input, startIndex2);
183 tokenLength = HttpRuleParser.GetTokenLength(input, startIndex2);
184 if (tokenLength == 0)
185 {
186 return 0;
187 }
188 protocolVersion = input.Substring(startIndex2, tokenLength);
189 startIndex2 += tokenLength;
190 whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, startIndex2);
191 startIndex2 += whitespaceLength;
192 }
193 else
194 {
195 protocolVersion = input.Substring(startIndex, tokenLength);
196 }
197 if (whitespaceLength == 0)
198 {
199 return 0;
200 }
201 return startIndex2;
202 }
203
205 {
206 return new ViaHeaderValue(this);
207 }
208
209 private static void CheckReceivedBy(string receivedBy)
210 {
211 if (string.IsNullOrEmpty(receivedBy))
212 {
214 }
215 if (HttpRuleParser.GetHostLength(receivedBy, 0, allowToken: true, out var _) != receivedBy.Length)
216 {
218 }
219 }
220}
static CultureInfo InvariantCulture
static readonly GenericHeaderParser SingleValueViaParser
static void CheckValidComment(string value, string parameterName)
static void CheckValidToken(string value, string parameterName)
static void CheckReceivedBy(string receivedBy)
override bool Equals([NotNullWhen(true)] object? obj)
static int GetProtocolEndIndex(string input, int startIndex, out string protocolName, out string protocolVersion)
ViaHeaderValue(string protocolVersion, string receivedBy)
ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName, string? comment)
static ViaHeaderValue Parse(string? input)
static int GetViaLength(string input, int startIndex, out object parsedValue)
ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName)
static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out ViaHeaderValue? parsedValue)
static HttpParseResult GetCommentLength(string input, int startIndex, out int length)
static int GetHostLength(string input, int startIndex, bool allowToken, out string host)
static int GetTokenLength(string input, int startIndex)
static int GetWhitespaceLength(string input, int startIndex)
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)