private void SendNegotiationResponse(WebSocketHandshake handshake, StreamWriter writer) { writer.Write("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"); if (handshake.Response.Cookies.Count > 0) { foreach (var cookie in handshake.Response.Cookies) { writer.Write("Set-Cookie: "); writer.Write(cookie.ToString()); writer.Write("\r\n"); } } writer.Write("Sec-WebSocket-Accept: "); writer.Write(handshake.GenerateHandshake()); if (handshake.Request.Headers.HeaderNames.Contains(WebSocketHeaders.Protocol)) { writer.Write("\r\nSec-WebSocket-Protocol: "); writer.Write(handshake.Response.WebSocketProtocol); } WriteHandshakeCookies(handshake, writer); writer.Write("\r\n\r\n"); }
private void SendNegotiationResponse(WebSocketHandshake handshake, StreamWriter writer) { writer.Write("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"); if (handshake.Response.Cookies.Count > 0) { foreach (var cookie in handshake.Response.Cookies) { writer.Write("Set-Cookie: "); writer.Write(cookie.ToString()); writer.Write("\r\n"); } } writer.Write("Sec-WebSocket-Accept: "); writer.Write(handshake.GenerateHandshake()); // https://tools.ietf.org/html/rfc6455#section-4.2.2 /* * Sec-WebSocket-Protocol * If the client's handshake did not contain such a header field or if * the server does not agree to any of the client's requested * subprotocols, the only acceptable value is null. The absence * of such a field is equivalent to the null value (meaning that * if the server does not wish to agree to one of the suggested * subprotocols, it MUST NOT send back a |Sec-WebSocket-Protocol| * header field in its response). */ if (handshake.Response.WebSocketProtocol != null) { writer.Write("\r\nSec-WebSocket-Protocol: "); writer.Write(handshake.Response.WebSocketProtocol); } WriteHandshakeCookies(handshake, writer); writer.Write("\r\n\r\n"); }
private void SendNegotiationResponse(WebSocketHandshake handshake, StreamWriter writer) { writer.Write("HTTP/1.1 101 Switching Protocols\r\n"); writer.Write("Upgrade: websocket\r\n"); writer.Write("Connection: Upgrade\r\n"); writer.Write("Sec-WebSocket-Accept: "); writer.Write(handshake.GenerateHandshake()); if (handshake.Request.Headers.AllKeys.Contains("Sec-WebSocket-Protocol")) { writer.Write("\r\n"); writer.Write("Sec-WebSocket-Protocol: "); writer.Write(handshake.Request.WebSocketProtocol); } if (handshake.ResponseExtensions.Any()) { Boolean firstExt=true, firstOpt=true; writer.Write("\r\n"); writer.Write("Sec-WebSocket-Extensions: "); foreach (var extension in handshake.ResponseExtensions) { if(!firstExt) writer.Write(","); writer.Write(extension.Name); var serverAcceptedOptions = extension.Options.Where(x => !x.ClientAvailableOption); if(extension.Options.Any()) { writer.Write(";"); foreach (var extOption in serverAcceptedOptions) { if(!firstOpt) writer.Write(";"); writer.Write(extOption.Name); if (extOption.Value != null) { writer.Write("="); writer.Write(extOption.Value); } firstOpt = false; } firstExt = false; } } } writer.Write("\r\n"); writer.Write("\r\n"); }
private void SendNegotiationResponse(WebSocketHandshake handshake, StreamWriter writer) { writer.Write("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"); if (handshake.Response.Cookies.Count > 0) { foreach (var cookie in handshake.Response.Cookies) { writer.Write("Set-Cookie: "); writer.Write(cookie.ToString()); writer.Write("\r\n"); } } writer.Write("Sec-WebSocket-Accept: "); writer.Write(handshake.GenerateHandshake()); if (handshake.Request.Headers.HeaderNames.Contains("Sec-WebSocket-Protocol")) { writer.Write("\r\nSec-WebSocket-Protocol: "); writer.Write(handshake.Response.WebSocketProtocol); } WriteHandshakeCookies(handshake, writer); writer.Write("\r\n\r\n"); }
private void SendNegotiationResponse(WebSocketHandshake handshake, StreamWriter writer) { writer.Write("HTTP/1.1 101 Switching Protocols\r\n"); writer.Write("Upgrade: websocket\r\n"); writer.Write("Connection: Upgrade\r\n"); if (handshake.Response.Cookies.Count > 0) { foreach (var cookie in handshake.Response.Cookies) { writer.Write("Set-Cookie: "); writer.Write(cookie.ToString()); writer.Write("\r\n"); } } writer.Write("Sec-WebSocket-Accept: "); writer.Write(handshake.GenerateHandshake()); if (handshake.Request.Headers.AllKeys.Contains("Sec-WebSocket-Protocol")) { writer.Write("\r\n"); writer.Write("Sec-WebSocket-Protocol: "); writer.Write(handshake.Response.WebSocketProtocol); } if (handshake.Response.WebSocketExtensions.Any()) { Boolean firstExt = true, firstOpt = true; writer.Write("\r\n"); writer.Write("Sec-WebSocket-Extensions: "); foreach (var extension in handshake.Response.WebSocketExtensions) { if (!firstExt) { writer.Write(","); } writer.Write(extension.Name); var serverAcceptedOptions = extension.Options.Where(x => !x.ClientAvailableOption); if (extension.Options.Any()) { writer.Write(";"); foreach (var extOption in serverAcceptedOptions) { if (!firstOpt) { writer.Write(";"); } writer.Write(extOption.Name); if (extOption.Value != null) { writer.Write("="); writer.Write(extOption.Value); } firstOpt = false; } firstExt = false; } } } writer.Write("\r\n"); writer.Write("\r\n"); }
private void SendNegotiationResponse(WebSocketHandshake handshake, StreamWriter writer) { writer.Write("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"); if (handshake.Response.Cookies.Count > 0) { foreach (var cookie in handshake.Response.Cookies) { writer.Write("Set-Cookie: "); writer.Write(cookie.ToString()); writer.Write("\r\n"); } } writer.Write("Sec-WebSocket-Accept: "); writer.Write(handshake.GenerateHandshake()); // https://tools.ietf.org/html/rfc6455#section-4.2.2 /* Sec-WebSocket-Protocol If the client's handshake did not contain such a header field or if the server does not agree to any of the client's requested subprotocols, the only acceptable value is null. The absence of such a field is equivalent to the null value (meaning that if the server does not wish to agree to one of the suggested subprotocols, it MUST NOT send back a |Sec-WebSocket-Protocol| header field in its response). */ if (handshake.Response.WebSocketProtocol != null) { writer.Write("\r\nSec-WebSocket-Protocol: "); writer.Write(handshake.Response.WebSocketProtocol); } WriteHandshakeCookies(handshake, writer); writer.Write("\r\n\r\n"); }