From cd3ae128962c01b1d35a8f0017fab45e93d09574 Mon Sep 17 00:00:00 2001 From: linch90 Date: Fri, 28 Feb 2020 21:00:17 +0800 Subject: [PATCH] fix(httpformcollection): fix the HttpFormCollection type parameter can only conform to the name of "form". --- .../HttpMessageListener.cs | 79 ++++++++++++------- .../IUserService.cs | 2 +- .../Domain/PersonService.cs | 4 +- .../Domain/UserService.cs | 4 +- 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/Surging.Core/Surging.Core.KestrelHttpServer/HttpMessageListener.cs b/src/Surging.Core/Surging.Core.KestrelHttpServer/HttpMessageListener.cs index 6ade9cfe7..cc15d385f 100644 --- a/src/Surging.Core/Surging.Core.KestrelHttpServer/HttpMessageListener.cs +++ b/src/Surging.Core/Surging.Core.KestrelHttpServer/HttpMessageListener.cs @@ -85,7 +85,10 @@ public async Task OnReceived(IMessageSender sender,string messageId, HttpContext if (context.Request.HasFormContentType) { var collection = await GetFormCollection(context.Request); - httpMessage.Parameters.Add("form", collection); + foreach (var item in collection) + { + httpMessage.Parameters.Add(item.Key, item.Value); + } if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) return; httpMessage.Attachments = RpcContext.GetContext().GetContextParameters(); @@ -189,59 +192,75 @@ public async Task OnException(HttpContext context, HttpServerMessageSender return true; } - private async Task GetFormCollection(HttpRequest request) + private async Task> GetFormCollection(HttpRequest request) { - var boundary = GetName("boundary=", request.ContentType); + var boundary = GetName("boundary=", request.ContentType); var reader = new MultipartReader(boundary, request.Body); var collection = await GetMultipartForm(reader); - var fileCollection = new HttpFormFileCollection(); - var fields = new Dictionary(); - foreach (var item in collection) + + return collection.ToDictionary(item => item.Key, item => { - if (item.Value is HttpFormFileCollection) + var fieldsDict = new Dictionary(); + if (item.Value.Fields.HasValue) { - var itemCollection = item.Value as HttpFormFileCollection; - fileCollection.AddRange(itemCollection); + fieldsDict.Add(item.Key, item.Value.Fields.Value); } - else - { - var itemCollection = item.Value as Dictionary; - fields = fields.Concat(itemCollection).ToDictionary(k => k.Key, v => v.Value); - } - } - return new HttpFormCollection(fields, fileCollection); + return new HttpFormCollection(fieldsDict, item.Value.HttpFormFileCollection); + }); } - private async Task> GetMultipartForm(MultipartReader reader) + private async Task> GetMultipartForm(MultipartReader reader) { - var section = await reader.ReadNextSectionAsync(); - var collection = new Dictionary(); + var section = await reader.ReadNextSectionAsync(); + var collection = new Dictionary(); if (section != null) - { - var name=GetName("name=",section.ContentDisposition); - var fileName = GetName("filename=",section.ContentDisposition); + { + var name = GetName("name=", section.ContentDisposition); + var fileName = GetName("filename=", section.ContentDisposition); var buffer = new MemoryStream(); await section.Body.CopyToAsync(buffer); - if(string.IsNullOrEmpty(fileName)) + if (string.IsNullOrEmpty(fileName)) { var fields = new Dictionary(); StreamReader streamReader = new StreamReader(buffer); - fields.Add(name, new StringValues(UTF8Encoding.Default.GetString(buffer.GetBuffer(),0,(int)buffer.Length))); - collection.Add(name, fields); + fields.Add(name, new StringValues(Encoding.Default.GetString(buffer.GetBuffer(), 0, (int)buffer.Length))); + collection.Add(name, (fields[name], null)); } else { var fileCollection = new HttpFormFileCollection(); StreamReader streamReader = new StreamReader(buffer); - fileCollection.Add(new HttpFormFile(buffer.Length,name,fileName,buffer.GetBuffer())); - collection.Add(name, fileCollection); + fileCollection.Add(new HttpFormFile(buffer.Length, name, fileName, buffer.GetBuffer())); + collection.Add(name, (null, fileCollection)); } - var formCollection= await GetMultipartForm(reader); - foreach(var item in formCollection) + var formCollection = await GetMultipartForm(reader); + foreach (var item in formCollection) { if (!collection.ContainsKey(item.Key)) - collection.Add(item.Key,item.Value); + collection.Add(item.Key, item.Value); + else + { + var (fields, httpFormFileCollection) = collection[item.Key]; + if (item.Value.Fields.HasValue && !fields.HasValue) + { + fields = item.Value.Fields.Value; + } + + if (httpFormFileCollection == null) + { + httpFormFileCollection = item.Value.HttpFormFileCollection; + } + else + { + var formFiles = + item.Value.HttpFormFileCollection.Where(v => + !httpFormFileCollection.Exists(p => p.FileName == v.FileName)); + httpFormFileCollection.AddRange(formFiles); + } + + collection[item.Key] = (fields, httpFormFileCollection); + } } } return collection; diff --git a/src/Surging.IModuleServices/Surging.IModuleServices.Common/IUserService.cs b/src/Surging.IModuleServices/Surging.IModuleServices.Common/IUserService.cs index cdeb26c78..e7c5201e8 100644 --- a/src/Surging.IModuleServices/Surging.IModuleServices.Common/IUserService.cs +++ b/src/Surging.IModuleServices/Surging.IModuleServices.Common/IUserService.cs @@ -164,7 +164,7 @@ public interface IUserService: IServiceKey /// /// HttpFormCollection 类型参数 /// - Task UploadFile(HttpFormCollection form); + Task UploadFile(HttpFormCollection form1); /// /// 测试下载文件 diff --git a/src/Surging.Modules/Surging.Modules.Common/Domain/PersonService.cs b/src/Surging.Modules/Surging.Modules.Common/Domain/PersonService.cs index e9f02884e..f310e6855 100644 --- a/src/Surging.Modules/Surging.Modules.Common/Domain/PersonService.cs +++ b/src/Surging.Modules/Surging.Modules.Common/Domain/PersonService.cs @@ -122,9 +122,9 @@ public Task GetUser(List idList) return Task.FromResult("type is List"); } - public async Task UploadFile(HttpFormCollection form) + public async Task UploadFile(HttpFormCollection form1) { - var files = form.Files; + var files = form1.Files; foreach (var file in files) { using (var stream = new FileStream(Path.Combine(AppContext.BaseDirectory, file.FileName), FileMode.Create)) diff --git a/src/Surging.Modules/Surging.Modules.Common/Domain/UserService.cs b/src/Surging.Modules/Surging.Modules.Common/Domain/UserService.cs index 13e357fb4..59dc74f8a 100644 --- a/src/Surging.Modules/Surging.Modules.Common/Domain/UserService.cs +++ b/src/Surging.Modules/Surging.Modules.Common/Domain/UserService.cs @@ -121,9 +121,9 @@ public Task> GetApiResult() return Task.FromResult(new ApiResult() { Value = new UserModel { Name = "fanly" }, StatusCode = 200 }); } - public async Task UploadFile(HttpFormCollection form) + public async Task UploadFile(HttpFormCollection form1) { - var files = form.Files; + var files = form1.Files; foreach (var file in files) { using (var stream = new FileStream(Path.Combine(AppContext.BaseDirectory, file.FileName), FileMode.OpenOrCreate))