Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HttpWebSocket.cs
Go to the documentation of this file.
3using System.IO;
6using System.Text;
9
10namespace System.Net.WebSockets;
11
12internal static class HttpWebSocket
13{
15
16 private static bool WebSocketsSupported { get; } = Environment.OSVersion.Version >= new Version(6, 2);
17
18
19 internal static string GetSecWebSocketAcceptString(string secWebSocketKey)
20 {
21 string s = secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
22 byte[] bytes = Encoding.UTF8.GetBytes(s);
23 byte[] inArray = SHA1.HashData(bytes);
25 }
26
28 {
29 acceptProtocol = string.Empty;
30 if (string.IsNullOrEmpty(clientSecWebSocketProtocol))
31 {
32 if (subProtocol != null)
33 {
35 }
36 return false;
37 }
38 if (subProtocol == null)
39 {
40 return true;
41 }
42 string[] array = clientSecWebSocketProtocol.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
44 foreach (string b in array)
45 {
46 if (string.Equals(acceptProtocol, b, StringComparison.OrdinalIgnoreCase))
47 {
48 return true;
49 }
50 }
52 }
53
81
82 private static void ValidateWebSocketHeaders(HttpListenerContext context)
83 {
85 {
87 }
88 if (!context.Request.IsWebSocketRequest)
89 {
90 throw new WebSocketException(WebSocketError.NotAWebSocket, System.SR.Format(System.SR.net_WebSockets_AcceptNotAWebSocket, "ValidateWebSocketHeaders", "Connection", "Upgrade", "websocket", context.Request.Headers["Upgrade"]));
91 }
92 string text = context.Request.Headers["Sec-WebSocket-Version"];
93 if (string.IsNullOrEmpty(text))
94 {
95 throw new WebSocketException(WebSocketError.HeaderError, System.SR.Format(System.SR.net_WebSockets_AcceptHeaderNotFound, "ValidateWebSocketHeaders", "Sec-WebSocket-Version"));
96 }
97 if (!string.Equals(text, SupportedVersion, StringComparison.OrdinalIgnoreCase))
98 {
100 }
101 string text2 = context.Request.Headers["Sec-WebSocket-Key"];
102 bool flag = string.IsNullOrWhiteSpace(text2);
103 if (!flag)
104 {
105 try
106 {
107 flag = Convert.FromBase64String(text2).Length != 16;
108 }
109 catch
110 {
111 flag = true;
112 }
113 }
114 if (flag)
115 {
116 throw new WebSocketException(WebSocketError.HeaderError, System.SR.Format(System.SR.net_WebSockets_AcceptHeaderNotFound, "ValidateWebSocketHeaders", "Sec-WebSocket-Key"));
117 }
118 }
119
127
129 {
130 try
131 {
135 string secWebSocketVersion = request.Headers["Sec-WebSocket-Version"];
136 string origin = request.Headers["Origin"];
138 if (ProcessWebSocketProtocolHeader(request.Headers["Sec-WebSocket-Protocol"], subProtocol, out var acceptProtocol))
139 {
141 response.Headers.Add("Sec-WebSocket-Protocol", acceptProtocol);
142 }
143 string secWebSocketKey = request.Headers["Sec-WebSocket-Key"];
145 response.Headers.Add("Connection", "Upgrade");
146 response.Headers.Add("Upgrade", "websocket");
147 response.Headers.Add("Sec-WebSocket-Accept", secWebSocketAcceptString);
148 response.StatusCode = 101;
149 response.ComputeCoreHeaders();
150 ulong num = SendWebSocketHeaders(response);
151 if (num != 0L)
152 {
153 throw new WebSocketException((int)num, System.SR.Format(System.SR.net_WebSockets_NativeSendResponseHeaders, "AcceptWebSocketAsync", num));
154 }
155 if (System.Net.NetEventSource.Log.IsEnabled())
156 {
157 System.Net.NetEventSource.Info(null, FormattableStringFactory.Create("{0} = {1}", "Origin", origin), "AcceptWebSocketAsyncCore");
158 System.Net.NetEventSource.Info(null, FormattableStringFactory.Create("{0} = {1}", "Sec-WebSocket-Version", secWebSocketVersion), "AcceptWebSocketAsyncCore");
159 System.Net.NetEventSource.Info(null, FormattableStringFactory.Create("{0} = {1}", "Sec-WebSocket-Key", secWebSocketKey), "AcceptWebSocketAsyncCore");
160 System.Net.NetEventSource.Info(null, FormattableStringFactory.Create("{0} = {1}", "Sec-WebSocket-Accept", secWebSocketAcceptString), "AcceptWebSocketAsyncCore");
161 System.Net.NetEventSource.Info(null, FormattableStringFactory.Create("{0} = {1}", "Sec-WebSocket-Protocol", request.Headers["Sec-WebSocket-Protocol"]), "AcceptWebSocketAsyncCore");
162 System.Net.NetEventSource.Info(null, FormattableStringFactory.Create("{0} = {1}", "Sec-WebSocket-Protocol", acceptProtocol), "AcceptWebSocketAsyncCore");
163 }
164 await response.OutputStream.FlushAsync().SuppressContextFlow();
168 httpRequestStream.SwitchToOpaqueMode();
172 if (System.Net.NetEventSource.Log.IsEnabled())
173 {
174 System.Net.NetEventSource.Associate(context, httpListenerWebSocketContext, "AcceptWebSocketAsyncCore");
176 }
178 }
179 catch (Exception message)
180 {
181 if (System.Net.NetEventSource.Log.IsEnabled())
182 {
183 System.Net.NetEventSource.Error(context, message, "AcceptWebSocketAsyncCore");
184 }
185 throw;
186 }
187 }
188
190 {
191 return task.ConfigureAwait(continueOnCapturedContext: false);
192 }
193
195 {
196 return task.ConfigureAwait(continueOnCapturedContext: false);
197 }
198
200 {
201 return response.SendHeaders(null, null, global::Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA | global::Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA | global::Interop.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_OPAQUE, isWebSocketHandshake: true);
202 }
203
205 {
206 if (innerStream == null)
207 {
208 throw new ArgumentNullException("innerStream");
209 }
210 if (!innerStream.CanRead)
211 {
212 throw new ArgumentException(System.SR.net_writeonlystream, "innerStream");
213 }
214 if (!innerStream.CanWrite)
215 {
216 throw new ArgumentException(System.SR.net_readonlystream, "innerStream");
217 }
218 }
219
220 internal static void ThrowIfConnectionAborted(Stream connection, bool read)
221 {
222 if ((!read && !connection.CanWrite) || (read && !connection.CanRead))
223 {
224 throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely);
225 }
226 }
227
233}
void Add(TKey key, TValue value)
static string ToBase64String(byte[] inArray)
Definition Convert.cs:2675
static unsafe byte[] FromBase64String(string s)
Definition Convert.cs:2904
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static void Associate(object first, object second, [CallerMemberName] string memberName=null)
static void ValidateOptions(string subProtocol, int receiveBufferSize, int sendBufferSize, TimeSpan keepAliveInterval)
static string GetSecWebSocketAcceptString(string secWebSocketKey)
static ConfiguredTaskAwaitable< T > SuppressContextFlow< T >(this Task< T > task)
static Task< HttpListenerWebSocketContext > AcceptWebSocketAsync(HttpListenerContext context, string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment< byte > internalBuffer)
static void ThrowPlatformNotSupportedException_WSPC()
static ConfiguredTaskAwaitable SuppressContextFlow(this Task task)
static bool ProcessWebSocketProtocolHeader(string clientSecWebSocketProtocol, string subProtocol, out string acceptProtocol)
static void ValidateWebSocketHeaders(HttpListenerContext context)
static unsafe ulong SendWebSocketHeaders(HttpListenerResponse response)
static void ValidateInnerStream(Stream innerStream)
static async Task< HttpListenerWebSocketContext > AcceptWebSocketAsyncCore(HttpListenerContext context, string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment< byte > internalBuffer)
static void ThrowIfConnectionAborted(Stream connection, bool read)
static WebSocket Create(Stream innerStream, string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment< byte > internalBuffer)
static void Validate(int count, int receiveBufferSize, int sendBufferSize, bool isServerBuffer)
static void ValidateSubprotocol(string subProtocol)
static void ValidateArraySegment(ArraySegment< byte > arraySegment, string parameterName)
static FormattableString Create(string format, params object?[] arguments)
static string net_WebSockets_AcceptUnsupportedProtocol
Definition SR.cs:128
static string net_WebSockets_NativeSendResponseHeaders
Definition SR.cs:124
static string net_readonlystream
Definition SR.cs:152
static string net_WebSockets_AcceptHeaderNotFound
Definition SR.cs:132
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_WebSockets_AcceptUnsupportedWebSocketVersion
Definition SR.cs:134
static string net_WebSockets_ClientAcceptingNoProtocols
Definition SR.cs:126
static string net_WebSockets_AcceptNotAWebSocket
Definition SR.cs:130
static string net_writeonlystream
Definition SR.cs:116
static string net_WebSockets_UnsupportedPlatform
Definition SR.cs:150
static string net_WebSockets_ArgumentOutOfRange_TooSmall
Definition SR.cs:146
static string net_WebSockets_ArgumentOutOfRange_TooBig
Definition SR.cs:148
Definition SR.cs:7
static byte[] HashData(byte[] source)
Definition SHA1.cs:66
static Encoding UTF8
Definition Encoding.cs:526
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5