Beispiel #1
0
 protected HttpParseResult SDK_OnUpgrade(IntPtr pSender, IntPtr connId, HttpUpgradeType enUpgradeType)
 {
     if (OnUpgrade != null)
     {
         return(OnUpgrade(connId, enUpgradeType));
     }
     return(HttpParseResult.Ok);
 }
Beispiel #2
0
        protected override HttpParseResult SDK_OnUpgrade(IntPtr pSender, IntPtr connId, HttpUpgradeType upgradeType)
        {
            if (upgradeType == HttpUpgradeType.HttpTunnel)
            {
                SendResponse(connId, HttpStatusCode.Ok, "Connection Established", null, null, 0);
            }
            else if (upgradeType == HttpUpgradeType.WebSocket)
            {
                THeader[] headers =
                {
                    new THeader()
                    {
                        Name = "Connection", Value = "Upgrade"
                    },
                    new THeader()
                    {
                        Name = "Upgrade", Value = "WebSocket"
                    },
                    new THeader(),
                    new THeader(),
                };

                var keyName         = "Sec-WebSocket-Key";
                var secWebSocketKey = GetHeader(connId, keyName);
                if (string.IsNullOrEmpty(secWebSocketKey))
                {
                    return(HttpParseResult.Error);
                }


                SHA1   sha1           = new SHA1CryptoServiceProvider();
                byte[] bytes_sha1_in  = Encoding.UTF8.GetBytes(secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
                byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
                string str_sha1_out   = Convert.ToBase64String(bytes_sha1_out);

                headers[2].Name  = "Sec-WebSocket-Accept";
                headers[2].Value = str_sha1_out;


                var secWebSocketProtocol = GetHeader(connId, "Sec-WebSocket-Protocol");
                if (!string.IsNullOrEmpty(secWebSocketProtocol))
                {
                    var arr = secWebSocketProtocol.Split(new[] { ',', ' ' });
                    if (arr.Length > 0)
                    {
                        headers[3].Name  = "Sec-WebSocket-Protocol";
                        headers[3].Value = arr[0];
                    }
                }

                SendResponse(connId, HttpStatusCode.SwitchingProtocols, null, headers, null, 0);
            }
            return(HttpParseResult.Ok);
        }
Beispiel #3
0
 private HttpParseResult OnUpgrade(IntPtr connId, HttpUpgradeType upgradeType)
 {
     Console.WriteLine("OnUpgrade({0}, {1})", connId, upgradeType);
     return(HttpParseResult.Ok);
 }