Example #1
0
        private void LoopObject(NestedDictionary dict, JsonElement?jsonObj)
        {
            if (jsonObj == null || jsonObj.Value.ValueKind != JsonValueKind.Object)
            {
                return;
            }
            var obj = jsonObj.Value;

            foreach (var prop in obj.EnumerateObject())
            {
                switch (prop.Value.ValueKind)
                {
                case JsonValueKind.Array:
                    var subList = new List <object>();
                    foreach (var arrVal in prop.Value.EnumerateArray())
                    {
                        if (arrVal.ValueKind == JsonValueKind.Object)
                        {
                            var subDict = new NestedDictionary();
                            subList.Add(subDict);
                            LoopObject(subDict, arrVal);
                        }
                        else
                        {
                            subList.Add(GetJsonValue(arrVal));
                        }
                    }
                    dict[prop.Name] = subList;
                    break;

                case JsonValueKind.Object:
                    var newDict = new NestedDictionary();
                    dict.Set(prop.Name, newDict);
                    LoopObject(newDict, prop.Value);
                    break;

                default:
                    dict.Set(prop.Name, GetJsonValue(prop.Value));
                    break;
                }
            }
        }
Example #2
0
        public async Task ProcessForm(CancellationToken token = default)
        {
            if (FormProcessed)
            {
                return;
            }
            Form = new NestedDictionary();
            if (!IsForm)
            {
                return;
            }
            _context.Request.Headers.TryGetValue("Content-Type", out var cts);
            var ct = cts.FirstOrDefault();

            if (ct == null)
            {
                return;
            }

            if (ct.Contains("multipart/form-data"))
            {
                var boundaryRegex = new Regex(@"boundary\s*=\s*([^;]+)");
                var matches       = boundaryRegex.Match(ct);
                if (!matches.Success)
                {
                    return;
                }

                var body = _context.Request.Body;
                // TODO: get encoding from Content-Type or fallback to default
                var reader = new Parser(body, matches.Groups[1].Value, Encoding.Default, _formOptions);
                await reader.Parse(token);

                foreach (var paramList in reader.Parameters.Values)
                {
                    var param = paramList[paramList.Count - 1];
                    Form.Set(param.Name, param.Data);
                    if (!(param.Data is FileParameter fp))
                    {
                        continue;
                    }
                    if (_tempFiles == null)
                    {
                        _tempFiles = new List <FileParameter>();
                    }
                    _tempFiles.Add(fp);
                }

                FormProcessed = true;
                return;
            }

            try
            {
                var rForm = _context.Request.Form;
                foreach (var key in rForm.Keys)
                {
                    Form.Set(key, rForm[key].LastOrDefault());
                }
                FormProcessed = true;
            }
            catch (InvalidOperationException)
            {
                // TODO: report malformed errors somehow
            }
        }