From c2eb375f915414130c1198bb90ad4846200dd135 Mon Sep 17 00:00:00 2001 From: nsnail Date: Thu, 8 Sep 2022 17:56:38 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E5=BD=93body=20=E4=B8=BA=20FileBufferingRe?= =?UTF-8?q?adStream=EF=BC=8C=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA=E7=8E=B0r?= =?UTF-8?q?equest.Body.Length=3D0=E7=9A=84=E6=83=85=E5=86=B5=20*=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9package=20id=20=E9=81=BF=E5=85=8D=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=88=B0=E5=8E=9F=E7=89=88=20*=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E5=88=B0nuget.org?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nuget/build-and-push.ps1 | 24 +++ src/Ocelot/Ocelot.csproj | 82 +++---- src/Ocelot/Request/Mapper/RequestMapper.cs | 240 +++++++++++---------- 3 files changed, 186 insertions(+), 160 deletions(-) create mode 100644 nuget/build-and-push.ps1 diff --git a/nuget/build-and-push.ps1 b/nuget/build-and-push.ps1 new file mode 100644 index 00000000..731cca83 --- /dev/null +++ b/nuget/build-and-push.ps1 @@ -0,0 +1,24 @@ +# 定义参数 +Param( + # Nuget APIKey + [string] $apikey +) + +if ($apikey -eq $null -or $apikey -eq "") +{ + Write-Error "必须指定apiKey"; + return; +} + +rm -r ../src/Ocelot/bin/Release +dotnet build -c Release ../Ocelot.sln +$files = Get-ChildItem -Path ../src/Ocelot/bin/Release -Filter *-ns*.nupkg +foreach($file in $files) +{ + dotnet nuget push $file.fullName --skip-duplicate --api-key $apikey --source https://api.nuget.org/v3/index.json +} +$files = Get-ChildItem -Path ../src/Ocelot/bin/Release -Filter *-ns*.snupkg +foreach($file in $files) +{ + dotnet nuget push $file.fullName --skip-duplicate --api-key $apikey --source https://api.nuget.org/v3/index.json +} \ No newline at end of file diff --git a/src/Ocelot/Ocelot.csproj b/src/Ocelot/Ocelot.csproj index cd61103b..2cd03ab1 100644 --- a/src/Ocelot/Ocelot.csproj +++ b/src/Ocelot/Ocelot.csproj @@ -1,41 +1,41 @@ -锘 - - net6.0 - true - Ocelot is an API Gateway. The project is aimed at people using .NET running a micro services / service orientated architecture that need a unified point of entry into their system. In particular I want easy integration with IdentityServer reference and bearer tokens. reference tokens. Ocelot is a bunch of middlewares in a specific order. Ocelot manipulates the HttpRequest object into a state specified by its configuration until it reaches a request builder middleware where it creates a HttpRequestMessage object which is used to make a request to a downstream service. The middleware that makes the request is the last thing in the Ocelot pipeline. It does not call the next middleware. The response from the downstream service is stored in a per request scoped repository and retrived as the requests goes back up the Ocelot pipeline. There is a piece of middleware that maps the HttpResponseMessage onto the HttpResponse object and that is returned to the client. That is basically it with a bunch of other features. - Ocelot - 0.0.0-dev - Ocelot - Ocelot - API Gateway;.NET core - https://github.com/ThreeMammals/Ocelot - http://github.com/images/ocelot_logo.png - win10-x64;osx.10.11-x64;osx.10.12-x64;win7-x64 - false - false - True - false - Tom Pallister - ..\..\codeanalysis.ruleset - - - full - True - - - - - - - NU1701 - - - - all - - - - - - - +锘 + + net6.0 + true + Ocelot is an API Gateway. The project is aimed at people using .NET running a micro services / service orientated architecture that need a unified point of entry into their system. In particular I want easy integration with IdentityServer reference and bearer tokens. reference tokens. Ocelot is a bunch of middlewares in a specific order. Ocelot manipulates the HttpRequest object into a state specified by its configuration until it reaches a request builder middleware where it creates a HttpRequestMessage object which is used to make a request to a downstream service. The middleware that makes the request is the last thing in the Ocelot pipeline. It does not call the next middleware. The response from the downstream service is stored in a per request scoped repository and retrived as the requests goes back up the Ocelot pipeline. There is a piece of middleware that maps the HttpResponseMessage onto the HttpResponse object and that is returned to the client. That is basically it with a bunch of other features. + Ocelot + 18.0.1-ns1 + Ocelot + Ocelot.NS + API Gateway;.NET core + https://github.com/ThreeMammals/Ocelot + http://github.com/images/ocelot_logo.png + win10-x64;osx.10.11-x64;osx.10.12-x64;win7-x64 + false + false + True + false + Tom Pallister + ..\..\codeanalysis.ruleset + + + full + True + + + + + + + NU1701 + + + + all + + + + + + + diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index 304f4222..cd91d24c 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -1,119 +1,121 @@ -锘縩amespace Ocelot.Request.Mapper -{ - using Microsoft.AspNetCore.Http; - using Microsoft.AspNetCore.Http.Extensions; - using Microsoft.Extensions.Primitives; - using Ocelot.Configuration; - using Ocelot.Responses; - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Net.Http; - using System.Threading.Tasks; - - public class RequestMapper : IRequestMapper - { - private readonly string[] _unsupportedHeaders = { "host" }; - - public async Task> Map(HttpRequest request, DownstreamRoute downstreamRoute) - { - try - { - var requestMessage = new HttpRequestMessage() - { - Content = await MapContent(request), - Method = MapMethod(request, downstreamRoute), - RequestUri = MapUri(request), - Version = downstreamRoute.DownstreamHttpVersion, - }; - - MapHeaders(request, requestMessage); - - return new OkResponse(requestMessage); - } - catch (Exception ex) - { - return new ErrorResponse(new UnmappableRequestError(ex)); - } - } - - private async Task MapContent(HttpRequest request) - { - if (request.Body == null || (request.Body.CanSeek && request.Body.Length <= 0)) - { - return null; - } - - // Never change this to StreamContent again, I forgot it doesnt work in #464. - var content = new ByteArrayContent(await ToByteArray(request.Body)); - - if (!string.IsNullOrEmpty(request.ContentType)) - { - content.Headers - .TryAddWithoutValidation("Content-Type", new[] { request.ContentType }); - } - - AddHeaderIfExistsOnRequest("Content-Language", content, request); - AddHeaderIfExistsOnRequest("Content-Location", content, request); - AddHeaderIfExistsOnRequest("Content-Range", content, request); - AddHeaderIfExistsOnRequest("Content-MD5", content, request); - AddHeaderIfExistsOnRequest("Content-Disposition", content, request); - AddHeaderIfExistsOnRequest("Content-Encoding", content, request); - - return content; - } - - private void AddHeaderIfExistsOnRequest(string key, HttpContent content, HttpRequest request) - { - if (request.Headers.ContainsKey(key)) - { - content.Headers - .TryAddWithoutValidation(key, request.Headers[key].ToList()); - } - } - - private HttpMethod MapMethod(HttpRequest request, DownstreamRoute downstreamRoute) - { - if (!string.IsNullOrEmpty(downstreamRoute?.DownstreamHttpMethod)) - { - return new HttpMethod(downstreamRoute.DownstreamHttpMethod); - } - - return new HttpMethod(request.Method); - } - - private Uri MapUri(HttpRequest request) - { - return new Uri(request.GetEncodedUrl()); - } - - private void MapHeaders(HttpRequest request, HttpRequestMessage requestMessage) - { - foreach (var header in request.Headers) - { - if (IsSupportedHeader(header)) - { - requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); - } - } - } - - private bool IsSupportedHeader(KeyValuePair header) - { - return !_unsupportedHeaders.Contains(header.Key.ToLower()); - } - - private async Task ToByteArray(Stream stream) - { - using (stream) - { - using (var memStream = new MemoryStream()) - { - await stream.CopyToAsync(memStream); - return memStream.ToArray(); - } - } - } - } -} +锘縩amespace Ocelot.Request.Mapper +{ + using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Http.Extensions; + using Microsoft.Extensions.Primitives; + using Ocelot.Configuration; + using Ocelot.Responses; + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Net.Http; + using System.Threading.Tasks; + + public class RequestMapper : IRequestMapper + { + private readonly string[] _unsupportedHeaders = { "host" }; + + public async Task> Map(HttpRequest request, DownstreamRoute downstreamRoute) + { + try + { + var requestMessage = new HttpRequestMessage() + { + Content = await MapContent(request), + Method = MapMethod(request, downstreamRoute), + RequestUri = MapUri(request), + Version = downstreamRoute.DownstreamHttpVersion, + }; + + MapHeaders(request, requestMessage); + + return new OkResponse(requestMessage); + } + catch (Exception ex) + { + return new ErrorResponse(new UnmappableRequestError(ex)); + } + } + + private async Task MapContent(HttpRequest request) + { + // if (request.Body == null || (request.Body.CanSeek && request.Body.Length) + // 鈫 褰揵ody 涓 FileBufferingReadStream锛屽彲鑳戒細鍑虹幇request.Body.Length=0鐨勬儏鍐碉紝 閭d箞 灏变細婕忔帀鍚庣画鐨勬祦绋 銆備慨鏀逛负 鈫 + if (request.Body.CanSeek && request.Body.Length <= 0 && (request.ContentLength ?? 0) <=0) + { + return null; + } + + // Never change this to StreamContent again, I forgot it doesnt work in #464. + var content = new ByteArrayContent(await ToByteArray(request.Body)); + + if (!string.IsNullOrEmpty(request.ContentType)) + { + content.Headers + .TryAddWithoutValidation("Content-Type", new[] { request.ContentType }); + } + + AddHeaderIfExistsOnRequest("Content-Language", content, request); + AddHeaderIfExistsOnRequest("Content-Location", content, request); + AddHeaderIfExistsOnRequest("Content-Range", content, request); + AddHeaderIfExistsOnRequest("Content-MD5", content, request); + AddHeaderIfExistsOnRequest("Content-Disposition", content, request); + AddHeaderIfExistsOnRequest("Content-Encoding", content, request); + + return content; + } + + private void AddHeaderIfExistsOnRequest(string key, HttpContent content, HttpRequest request) + { + if (request.Headers.ContainsKey(key)) + { + content.Headers + .TryAddWithoutValidation(key, request.Headers[key].ToList()); + } + } + + private HttpMethod MapMethod(HttpRequest request, DownstreamRoute downstreamRoute) + { + if (!string.IsNullOrEmpty(downstreamRoute?.DownstreamHttpMethod)) + { + return new HttpMethod(downstreamRoute.DownstreamHttpMethod); + } + + return new HttpMethod(request.Method); + } + + private Uri MapUri(HttpRequest request) + { + return new Uri(request.GetEncodedUrl()); + } + + private void MapHeaders(HttpRequest request, HttpRequestMessage requestMessage) + { + foreach (var header in request.Headers) + { + if (IsSupportedHeader(header)) + { + requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); + } + } + } + + private bool IsSupportedHeader(KeyValuePair header) + { + return !_unsupportedHeaders.Contains(header.Key.ToLower()); + } + + private async Task ToByteArray(Stream stream) + { + using (stream) + { + using (var memStream = new MemoryStream()) + { + await stream.CopyToAsync(memStream); + return memStream.ToArray(); + } + } + } + } +}