Пример #1
0
 private void OnRequest(HttpServerRequest req, HttpConnection conn)
 {
     if (conn.Writable)
     {
         conn.Write("HTTP/1.1 426 Upgrade Required\r\n" +
                    "Connection: close\r\n" +
                    "Content-Length: 0\r\n" +
                    "\r\n");
     }
     conn.Close();
 }
Пример #2
0
 internal void HandleRequest(HttpServerRequest msg, HttpConnection conn)
 {
     if (OnRequest != null)
     {
         OnRequest(msg, conn);
     }
     else
     {
         var s = "HTTP 500 Internal Server Error\r\n\r\n";
         conn.Write(new ArraySegment <byte>(Encoding.ASCII.GetBytes(s)));
         conn.End();
     }
 }
Пример #3
0
        internal void HandleUpgrade(HttpServerRequest msg, TcpSocket socket, ArraySegment <byte> header)
        {
            try {
                if (OnUpgrade != null)
                {
                    OnUpgrade.Invoke(msg, socket, header);
                }
                else
                {
                    socket.Close();
                }
            }

            catch {
                socket.Close();
            }
        }
Пример #4
0
        /// <summary>
        /// Handle a HTTP Upgrade request.
        /// </summary>
        /// <param name="req"></param>
        /// <param name="conn"></param>
        public void HandleUpgrade(HttpServerRequest req, TcpSocket socket, ArraySegment <byte> head)
        {
            if (!socket.Readable || !socket.Writable)
            {
                socket.Destroy();
                return;
            }

            if (OnConnection == null)
            {
                AbortConnection(socket, 400);
                return;
            }

            var upgrade = req.Headers["upgrade"];
            var version = req.Headers["sec-websocket-version"];

            if ((version != "13" && version != "8") ||
                !string.Equals(upgrade, "websocket", StringComparison.InvariantCultureIgnoreCase))
            {
                socket.Write(Encoding.ASCII.GetBytes(
                                 "HTTP/1.1 400 Bad Request\r\n" +
                                 "Connection: close\r\n" +
                                 "Sec-WebSocket-Version: 13, 8\r\n"));
                socket.Close();
                return;
            }

            string acceptKey;

            using (var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider()) {
                var key = req.Headers["sec-websocket-key"];
                acceptKey = Convert.ToBase64String(sha1.ComputeHash(Encoding.ASCII.GetBytes(key + GUID)));
            }

            socket.Write(Encoding.UTF8.GetBytes(
                             "HTTP/1.1 101 Switching Protocols\r\n" +
                             "Upgrade: websocket\r\n" +
                             "Connection: Upgrade\r\n" +
                             "Sec-WebSocket-Accept: " + acceptKey + "\r\n\r\n"));
            socket.Flush();

            OnConnection.Invoke(new WebSocket(socket, req, head));
        }
Пример #5
0
        internal WebSocket(TcpSocket socket, HttpServerRequest context, ArraySegment <byte> head)
        {
            _context                = context;
            _socket                 = socket;
            _socket.OnData         += OnData;
            _socket.OnDisconnected += OnSocketDisconnected;
            _recvstate              = RecvStat.Start1;
            _fragmented             = 0;
            _payloadData            = new BufferData();
            _sendingQueue           = new BufferData();
            _sendingTextMode        = false;
            _sendingMaskMode        = false;
            _isserver               = true;
            _state = SocketStat.Open;

            if (head.Count > 0)
            {
                OnData(_socket, head);
            }

            _socket.NoDelay = true;
            _socket.ReceiveStart();
        }
Пример #6
0
 public void End()
 {
     _socket.Flush();
     _msg = null;
     if (_parser.ShouldKeepAlive())
     {
         _parser.Pause(false);
         if (_pausedData.Count > 0)
         {
             var data = _pausedData;
             _pausedData = new ArraySegment <byte>();
             OnData(_socket, data);
         }
         if (_parser.Errno == HttpParserError.OK)
         {
             _socket.ReceiveStart();
         }
     }
     else
     {
         Close();
     }
 }
Пример #7
0
 private int OnMessageBegin()
 {
     _msg = new HttpServerRequest();
     return(0);
 }