Skip to content

Commit 83f5144

Browse files
committed
Presenter: checks $allowedMethods
1 parent dca911e commit 83f5144

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Application/UI/Presenter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ abstract class Presenter extends Control implements Application\IPresenter
7474
/** @var bool use absolute Urls or paths? */
7575
public $absoluteUrls = false;
7676

77+
/** @var string[] */
78+
public $allowedMethods = ['GET', 'POST', 'HEAD', 'PUT', 'DELETE'];
79+
7780
/** @var Nette\Application\Request|null */
7881
private $request;
7982

@@ -215,6 +218,7 @@ public function run(Application\Request $request): Application\Response
215218
try {
216219
// STARTUP
217220
$this->checkRequirements(static::getReflection());
221+
$this->checkHttpMethod();
218222
Arrays::invoke($this->onStartup, $this);
219223
$this->startup();
220224
if (!$this->startupCheck) {
@@ -333,6 +337,17 @@ public function detectedCsrf(): void
333337
}
334338

335339

340+
protected function checkHttpMethod(): void
341+
{
342+
if ($this->allowedMethods &&
343+
!in_array($method = $this->httpRequest->getMethod(), $this->allowedMethods, true)
344+
) {
345+
$this->httpResponse->setHeader('Allow', implode(',', $this->allowedMethods));
346+
$this->error("Method $method is not allowed", Nette\Http\IResponse::S405_MethodNotAllowed);
347+
}
348+
}
349+
350+
336351
/********************* signal handling ****************d*g**/
337352

338353

0 commit comments

Comments
 (0)