falconry / falcon

The no-magic web data plane API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.
https://falcon.readthedocs.io/en/stable/
Apache License 2.0
9.53k stars 944 forks source link

Implement an additional middleware on_finalize() method #1581

Open kgriffs opened 5 years ago

kgriffs commented 5 years ago

Implement an additional on_finalize() middleware method for ASGI and WSGI that is called immediately before the response body is returned to the server. This can be used by apps to implement protocol extensions (e.g., server push).

CaselIT commented 4 years ago

Could you expand how would this would work? In particular:

I guess the location of the call in the application would after the lines here

https://github.com/falconry/falcon/blob/00e5c9c2499c3afb5f8d48fca80591531563d461/falcon/app.py#L344-L346

https://github.com/falconry/falcon/blob/00e5c9c2499c3afb5f8d48fca80591531563d461/falcon/asgi/app.py#L393-L395

If the objective is to support mainly the last point, maybe it would be simpler to detach if from the middleware, and have something similar to the add_error_handler, something like a add_success_handler or similar, that accepts a single function that could return a custom response to the server. I'm not sure if we should accept multiple functions, that are called one after the other, in order, where the first that returns a custom response would "win" and the processing of the other function would end, along with the processing of the rest of the application.

Something like

for handler in self._success_handlers:
  body= handler(req, res, ...)  # what other parameters should be pass?
  if body is not None:
    return body
vytas7 commented 4 years ago

@kgriffs @CaselIT Is it the same issue as https://github.com/falconry/falcon/issues/1505, or is the intent different here?

CaselIT commented 4 years ago

I had not seen the other one, and I'm not sure what the objective of this should be, so the questions.

kgriffs commented 3 years ago

Seems like it is similar to the other issue, but I don't want to lose the points brought up in the above discussion.