Ejemplo n.º 1
0
        private static void ReceivedSocketData(ref Socket handleSocket)
        {
            byte[] buffer        = new byte[1096];
            int    bytes         = 0;
            string request       = "";
            int    ContentLength = 0;

            for (; ;)
            {
                if (buffer.Length < bytes + 300)
                {
                    Array.Resize(ref buffer, bytes + 2000);
                }
                else
                {
                    bytes += handleSocket.Receive(buffer, bytes, 60, SocketFlags.None);
                }
                if (bytes > 500 && bytes < 1000 && buffer.Length == 1096)
                {
                    request = Encoding.ASCII.GetString(buffer, 0, bytes);
                    if (request.Contains("content-length:") || request.Contains("Content-Length:"))
                    {
                        ContentLength = GetRequestContentLenght(ref request);
                        if (ContentLength > 0 && ContentLength < 210000000)
                        {
                            Array.Resize(ref buffer, ContentLength + bytes);
                        }
                        else if (ContentLength > 210000000)
                        {
                            handleSocket.Close();
                        }
                    }
                }
                if (handleSocket.Available == 0 && bytes >= ContentLength)
                {
                    break;
                }
                if (handleSocket.Available == 0 && bytes < ContentLength)
                {
                    if ((handleSocket.Poll(10000, SelectMode.SelectRead) && (handleSocket.Available == 0)) || !handleSocket.Connected)
                    {
                        handleSocket.Close();
                        Logger.WriteLog("Remote socket was disconnected.", LogLevel.Usual);
                        break;
                    }
                }
                if (bytes > 210000000)
                {
                    HttpIternalServerError(ref handleSocket);
                    handleSocket.Close();
                    break;
                }
            }
            if (handleSocket.Connected)
            {
                request = Encoding.ASCII.GetString(buffer, 0, bytes);
                IdentifyRequest(ref request, ref handleSocket, ref buffer, ref bytes);
            }
            if (handleSocket.Connected)
            {
                handleSocket.Close();
            }
        }