pharaoh icon indicating copy to clipboard operation
pharaoh copied to clipboard

fix: Unable to use Custom Shelf Middlewares

Open Zfinix opened this issue 6 months ago • 0 comments

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 avatar Aug 09 '24 06:08 Zfinix