Пример #1
0
        public void MultipartReader_BufferSizeMustBeLargerThanBoundary_Throws()
        {
            var stream = MakeStream(ThreePartBody);

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                var reader = new MultipartReader(Boundary, stream, 5);
            });
        }
Пример #2
0
        public async Task MultipartReader_HeadersLengthExceeded_Throws()
        {
            var stream = MakeStream(OnePartBodyTwoHeaders);
            var reader = new MultipartReader(Boundary, stream)
            {
                HeadersLengthLimit = 60,
            };

            var exception = await Assert.ThrowsAsync <InvalidDataException>(() => reader.ReadNextSectionAsync());

            Assert.Equal("Line length limit 17 exceeded.", exception.Message);
        }
Пример #3
0
        public async Task MutipartReader_HeaderCountExceeded_Throws()
        {
            var stream = MakeStream(OnePartBodyTwoHeaders);
            var reader = new MultipartReader(Boundary, stream)
            {
                HeadersCountLimit = 1,
            };

            var exception = await Assert.ThrowsAsync <InvalidDataException>(() => reader.ReadNextSectionAsync());

            Assert.Equal("Multipart headers count limit 1 exceeded.", exception.Message);
        }
Пример #4
0
        public async Task MultipartReader_ReadSinglePartBodyWithoutLastCRLF_Success()
        {
            var stream = MakeStream(OnePartBodyWithoutFinalCRLF);
            var reader = new MultipartReader(Boundary, stream);

            var section = await reader.ReadNextSectionAsync();

            Assert.NotNull(section);
            Assert.Single(section.Headers);
            Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
            var buffer = new MemoryStream();
            await section.Body.CopyToAsync(buffer);

            Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

            Assert.Null(await reader.ReadNextSectionAsync());
        }
Пример #5
0
        public async Task MultipartReader_ThreePartBody_Success()
        {
            var stream = MakeStream(ThreePartBody);
            var reader = new MultipartReader(Boundary, stream);

            var section = await reader.ReadNextSectionAsync();

            Assert.NotNull(section);
            Assert.Single(section.Headers);
            Assert.Equal("form-data; name=\"text\"", section.Headers["Content-Disposition"][0]);
            var buffer = new MemoryStream();
            await section.Body.CopyToAsync(buffer);

            Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

            section = await reader.ReadNextSectionAsync();

            Assert.NotNull(section);
            Assert.Equal(2, section.Headers.Count);
            Assert.Equal("form-data; name=\"file1\"; filename=\"a.txt\"", section.Headers["Content-Disposition"][0]);
            Assert.Equal("text/plain", section.Headers["Content-Type"][0]);
            buffer = new MemoryStream();
            await section.Body.CopyToAsync(buffer);

            Assert.Equal("Content of a.txt.\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

            section = await reader.ReadNextSectionAsync();

            Assert.NotNull(section);
            Assert.Equal(2, section.Headers.Count);
            Assert.Equal("form-data; name=\"file2\"; filename=\"a.html\"", section.Headers["Content-Disposition"][0]);
            Assert.Equal("text/html", section.Headers["Content-Type"][0]);
            buffer = new MemoryStream();
            await section.Body.CopyToAsync(buffer);

            Assert.Equal("<!DOCTYPE html><title>Content of a.html.</title>\r\n", Encoding.ASCII.GetString(buffer.ToArray()));

            Assert.Null(await reader.ReadNextSectionAsync());
        }
Пример #6
0
        public async Task MultipartReader_ReadInvalidUtf8SurrogateHeader_ReplacementCharacters()
        {
            var body1 =
                "--9051914041544843365972754266\r\n" +
                "Content-Disposition: form-data; name=\"text\" filename=\"a";

            var body2 =
                ".txt\"\r\n" +
                "\r\n" +
                "text default\r\n" +
                "--9051914041544843365972754266--\r\n";
            var stream = new MemoryStream();
            var bytes  = Encoding.UTF8.GetBytes(body1);

            stream.Write(bytes, 0, bytes.Length);

            // Write an invalid utf-8 segment in the middle
            stream.Write(new byte[] { 0xED, 0xA0, 85 }, 0, 3);

            bytes = Encoding.UTF8.GetBytes(body2);
            stream.Write(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            var reader = new MultipartReader(Boundary, stream);

            var section = await reader.ReadNextSectionAsync();

            Assert.NotNull(section);
            Assert.Single(section.Headers);
            Assert.Equal("form-data; name=\"text\" filename=\"a\uFFFD\uFFFDU.txt\"", section.Headers["Content-Disposition"][0]);
            var buffer = new MemoryStream();
            await section.Body.CopyToAsync(buffer);

            Assert.Equal("text default", Encoding.ASCII.GetString(buffer.ToArray()));

            Assert.Null(await reader.ReadNextSectionAsync());
        }
Пример #7
0
        public async Task <JsonResult> UploadHeadImage()
        {
            ResponseResult result = new ResponseResult();

            var    boundary        = this.Request.GetMultipartBoundary();
            string targetDirectory = "wwwroot\\uploadfiles";

            //检查相应目录
            if (!Directory.Exists(targetDirectory))
            {
                Directory.CreateDirectory(targetDirectory);
            }
            var result2  = this.Request.Headers.TryGetValue("wxq", out StringValues authStr);
            var filePath = string.Empty;
            var reader   = new Microsoft.AspNetCore.WebUtilities.MultipartReader(boundary, this.Request.Body);

            try
            {
                CancellationToken cancellationToken = new System.Threading.CancellationToken();
                var section = await reader.ReadNextSectionAsync(cancellationToken);

                while (section != null)
                {
                    ContentDispositionHeaderValue header = section.GetContentDispositionHeader();

                    if (header.FileName.HasValue || header.FileNameStar.HasValue)
                    {
                        var fileSection = section.AsFileSection();

                        var fileName = fileSection.FileName;
                        var mimeType = fileSection.Section.ContentType;
                        filePath = Path.Combine(targetDirectory, fileName);

                        using (var writeStream = System.IO.File.Create(filePath))
                        {
                            const int bufferSize = 1024;
                            await fileSection.FileStream.CopyToAsync(writeStream, bufferSize, cancellationToken);
                        }
                    }
                    //else
                    //{
                    //  取formdata中的信息
                    //    var formDataSection = section.AsFormDataSection();
                    //    var name = formDataSection.Name;
                    //    var value = await formDataSection.GetValueAsync();
                    //    uploadSectionInfo.Dicts.Add(new KV(name, value));

                    //}
                    section = await reader.ReadNextSectionAsync(cancellationToken);
                }
            }
            catch (OperationCanceledException)
            {
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                //return (false, "用户取消操作", null);
            }

            return(Json(result));
        }