Ejemplo n.º 1
0
        public static async Task <Request> ParseFromTextBlockAsync(Api parent, string textBlock, bool hasImageData,
                                                                   string boundary)
        {
            var request    = new Request(parent);
            var lines      = textBlock.Split(new[] { "\r\n" }, StringSplitOptions.None);
            var prevIsNull = false;
            var prevType   = string.Empty;

            foreach (var text in lines)
            {
                if (string.IsNullOrEmpty(text))
                {
                    prevIsNull = true;
                    continue;
                }

                if (prevIsNull && ((prevType == nameof(request.Header)) || (prevType == nameof(request.Cookie))))
                {
                    var bodyBlock = textBlock.Substring(textBlock.IndexOf(text, StringComparison.Ordinal));
                    var body      = await RequestBody.ParseFromTextBlockAsync(request, bodyBlock, hasImageData, boundary);

                    if ((request.Body == null) && (body != null))
                    {
                        request.Body = body;
                        prevType     = nameof(request.Body);
                        continue;
                    }
                }

                if (prevType == string.Empty)
                {
                    var address = await RequestAddress.ParseFromTextLineAsync(text);

                    if ((request.Address == null) && (address != null))
                    {
                        request.Address = address;
                        if (address.Method == "GET")
                        {
                            var index = address.Url.IndexOf("?", StringComparison.Ordinal);
                            if (index > -1)
                            {
                                var getParameters = address.Url.Substring(index + 1);
                                if (RequestBody.RegexString.IsMatch(getParameters))
                                {
                                    var body       = new RequestBody(request);
                                    var parameters = Regex.Split(getParameters, "&");
                                    foreach (var parameter in parameters)
                                    {
                                        var keyValues = Regex.Split(parameter, "=");
                                        if (keyValues.Length == 2)
                                        {
                                            if (body.Content.ContainsKey(keyValues[0]))
                                            {
                                                Console.WriteLine($@"Parameter {keyValues[0]} is duplicated in get request");
                                            }
                                            else
                                            {
                                                body.Content.Add(keyValues[0],
                                                                 new KeyValueState <string, object>(body, keyValues[0],
                                                                                                    Utility.TryParseToObject(keyValues[1], body)));
                                            }
                                        }
                                    }
                                    request.Body = body;
                                    prevType     = nameof(request.Body);
                                }
                            }
                        }
                        prevType = nameof(request.Address);
                        continue;
                    }
                }

                if ((prevType == nameof(request.Address)) || (prevType == nameof(request.Header)))
                {
                    var cookie = await Cookie.ParseFromTextLineAsync(request, text);

                    if ((request.Cookie == null) && (cookie != null))
                    {
                        request.Cookie = cookie;
                        prevType       = nameof(request.Cookie);
                        continue;
                    }
                }

                if ((prevType == nameof(request.Address)) || (prevType == nameof(request.Header)) ||
                    (prevType == nameof(request.Cookie)))
                {
                    if (request.Header == null)
                    {
                        request.Header = new Header(request);
                    }
                    if (await request.Header.AppendAsync(text))
                    {
                        prevType = nameof(request.Header);
                    }
                }
            }

            SetDefaultValue(request);

            return(request);
        }
Ejemplo n.º 2
0
        public static RequestBody ParseFromTextBlock(Request parent, string textBlock, bool hasImageData,
                                                     string boundary)
        {
            var body    = new RequestBody(parent);
            var matches = RegexJson.Matches(textBlock);

            if ((matches.Count == 1) && (matches[0].Groups.Count == 2))
            {
                textBlock = textBlock.Replace(matches[0].Groups[1].Value, string.Empty);
            }
            if (RegexString.IsMatch(textBlock))
            {
                var parameters = Regex.Split(textBlock, "&");
                foreach (var parameter in parameters)
                {
                    if (parameter.StartsWith(SpecialParameter + "="))
                    {
                        body.Content.Add(SpecialParameter,
                                         new KeyValueState <string, object>(body, SpecialParameter,
                                                                            Utility.TryParseToObject(parameter.Substring((SpecialParameter + "=").Length), body)));
                    }
                    else
                    {
                        var keyValues = Regex.Split(parameter, "=");
                        if (keyValues.Length == 2)
                        {
                            if (body.Content.ContainsKey(keyValues[0]))
                            {
                                Console.WriteLine($@"Parameter {keyValues[0]} is duplicated in get request");
                            }
                            else
                            {
                                body.Content.Add(keyValues[0],
                                                 new KeyValueState <string, object>(body, keyValues[0],
                                                                                    Utility.TryParseToObject(keyValues[1], body)));
                            }
                        }
                    }
                }
                return(body);
            }
            Utility.TryParseToObject(textBlock, body,
                                     jObj => { body.Content.Add("Content", new KeyValueState <string, object>(body, "Content", jObj)); },
                                     text =>
            {
                if (hasImageData && !string.IsNullOrEmpty(boundary))
                {
                    var parameters = text.Split(new[] { $"--{boundary}" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in parameters)
                    {
                        if (item.StartsWith("--"))
                        {
                            continue;
                        }
                        var match = RegexImageDataKey.Match(item);
                        if (match.Success && (match.Groups.Count == 3))
                        {
                            var key   = match.Groups[1].Value;
                            var value =
                                Utility.Trim(
                                    item.Substring(item.IndexOf("\r\n", match.Groups[0].Index,
                                                                StringComparison.Ordinal)));
                            var valueMatche = RegexImageDataValue.Match(value);
                            if (valueMatche.Success && (valueMatche.Groups.Count == 2))
                            {
                                value = value.Replace(valueMatche.Groups[1].Value, string.Empty);
                            }
                            body.Content.Add(key,
                                             new KeyValueState <string, object>(body, key, Utility.TryParseToObject(value, body)));
                        }
                    }
                }
                else
                {
                    body.Content.Add("Content", new KeyValueState <string, object>(body, "Content", text.Trim()));
                }
            });
            return(body);
        }