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

Refactor/adjusts middleware #190

Closed
wants to merge 5 commits into from

Conversation

gabriel-f-santos
Copy link
Contributor

@gabriel-f-santos gabriel-f-santos commented May 16, 2024

Contain

  • New feature
  • Bugfix
  • Tests
  • Documentation
  • Build/CI/CD
  • Breaking changes
  • Refactor
  • Configuration changes
  • Migrations

Details

  • This is an proposal to avoid consume the request body in middleware and hang the request (to avoid this should wrap the incoming receive and pass the wraper to self.app(scope, wrapped_receive, send) ).
    (Consume request body in middleware is problematic encode/starlette#495 (comment))
    `class LoggedRequestBodySizeMiddleware:
    def init(self, app):
    self.app = app

    async def call(self, scope, receive, send):
    if scope["type"] != "http":
    await self.app(scope, receive, send)
    return

      body_size = 0
    
      async def receive_logging_request_body_size():
          nonlocal body_size
    
          message = await receive()
          assert message["type"] == "http.request"
    
          body_size += len(message.get("body", b""))
    
          if not message.get("more_body", False):
              print(f"Size of request body was: {body_size} bytes")
    
          return message
    
      await self.app(scope, receive_logging_request_body_size, send)`
    
  • Get data from connection data (scope) instead of request data (receive)

@gabriel-f-santos gabriel-f-santos added the enhancement New feature or request label May 16, 2024
@gabriel-f-santos gabriel-f-santos requested a review from a team May 16, 2024 17:56
@gabriel-f-santos gabriel-f-santos self-assigned this May 16, 2024
@julianolf julianolf deleted the refactor/adjusts-middleware branch September 12, 2024 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant