예제 #1
0
        private void Receive()
        {
            //TODO: REPLACE ALL URI'S WITH MORE THAN 1 DOT'S WITH &URI_DOT!

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("[*] Server: PULLING_CLIENT_HEADER");
            string rawClientHeader = StreamReader.ReadToEndAndKeepAlive();

            //Parse rawClientHeader to ClientHeader
            if (rawClientHeader.Equals(""))
            {
                return;
            }

            try
            {
                __RequestedDataPath = rawClientHeader.Split(' ')[1];
            }
            catch
            {
                EndResponse();
                return;
            }
            if (__RequestedDataPath.Length == 1)
            {
                // REQUEST PATH IS / ~ THEREFORE INDEX
                __RequestedDataPath = "/" + GlobalShares.INIParser.GetValue("HTTP", "Index"); //TODO ; PARSE INI
            }
            string _Uri = ServerHeader.WWWFolder + __RequestedDataPath.Replace("/", @"/");

            _Uri = Extensions.Extensions.ClearUri(_Uri);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("[*] Server: PULLING_REQUEST=" + _Uri);

            string _full = "";

            if (_Uri.Contains("?"))
            {
                _full = _Uri.Remove(_Uri.IndexOf('?'), _Uri.Length - _Uri.IndexOf('?'));
            }
            else
            {
                _full = _Uri;
            }
            if ((!File.Exists(_full) || ServerErrors.GetError(_full) != ServerErrors.ServerErrorsEnums.NoError))
            {
                switch (ServerErrors.GetError(_Uri))
                {
                case ServerErrors.ServerErrorsEnums.NotSupportedYet:
                    __RequestedDataBuffer = File.ReadAllBytes(ServerHeader.WWWFolder + @"/" + GlobalShares.INIParser.GetValue("Errors", "Error_php"));
                    break;

                case ServerErrors.ServerErrorsEnums.NoError:
                    __RequestedDataBuffer   = File.ReadAllBytes(ServerHeader.WWWFolder + @"/" + GlobalShares.INIParser.GetValue("Errors", "Error_404"));
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[*] Server: USER_REQUEST_404");
                    break;

                case ServerErrors.ServerErrorsEnums.NotSupported:
                    __RequestedDataBuffer   = File.ReadAllBytes(ServerHeader.WWWFolder + @"/" + GlobalShares.INIParser.GetValue("Errors", "Error_php"));
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("[*] Server: USER_REQUEST_404");
                    break;
                }
            }
            else
            {
                // PASS URI TO PLUGIN-MANAGER AND DETERMINE WHETHER AN APPLICATION IS ALLOCATED TO IT [SOLVED]
                if (PlugInManager.HandlePlugIn(_Uri))
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("[*] Server: RUNNING_PLUGIN");

                    string _extension = Path.GetExtension(_Uri).Replace(".", "");
                    if (_extension.Contains("?"))
                    {
                        _extension = _extension.Remove(_extension.IndexOf('?'), _extension.Length - _extension.IndexOf('?'));
                    }
                    if (_extension == "php")
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.WriteLine("[*] PlugIn: RUNNING_PHP_EXE");

                        Webserver.__Handler.InvokeAnonymous(delegate
                        {
                            Webserver.__Handler.logLB.Items.Add("[*] RUNNING HYPERTEXT PREPROCESSOR!");
                            Webserver.__Handler.logLB.TopIndex = Webserver.__Handler.logLB.Items.Count - 1;
                        });

                        string _get_arguments = "";
                        if (!_Uri.Contains("?"))
                        {
                            _Uri += "?";
                        }

                        _get_arguments        = _Uri.Remove(0, _Uri.IndexOf('?') + 1).Replace('&', ' ');
                        __RequestedDataBuffer = PlugInManager.PullPlugInData(_Uri, "-f " + _Uri.Remove(_Uri.IndexOf('?'), _Uri.Length - _Uri.IndexOf('?')).Replace(GlobalShares.INIParser.GetValue("HTTP", "PublicFolder") + @"/", "") + " " + _get_arguments);
                    }
                }
                else
                {
                    __RequestedDataBuffer = File.ReadAllBytes(_Uri);
                    //if (_Uri.Contains(".ico")) // FIX ACCESS-VIOLATION
                    //    __RequestedDataBuffer = File.ReadAllBytes(_Uri);
                    //else
                    //{
                    //    using (var _file = MemoryMappedFile.CreateFromFile(_Uri))
                    //    {
                    //        Stream _requestFileStream = _file.CreateViewStream();
                    //        using (var _memoryStream = new MemoryStream())
                    //        {

                    //            Console.ForegroundColor = ConsoleColor.Red;
                    //            Console.WriteLine("[*] SERVER: USING MEMORYMAPPEDSTREAM");

                    //            _requestFileStream.CopyTo(_memoryStream);
                    //            __RequestedDataBuffer = _memoryStream.ToArray();

                    //            _memoryStream.Close();
                    //            _requestFileStream.Close();
                    //            _file.Dispose();
                    //        }
                    //    }
                    //    //Webserver.FreeMemory(2); // Dangerous
                    //}
                }

                /*
                 * FATAL PROBLEM:
                 * MEMORY-LACK! [SOLVED; BUT DANGEROUS]
                 */
            }


            BeginResponse(!File.Exists(_Uri) ? "error" : "");
            Send(__RequestedDataBuffer);
            EndResponse();
        }