示例#1
0
 public static void ServerStart(ConfigurationFile ConfigFile)
 {
     Console.WriteLine($"\n{DateTime.Now}: SERVER START");
     Console.WriteLine("Configurations:");
     Console.WriteLine($"\tport: {ConfigFile.Port}");
     Console.WriteLine($"\tdefault page: {ConfigFile.DefaultPage}");
     Console.WriteLine($"\troot directory: {ConfigFile.RootPath}\n");
 }
示例#2
0
        public Client(TcpClient TcpClient, ConfigurationFile Config)
        {
            string Request = string.Empty;      // declare string which will contain client's request
            byte[] Buffer = new byte[1024];     // client data buffer
            int Count;                          // count income bytes

            // read data while its coming
            while ((Count = TcpClient.GetStream().Read(Buffer, 0, Buffer.Length)) > 0)
            {
                // encode to string and put in our request string
                Request += Encoding.ASCII.GetString(Buffer, 0, Count);

                // request must end with \r\n\r\n
                // or stop reading if its > 4kbyte
                if (Request.IndexOf("\r\n\r\n") >= 0 || Request.Length > 4096)
                {
                    break;
                }
            }

            // parse query
            Match ReqMatch = Regex.Match(Request, @"^\w+\s+([^\s\?]+)[^\s]*\s+HTTP/.*|");

            // if query is wrong
            if (ReqMatch == Match.Empty)
            {
                SendError(TcpClient, 400);     // error 400
                return;
            }

            // query to string
            string RequestUri = ReqMatch.Groups[1].Value;

            // transforming escaped characters.
            //example : "%20" -> " "
            RequestUri = Uri.UnescapeDataString(RequestUri);

            // if ".." http://example.com/../../file.txt
            if (RequestUri.IndexOf("..") >= 0)
            {
                SendError(TcpClient, 400);
                return;
            }

            // if query end with "/" -> add "index.html"
            if (RequestUri.EndsWith("/"))
            {
                RequestUri += Config.DefaultPage;
            }

            //root from config file + request
            string FilePath = Config.RootPath + RequestUri;

            // file not found -> 404 error
            if (!File.Exists(FilePath))
            {
                SendError(TcpClient, 404);
                return;
            }

            // get file extension
            string Extension = RequestUri.Substring(RequestUri.LastIndexOf('.'));

            string ContentType = GetTheType(Extension);

            // check the file
            FileStream FS;
            try
            {
                FS = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception)
            {
                // if exeption - 500 error
                SendError(TcpClient, 500);
                return;
            }

            // send response
            string Headers = "HTTP/1.1 200 OK\nContent-Type: " + ContentType + "\nContent-Length: " + FS.Length + "\n\n";
            byte[] HeadersBuffer = Encoding.ASCII.GetBytes(Headers);
            TcpClient.GetStream().Write(HeadersBuffer, 0, HeadersBuffer.Length);

            while (FS.Position < FS.Length)
            {
                // read and send to client
                try
                {
                    Thread.Sleep(100); //NO to dDos ;)
                    Count = FS.Read(Buffer, 0, Buffer.Length);
                    TcpClient.GetStream().Write(Buffer, 0, Count);
                }
                catch
                {
                    UI.Error(0);
                    break;
                }
            }

            // close file and connection
            FS.Close();
            TcpClient.Close();
        }