Ejemplo n.º 1
0
        public async Task <string> CurlRequest(string text)
        {
            string result;

            if (text.StartsWith("curlorig"))
            {
                Log.LogDebug("curlorig " + text.Substring(9));

                result = await HTTPUtility.GetRequestAsync(text.Substring(9));
            }
            else
            {
                bool verbose      = text.IndexOf(" -i", StringComparison.Ordinal) > 0;
                bool autoredirect = text.IndexOf(" -L", StringComparison.Ordinal) > 0;

                string url     = Regex.Match(text, "(?:\")(.*?)(?=\")").Groups[1].Value;
                var    matches = Regex.Matches(text, "(?:-H\\s\")(.*?)(?=\")");
                var    header  = (
                    from Match match in matches
                    select match.Groups
                    into groups
                    where groups.Count > 1
                    select groups[1].Value
                    into value
                    where value.Contains(": ")
                    select value).ToDictionary(value => value.Remove(value.IndexOf(": ", StringComparison.Ordinal)),
                                               value => value.Substring(value.IndexOf(": ", StringComparison.Ordinal) + 2));
                if (text.Contains("--data"))
                {
                    Log.LogDebug("POST DATA");
                    try {
                        string dataString = Regex.Match(text, "(?:--data\\s\")(.*?)(?=\")").Groups[1].Value;
                        result = await HTTPUtility.PostRequestAsync(url, dataString, header, verbose, autoredirect);
                    } catch (Exception e) {
                        result = e.ToString();
                        Log.LogDebug(e.ToString());
                    }
                }
                else
                {
                    Log.LogInformation(url);

                    result = await HTTPUtility.GetRequestAsync(url, header, verbose, autoredirect);
                }
            }

            return(result);
        }