Пример #1
0
        static async Task Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            const string NewLine = "\r\n";
            string       username, password;

            TcpListener tcpListener = new TcpListener(
                IPAddress.Loopback, 80);

            tcpListener.Start();

            using var connection = new SqlConnection("Server=.;Database=DapperDB;Trusted_Connection=True");

            connection.Open();

            while (true)
            {
                var client = tcpListener.AcceptTcpClient();
                using (var stream = client.GetStream())
                {
                    byte[] buffer = new byte[1000000];
                    var    lenght = stream.Read(buffer, 0, buffer.Length);

                    string requestString =
                        Encoding.UTF8.GetString(buffer, 0, lenght);
                    Console.WriteLine(requestString);

                    var currentPage = "There's no page in the URL";
                    var parsedURL   = requestString.Split(" HTTP")[0].Split("/");
                    if (parsedURL.Length > 1) //meaining there's a page added
                    {
                        currentPage = parsedURL[1];
                    }
                    Console.WriteLine(currentPage);

                    if (currentPage == "login")
                    {
                        ParseUsernameAndPassword(requestString, out username, out password);

                        string htmlPost = $"<h1>{DapperDb.Login(connection, username, password)}</h1>";

                        string responsePost = GenerateResponse(NewLine, htmlPost);

                        byte[] responseBytesPost = Encoding.UTF8.GetBytes(responsePost);
                        stream.Write(responseBytesPost);

                        Console.WriteLine(new string('=', 70));

                        continue;
                    }

                    if (currentPage == "register")
                    {
                        ParseUsernameAndPassword(requestString, out username, out password);

                        string htmlPost = $"<h1>{DapperDb.Register(connection,username, password)}</h1>";

                        string responsePost = GenerateResponse(NewLine, htmlPost);

                        byte[] responseBytesPost = Encoding.UTF8.GetBytes(responsePost);
                        stream.Write(responseBytesPost);

                        Console.WriteLine(new string('=', 70));

                        continue;
                    }

                    string html = $"<h1>Hello from NikiServer {DateTime.Now}</h1>" +
                                  $"<h3>Please login</h3>" +
                                  $"<form action=/login method=post><input name=username /><input name=password />" +
                                  $"<input type=submit value = Login /></form>" +
                                  $"<h3>Or please register if you don't have an account</h3>" +
                                  $"<form action=/register method=post><input name=username /><input name=password />" +
                                  $"<input type=submit value = 'Create User' /></form>";

                    string response = GenerateResponse(NewLine, html);

                    byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                    stream.Write(responseBytes);

                    Console.WriteLine(new string('=', 70));
                }
            }
        }