Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WebSocketValidate.cs
Go to the documentation of this file.
1using System.Text;
2
4
5internal static class WebSocketValidate
6{
7 internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, WebSocketState[] validStates)
8 {
9 string p = string.Empty;
10 if (validStates != null && validStates.Length != 0)
11 {
12 foreach (WebSocketState webSocketState in validStates)
13 {
14 if (currentState == webSocketState)
15 {
16 if (isDisposed)
17 {
18 throw new ObjectDisposedException("WebSocket");
19 }
20 return;
21 }
22 }
23 p = string.Join(", ", validStates);
24 }
25 throw new WebSocketException(WebSocketError.InvalidState, System.SR.Format(System.SR.net_WebSockets_InvalidState, currentState, p));
26 }
27
28 internal static void ValidateSubprotocol(string subProtocol)
29 {
30 if (string.IsNullOrWhiteSpace(subProtocol))
31 {
33 }
34 string text = null;
35 for (int i = 0; i < subProtocol.Length; i++)
36 {
37 char c = subProtocol[i];
38 if (c < '!' || c > '~')
39 {
40 text = $"[{c}]";
41 break;
42 }
43 if (!char.IsLetterOrDigit(c) && "()<>@,;:\\\"/[]?={} ".IndexOf(c) >= 0)
44 {
45 text = c.ToString();
46 break;
47 }
48 }
49 if (text != null)
50 {
52 }
53 }
54
55 internal static void ValidateCloseStatus(WebSocketCloseStatus closeStatus, string statusDescription)
56 {
57 if (closeStatus == WebSocketCloseStatus.Empty && !string.IsNullOrEmpty(statusDescription))
58 {
59 throw new ArgumentException(System.SR.Format(System.SR.net_WebSockets_ReasonNotNull, statusDescription, WebSocketCloseStatus.Empty), "statusDescription");
60 }
61 if ((closeStatus >= (WebSocketCloseStatus)0 && closeStatus <= (WebSocketCloseStatus)999) || closeStatus == (WebSocketCloseStatus)1006 || closeStatus == (WebSocketCloseStatus)1015)
62 {
63 throw new ArgumentException(System.SR.Format(System.SR.net_WebSockets_InvalidCloseStatusCode, (int)closeStatus), "closeStatus");
64 }
65 int num = 0;
66 if (!string.IsNullOrEmpty(statusDescription))
67 {
68 num = Encoding.UTF8.GetByteCount(statusDescription);
69 }
70 if (num > 123)
71 {
72 throw new ArgumentException(System.SR.Format(System.SR.net_WebSockets_InvalidCloseStatusDescription, statusDescription, 123), "statusDescription");
73 }
74 }
75
76 internal static void ValidateArraySegment(ArraySegment<byte> arraySegment, string parameterName)
77 {
78 if (arraySegment.Array == null)
79 {
80 throw new ArgumentNullException(parameterName + ".Array");
81 }
82 if (arraySegment.Offset < 0 || arraySegment.Offset > arraySegment.Array.Length)
83 {
84 throw new ArgumentOutOfRangeException(parameterName + ".Offset");
85 }
86 if (arraySegment.Count < 0 || arraySegment.Count > arraySegment.Array.Length - arraySegment.Offset)
87 {
88 throw new ArgumentOutOfRangeException(parameterName + ".Count");
89 }
90 }
91}
static void ValidateSubprotocol(string subProtocol)
static void ValidateArraySegment(ArraySegment< byte > arraySegment, string parameterName)
static void ValidateCloseStatus(WebSocketCloseStatus closeStatus, string statusDescription)
static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, WebSocketState[] validStates)
static string net_WebSockets_InvalidCharInProtocolString
Definition SR.cs:138
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_WebSockets_InvalidCloseStatusDescription
Definition SR.cs:144
static string net_WebSockets_InvalidCloseStatusCode
Definition SR.cs:142
static string net_WebSockets_ReasonNotNull
Definition SR.cs:140
static string net_WebSockets_InvalidState
Definition SR.cs:14
static string net_WebSockets_InvalidEmptySubProtocol
Definition SR.cs:136
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526