Пример #1
0
        public static byte[] PhpHandler(HttpResponse response, HttpRequest request, string php_uri, string form)
        {
            byte[] buffer;
            switch (request.Method)
            {
                #region Process GET requests
            case "GET":
            {
                Environment.SetEnvironmentVariable("REDIRECT_STATUS", "true");
                Environment.SetEnvironmentVariable("GATEWAY_INTERFACE", "CGI/1.1");
                Environment.SetEnvironmentVariable("SCRIPT_FILENAME", php_uri);
                Environment.SetEnvironmentVariable("QUERY_STRING", form + '\n');
                Environment.SetEnvironmentVariable("REQUEST_METHOD", "GET");

                ProcessStartInfo pri = new ProcessStartInfo(HttpServer.PHP_PATH);
                pri.UseShellExecute        = false;
                pri.RedirectStandardInput  = true;
                pri.RedirectStandardOutput = true;

                Process handle = Process.Start(pri);
                System.IO.StreamReader myOutput = handle.StandardOutput;
                response.Version      = request.Version;
                response.StatusCode   = Convert.ToString((int)HttpStatusCode.Ok);
                response.ReasonPhrase = Convert.ToString(HttpStatusCode.Ok.ToString());
                response.Header.Add("Server", "Niushen/6.6.66(Niuix) DAV/2 mod_jk/1.2.23");
                buffer = Encoding.UTF8.GetBytes((response.GetResponse() + myOutput.ReadToEnd()));
                if (handle.HasExited == false)
                {
                    //当php解释进程未正常退出时
                    //强制杀死该进程
                    handle.Kill();
                    handle.Close();
                }
                break;
            }

                #endregion
                #region POST requests
            case "POST":
            {
                Environment.SetEnvironmentVariable("REDIRECT_STATUS", "true");
                Environment.SetEnvironmentVariable("GATEWAY_INTERFACE", "CGI/1.1");
                Environment.SetEnvironmentVariable("SCRIPT_FILENAME", php_uri);
                Environment.SetEnvironmentVariable("REQUEST_METHOD", "POST");
                Environment.SetEnvironmentVariable("CONTENT_LENGTH", request.Header["Content-Length"]);
                Environment.SetEnvironmentVariable("CONTENT_TYPE", request.Header["Content-Type"]);

                ProcessStartInfo pri = new ProcessStartInfo(HttpServer.PHP_PATH);
                pri.UseShellExecute        = false;
                pri.RedirectStandardInput  = true;
                pri.RedirectStandardOutput = true;

                Process handle = Process.Start(pri);
                System.IO.StreamWriter myInput  = handle.StandardInput;
                System.IO.StreamReader myOutput = handle.StandardOutput;

                myInput.Write(request.Content + '\n');
                myInput.Flush();
                myInput.Close();

                response.Version      = request.Version;
                response.StatusCode   = Convert.ToString((int)HttpStatusCode.Ok);
                response.ReasonPhrase = Convert.ToString(HttpStatusCode.Ok.ToString());
                response.Header.Add("Server", "Niushen/6.6.66(Niuix) DAV/2 mod_jk/1.2.23");
                buffer = Encoding.UTF8.GetBytes((response.GetResponse() + myOutput.ReadToEnd()));

                if (handle.HasExited == false)
                {
                    handle.Kill();
                    handle.Close();
                }

                break;
            }

                #endregion
            default:
            {
                response = new HttpResponse();
                GetServerInfo.GetInfo(response, 200);
                buffer = Encoding.UTF8.GetBytes(response.GetResponse());
                break;
            }
            }
            return(buffer);
        }
Пример #2
0
        public void SSLClientHandler()

        {
            Stream    RawStream = client.GetStream();
            SslStream sslStream = new SslStream(RawStream);

            try
            {
                sslStream.AuthenticateAsServer(HttpServer.SERVER_CERT, false, SslProtocols.Tls, true);

                //读取请求行
                request = GetRequest(sslStream);
                if (request == null)
                {
                    throw new HttpException.HttpException("400 Bad Request", 400);
                }

                #region Process CGI(PHP) Scripts
                string pattern = @"^(?<uri>\S+\.php)(\?(?<form>\S*))*$";  //提取出CGI动态资源
                Match  header  = Regex.Match(request.Uri, pattern);
                if (header.Success == true)
                {
                    string uri  = header.Result("${uri}");
                    string form = header.Result("${form}");
                    //生成php脚本路径
                    //php解释器不支持..的相对路径
                    string php_uri = FileHandler.ParseUri(uri);
                    response = new HttpResponse();
                    byte[] buffer = CgiHandler.PhpHandler(response, request, php_uri, form);
                    sslStream.Write(buffer, 0, buffer.Length);
                }
                #endregion
                else
                {
                    //处理Http request并生成响应头
                    response = GetResponse(request);
                    //将响应报文response写入outoutStream中
                    WriteResponse(sslStream, response);
                }
                //断开连接
                sslStream.Flush();
                sslStream.Close();
            }
            #region Http Exception Handler
            catch (HttpException.HttpException ex)
            {
                HttpResponse temp = new HttpResponse();
                response = GetServerInfo.GetInfo(temp, ex.status);
                WriteResponse(sslStream, response);
            }
            #endregion
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
                client.Close();
                ((IDisposable)client).Dispose();
            }
        }