示例#1
0
        public async Task <FormCollection> ReadFormAsync(CancellationToken cancellationToken)
        {
            if (Form != null)
            {
                return(Form);
            }

            if (!HasFormContentType)
            {
                throw new InvalidOperationException("Incorrect Content-Type: " + _request.ContentType);
            }

            cancellationToken.ThrowIfCancellationRequested();

            _request.EnableRewind();

            IDictionary <string, string[]> formFields = null;
            var files = new FormFileCollection();

            // Some of these code paths use StreamReader which does not support cancellation tokens.
            //using (cancellationToken.Register(_request.HttpContext.Abort))
            //{
            var contentType = ContentType;

            // Check the content-type
            if (HasApplicationFormContentType(contentType))
            {
                var encoding = FilterEncoding(contentType.Encoding);
                formFields = await FormReader.ReadFormAsync(_request.Body, encoding, cancellationToken);
            }
            else if (HasMultipartFormContentType(contentType))
            {
                var formAccumulator = new KeyValueAccumulator <string, string>(StringComparer.OrdinalIgnoreCase);

                var boundary        = GetBoundary(contentType);
                var multipartReader = new MultipartReader(boundary, _request.Body);
                var section         = await multipartReader.ReadNextSectionAsync(cancellationToken);

                while (section != null)
                {
                    var headers = new HeaderDictionary(section.Headers);
                    ContentDispositionHeaderValue contentDisposition;
                    ContentDispositionHeaderValue.TryParse(headers.Get(HeaderNames.ContentDisposition), out contentDisposition);
                    if (HasFileContentDisposition(contentDisposition))
                    {
                        // Find the end
                        await section.Body.DrainAsync(cancellationToken);

                        var file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length)
                        {
                            Headers = headers,
                        };
                        files.Add(file);
                    }
                    else if (HasFormDataContentDisposition(contentDisposition))
                    {
                        // Content-Disposition: form-data; name="key"
                        //
                        // value

                        var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        MediaTypeHeaderValue mediaType;
                        MediaTypeHeaderValue.TryParse(headers.Get(HeaderNames.ContentType), out mediaType);
                        var encoding = FilterEncoding(mediaType != null ? mediaType.Encoding : null);
                        using (var reader = new StreamReader(section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true))
                        {
                            var value = await reader.ReadToEndAsync();

                            formAccumulator.Append(key, value);
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(false, "Unrecognized content-disposition for this section: " + headers.Get(HeaderNames.ContentDisposition));
                    }

                    section = await multipartReader.ReadNextSectionAsync(cancellationToken);
                }

                formFields = formAccumulator.GetResults();
            }
            //}

            Form = new FormCollection(formFields, files);
            return(Form);
        }
示例#2
0
 public FormCollection(IDictionary <string, string[]> store, FormFileCollection files)
     : base(store)
 {
     Files = files;
 }