zendframework / zend-expressive-router

Router subcomponent for Expressive
BSD 3-Clause "New" or "Revised" License
28 stars 13 forks source link

Copy matched parameters for HEAD requests #74

Closed geerteltink closed 6 years ago

geerteltink commented 6 years ago

Someone is spamming my site with HEAD requests and they are causing exceptions. I don't care much about the spamming, I'm more interested in why the request fails. It turns out for a normal GET request, the matched route parameters are copied into the request as attributes in the RouteMiddleware. The ImplicitHeadMiddleware runs after the RouteMiddleware and it tries to match the request again as a HEAD request, however it doesn't copy the matched route parameters into the request. This causes the handler to load it as a GET request, but it doesn't get the parameters and thus throws exceptions. I could check for an existing id but without a valid id, the GET route wouldn't be matched anyway and so shouldn't the HEAD request.

There are 2 solutions I can think of.

  1. Always use the RouteResult to get matched parameters. But doing that, it means that modules can't be reused outside Expressive as other frameworks don't have a RouteResult.
  2. Copy the parameters like RouteMiddleware does so the handler doesn't need to check if it is a GET or HEAD request and $request->getAttribute('id') can be used.

Unless someone has a better idea, I'll go for solution 2.

weierophinney commented 6 years ago

Thanks, @xtreamwayz!