-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1305 RateLimiting headers #1307
Open
jlukawska
wants to merge
21
commits into
ThreeMammals:develop
Choose a base branch
from
jlukawska:fix/1305-ratelimiting-headers
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e741e62
Merge pull request #7 from ThreeMammals/develop
jlukawska 1060ef9
set rate limiting headers on the proper httpcontext
jlukawska b2d4deb
Fix CS0169: The field '_responseStatusCode' is never used
raman-m f820be4
IDE1006 Naming rule violation: These words must begin with upper case…
raman-m 1aac06a
Merge branch 'ThreeMammals:develop' into develop
jlukawska d174272
Merge remote-tracking branch 'origin/develop' into fix/1305-ratelimit…
0de9c39
fix after merge
03db4c5
CR fix
fa8c3e8
fix to tests
3ea824b
Merge branch 'develop' into fix/1305-ratelimiting-headers
raman-m ec15a65
fix Retry-After header
e852df9
Merge remote-tracking branch 'origin/fix/1305-ratelimiting-headers' i…
d01f253
merge fix
3530e7a
add Retry-After header checks in tests
eede977
refactor of ClientRateLimitTests
3f527fd
Merge pull request #10 from ThreeMammals/develop
jlukawska c30edac
Merge remote-tracking branch 'origin/develop' into fix/1305-ratelimit…
jlukawska 98df368
merge fix
jlukawska c110a59
Merge remote-tracking branch 'origin/develop' into fix/1305-ratelimit…
jlukawska 59e9886
Merge remote-tracking branch 'origin/develop' into fix/1305-ratelimit…
jlukawska 53fbcd2
Merge remote-tracking branch 'origin/develop' into fix/1305-ratelimit…
jlukawska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Net.Http.Headers; | ||
using Ocelot.Configuration; | ||
using Ocelot.Logging; | ||
using Ocelot.Middleware; | ||
|
@@ -10,15 +12,18 @@ public class RateLimitingMiddleware : OcelotMiddleware | |
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IRateLimiting _limiter; | ||
private readonly IServiceProvider _container; | ||
|
||
public RateLimitingMiddleware( | ||
RequestDelegate next, | ||
IOcelotLoggerFactory factory, | ||
IRateLimiting limiter) | ||
IRateLimiting limiter, | ||
IServiceProvider container) | ||
: base(factory.CreateLogger<RateLimitingMiddleware>()) | ||
{ | ||
_next = next; | ||
_limiter = limiter; | ||
_container = container; | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
|
@@ -71,8 +76,13 @@ public async Task Invoke(HttpContext httpContext) | |
//set X-Rate-Limit headers for the longest period | ||
if (!options.DisableRateLimitHeaders) | ||
{ | ||
var headers = _limiter.GetHeaders(httpContext, identity, options); | ||
httpContext.Response.OnStarting(SetRateLimitHeaders, state: headers); | ||
var httpContextAccessor = _container.GetService<IHttpContextAccessor>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check if the original http context is null - this shouldn't happen though -, why not use var originalHttpContext = httpContextAccessor?.HttpContext;
if(originalHttpContext != null)
{
var headers = _limiter.GetHeaders(originalHttpContext, identity, options);
originalHttpContext.Response.OnStarting(SetRateLimitHeaders, state: headers);
} instead? |
||
if (httpContextAccessor != null) | ||
{ | ||
var originalHttpContext = httpContextAccessor.HttpContext; | ||
var headers = _limiter.GetHeaders(originalHttpContext, identity, options); | ||
originalHttpContext.Response.OnStarting(SetRateLimitHeaders, state: headers); | ||
} | ||
} | ||
|
||
await _next.Invoke(httpContext); | ||
|
@@ -119,7 +129,8 @@ public virtual DownstreamResponse ReturnQuotaExceededResponse(HttpContext httpCo | |
|
||
if (!option.DisableRateLimitHeaders) | ||
{ | ||
http.Headers.TryAddWithoutValidation("Retry-After", retryAfter); // in seconds, not date string | ||
http.Headers.TryAddWithoutValidation(HeaderNames.RetryAfter, retryAfter); // in seconds, not date string | ||
httpContext.Response.Headers[HeaderNames.RetryAfter] = retryAfter; | ||
} | ||
|
||
return new DownstreamResponse(http); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Injecting the service provider instead of directly injecting the
IHttpContextAccessor
object can be considered a significant design flaw, in my opinion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and it is a bad practice to embeded service provider