コード例 #1
0
        private static RequestItem ParseArguments(string[] options)
        {
            var  result    = new RequestItem();
            bool methodSet = false;
            bool urlSet    = false;

            foreach (String option in options)
            {
                if (!option.StartsWith("-"))
                {
                    bool found = false;

                    if (!methodSet)
                    {
                        if (option.Equals(Consts.HTTP_GET, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_GET;
                            methodSet     = true;
                            found         = true;
                        }
                        else if (option.Equals(Consts.HTTP_POST, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_POST;
                            methodSet     = true;
                            found         = true;
                        }
                        else if (option.Equals(Consts.HTTP_PUT, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_PUT;
                            methodSet     = true;
                            found         = true;
                        }
                        else if (option.Equals(Consts.HTTP_DELETE, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_DELETE;
                            methodSet     = true;
                            found         = true;
                        }
                    }


                    if (!found)
                    {
                        if (!urlSet)
                        {
                            string url;
                            //This must be the url
                            if (!option.ToLower().StartsWith("http://") && !option.ToLower().StartsWith("https://") && !option.ToLower().StartsWith("ws://"))
                            {
                                url = "http://" + option;
                            }
                            else
                            {
                                url = option;
                            }

                            if (!IsWebUrl(url))
                            {
                                throw new ArgumentException("Not a valid url!");
                            }
                            result.Url = url;
                            urlSet     = true;
                        }
                        else
                        {
                            if (option.Contains("=="))
                            {
                                // Querystring Parameters
                                if (result.QueryStringParameters == null)
                                {
                                    result.QueryStringParameters = new List <string> {
                                        option.Replace("==", "=")
                                    }
                                }
                                ;
                                else
                                {
                                    result.QueryStringParameters.Add(option.Replace("==", "="));
                                }
                            }
                            else if (option.Contains("="))
                            {
                                //Parameters
                                if (result.Paramters == null)
                                {
                                    result.Paramters = new List <string> {
                                        option
                                    }
                                }
                                ;
                                else
                                {
                                    result.Paramters.Add(option);
                                }
                            }
                            else if (option.Contains("@"))
                            {
                                //Files
                                //check if file exists
                                var fileInfo = option.Split('@');

                                if (fileInfo.Length == 2)
                                {
                                    if (!File.Exists(fileInfo[1]))
                                    {
                                        throw new FileNotFoundException(string.Format("file '{0}' does not exist.", fileInfo[1]));
                                    }


                                    if (result.Files == null)
                                    {
                                        result.Files = new List <string> {
                                            option
                                        }
                                    }
                                    ;
                                    else
                                    {
                                        result.Files.Add(option);
                                    }
                                }
                            }
                            else if (option.Contains(":"))
                            {
                                //Header
                                if (result.Headers == null)
                                {
                                    result.Headers = new List <string> {
                                        option
                                    }
                                }
                                ;
                                else
                                {
                                    result.Headers.Add(option);
                                }
                            }
                        }
                    }
                }
            }

            if (!methodSet)
            {
                result.Method = result.Paramters == null ? Consts.HTTP_GET : Consts.HTTP_POST;
            }

            return(result);
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: ChrisOMetz/HTTPie.net
        private static RequestItem ParseArguments(string[] options)
        {
            var result = new RequestItem();
            bool methodSet = false;
            bool urlSet = false;

            foreach (String option in options)
            {
                if (!option.StartsWith("-"))
                {
                    bool found = false;

                    if (!methodSet)
                    {
                        if (option.Equals(Consts.HTTP_GET, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_GET;
                            methodSet = true;
                            found = true;
                        }
                        else if (option.Equals(Consts.HTTP_POST, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_POST;
                            methodSet = true;
                            found = true;
                        }
                        else if (option.Equals(Consts.HTTP_PUT, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_PUT;
                            methodSet = true;
                            found = true;
                        }
                        else if (option.Equals(Consts.HTTP_DELETE, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Method = Consts.HTTP_DELETE;
                            methodSet = true;
                            found = true;
                        }
                    }


                    if (!found)
                    {
                        if (!urlSet)
                        {
                            string url;
                            //This must be the url
                            if (!option.ToLower().StartsWith("http://") && !option.ToLower().StartsWith("https://") && !option.ToLower().StartsWith("ws://"))
                                url = "http://" + option;
                            else
                                url = option;

                            if (!IsWebUrl(url))
                            {
                                throw new ArgumentException("Not a valid url!");
                            }
                            result.Url = url;
                            urlSet = true;
                        }
                        else
                        {

                            if (option.Contains("=="))
                            {
                                // Querystring Parameters
                                if (result.QueryStringParameters == null)
                                    result.QueryStringParameters = new List<string> { option.Replace("==", "=") };
                                else
                                    result.QueryStringParameters.Add(option.Replace("==", "="));
                            }
                            else if (option.Contains("="))
                            {
                                //Parameters
                                if (result.Paramters == null)
                                    result.Paramters = new List<string> {option};
                                else
                                    result.Paramters.Add(option);
                            }
                            else if (option.Contains("@"))
                            {
                                //Files
                                //check if file exists
                                var fileInfo = option.Split('@');

                                if (fileInfo.Length == 2)
                                {
                                    if (!File.Exists(fileInfo[1]))
                                    {
                                        throw new FileNotFoundException(string.Format("file '{0}' does not exist.", fileInfo[1]));
                                    }


                                    if (result.Files == null)
                                        result.Files = new List<string> { option };
                                    else
                                        result.Files.Add(option);

                                }
                            }
                            else if (option.Contains(":"))
                            {
                                //Header
                                if (result.Headers == null)
                                    result.Headers = new List<string> { option };
                                else
                                    result.Headers.Add(option);
                            }
                        }
                        
                    }

                }
            }

            if (!methodSet)
            {
                result.Method = result.Paramters == null ? Consts.HTTP_GET : Consts.HTTP_POST;
            }

            return result;
        }