public static string HttpRequest(AphidObject request)
        {
            var req = request.ConvertTo<AphidRequest>();

            try
            {
                var req2 = req.ToHttpRequest();
                using (var client = HttpClient.Connect(req.Host, req.Port))
                {
                    client.Write(req2);
                    var resp = client.Read().GetBodyString();
                    return resp;
                }
            }
            finally
            {
                if (req.Files != null)
                {
                    foreach (var f in req.Files)
                    {
                        if (f.Stream != null)
                        {
                            f.Stream.Dispose();
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private static AphidObject Exec(AphidObject exeObj, AphidObject argsObj, AphidObject optionsObj)
        {
            string exe = (string)exeObj.Value, args = argsObj != null ? (string)argsObj.Value : null;

            var opt = optionsObj != null ?
                optionsObj.ConvertTo<ExecOptions>() :
                new ExecOptions();

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo(exe, args)
                {
                    RedirectStandardOutput = opt.RedirectOutput,
                    RedirectStandardError = opt.RedirectOutput,
                    UseShellExecute = !opt.RedirectOutput,
                }
            };

            if (!string.IsNullOrEmpty(opt.Working))
            {
                process.StartInfo.WorkingDirectory = opt.Working;
                process.StartInfo.UseShellExecute = false;
            }

            var sb = new StringBuilder();

            DataReceivedEventHandler handler = (o, e) =>
            {
                lock (sb)
                {
                    sb.AppendLine(e.Data);
                }
            };

            if (opt.RedirectOutput)
            {
                process.OutputDataReceived += handler;
                process.ErrorDataReceived += handler;
            }

            process.Start();

            if (opt.RedirectOutput)
            {
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
            }

            if (opt.RedirectOutput || opt.WaitForExit)
            {
                process.WaitForExit();

                var retVal = new AphidObject();
                retVal.Add("exitCode", new AphidObject((decimal)process.ExitCode));

                if (opt.RedirectOutput)
                {
                    retVal.Add("output", new AphidObject(sb.ToString()));
                }

                return retVal;
            }
            else
            {
                return null;
            }
        }