예제 #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
파일: HttpServer.cs 프로젝트: tiwb/AngeIO
 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
파일: HttpServer.cs 프로젝트: tiwb/AngeIO
        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
파일: WebSocket.cs 프로젝트: tiwb/AngeIO
        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);
 }