Example #1
0
        private static SqlCommand ConfigureCommand(string connectionInfo, PipelineCommand command, ExecutionLog log)
        {
            // Determine if we have an actual connection string, or a connection string name
            var connectionString = IsConnectionStringName(connectionInfo) ? ConfigurationManager.ConnectionStrings[connectionInfo].ToString() : connectionInfo;

            log.AddMessage(connectionString != connectionInfo ? "Connection argument is a valid, authorized connection string key" : "Connection argument is not a connection string name; assuming a raw connection string");

            var sqlCommand = new SqlCommand()
            {
                Connection = new SqlConnection(connectionString)
            };

            sqlCommand.Connection.StatisticsEnabled = true;

            // Add all variables as params
            foreach (var variable in command.Pipeline.Variables.Where(v => v.Key != Pipeline.GLOBAL_VARIABLE_NAME))
            {
                sqlCommand.Parameters.AddWithValue(variable.Key, variable.Value.Value ?? String.Empty);
                log.AddMessage($"Added SQL param: \"{variable.Key}\"");
            }

            try
            {
                sqlCommand.Connection.Open();
            }
            catch (Exception e)
            {
                throw new DeninaException("Error connecting to SQL Server with connection string \"" + connectionInfo + "\"", e);
            }

            return(sqlCommand);
        }
Example #2
0
        public static string StripTags(string input, PipelineCommand command)
        {
            var parser = new HtmlParser();
            var doc    = parser.Parse(input);

            return(doc.DocumentElement.TextContent);
        }
Example #3
0
        public static string FormatNodes(string input, PipelineCommand command)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(input);

            var xpath    = command.GetArgument("xpath");
            var template = command.GetArgument("template");

            var patterns = Regex.Matches(template, "{([^}]*)}").Cast <Match>().Select(m => m.Value).ToList();

            var output = new StringBuilder();

            foreach (XmlNode node in xmlDoc.SelectNodes(xpath))
            {
                var thisTemplate = String.Copy(template);
                foreach (var pattern in patterns)
                {
                    var result = node.SelectSingleNode(pattern.Trim(new char[] { '}', '{' }));

                    var value = String.Empty;
                    if (result != null)
                    {
                        value = result.InnerXml;
                    }

                    thisTemplate = thisTemplate.Replace(pattern, value);
                }
                output.Append(thisTemplate);
            }

            return(output.ToString());
        }
Example #4
0
        private static SqlCommand ConfigureCommand(string connectionStringName, PipelineCommand command)
        {
            var sqlCommand = new SqlCommand()
            {
                Connection = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionStringName].ToString())
            };

            // Add all variables as params
            foreach (var variable in command.Pipeline.Variables)
            {
                sqlCommand.Parameters.AddWithValue(variable.Key, variable.Value.Value ?? String.Empty);
            }

            try
            {
                sqlCommand.Connection.Open();
            }
            catch (Exception e)
            {
                throw new DeninaException("Error connecting to SQL Server with connection string \"" + connectionStringName + "\"", e);
            }


            return(sqlCommand);
        }
Example #5
0
 public static string LineBreaks(string input, PipelineCommand command, ExecutionLog log)
 {
     input = input.Replace("\r\n", "\n");
     input = input.Replace("\n", Environment.NewLine);
     input = input.Replace(Environment.NewLine, "<br/>");
     return(input);
 }
Example #6
0
        public static string Get(string input, PipelineCommand command)
        {
            var url = GetUrlArgument(input, command);

            var client = new WebClient();

            return(client.DownloadString(url));
        }
Example #7
0
        public static string Extract(string input, PipelineCommand command)
        {
            var parser  = new HtmlParser();
            var doc     = parser.Parse(input);
            var element = doc.QuerySelector(command.GetArgument("path"));

            return(element == null ? String.Empty : element.InnerHtml);
        }
Example #8
0
        public static string ExtractFromJson(string input, PipelineCommand command, ExecutionLog log)
        {
            var path = command.GetArgument(key: "path");

            var jobject = (JToken)JObject.Parse(json: input);

            return((string)jobject.SelectToken(path: path));
        }
Example #9
0
        public static string SetVar(string input, PipelineCommand command, ExecutionLog log)
        {
            var variableName = command.GetArgument("var");
            var value        = command.GetArgument("value", String.Empty);

            command.Pipeline.SafeSetVariable(variableName, value);

            return(input);
        }
Example #10
0
        public static string AddQuerysgtringArg(string input, PipelineCommand command, ExecutionLog log)
        {
            var uriBuilder            = new UriBuilder(input);
            NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);

            query[command.GetArgument("key")] = command.GetArgument("value");
            uriBuilder.Query = query.ToString();
            return(uriBuilder.Uri.AbsoluteUri);
        }
Example #11
0
        public static string AddQuerysgtringArg(string input, PipelineCommand command)
        {
            var uriBuilder            = new UriBuilder(input);
            NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);

            query[command.CommandArgs["1"]] = command.CommandArgs["2"];
            uriBuilder.Query = query.ToString();
            return(uriBuilder.Uri.AbsoluteUri);
        }
Example #12
0
 public static string ReplaceAll(string input, PipelineCommand command)
 {
     // ReplaceAll with no arguments is essentially the same as Clear
     if (!command.CommandArgs.Any())
     {
         return(String.Empty);
     }
     return(command.GetArgument("text"));
 }
Example #13
0
        public static string Now(string input, PipelineCommand command, ExecutionLog log)
        {
            var formatString = "f";

            if (command.CommandArgs.Count == 1)
            {
                formatString = command.GetArgument("format");
            }
            return(DateTime.Now.ToString(formatString));
        }
Example #14
0
        public static string FormatLines(string input, PipelineCommand command)
        {
            var returnString = new StringBuilder();

            foreach (var line in input.Split(new [] { Environment.NewLine }, StringSplitOptions.None))
            {
                returnString.AppendFormat(command.GetArgument("template"), line);
            }
            return(returnString.ToString());
        }
Example #15
0
        public static string FromText(string input, PipelineCommand command, ExecutionLog log)
        {
            var template = command.GetArgument("template");

            var parsedTemplate = DotLiquid.Template.Parse(template);

            return(parsedTemplate.Render(Hash.FromAnonymousObject(
                                             new { data = input, vars = new Drops.VarsDrop(command.Pipeline.Variables) }
                                             )));
        }
Example #16
0
 public static string InitVar(string input, PipelineCommand command, ExecutionLog log)
 {
     foreach (var variableName in command.GetMultiArgument("var"))
     {
         if (!command.Pipeline.Variables.ContainsKey(variableName))
         {
             command.Pipeline.SafeSetVariable(variableName, String.Empty);
         }
     }
     return(input);
 }
Example #17
0
        public static string IsEqualTo(string input, PipelineCommand command)
        {
            var value = command.GetArgument("value");

            if (input == value)
            {
                command.SendToLabel = command.DefaultArgument;
            }

            return(input);
        }
Example #18
0
        public static string ExtractRegex(string input, PipelineCommand command)
        {
            var result = Regex.Match(input, command.GetArgument("pattern"));

            if (result.Groups.Count == 0)
            {
                return(String.Empty);
            }

            return(result.Groups[1].Value);
        }
Example #19
0
        public static string Replace(string input, PipelineCommand command)
        {
            var oldValue = command.GetArgument("old");
            var newValue = command.GetArgument("new", String.Empty);

            var respectCase = false;

            Boolean.TryParse(command.GetArgument("case", "false"), out respectCase);

            return(respectCase ? input.Replace(oldValue, newValue) :  Regex.Replace(input, oldValue, newValue, RegexOptions.IgnoreCase));
        }
Example #20
0
        public static string IsEmpty(string input, PipelineCommand command)
        {
            var value = command.GetArgument("value", input);
            var label = command.GetArgument("label");

            if (String.IsNullOrWhiteSpace(value))
            {
                command.SendToLabel = label;
            }

            return(input);
        }
Example #21
0
        public static string NoVar(string input, PipelineCommand command)
        {
            var varName   = command.GetArgument("var");
            var labelName = command.GetArgument("label");

            if (!command.Pipeline.IsSet(varName))
            {
                command.SendToLabel = labelName;
            }

            return(input);
        }
Example #22
0
        public void PopulateCommand()
        {
            var command = new PipelineCommand();

            command.FullyQualifiedCommandName = "Replace";
            command.CommandArgs.Add("1", "a");
            command.CommandArgs.Add("2", "b");

            Assert.AreEqual("Replace", command.FullyQualifiedCommandName);
            Assert.AreEqual("core.replace", command.NormalizedCommandName);
            Assert.AreEqual("a", command.CommandArgs.First().Value);
            Assert.AreEqual("b", command.CommandArgs.Last().Value);
        }
Example #23
0
        public static string AppendVar(string input, PipelineCommand command, ExecutionLog log)
        {
            // If they didn't provide a value, then use the input
            var valueToAppend = command.GetArgument("value", input);
            var variableName  = command.GetArgument("var");

            var oldValue = command.Pipeline.GetVariable(variableName);
            var newValue = String.Concat(oldValue, valueToAppend);

            command.Pipeline.SafeSetVariable(variableName, newValue);

            return(input);
        }
Example #24
0
        public static string GetXml(string input, PipelineCommand command, ExecutionLog log)
        {
            var sql            = command.GetArgument("sql,proc", input);
            var connectionInfo = command.GetArgument("connection");

            log.AddMessage($"Allowed connection strings are: {Pipeline.GetGlobalVariable(ALLOWED_CONNECTION_STRINGS_VARIABLE_NAME)}");
            IsValidConnectionStringName(connectionInfo);

            var sqlCommand = ConfigureCommand(connectionInfo, command, log);

            // If this isn't a stored proc, then we need to append the XML stuff to it.  If it is a stored proc, when we assume that's already there.
            if (!command.HasArgument("proc"))
            {
                sqlCommand.CommandText = String.Concat(sql, " FOR XML PATH, ROOT('rows'), ELEMENTS XSINIL");
            }
            else
            {
                sqlCommand.CommandText = sql;
            }

            var xml = "<rows/>";

            try
            {
                var reader = sqlCommand.ExecuteXmlReader();
                log.AddMessage($"Successful SQL execution in {sqlCommand.Connection.RetrieveStatistics()["ExecutionTime"]}ms");
                reader.Read();
                var rawResult = reader.ReadOuterXml();
                if (!String.IsNullOrWhiteSpace(rawResult))
                {
                    xml = rawResult;
                }

                var xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch (Exception e)
                {
                    throw new DeninaException("Unable to parse XML response.", e);
                }
            }
            catch (Exception e)
            {
                throw new DeninaException("Error executing SQL", e);
            }

            return(String.IsNullOrWhiteSpace(xml) ? ">" : xml);
        }
Example #25
0
        public static string Format(string input, PipelineCommand command)
        {
            var template = command.GetArgument("template");

            foreach (var variable in command.Pipeline.Variables)
            {
                template = template.Replace(String.Concat("{", variable.Key, "}"), Convert.ToString(variable.Value));
            }

            // Escape any remaining {string} patterns, because these will break String.Format...
            template = Regex.Replace(template, "{([^0-9]+)}", "{{$1}}");

            return(String.Format(template, input));
        }
Example #26
0
        public static string SetAttribute(string input, PipelineCommand command)
        {
            var path      = command.GetArgument("path");
            var attribute = command.GetArgument("attribute");
            var value     = command.GetArgument("val");

            var parser  = new HtmlParser();
            var doc     = parser.Parse(input);
            var element = doc.QuerySelector(path);

            element.Attributes.ToList().First(a => a.Name == attribute).Value = value;

            return(doc.DocumentElement.InnerHtml);
        }
Example #27
0
        public static string GetXml(string input, PipelineCommand command)
        {
            var sql = command.GetArgument("sql,proc", input);
            var connectionStringName = command.GetArgument("connection");

            IsValidConnectionStringName(connectionStringName);

            var sqlCommand = ConfigureCommand(connectionStringName, command);

            // If this isn't a stored proc, then we need to append the XML stuff to it.  If it is a stored proc, when we assume that's already there.
            if (!command.HasArgument("proc"))
            {
                sqlCommand.CommandText = String.Concat(sql, " FOR XML PATH, ROOT('rows'), ELEMENTS XSINIL");
            }
            else
            {
                sqlCommand.CommandText = sql;
            }

            var xml = "<rows/>";

            try
            {
                var reader = sqlCommand.ExecuteXmlReader();
                reader.Read();
                var rawResult = reader.ReadOuterXml();
                if (!String.IsNullOrWhiteSpace(rawResult))
                {
                    xml = rawResult;
                }

                var xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.LoadXml(xml);
                }
                catch (Exception e)
                {
                    throw new DeninaException("Unable to parse XML response.", e);
                }
            }
            catch (Exception e)
            {
                throw new DeninaException("Error executing SQL", e);
            }

            return(String.IsNullOrWhiteSpace(xml) ? ">" : xml);
        }
Example #28
0
        public static string CountNodes(string input, PipelineCommand command)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(input);

            var xpath = command.GetArgument("xpath");

            if (xmlDoc.SelectNodes(xpath) == null)
            {
                return("0");
            }

            return(xmlDoc.SelectNodes(xpath).Count.ToString());
        }
Example #29
0
        public void AddCommandByObject()
        {
            var command = new PipelineCommand();

            command.FullyQualifiedCommandName = "Replace";
            command.CommandArgs.Add("a", "b");

            var pipeline = new Pipeline();

            pipeline.AddCommand(command);

            Assert.AreEqual(1, pipeline.Commands.Count());
            Assert.AreEqual("Replace", pipeline.Commands.First().FullyQualifiedCommandName);
            Assert.AreEqual("b", pipeline.Commands.First().CommandArgs.First().Value);
        }
Example #30
0
        private static string GetUrlArgument(string input, PipelineCommand command, string argumentName = "url")
        {
            // The sandbox variable must be set...
            if (!Pipeline.IsSetGlobally(ALLOWED_DOMAINS_VARIABLE_NAME))
            {
                throw new DeninaException("Allowed domains variable must be defined.");
            }

            var url = command.GetArgument(argumentName, input);


            // If this URL is relative, swap the URL into the current request, to assume the current host and scheme
            if (url.StartsWith("/"))
            {
                var currentUrl = HttpContext.Current.Request.Url;
                var builder    = new UriBuilder();
                builder.Host   = currentUrl.Host;
                builder.Scheme = currentUrl.Scheme;
                builder.Port   = currentUrl.Port;
                builder.Path   = url;
                url            = builder.Uri.AbsoluteUri;
            }

            Uri parsedUri;

            try
            {
                parsedUri = new Uri(url);
            }
            catch (Exception e)
            {
                throw new DeninaException("Invalid URL provided.", e);
            }

            // If it's set to the wildcard, we can skip the check entirely
            if (Pipeline.GetGlobalVariable(ALLOWED_DOMAINS_VARIABLE_NAME).ToString() != ALL_DOMAINS_WILDCARD)
            {
                var allowedDomains = Pipeline.GetGlobalVariable(ALLOWED_DOMAINS_VARIABLE_NAME).ToString().Split(',').Select(s => s.Trim().ToLower()).ToList();

                if (!allowedDomains.Contains(parsedUri.Host.ToLower()))
                {
                    throw new DeninaException(String.Concat("Host not authorized: ", parsedUri.Host));
                }
            }

            return(url);
        }