Skip to content
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

fix: Unable to use Custom Shelf Middlewares #133

Open
Zfinix opened this issue Aug 9, 2024 · 0 comments
Open

fix: Unable to use Custom Shelf Middlewares #133

Zfinix opened this issue Aug 9, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@Zfinix
Copy link

Zfinix commented Aug 9, 2024

Description

A clear and concise description of what the bug is.

Code Snippet

import 'package:pharaoh/pharaoh.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf_rate_limiter/shelf_rate_limiter.dart';

void main() async {
  final app = Pharaoh();

  shelf.Middleware rateLimit() {
    final memoryStorage = MemStorage();
    final rateLimiter = ShelfRateLimiter(
      storage: memoryStorage,
      duration: Duration(seconds: 10),
      maxRequests: 1,
    );
    return rateLimiter.rateLimiter();
  }

  app
    ..use(logRequests)
    ..use(useShelfMiddleware(rateLimit()))
    ..get('/', (req, res) => res.ok('API V1'));

  await app.listen(port: 8080);
}

calling curl http://localhost:8080/ twice to test the rate-limit results in curl: (18) transfer closed with 42 bytes remaining to read on the second attempt.

Server-side error:

-------------------------------------------------------
Path:             /
Method:           GET
Content-Type      null
-------------------------------------------------------

Unhandled exception:
HttpException: Content size below specified contentLength.  23 bytes written but expected 42., uri = /

Expected Behavior

API responds with rate-limit headers and an error stating the rate-limit has been reached.

Additional Context

It looks like this happens when ShelfRateLimiter tries to call response.change()

@Zfinix Zfinix added the bug Something isn't working label Aug 9, 2024
@codekeyz codekeyz self-assigned this Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants