summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php')
-rw-r--r--core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php38
1 files changed, 38 insertions, 0 deletions
diff --git a/core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php b/core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php
new file mode 100644
index 0000000..66a3bf6
--- /dev/null
+++ b/core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod.
+ */
+
+namespace Drupal\Core\PageCache\RequestPolicy;
+
+use Drupal\Core\PageCache\RequestPolicyInterface;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * Reject when running from the command line or when HTTP method is not safe.
+ *
+ * The policy denies caching if the request was initiated from the command line
+ * interface (drush) or the request method is neither GET nor HEAD (see RFC
+ * 2616, section 9.1.1 - Safe Methods).
+ */
+class CommandLineOrUnsafeMethod implements RequestPolicyInterface {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function check(Request $request) {
+ if ($this->isCli() || !$request->isMethodSafe()) {
+ return static::DENY;
+ }
+ }
+
+ /**
+ * Indicates whether this is a CLI request.
+ */
+ protected function isCli() {
+ return PHP_SAPI === 'cli';
+ }
+
+}