コード例 #1
0
ファイル: Draft76Handler.cs プロジェクト: wind959/ntminer
        private static byte[] Handshake(WebSocketHttpRequest request, string subProtocol)
        {
            NTWebSocketLog.Debug("Building Draft76 Response");

            var builder = new StringBuilder();

            builder.Append("HTTP/1.1 101 WebSocket Protocol Handshake\r\n");
            builder.Append("Upgrade: WebSocket\r\n");
            builder.Append("Connection: Upgrade\r\n");
            builder.AppendFormat("Sec-WebSocket-Origin: {0}\r\n", request["Origin"]);
            builder.AppendFormat("Sec-WebSocket-Location: {0}://{1}{2}\r\n", request.Scheme, request["Host"], request.Path);

            if (subProtocol != null)
            {
                builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", subProtocol);
            }

            builder.Append("\r\n");

            var key1      = request["Sec-WebSocket-Key1"];
            var key2      = request["Sec-WebSocket-Key2"];
            var challenge = new ArraySegment <byte>(request.Bytes, request.Bytes.Length - 8, 8);

            var answerBytes = CalculateAnswerBytes(key1, key2, challenge);

            byte[] byteResponse       = Encoding.ASCII.GetBytes(builder.ToString());
            int    byteResponseLength = byteResponse.Length;

            Array.Resize(ref byteResponse, byteResponseLength + answerBytes.Length);
            Array.Copy(answerBytes, 0, byteResponse, byteResponseLength, answerBytes.Length);

            return(byteResponse);
        }
コード例 #2
0
ファイル: Hybi13Handler.cs プロジェクト: wind959/ntminer
        private static void ProcessFrame(FrameType frameType, byte[] data, Action <string> onMessage, Action onClose, Action <byte[]> onBinary, Action <byte[]> onPing, Action <byte[]> onPong)
        {
            switch (frameType)
            {
            case FrameType.Close:
                if (data.Length == 1 || data.Length > 125)
                {
                    throw new WebSocketException(WebSocketStatusCodes.ProtocolError);
                }

                if (data.Length >= 2)
                {
                    var closeCode = (ushort)data.Take(2).ToArray().ToLittleEndianInt();
                    if (!WebSocketStatusCodes.ValidCloseCodes.Contains(closeCode) && (closeCode < 3000 || closeCode > 4999))
                    {
                        throw new WebSocketException(WebSocketStatusCodes.ProtocolError);
                    }
                }

                if (data.Length > 2)
                {
                    ReadUTF8PayloadData(data.Skip(2).ToArray());
                }

                onClose();
                break;

            case FrameType.Binary:
                onBinary(data);
                break;

            case FrameType.Ping:
                onPing(data);
                break;

            case FrameType.Pong:
                onPong(data);
                break;

            case FrameType.Text:
                onMessage(ReadUTF8PayloadData(data));
                break;

            default:
                NTWebSocketLog.Debug("Received unhandled " + frameType);
                break;
            }
        }
コード例 #3
0
ファイル: Hybi13Handler.cs プロジェクト: wind959/ntminer
        private static byte[] BuildHandshake(WebSocketHttpRequest request, string subProtocol)
        {
            NTWebSocketLog.Debug("Building Hybi-14 Response");

            var builder = new StringBuilder();

            builder.Append("HTTP/1.1 101 Switching Protocols\r\n");
            builder.Append("Upgrade: websocket\r\n");
            builder.Append("Connection: Upgrade\r\n");
            if (subProtocol != null)
            {
                builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", subProtocol);
            }

            var responseKey = CreateResponseKey(request["Sec-WebSocket-Key"]);

            builder.AppendFormat("Sec-WebSocket-Accept: {0}\r\n", responseKey);
            builder.Append("\r\n");

            return(Encoding.ASCII.GetBytes(builder.ToString()));
        }
コード例 #4
0
 private static byte[] Handshake(WebSocketHttpRequest request, string subProtocol)
 {
     NTWebSocketLog.Debug("Building Flash Socket Policy Response");
     return(Encoding.UTF8.GetBytes(PolicyResponse));
 }