示例#1
0
        private void SendError(TcpClient Client, int Code)
        {
            ClientResponseProcessor CRP = new ClientResponseProcessor();

            CRP.generateError(Code);
            closeClientWithBuffer(Client, CRP.toBuffer());
        }
示例#2
0
        public Client(TcpClient Client)
        {
            string Request = "";

            byte[] Buffer = new byte[1024];
            int    Count;

            while ((Count = Client.GetStream().Read(Buffer, 0, Buffer.Length)) > 0)
            {
                Request += Encoding.ASCII.GetString(Buffer, 0, Count);
                // запит має закінчуватись на \r\n\r\n
                if (Request.IndexOf("\r\n\r\n") >= 0 || Request.Length > 4096)
                {
                    break;
                }
            }
            // шукаємо в стрічці запит користувача
            Match ReqMatch = Regex.Match(Request, @"^\w+\s+([^\s\?]+)[^\s]*\s+HTTP/.*|");

            if (ReqMatch == Match.Empty)
            {
                SendError(Client, 400);
                return;
            }

            string RequestUri = ReqMatch.Groups[1].Value;

            RequestUri = Uri.UnescapeDataString(RequestUri);

            // захист від хакерських атак
            if (RequestUri.IndexOf("..") >= 0)
            {
                SendError(Client, 400);
                return;
            }
            string filename = Directory.GetCurrentDirectory() + "\\" + "data.csv";

            if (RequestUri.EndsWith("/") || RequestUri == "")
            {
                ClientResponseProcessor CRP = new ClientResponseProcessor();

                try
                {
                    string data = "";
                    if (File.Exists(filename))
                    {
                        data += "<!DOCTYPE html>";
                        data += "<html>";
                        DataBaseReader DBReader   = new DataBaseReader(filename);
                        List <long>    dataFromDB = DBReader.GetDataFromDB();
                        int            i          = 0;

                        data += dataFromDB.Select(x =>
                        {
                            string Div       = (((++i) > 3) ? "<div>" : "<div><b>");
                            string DivClose  = ((i > 3) ? "</div>" : "</b></div>");
                            string H         = ((i < 4) ? "<h" + i + ">" : "");
                            string HClose    = ((i < 4) ? "<h" + i + ">" : "");
                            string BestScore = ((i == 1) ? "&#128077;" : "");
                            return(Div + H + x + BestScore + HClose + DivClose);
                        }).Aggregate("", (prev, curr) => prev + curr);
                        data += "</html>";
                    }
                    CRP.add(data);
                    byte[] ResponseBuffer = CRP.toBuffer();

                    string Headers       = "HTTP/1.1 200 OK\nContent-Type: " + "text/html" + "\nContent-Length: " + ResponseBuffer.Length + "\n\n";
                    byte[] HeadersBuffer = Encoding.ASCII.GetBytes(Headers);
                    Client.GetStream().Write(HeadersBuffer, 0, HeadersBuffer.Length);
                    closeClientWithBuffer(Client, ResponseBuffer);
                    Console.WriteLine("sent");
                }
                catch (Exception e) { Console.WriteLine("error " + e.ToString()); }
                Client.Close();
            }
            else
            {
                try
                {
                    string newValue = RequestUri.Replace("/", "");
                    if (newValue == "favicon.ico")
                    {
                        return;
                    }
                    long newIntValue = Int32.Parse(newValue);
                    if (newIntValue != prev)
                    {
                        prev = newIntValue;
                        File.AppendAllText((filename), newIntValue + ",");
                        Console.WriteLine("added new " + RequestUri.Replace("/", ""));
                    }
                }
                catch (Exception e) { Console.WriteLine("error " + e.ToString()); }
                Client.Close();
                return;
            }
        }