public void SetBuffer(int receiveBufferSize, int sendBufferSize, ArraySegment <byte> buffer) { ThrowIfReadOnly(); WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, sendBufferSize); WebSocketHelpers.ValidateArraySegment(buffer, "buffer"); WebSocketBuffer.Validate(buffer.Count, receiveBufferSize, sendBufferSize, false); this.receiveBufferSize = receiveBufferSize; this.sendBufferSize = sendBufferSize; // Only full-trust applications can specify their own buffer to be used as the // internal buffer for the WebSocket object. This is because the contents of the // buffer are used internally by the WebSocket as it marshals data with embedded // pointers to native code. A malicious application could use this to corrupt // native memory. if (AppDomain.CurrentDomain.IsFullyTrusted) { this.buffer = buffer; } else { // We silently ignore the passed in buffer and will create an internal // buffer later. this.buffer = null; } }
public void SetBuffer(int receiveBufferSize, int sendBufferSize) { ThrowIfReadOnly(); WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, sendBufferSize); this.buffer = null; this.receiveBufferSize = receiveBufferSize; this.sendBufferSize = sendBufferSize; }
public void AddSubProtocol(string subProtocol) { ThrowIfReadOnly(); WebSocketHelpers.ValidateSubprotocol(subProtocol); // Duplicates not allowed. foreach (string item in requestedSubProtocols) { if (string.Equals(item, subProtocol, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(SR.GetString(SR.net_WebSockets_NoDuplicateProtocol, subProtocol), "subProtocol"); } } requestedSubProtocols.Add(subProtocol); }
// Validate the response headers and return the sub-protocol. private string ValidateResponse(HttpWebRequest request, HttpWebResponse response) { // 101 if (response.StatusCode != HttpStatusCode.SwitchingProtocols) { throw new WebSocketException(SR.GetString(SR.net_WebSockets_Connect101Expected, (int)response.StatusCode)); } // Upgrade: websocket string upgradeHeader = response.Headers[HttpKnownHeaderNames.Upgrade]; if (!string.Equals(upgradeHeader, WebSocketHelpers.WebSocketUpgradeToken, StringComparison.OrdinalIgnoreCase)) { throw new WebSocketException(SR.GetString(SR.net_WebSockets_InvalidResponseHeader, HttpKnownHeaderNames.Upgrade, upgradeHeader)); } // Connection: Upgrade string connectionHeader = response.Headers[HttpKnownHeaderNames.Connection]; if (!string.Equals(connectionHeader, HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase)) { throw new WebSocketException(SR.GetString(SR.net_WebSockets_InvalidResponseHeader, HttpKnownHeaderNames.Connection, connectionHeader)); } // Sec-WebSocket-Accept derived from request Sec-WebSocket-Key string websocketAcceptHeader = response.Headers[HttpKnownHeaderNames.SecWebSocketAccept]; string expectedAcceptHeader = WebSocketHelpers.GetSecWebSocketAcceptString( request.Headers[HttpKnownHeaderNames.SecWebSocketKey]); if (!string.Equals(websocketAcceptHeader, expectedAcceptHeader, StringComparison.OrdinalIgnoreCase)) { throw new WebSocketException(SR.GetString(SR.net_WebSockets_InvalidResponseHeader, HttpKnownHeaderNames.SecWebSocketAccept, websocketAcceptHeader)); } // Sec-WebSocket-Protocol matches one from request // A missing header is ok. It's also ok if the client didn't specify any. string subProtocol = response.Headers[HttpKnownHeaderNames.SecWebSocketProtocol]; if (!string.IsNullOrWhiteSpace(subProtocol) && options.RequestedSubProtocols.Count > 0) { bool foundMatch = false; foreach (string requestedSubProtocol in options.RequestedSubProtocols) { if (string.Equals(requestedSubProtocol, subProtocol, StringComparison.OrdinalIgnoreCase)) { foundMatch = true; break; } } if (!foundMatch) { throw new WebSocketException(SR.GetString(SR.net_WebSockets_AcceptUnsupportedProtocol, string.Join(", ", options.RequestedSubProtocols), subProtocol)); } } return(string.IsNullOrWhiteSpace(subProtocol) ? null : subProtocol); // May be null or valid. }