Review main Merged

67042[TASK] Propagate "immediate" responses through the middleware stack

Benjamin Franzke CR +2 V +2 Hard +217 −70 updated 01/07/21, 20:13

Commit message

When a controller finds some pre-requisites of the called
action are not fulfilled, it can create an 'early' response
and throw it as ImmediateResponseException. This exception
bypasses the middleware stack, and is caught in the Application
to emit the response. This is a big win in contrast to
die/exit calls.

Some middlewares however expect to be always called when the
response bubbles up. Good examples are 'locking' middlewares:
They set up a lock when called, dispatch to inner middlewares,
and tear town the lock when a response is retrieved.

The tear down code of those middlewares is bypassed when
a controller throws an ImmediateResponseException.

The patch introduces a second exception: PropagateResponseException.
This one is caught by a new very inner middleware positioned at the
end of the middleware stack, just before the request is dispatched
to some controller. It then sends the response up the outer
middlewares. This allows middlewares like a 'locking' middleware
to do it's job without being bypassed, and at the same time
allows a controller to bypass any further local processing by
throwing such a response exception.

ImmediateResponseException exists as before and can still be used
to directly bypass the middleware stack and is kept as safety net
in case a PropagateResponseException is thrown within a middleware.
PropagateResponseException therefore extends ImmediateResponseException.

It's however discouraged to throw ImmediateResponseException from
within controllers - they require knowledge on what middlewares
do in their 'tear down' part and there shouldn't be a reason to
bypass them.

Middleware/BackendUserAuthenticator is adapted to properly
handle ImmediateResponseException that would have been thrown
BackendUserAuthentication::backendCheckLogin(). BackendUserAuthentication
is therefore refactored to allow to call the backend login
initialization without (duplicate) login check.
This allows to propagate redirect/maintenance responses.

Review

  • Code review CR +2 Done
    • Benni Mack +2
    • Christian Kuhn +1
  • Verified V +2 Done
    • Benni Mack +2
    • Christian Kuhn +1
    • TYPO3com +1 CI

Issues 1

Files 18

Review log 3049

Show 1724 earlier messages
  1. Benjamin Franzke uploaded patch set 1 12/06/20, 19:23

  2. TYPO3com voted on patch set 1 V -1 12/06/20, 19:23 Completed build in 20s on Sun, 6 Dec, 07:23 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7878

  3. Benjamin Franzke uploaded patch set 2 12/07/20, 07:24

  4. TYPO3com commented on patch set 2 12/07/20, 07:33 Completed build in 08m 54s on Mon, 7 Dec, 07:33 AM Test Summary: 231 of 32654 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7882

  5. Benjamin Franzke uploaded patch set 3 12/07/20, 08:00

  6. TYPO3com voted on patch set 3 V +1 12/07/20, 08:09 Completed build in 09m 27s on Mon, 7 Dec, 08:09 AM Test Summary: 32413 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7885

  7. Benjamin Franzke uploaded patch set 4 12/07/20, 08:13

  8. TYPO3com voted on patch set 4 V -1 12/07/20, 08:24 Completed build in 10m 58s on Mon, 7 Dec, 08:24 AM Test Summary: 32046 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7886

  9. TYPO3com voted on patch set 4 V +1 12/07/20, 08:48 Completed build in 09m 57s on Mon, 7 Dec, 08:48 AM Test Summary: 32415 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7887

  10. Daniel Goerz commented on patch set 4 12/07/20, 10:14

    I like the idea. After looking at the approach (if I understood it correctly) a PropagateResponseException would only be catched by the new MW when it is thrown in the last RequestHandler (aka the application), right? Therefore I thought: Would it be an option to wrap each middleware in a try catch block for PropagateResponseException in the MiddlewareDispatcher (specifically in the handle() methods of the RequestHandler wrapper classes? That way we would not need a middleware to fetch the response from the exception and propagate it back plus any code called from any middleware could make use of the PropagateResponseException.
  11. Benjamin Franzke commented on patch set 4 12/07/20, 10:26

    Reply

    Upfront, given the following TYPO3 naming scheme Http\"Application" -> Middlewares -> Http\RequestHandler With this change: * Application still catches for ImmediateResponseException (and also PropagateResponseException if not catched before). * PropagateResponseExcpetion is catched as last middleware, thus it catches everything withing in requesthandler. > plus any code called from any middleware could make use of the PropagateResponseException. Yea,it's good to support that as safety net, it can still be used (although code called from middlewares SHOULD be clean code and therefore *return* responses instead of throwing them): As PropagateResponseException extends ImmediateResponseException, AbstractApplication will always catch it. So, after all, throwing "response"s is a hack around a legacy codebase. Therefore we should avoid putting this into the clean MiddlewareDispatcher. I personally feel a new middleware for this is more clean and decoupled than attaching this to the generic middleware pipeline.
  12. Daniel Goerz commented on patch set 4 12/07/20, 10:43

    Reply

    I agree that middlewares should be clean, but so should the Http\RequestHandler ;) I also understand why it might not be desirable to introduce hacky solutions into clean code like the MiddlewareDispatcher. However, with the current solution the PropagateResponseExcpetion in BackendUserAuthentication would not be propagated at all, right? So I guess one could make the argument that if we introduce such a thing as a PropagateResponseExcpetion we should ensure that it is always propagated as an response through the middleware stack.
  13. Daniel Goerz commented on patch set 4 12/07/20, 10:52

    Reply

    public function handle(ServerRequestInterface $request): ResponseInterface { try { $response = $this->middleware->process($request, $this->next); } catch (PropagateResponseException $e) { $response = $e->getResponse(); } return $response; } Wouldn't be too bad. Just saying 😊
  14. Benjamin Franzke commented on patch set 4 12/07/20, 14:40

    Reply

    > I agree that middlewares should be clean, but so should the Http\RequestHandler ;) Hm? what's the issue with the RequestHandler? I didn't touch it :) > However, with the current solution the PropagateResponseExcpetion in BackendUserAuthentication would not be propagated at all, right? Yes, you are right, it would be handled in the AbstractApplication (with it's ImmediateResponseException) right now. So that's certainly not ideal, but shows, that the middleware should rather call code that *returns* the response, instead of throwing it. Which means that we should refactor BackendUserAuthentication to return a redirect response instead of throwing it. What I mean: Requiring middlewares to properly take care of response proagation is a good thing and prevents code smell by reyling one exceptions instead of clear code-paths. Using Immediate/PropagateResponseException is a bit like `goto` in C, it considered harmful, but sometimes needed. Therefore I wouldn't like to introduce it as the primary method of propagation, but only as safety net. I hope that makes sense.
  15. Benni Mack commented on patch set 4 12/07/20, 15:39

    BackendUserAuthentication.php:2346

    since this is called in a PSR-15 middleware, a "ImmediateResponseException" is more appropriate I guess?
  16. Benjamin Franzke commented on patch set 4 12/07/20, 16:46

    BackendUserAuthentication.php:2346 Reply

    Yes, true, despite the fact that PropagateResponseException is a ImmediateResponseException, ImmediateResponseException would be better here as it reveals that we should refactor this to return the redirect response directly back to the middleware, instead of using hacky propagation/immediate exceptions (which I rather see as a method for legacy code, that can't directly pass back a response to the request handler). See also discussion in https://review.typo3.org/c/Packages/TYPO3.CMS/+/67042/4#message-089859671ad318222896597db81dd996ce1b7680
  17. Benni Mack commented on patch set 4 12/07/20, 16:58

    BackendUserAuthentication.php:2346 Reply

    Yes. Agreed. I do have a draft to get rid of this ugly method. There are "RuntimeExceptions" thrown in this method :D
  18. Daniel Goerz commented on patch set 4 12/07/20, 17:05

    Reply

    It does. I am fine with this approach, just wanted to point out a possible alternative in MiddlewareDispatcher in case you did not think of it. I guess any solutions is not ment to stay, so it does not matter too much. For internal core code we now should keep in mind that code called from a middleware cannot expect a PropagateResponseException to do its thing. Which is fine and wont come up too often.
  19. Benjamin Franzke uploaded patch set 5 12/07/20, 17:37

  20. Benjamin Franzke commented on patch set 5 12/07/20, 17:37

    BackendUserAuthentication.php:2346 Reply

    Ok, won't touch that area within this patch then 👍.
  21. TYPO3com voted on patch set 5 V +1 12/07/20, 17:48 Completed build in 10m 47s on Mon, 7 Dec, 05:48 PM Test Summary: 32413 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7931

  22. Benjamin Franzke uploaded patch set 6 12/08/20, 08:29

  23. Benjamin Franzke uploaded patch set 7 12/08/20, 08:32

  24. TYPO3com voted on patch set 6 V +1 12/08/20, 08:40 Completed build in 10m 30s on Tue, 8 Dec, 08:40 AM Test Summary: 32413 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7949

  1. TYPO3com voted on patch set 7 V +1 12/08/20, 08:44 Completed build in 11m 31s on Tue, 8 Dec, 08:44 AM Test Summary: 32411 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7950

  2. Benjamin Franzke uploaded patch set 8 12/08/20, 10:16

  3. TYPO3com voted on patch set 8 V -1 12/08/20, 10:20 Completed build in 04m 17s on Tue, 8 Dec, 10:20 AM Test Summary: 3 of 20937 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7955

  4. Benjamin Franzke uploaded patch set 9 12/08/20, 12:40

  5. TYPO3com voted on patch set 9 V -1 12/08/20, 12:51 Completed build in 10m 38s on Tue, 8 Dec, 12:51 PM Test Summary: 1 of 32647 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7959

  6. TYPO3com voted on patch set 9 V +1 12/08/20, 13:06 Completed build in 09m 05s on Tue, 8 Dec, 01:06 PM Test Summary: 32413 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7962

  7. Benjamin Franzke uploaded patch set 10 12/08/20, 17:51

    This is a rebase on master
  8. TYPO3com voted on patch set 10 V +1 12/08/20, 18:03 Completed build in 11m 30s on Tue, 8 Dec, 06:03 PM Test Summary: 32413 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7971

  9. Benjamin Franzke uploaded patch set 11 12/14/20, 10:23

  10. TYPO3com voted on patch set 11 V -1 12/14/20, 10:34 Completed build in 10m 50s on Mon, 14 Dec, 10:34 AM Test Summary: 30789 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8149

  11. TYPO3com commented on patch set 11 12/14/20, 11:55 Completed build in 09m 35s on Mon, 14 Dec, 11:55 AM Test Summary: 1 of 32658 failed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8153

  12. TYPO3com voted on patch set 11 V +1 12/14/20, 12:08 Completed build in 09m 52s on Mon, 14 Dec, 12:08 PM Test Summary: 32417 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8154

  13. Benni Mack commented on patch set 11 12/18/20, 18:43

    @daniel: what's your take on the current patchset?
  14. Benjamin Franzke uploaded patch set 12 01/06/21, 13:52

  15. TYPO3com voted on patch set 12 V -1 01/06/21, 14:02 Completed build in 09m 53s on Wed, 6 Jan, 02:02 PM Test Summary: 32561 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8683

  16. TYPO3com voted on patch set 12 V +1 01/06/21, 14:44 Completed build in 1h 01m 05s on Wed, 6 Jan, 02:44 PM Test Summary: 32567 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8683

  17. Christian Kuhn uploaded patch set 13 01/07/21, 18:32

  18. Christian Kuhn commented on patch set 13 V +1 CR +1 01/07/21, 18:40

    looks good to me, now.
  19. TYPO3com voted on patch set 13 V +1 01/07/21, 18:43 Completed build in 10m 41s on Thu, 7 Jan, 06:43 PM Test Summary: 32567 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8745

  20. Christian Kuhn uploaded patch set 14 01/07/21, 18:44

  21. Christian Kuhn voted on patch set 14 V +1 CR +1 01/07/21, 18:44

  22. TYPO3com voted on patch set 14 V +1 01/07/21, 18:55 Completed build in 10m 53s on Thu, 7 Jan, 06:55 PM Test Summary: 32567 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-8746

  23. Benni Mack voted on patch set 14 V +2 CR +2 01/07/21, 19:13

  24. Benni Mack merged patch set 15 01/07/21, 19:13

  25. Benjamin Franzke commented on patch set 15 01/07/21, 20:13

    ImmediateResponseException.php:29 Unresolved

    Note, at best Controllers actually return a response, if possible. So this is more thought for code that can't return a response (right now). But not much of a problem, just wanted to mention. Thanks for adding more explanations.